Skip to content

Instantly share code, notes, and snippets.

@scottmo
Last active June 24, 2019 20:16
Show Gist options
  • Save scottmo/c41a651b0c07dbe86d77a878c1bdb5d6 to your computer and use it in GitHub Desktop.
Save scottmo/c41a651b0c07dbe86d77a878c1bdb5d6 to your computer and use it in GitHub Desktop.
JS Snippets
/*
* promiseSerial resolves Promises sequentially.
* Puts results into an array and get it from then()
*
* @example
* const urls = ['/url1', '/url2', '/url3']
* const funcs = urls.map(url => () => $.ajax(url))
*
* promiseSerial(funcs)
* .then(console.log.bind(console))
* .catch(console.error.bind(console))
*/
const chainPromise = funcs =>
funcs.reduce((promise, func) =>
// can pass result to func if prev result is needed
promise.then(result => func().then(Array.prototype.concat.bind(result))),
Promise.resolve([]));
function generateCombo(sets,f,context){
if (!context) context=this;
var p=[],max=sets.length-1,lens=[];
for (var i=sets.length;i--;) lens[i]=sets[i].length;
function dive(d){
var a=sets[d], len=lens[d];
if (d==max) for (var i=0;i<len;++i) p[d]=a[i], f.call(context,p);
else for (var i=0;i<len;++i) p[d]=a[i], dive(d+1);
p.pop();
}
dive(0);
}
generateCombo([['a', 'b'], [1, 2]], function(combo) {
console.log(combo);
}); // [a, 1], [a, 2], [b, 1], [b, 2]
// From http://stackoverflow.com/a/105074
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4()
+ '-' + s4()
+ '-' + s4()
+ '-' + s4()
+ '-' + s4() + s4() + s4();
}
function isEqual(objA, objB, config) {
function isComparableKey(key) {
return (config.ignore || []).indexOf(key) < 0;
}
var aProps = Object.getOwnPropertyNames(objA).filter(isComparableKey);
var bProps = Object.getOwnPropertyNames(objB).filter(isComparableKey);
if (aProps.length !== bProps.length) {
return false;
}
for (var i = 0; i < aProps.length; i++) {
var propName = aProps[i];
var propA = aProps[propName];
var propB = bProps[propName];
if (config.shallow) {
if (propA !== propB) {
return false;
}
} else {
if (typeof propA === 'object') {
if (!isEqual(propA, propB)) {
return false;
}
} else {
if (propA !== propB) {
return false;
}
}
}
}
return true;
}
function memoizedPromise(fn) {
var cache = {};
return function() {
var argsStr = JSON.stringify(arguments);
if (cache[argsStr]) {
return Promise.resolve(cache[argsStr]);
} else {
return fn.apply(null, arguments).then(function(result) {
cache[argsStr] = result;
return result;
});
}
}
}
function permute(permutation) {
const length = permutation.length;
const result = [permutation.slice()];
const c = new Array(length).fill(0);
let i = 1;
let k;
while (i < length) {
if (c[i] < i) {
k = i % 2 && c[i];
let p = permutation[i];
permutation[i] = permutation[k];
permutation[k] = p;
c[i] += 1;
i = 1;
result.push(permutation.slice());
} else {
c[i] = 0;
i += 1;
}
}
return result;
}
function zeroFilled(input, targetLength, doAppend) {
const zerosToAdd = targetLength - input.length;
let zeros = '';
for (let i = 0; i < zerosToAdd; i++) {
zeros += '0';
}
return doAppend ? input + zeros : zeros + input;
}
/**
> zip([['row0col0', 'row0col1', 'row0col2'],
['row1col0', 'row1col1', 'row1col2']]);
[["row0col0","row1col0"],
["row0col1","row1col1"],
["row0col2","row1col2"]]
*/
const zip = rows => rows[0].map((_,c) => rows.map(row=>row[c]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment