Search This Blog

2021/08/25

Group By using Underscore.js

Suppose you want to do group by on object array then we can use underscore.js

 I have object array x which has keys amount,date & valid.I want to do sum of amount based on date key and use only those record which are valid as 1.

Here is example code

var _ = require('underscore');
var x=[
    {
        "date":"2021-04-14",
        "amount":1300,
        "valid":1
    },
    {
        "date":"2021-05-14",
        "amount":700,
        "valid":0
    },
    {
        "date":"2021-06-13",
        "amount":2300,
        "valid":1
    },
    {
        "date":"2021-07-10",
        "amount":1000,
        "valid":0
    },
    {
        "date":"2021-07-10",
        "amount":1300,
        "valid":1
    },
    {
        "date":"2021-04-14",
        "amount":1700,
        "valid":1
    }
]
x = x.filter((m)=>{
    return m.valid==1
})
console.log("Filtered",x);
 
var grouped = _.groupBy(x, 'date');
console.log("group by date:",grouped);
 
var result=[];
for (const key in grouped) {
    var sum = grouped[key].reduce((accumulator,a)=>{
            return  accumulator + a.amount          
    },0);
 
    result.push({
        "date":key,
        "amount":sum
    })
}
console.log("result:",result);

Output:

Filtered [
  { date: '2021-04-14', amount: 1300, valid: 1 },
  { date: '2021-06-13', amount: 2300, valid: 1 },
  { date: '2021-07-10', amount: 1300, valid: 1 },
  { date: '2021-04-14', amount: 1700, valid: 1 }
]
group by date: {
  '2021-04-14': [
    { date: '2021-04-14', amount: 1300, valid: 1 },
    { date: '2021-04-14', amount: 1700, valid: 1 }
  ],
  '2021-06-13': [ { date: '2021-06-13', amount: 2300, valid: 1 } ],
  '2021-07-10': [ { date: '2021-07-10', amount: 1300, valid: 1 } ]
}
result: [
  { date: '2021-04-14', amount: 3000 },
  { date: '2021-06-13', amount: 2300 },
  { date: '2021-07-10', amount: 1300 }
]


2021/06/01

Using child_process to run long running task

long.js

//child
const longTask = () => {
let sum = 0;
for (let i = 0; i < 1e9; i++) {
sum += i;
};
return sum;
};

process.on('message', (msg) => {
console.log("child",msg);
const sum = longTask();
process.send(sum);
});



We have long running task longTask as above.To call this long running task we
create end point as below

Main.js

//parent

const http = require('http');
const { fork } = require('child_process');
const server = http.createServer();


server.on('request', (req, res) => {
if (req.url === '/get') {
const compute = fork('long.js');
compute.send('start');
compute.on('message', sum => {
res.end(`Sum is ${sum}`);
});
} else {
res.end('Ok')
}
});
server.listen(3000);


when localhost:3000/get called long.js is forked and now when message from
child process come response is send.


2021/02/18

Importing Json to Elastic Search using Postman

  Download file https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json?raw=true

 

 File looks like below,it is not json array but in different form

 

{"index":{"_id":"1"}}

{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}

{"index":{"_id":"6"}}

{"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"}



Now in postman 

    Keep Endpoint as localhost:9200/accounts/docs/_bulk

    set request header Content-Type:application/json

     select body to be binary and select file as account.json


 

Response:

{

    "took": 593,

    "errors": false,

    "items": [

        {

            "index": {

                "_index": "accounts",

                "_type": "docs",

                "_id": "1",

                "_version": 1,

                "result": "created",

                "_shards": {

                    "total": 2,

                    "successful": 1,

                    "failed": 0

                },

                "_seq_no": 0,

                "_primary_term": 1,

                "status": 201

            }

        },

        {

            "index": {

                "_index": "accounts",

                "_type": "docs",

                "_id": "6",

                "_version": 1,

                "result": "created",

                "_shards": {

                    "total": 2,

                    "successful": 1,

                    "failed": 0

                },

                "_seq_no": 1,

                "_primary_term": 1,

                "status": 201

            }

        },

//more entries here

    ]

}


With Postman you can view all inserted records with GET request against URL localhost:9200/accounts/_search?pretty&size=1000


2021/02/06

normal function vs fat arrow function

Fat Arrow function and normal function differ in following aspect

