Monday, 19 June 2017

underscore module with nodeJs tutorial

Underscore is a JavaScript library that provides a lot of useful helper functions to fast the node development. following are list of available array/object functions.

Question: What is underscore module?
Underscore is a JavaScript library that provides a lot of useful functional helpers without extending any built-in objects.



Question: How to install async module?
npm install underscore




Question: How to include request module in node project?
var _ = require("underscore");




Question: Give an example of each in underscore?
 
_.each(['Understand', 'underscore', 'Module'], function(value,pos){
    console.log(value+' stored at '+pos);    
});

Output
Understand stored at 0
underscore stored at 1
Module stored at 2




Question: What are other collections in underscore module?
each
map
reduce
reduceRight
find
filter
where
findWhere
reject
every
some
contains
invoke
pluck
max
min
sortBy
groupBy
indexBy
countBy
shuffle
sample
toArray
size
partition



Question: How to create a range array?
 
var lists=_.range(5);
console.log(_.first());




Question: How to get first nth element from array?
 
var lists=["One","two","three","four", "five"];
console.log(_.first(lists)); //One
console.log(_.first(lists,2)); //One two
console.log(_.first(lists,3)); //One two three




Question: How to check a variable is empty OR Not ?
var name=''
console.log(_.isEmpty(name));//true

name='Hello User'
console.log(_.isEmpty(name));//false




Question: What are array functions in underscore module?
first
initial
last
rest
compact
flatten
without
union
intersection
difference
uniq
zip
unzip
object
indexOf
lastIndexOf
sortedIndex
findIndex
findLastIndex
range



Question: How to get key value of an object data?
 
var lists={1:"One",2:"two",3:"three",4:"four", 5:"five"};
console.log(_.keys(lists)); //1,2,3,4,5




Question: What are the object functions in underscore module?
 
keys
allKeys
values
mapObject
pairs
invert
create
functions
findKey
extend
extendOwn
pick
omit
defaults
clone
tap
has
matcher
property
propertyOf
isEqual
isMatch
isEmpty
isElement
isArray
isObject
isArguments
isFunction
isString
isNumber
isFinite
isBoolean
isDate
isRegExp
isError
isNaN
isNull
isUndefined


Friday, 16 June 2017

Async module with NodeJs tutorial

Example of parallel, concat and logs example in async? List all the methods available in async

Question: What is async module?
Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript.



Question: How to install async module?
npm install async




Question: How to include request module in node project?
var async = require('async');




Question: Concatenate the files from different directories?
 
async.concat(['data', 'data1'], fs.readdir, function(err, files) {
    if (err) {
        return console.log(err);
    }    
    /*files is now a list of filenames that exist in the 3 directories*/
    console.log(files);    
});




Question: What are other collection methods available in async ?
concat
concatSeries
detect
detectLimit
detectSeries
each
eachLimit
eachOf
eachOfLimit
eachOfSeries
eachSeries
every
everyLimit
everySeries
filter
filterLimit
filterSeries
groupBy
groupByLimit
groupBySeries
map
mapLimit
mapSeries
mapValues
mapValuesLimit
mapValuesSeries
reduce
reduceRight
reject
rejectLimit
rejectSeries
some
someLimit
someSeries
sortBy
transform




Question: Give an example of parallel in async?
async.parallel([
    function(callback) {
        setTimeout(function() {
            callback(null, 'one');
        }, 200);
    },
    function(callback) {
        setTimeout(function() {
            callback(null, 'two');
        }, 100);
    }
],
// optional callback
function(err, results) {
        console.log(results); //['one','two']
});




Question: What other control methods in async?
applyEach
applyEachSeries
auto
autoInject
cargo
compose
doDuring
doUntil
doWhilst
during
forever
parallel
parallelLimit
priorityQueue
queue
race
retry
retryable
seq
series
times
timesLimit
timesSeries
tryEach
until
waterfall
whilst




Question: Give an example of async.log?
var hello = function(name, callback) {
    setTimeout(function() {
        callback(null, 'hello ' + name);
    }, 1000);
};
async.log(hello, 'World 1'); //Hello World 1
async.log(hello, 'World 2'); //Hello World 2
async.log(hello, 'World 3'); //Hello World 3




Question: What other Utiles methods in async?
Utils
apply
asyncify
constant
dir
ensureAsync
log
memoize
nextTick
reflect
reflectAll
setImmediate
timeout
unmemoize