Skip to content

Instantly share code, notes, and snippets.

View qborreda's full-sized avatar

Quique Borredá qborreda

View GitHub Profile
@qborreda
qborreda / useInterval
Created June 6, 2019 15:16
useInterval custom hook
/**
* React custom hook to fire a function given an interval
* @usage: useInterval(()=>{...},1000);
**/
function useInterval(callback, delay) {
const savedCallback = useRef();
useEffect(() => {
savedCallback.current = callback;
});
@qborreda
qborreda / compose.js
Created December 16, 2017 21:11
JavaScript ES6 utility functions
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
*/
@qborreda
qborreda / flatten_array.js
Created October 16, 2017 14:35
Flatten an array of numbers
function flattenArray(arr) {
let found = [];
arr.map((elem, index) => {
if (Array.isArray(elem)) {
found = found.concat(flattenArray(elem));
} else {
found.push(elem);
}
});
@qborreda
qborreda / promise_all.js
Last active October 2, 2017 11:38
Promisify a node function
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]);
})
@qborreda
qborreda / cookiesUtil.js
Created September 20, 2017 15:54
Util to get, set, delete cookies
export function setCookie(c_name, value, exdays, domain) {
let exdate = new Date();
let time = exdate.getTime();
time += 3600 * 1000;
exdate.setTime(time);
exdate.setDate(exdate.getDate() + exdays);
let c_value =
escape(value) + (exdays == null ? '' : '; expires=' + exdate.toUTCString());
c_value += domain ? ';domain=' + domain : '';
c_value += ';path=/';
@qborreda
qborreda / ultimate-ut-cheat-sheet.md
Created June 19, 2017 16:54 — forked from yoavniran/ultimate-ut-cheat-sheet.md
The Ultimate Unit Testing Cheat-sheet For Mocha, Chai and Sinon

The Ultimate Unit Testing Cheat-sheet

For Mocha, Chai and Sinon

using mocha/chai/sinon for node.js unit-tests? check out my utility: mocha-stirrer to easily reuse test components and mock require dependencies


@qborreda
qborreda / updateObjInArr.js
Created May 8, 2017 09:47
Updates key value within an array of objects
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
@qborreda
qborreda / uniq.js
Created April 26, 2017 14:45 — forked from telekosmos/uniq.js
Remove duplicates from js array (ES5/ES6)
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;
});
const getProp = (obj, key) =>
key.split('.').reduce( (o, x) =>
(typeof o == "undefined" || o === null) ? o : o[x]
, obj);
/*
* 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)
*/