using mocha/chai/sinon for node.js unit-tests? check out my utility: mocha-stirrer to easily reuse test components and mock require dependencies
This file contains hidden or 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
/** | |
* React custom hook to fire a function given an interval | |
* @usage: useInterval(()=>{...},1000); | |
**/ | |
function useInterval(callback, delay) { | |
const savedCallback = useRef(); | |
useEffect(() => { | |
savedCallback.current = callback; | |
}); |
This file contains hidden or 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 compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args))); | |
/* | |
const add5 = x => x + 5 | |
const multiply = (x, y) => x * y | |
const multiplyAndAdd5 = compose(add5, multiply) | |
multiplyAndAdd5(5, 2) -> 15 | |
*/ |
This file contains hidden or 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 flattenArray(arr) { | |
let found = []; | |
arr.map((elem, index) => { | |
if (Array.isArray(elem)) { | |
found = found.concat(flattenArray(elem)); | |
} else { | |
found.push(elem); | |
} | |
}); |
This file contains hidden or 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
let filenames = ['index.html', 'blog.html', 'terms.html']; | |
Promise.all(filenames.map(readFilePromise)) | |
.then(files => { | |
console.log('index:', files[0]); | |
console.log('blog:', files[1]); | |
console.log('terms:', files[2]); | |
}) |
This file contains hidden or 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 objArr = [ | |
{ letter: 'a', num: 1 }, | |
{ letter: 'b', num: 2 }, | |
{ letter: 'c', num: 3 } | |
] | |
const updateObjInArr = (oldArr, searchKey, oldVal, newVal) => { | |
return oldArr.map(item => { | |
if (item[searchKey] === oldVal) { | |
item[searchKey] = newVal |
This file contains hidden or 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
var uniqueArray = function(arrArg) { | |
return arrArg.filter(function(elem, pos,arr) { | |
return arr.indexOf(elem) == pos; | |
}); | |
}; | |
var uniqEs6 = (arrArg) => { | |
return arrArg.filter((elem, pos, arr) => { | |
return arr.indexOf(elem) == pos; | |
}); |
This file contains hidden or 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 getProp = (obj, key) => | |
key.split('.').reduce( (o, x) => | |
(typeof o == "undefined" || o === null) ? o : o[x] | |
, obj); |
This file contains hidden or 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
/* | |
* promiseSerial resolves Promises sequentially. | |
* @example | |
* const urls = ['/url1', '/url2', '/url3'] | |
* const funcs = urls.map(url => () => $.ajax(url)) | |
* | |
* promiseSerial(funcs) | |
* .then(console.log) | |
* .catch(console.error) | |
*/ |
NewerOlder