A list of some of my favorite JS libraries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Prompts */ | |
// npm init -y | |
// npm install express | |
// touch index.js | |
/// ------------------------ | |
// Contents of `index.js` file | |
import express from "express"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'hello'. includes('ell'); // true | |
'hello'.startswith('hell'); // true | |
'hello' endsWith('ello'); // true | |
'&' . repeat (3); // | |
'hello' .codePointAt(1); // 101 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using Promise to delay Execution | |
function delay(duration) { | |
return new Promise((resolve) => { | |
setTimeout(() => resolve(), duration); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const testingTeam = { | |
lead: 'Alex', | |
engineer: 'Bill', | |
// This is generator function | |
[Symbol.iterator]: function* () { | |
yield this.lead; | |
yield this.engineer | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Factory Function | |
function CreateBookStore(inventory) { | |
return { | |
inventory, | |
getInventoryValue() { | |
// if arrow function is used here, this will return `undefined`, | |
// hence using short-hand/object literal for function | |
// this is similar to `getInventoryValue: function () { ......... }` | |
return this.inventory.reduce((total, book) => total + book.price, 0); | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Car(options) { | |
this.title = options.title; | |
} | |
Car.prototype.drive = function () { | |
return 'drive' | |
} | |
function Toyota(options) { | |
Car.call(this, options); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Write some code, that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers. | |
// e.g. [[1,2,[3]],4] -> [1,2,3,4]. | |
function flatten(arr){ | |
let newArray = []; | |
for(let i=0; i< arr.length; i++){ | |
if(Array.isArray(arr[i])){ | |
newArray = newArray.concat(flatten(arr[i])) | |
}else{ | |
newArray.push(arr[i]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const supportsNativeSmoothScroll = 'scrollBehavior' in document.documentElement.style; | |
function scrollToView(elem) { | |
if(supportsNativeSmoothScroll){ | |
// https://caniuse.com/#feat=mdn-api_window_scrollto | |
// As of publish date of this gist, | |
// this is only supported in 52% browsers, | |
// So, the next section (`else{`) is a fallback | |
window.scrollTo({ | |
behavior: 'smooth', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// View Comment section for explanation | |
function _$(elem){ return d.querySelectorAll(elem) } | |
function observer(trigger, func_vis, func_hidden, threshold){ | |
var t = threshold ? threshold : 1, | |
observable_var; | |
NewerOlder