new keyword use:

    Regular functions created using function declarations or expressions are constructible and callable. Since regular functions are constructible, they can be called using the new keyword.

            function add(x,y)
            {
                console.log(x+y)
            }
            new add(5,6)
        output:
             11

    However, the arrow functions are only callable and not constructible.Hence, they can never be invoked with the new keyword.
        sub =(x,y)=>{
            console.log(x-y)
        }


        new sub(5,6)
        
        Output:
            TypeError: sub is not a constructor


    Fat Arrow function can not be used as a constructors.



Duplicate Named function parameter:
    function add(x,x)
    {
        console.log(x);
    }
    add(5,7)
    
    Output:
        7
        
    but
    
    add=(x,x)=>{
       console.log(x);
    }

    add(5,6)        
        
    Output:    
        SyntaxError: Duplicate parameter name not allowed in this context
    
    With arrow functions, duplicate named arguments are always, regardless of strict or non-strict mode, invalid.
    
    
use of this keyword:
    let fruit = {
        name: "Mango",
        FatArrow: () => {
        console.log("My name is " + this.name); // no 'this' binding here
        },
        Regular() {
        console.log("My name is " + this.name); // 'this' binding works here
        }
    };

    fruit.FatArrow();
    fruit.Regular();    
    
    output:
        My name is undefined
        My name is Mango
        
        
    fat arrow functions do not have their own this. The value of this inside an fat arrow function remains the same throughout the lifecycle of the function and is always bound to the value of this in         the closest non-arrow parent function
arguments keyword:

   let myFunc = {  
    showArgs(){
     console.log(arguments);
    }
   };
   myFunc.showArgs(1, 2, 3, 4,5);
   
   
   Output:
       [Arguments] { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 }
    
   but
   
    let myFunc = {  
        showArgs : () => {
        console.log(...arguments);
      }
    };
    myFunc.showArgs(10,30,45,78);    
   
   output:
    [Arguments] {
    '0': {},
    '1': [Function: require] {
    resolve: [Function: resolve] { paths: [Function: paths] },
    main: Module {
    id: '.',
    path: '/home/sangram/projects/test10',
    exports: {},
    filename: '/home/sangram/projects/test10/test.js',
    loaded: false,
    children: [],
    paths: [Array]
    },
    extensions: [Object: null prototype] {
    '.js': [Function (anonymous)],
    '.json': [Function (anonymous)],
    '.node': [Function (anonymous)]
    },
    cache: [Object: null prototype] {
    '/home/sangram/projects/test10/test.js': [Module]
    }
    },
    '2': Module {
    id: '.',
    path: '/home/sangram/projects/test10',
    exports: {},
    filename: '/home/sangram/projects/test10/test.js',
    loaded: false,
    children: [],
    paths: [
    '/home/sangram/projects/test10/node_modules',
    '/home/sangram/projects/node_modules',
    '/home/sangram/node_modules',
    '/home/node_modules',
    '/node_modules'
    ]
    },
    '3': '/home/sangram/projects/test10/test.js',
    '4': '/home/sangram/projects/test10'
    }

   Arrow functions do not have an arguments binding. However, they have access to the arguments object of the closest non-arrow parent function.

How to use arguments in arrow function?

    var bar = (...arguments) => console.log(arguments);

    bar(1,3,6,89,67)   
    Output:
        [ 1, 3, 6, 89, 67 ]

       
Call Apply bind:   
   Arrow function not suitable for call, apply and bind methods, which generally rely on establishing a scope.
   
  

2021/02/05

POSTGRES -RANK,DENSE_RANK,ROW_NUMBER difference

what is difference between row-number,rank & dense-rank in postgres ?

Lets understand by examples.


create table for purpose of explination    
    CREATE TABLE emp(id serial,name varchar(100))

insert some rows

    insert into emp(name) values ('sagar'),('SAGAR'),('sangram'),('sangram'),('sachin'),('swapnil')
    
    
ROW_NUMBER function:
    Example:
    
    select name, ROW_NUMBER() OVER(ORDER BY name asc) from emp;    
    output:
        name         row_number
        "sachin"    1
        "sagar"    2
        "SAGAR"    3
        "sangram"    4
        "sangram"    5
        "swapnil"    6

        This function will sort record based on name and assign rank to it based on order.
        
 
 RANK function:
      Example:
          select name, RANK() OVER(ORDER BY name) from emp;
      
      Output:
        name    rank
        sachin    1
        sagar    2
        SAGAR    3
        sangram    4
        sangram    4
        swapnil    6


    Here Rank of sangram is 4 for both entries as two entries for sangram next record swapnil has rank 6 instead of 5.
    
    This function is similar to row number except identical rows get same rank & rank is skipped if identical records are found previously
    
