Skip to content

Instantly share code, notes, and snippets.

View devarajchidambaram's full-sized avatar

devaraj devarajchidambaram

View GitHub Profile
@devarajchidambaram
devarajchidambaram / index.js
Created October 8, 2018 14:09
Pure vs Impure function
/**
Characteristics of Pure Function:
The return value of the pure func­tions solely depends on its arguments Hence, if you call the pure func­tions with the same set of argu­ments, you will always get the same return values.
They do not have any side effects like net­work or data­base calls
They do not mod­ify the argu­ments which are passed to them
Char­ac­ter­isitcs of Impure functions
const arr = [10, 12, 15, 21];
for (var i = 0; i < arr.length; i++) {
(function(i){
setTimeout(function() {
console.log('The index of this number is: ' + i);
}, 3000);
})(i)
}
@devarajchidambaram
devarajchidambaram / eventdelegation.html
Created October 4, 2018 11:58
Event delegation in js
<!---
Event delegation in JS
Ref
https://medium.freecodecamp.org/3-questions-to-watch-out-for-in-a-javascript-interview-725012834ccb
-->
<html>
<body>
<ul id="todo-app">
@devarajchidambaram
devarajchidambaram / index.js
Created October 4, 2018 05:06
Parseint vs Number in js
/**
parseInt("123hui")
returns 123
Number("123hui")
returns NaN
parseInt("1.234334")
returns 1
Number("1.234334")
@devarajchidambaram
devarajchidambaram / index.js
Created September 28, 2018 11:10
DNS lookup vs resolve
/**These functions are implemented quite differently than dns.lookup(). They do not use getaddrinfo(3) and they always perform a DNS
query on the network. This network communication is always done asynchronously, and does not use libuv's threadpool.
Always use DNS.resolve it network operations
Dont use use DNS.lookup its uses the threadpool (Thread pool is limited)**/
@devarajchidambaram
devarajchidambaram / text
Created September 7, 2018 07:49
List of Automation testing libraries for nodejs
Provide a testing structure (Mocha, Jasmine, Jest, Cucumber)
Provide assertions functions (Chai, Jasmine, Jest, Unexpected)
Generate, display, and watch test results (Mocha, Jasmine, Jest, Karma)
Generate and compare snapshots of component and data structures to make sure changes from previous runs are intended (Jest, Ava)
Provide mocks, spies, and stubs (Sinon, Jasmine, enzyme, Jest, testdouble)
Generate code coverage reports (Istanbul, Jest, Blanket)
Provide a browser or browser-like environment with a control on their scenarios execution (Protractor, Nightwatch, Phantom, Casper)
@devarajchidambaram
devarajchidambaram / stubfs.js
Created September 7, 2018 07:28
How to stub fs.readFile functions
const fs = require('fs')
function readFile (filename) {
if (fs.existsSync(filename)) {
return fs.readFileSync(filename, 'utf8')
}
throw new Error('Cannot find file ' + filename)
}
describe('mocking individual fs sync methods', () => {
@devarajchidambaram
devarajchidambaram / Timeout
Created September 6, 2018 07:19
Timeout express bound
var http = require('http');
var srvr = http.createServer(function (req, res) {
setTimeout(function(){
res.write('Hello World!');
res.end();
}, 5000)
});
srvr.listen(8080);
@devarajchidambaram
devarajchidambaram / passport.js
Created August 30, 2018 07:18
Passport js sample
/* EXPRESS SETUP */
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
@devarajchidambaram
devarajchidambaram / express_session.js
Created August 30, 2018 04:38
Express session in nodejs
var express = require('express');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var app = express();
app.use(cookieParser());
//For every request the session id is assigned if there is no session id
//We can attach fields in req.session.key = value pair
app.use(session({