Dense_Rank:
   Example:
   
       select name, DENSE_RANK() OVER(ORDER BY name) from emp
       
   Output:
    name        dense_rank
    "sachin"    1
    "sagar"    2
    "SAGAR"    3
    "sangram"    4
    "sangram"    4
    "swapnil"    5    
    
    Here sangram occurs twice so both get same rank 4 but next record swpnil get 5th rank & rank are not skipped if identical rows found.
   

2021/02/02

INSERT or update on postgres View

we usually create view is it feasible to insert or update on views instead of table lets explore.

   point to note that if insert succeed on view then data essentially got inserted into table only.


case simple view:
    create new view as follows

        create view vw_15
        as
        select * from book where id <=15

    attempt an insert

        insert into vw_15(title,author) values('my book','test')
    Output:

    INSERT 0 1

    Query returned successfully in 235 msec.

    Inference : insert got succeeded

Case view with computed column

    First we will create a view as follows
        create view vw_lw_author
        as
        select title,lower(author) as author from book

    try to insert into view

        insert into vw_lw_author(title,author) values('my book','SANGRAM')



    Output:
        ERROR:  cannot insert into column "author" of view "vw_lw_author"
        DETAIL:  View columns that are not columns of their base relation are not updatable.
        SQL state: 0A000
        
    Inference : insert failed
    
Case view with check option    
    create view vw_15_check
    as
    select * from book where id <=15 with check option    
    
     try to insert here
    
    insert into vw_15_check(title,author) values('my book','SANGRAM')

                                                 or

    insert into vw_lw_author(title,author) values('my book','sangram')
    
    Output:
        ERROR:  new row violates check option for view "vw_15_check"
        DETAIL:  Failing row contains (20, my book, SANGRAM, null, null, null, null).
        SQL state: 44000
    
    Inference : insert failed    
    
    
Updating with check option column
      our view

    create view vw_price_lte_200
    as
    select * from public."Movies" where "Price" <=200 with check option    
    
    we try to update
    
        update vw_price_lte_200 set "Price" = 150
        
        output:
            UPDATE 1

            Query returned successfully in 106 msec.
            
        Here update succeed


    Now try update that violate constraint
    
    update vw_price_lte_200 set "Price" = 600
    
    Output:
        ERROR:  new row violates check option for view "vw_price_lte_200"
        
    Here update fails.
        DETAIL:  Failing row contains (5, Sholay, 2019-10-10, Action, 600).
        SQL state: 44000

2021/01/29

 

In javascript there are console.log,console.debug,console.warn,console.error.so what each of this stands for


1) console.log : Black color text with no icon


2) console.info: Blue color text with icon


3) console.debug : Pure black color text


4) console.warn :Yellow color text with icon


5) console.error : Red Color text with icon


for purpose of illustartion I created a index.html file with following code


<html>

<head>

</head>

<body>

<script>

console.log('console.log');

console.info('console.info');

console.debug('console.debug');

console.warn('console.warn');

console.error('console.error');

</script>

</body>

</html>

Here is how it looks on browser console.



     

Console.log & console.debug looks same. while console.info has addition icon to console,log.

2021/01/28

How to check pair of word are anagram in Javascript

 An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.[1] For example, the word anagram itself can be rearranged into nag a ram, also the word binary into brainy and the word adobe into abode.

How to check given pait of word anagram

function IsAnagram(string11,string22)
{
    //remove white spaces & make both word in same case
    string1 = string11.replace(/ /g,'').toUpperCase();
    string2 = string22.replace(/ /g,'').toUpperCase();

    if(string1.length != string2.length)
    {
        return false;
    }else{

       let str1 =  [...string1];
       let str2 =  [...string2];

       str1 = str1.sort();
       str2 = str2.sort()

       string1 = str1.join('');
       string2 = str2.join('');

       if(string1 == string2)
       {
         return true;
       }else{
           return false;
       }

    }
}


console.log('"STOP" & "POST" combination is anagram :' + IsAnagram('STOP','POST'));

console.log('"FIRE" & "RIDE" combination is anagram :' + IsAnagram('FIRE','RIDE'));

console.log('"New York Times" & "monkeys write" combination is anagram :' + IsAnagram('New York Times','monkeys write'));


Output:
"STOP" & "POST" combination is anagram :true
"FIRE" & "RIDE" combination is anagram :false
"New York Times" & "monkeys write" combination is anagram :true

2021/01/10

Reading Files from Path Array ensure synchronous Nature:

we have a fileNames array which contain path to files read.Used bluebird to promisify fs module which makes us available promisified version readFileAsync of readFile.

Created three files 1.txt,2.txt and 3.txt with one two & three inside them inside data folder.Added one,two,three as text inside these three files.

Async Way:


var Promise = require("bluebird");

var fileNames =["data/1.txt","data/2.txt","data/3.txt"]

var fs = Promise.promisifyAll(require("fs"));


var promises = [];

console.log("Started");

for (var i = 0; i < fileNames.length; ++i) {

fs.readFileAsync(fileNames[i],'utf8').then(function(data){

console.log(data);

}).catch(function(err){

console.log(err);

})

}

console.log("Finished");


Output:

Started

Finished

two

one

three


Order of reading files is not maintained.


Using Promise.all


var Promise = require("bluebird");

var fileNames =["data/1.txt","data/2.txt","data/3.txt"]

var fs = Promise.promisifyAll(require("fs"));


console.log("Started");

var promises = [];

for (var i = 0; i < fileNames.length; ++i) {

promises.push(fs.readFileAsync(fileNames[i],'utf8'));

}

Promise.all(promises).then(function(result) {

console.log(result)

console.log("done");

});

console.log("Finished");


Output:

Started

Finished

[ 'one', 'two', 'three' ]

done


Response of reading three files get at once.


Synchronized Way:

var Promise = require("bluebird");

var fileNames =["data/1.txt","data/2.txt","data/3.txt"]

var fs = Promise.promisifyAll(require("fs"));


runLoop = async (cb)=>{

try{

let result =[];

for(let i=0;i < fileNames.length ;i++)

{

console.log("Fetching " + fileNames[i]) + "....";

await new Promise((resolve,reject)=>{

setTimeout(function(){

fs.readFileAsync(fileNames[i],'utf8').then(function(fileContent){

console.log("fileContent",fileContent)

result.push(fileContent)

resolve()

}).catch(function(err){

reject(err)

console.log("err",err);

//throw err;

})

}, 0);

});

}

cb(result)

}catch(ep)

{

console.log("ep",ep);

}

}


runLoop(function(data){

console.log("data",data);

})

 

Output:

Fetching data/1.txt

fileContent one

Fetching data/2.txt

fileContent two

Fetching data/3.txt

fileContent three

data [ 'one', 'two', 'three' ]



reads files in order they present in loop.I tried increasing content of 1.txt yet order remain same.


Synchronous For loop :

Loop can be made to wait until promise resolve which is put inside settimeout so as to wait for 2000 milliseconds.Each array item is printed after 2000 milliseconds delay.


let arr =[10,20,30,40,50,60,70,80,90]


runLoop = async ()=>{

for(let i=0;i < arr.length ;i++)

{

await new Promise((resolve,reject)=>{

setTimeout(function(){

resolve()

console.log(arr[i]);

}, 2000);

})

}

}


runLoop();


Output:

sangram@sangram-HP-Laptop-15-bs0xx:~/projects/nodejs/promise-all$ node loop.js

10

20

30

40

50

60

70

80

90


Here 10,20,30 ... these are printed after waiting for 2000 milliseconds for each iteration.

Promise.all in Node.js

Promise.all() static method used to aggregate results from multiple asynchronous operations.

The Promise.all() static method accepts a list of Promises and returns a Promise that:

  • resolves when every input Promise has resolved or

  • rejected when any of the input Promise has rejected.

Example 1:

const p1 = new Promise((resolve, reject) => {

setTimeout(() => {

console.log('The first promise has resolved');


resolve(10);

}, 1 * 1000);


});


const p2 = new Promise((resolve, reject) => {

setTimeout(() => {

console.log('The second promise has resolved');

resolve(20);

}, 2 * 1000);

});


const p3 = new Promise((resolve, reject) => {

setTimeout(() => {

console.log('The third promise has resolved');

resolve(30);

}, 3 * 1000);

});


Promise.all([p1, p2, p3])

.then(results => {

const total = results.reduce((p, c) => {return p + c},0);



console.log(`Results: ${results}`);

console.log(`Total: ${total}`);

});



Output:

The first promise has resolved

The second promise has resolved

The third promise has resolved

Results: 10,20,30

Total: 60


Here all promises resolves.Result of promise.all is an array containing result of each members result .e.g. [10,20,30]


Example 2:

const p1 = new Promise((resolve, reject) => {

setTimeout(() => {

console.log('The first promise has resolved');

resolve(10);

}, 1 * 1000);


});


const p2 = new Promise((resolve, reject) => {

setTimeout(() => {

console.log('The second promise has rejected');

reject('Failed');

}, 2 * 1000);

});


const p3 = new Promise((resolve, reject) => {

setTimeout(() => {

console.log('The third promise has resolved');

resolve(30);

}, 3 * 1000);

});


Promise.all([p1,p2, p3])

.then(function(results){

console.log(`Results: ${results}`);

}) // never execute

.catch(function(err){

console.log("err:",err);

});

Output:

The first promise has resolved

The second promise has rejected

err: Failed

The third promise has resolved




Here one promise get rejected so promise.all get rejected too.

2021/01/01

Immediately-invoked Function Expression (IIFE)

 An Immediately-invoked Function Expression (IIFE for friends) is a way to execute functions immediately, as soon as they are created.

IIFEs are very useful because they don’t pollute the global object, and they are a simple way to isolate variables declarations.

Example 1:

(function (a)

{

console.log(a);

})(5)


Output:

5

or It can be defined as


(function() {

console.log("Hello from IIFE!");

}());


Output:

Hello from IIFE!



Assigning the IIFE to a variable stores the function's return value, not the function definition itself.

Example 2:

var func = (function ()

{

var a=10;

return a

})();

console.log(func)


output:

10


The variable within the expression can not be accessed from outside it.

Example 3:

var func = (function ()

{

var a=10;

return a

})();

console.log(a);



output:

ReferenceError: a is not defined


Another way of writing IIFE:

Example 4:

!function() {

console.log("Hello from IIFE!");

}();




Whenever JavaScript sees function keyword as the first word in a valid statement, it expects that a function definition is going to take place. So to stop this from happening, we are prefixing “!” in-front of the function keyword on line 1


The above variation can be used by replacing “!” with “+”, “-”, or even “~” as well. Basically any unary operator can be used.

e.g. following is also valid IIFE

Example 5:

~function() {

console.log("Hello from IIFE!");

}();


Example 6:


(function IIFE_initGame() {

// Private variables that no one has access to outside this IIFE

var lives;

var weapons;

init();


// Private function that no one has access to outside this IIFE

function init() {

lives = 5;

weapons = 10;

console.log(`lives: ${lives} and weapons: ${weapons}`);

}

}());


Output:

lives: 5 and weapons: 10


we have declared two variables inside the IIFE and they are private to that IIFE. No one outside the IIFE has access to them. Similarly, we have an init function that no one has access to outside the IIFE. But the init function can access those private variables.



How to make variable private in Javascript

Javascript does not have access modifier to be precise like other programming languages say Java.So How can we make a variable private.Let us see.

Consider following code snippet


//Immediately invoked function expression

let Person = (function()

{

let _age=0;

return {

firstName:"sangram",

lastName:"desai",

get age()

{

return _age;

},

set age(val)

{

_age = val

}

};

})()


//printing all accessible properties of object

for(let prop in Person)

{

console.log(prop);

}


console.log(Person.firstName)

console.log(Person.lastName)


//public properties

Person.age = 40;

console.log(Person.age)


//private variable

console.log(Person._age)



Output:

firstName

lastName

age

sangram

desai

40

undefined


Here _age is defined outside to make it private property.firstName & lastName are public properties of Person object.we defined age property using getter & setter syntax to access private property _age

We have printed all accessible properties of Person Object which happens to be

firstName

lastName

age


note _age is not there.

Then we try to directly print _age as follows


console.log(Person._age)


It comes out as undefined.

Getter & Setter in Javascript:

Es6 has functionality define property getter & setter.

Lets us see how we can do it.


Here is our code


let Person =

{

firstName:"sangram",

lastName:"desai",


get fullName()

{

return `${this.firstName} ${this.lastName}`;

},


set fullName(val)

{

let parts = val.split(' ');

this.firstName = parts[0];

this.lastName = parts[1];

}

};


console.log(Person.fullName);

Person.fullName = "Saurabh Ganguli"

console.log(Person.fullName);

 

Output:

sangram desai
Saurabh Ganguli

We created getter property fullName as follows


get fullName()

{

return `${this.firstName} ${this.lastName}`;

}


while setter fullname is defined as follows


set fullName(val)

{

let parts = val.split(' ');

this.firstName = parts[0];

this.lastName = parts[1];

}


we can access fullName as Person.fullName .

To set fullName a value we can do following

Person.fullName = "Saurabh Ganguli".