Created
October 12, 2017 14:05
-
-
Save simontraill/2bbd6de7aafe5b7443156f85d85e016e to your computer and use it in GitHub Desktop.
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
/* | |
* groupBy | |
* ======= | |
* Given array of objects iter, group by object property prop and return | |
*/ | |
function gb(iter,prop) { | |
var r = {}; | |
iter.map(function(e) { | |
console.log(e); | |
(r[e[prop]] = r[e[prop]] || []).push(e); | |
}); | |
return r; | |
} | |
/* | |
* extend | |
* ====== | |
* - Extend ("Assign") object non-inherited properties from arguments | |
* - Bonus points for mentioning this is available as _.extend(), _.assign, or Object.assign() in ES6 | |
*/ | |
Object.prototype.e = function() { | |
for (var i=0;i<arguments.length;i++) { | |
for (var prop in arguments[i]) { | |
if(arguments[i].hasOwnProperty(prop)) { | |
this[prop] = arguments[i][prop]; | |
} | |
} | |
} | |
} | |
/* | |
* counter | |
* ======= | |
* - Increments and returns a counter on every call | |
* - Bonus points for talking about closures or IIFE | |
* - Synonyms: "add", "increment" | |
*/ | |
var c = (function() { | |
var _c = 0; | |
return function() { return _c ++ }; | |
})(); | |
/* | |
* find | |
* ==== | |
* - Return first matching item in this array based on a predicate | |
* - Bonus points for pointing out this is a (bad) polyfill for an ES6 method, and implemented in many other _ type libs | |
*/ | |
Array.prototype.f = function(pred) { | |
for (var i in this) { | |
if (pred(this[i])) { | |
return this[i]; | |
} | |
} | |
} | |
/* | |
* parseURL | |
* ======== | |
* Given a url string u and an anchor property p - "host", or "pathname" for example - return the requested property | |
*/ | |
function pu(u,p) { | |
var a = document.createElement('a'); | |
a.href = u; | |
return a[p]; | |
} | |
/* | |
* toggleActive | |
* =========== | |
* - Toggle presence of class 'active' for all DOM elements with class x | |
* - Assumes a little familiarity with jQuery and CSS selectors, which seems reasonable | |
* - Bonus points for understanding the ~= (contains) syntax | |
*/ | |
var ta = function(x) { | |
$('[class~=' + x + ']').each(function() { | |
$(this).toggleClass('active'); | |
}); | |
}; | |
/* | |
* disableClick | |
* ============= | |
* - Disable click events on elements matching selector | |
* - Again assumes jquery | |
*/ | |
function dc(selector) { | |
$(selector).click(function(e) { | |
e.preventDefault(); | |
e.stopPropagation(); | |
}); | |
} | |
/* | |
* cachePromise | |
* ============ | |
* - On success, memoise / "cache" the result of a potentially expensive promise, subsequently return it in preference to running the operation again | |
* - Assumes knowlege of a promise library (angular/react/bluebird.js/Q etc, or ES6 promises) | |
* - I've used ES6 style promises here becaue they should be simple enough to work out if you know any other promise implementation | |
*/ | |
var c; | |
function cp() { | |
return new Promise(function(resolve,reject) { | |
if (c) { | |
resolve(c); | |
} else { | |
callOut().then(function(success) { | |
c = success; | |
resolve(success); | |
}); | |
} | |
}); | |
} | |
/* | |
* promisify | |
* ========= | |
* - Turn x into a pre-resolved promise | |
* - Again, uses ES6-ish syntax | |
* - Bonus points: variants of this already exist in bluebird.js, angular, etc with different names | |
* - Synonyms: "when", "$when", "toPromise", "resolve" , etc | |
*/ | |
var p = function(x) { | |
return new Promise(function(resolve,reject) { | |
resolve(x); | |
}); | |
} | |
/* | |
* callbackToPromise | |
* ================= | |
* - Again I'm using ES6 syntax here because it's probably more ubiquitous | |
* - Return a promise with results of calling error,callback style function func. | |
* - Synonyms for p: 'promisify', 'toPromise', 'asPromise', 'callback to promise', etc. | |
*/ | |
function c2p(func) { | |
return new Promise(function(resolve,reject) { | |
func(function(error,result) { | |
if (error) { | |
reject(error); | |
} else { | |
resolve(result); | |
} | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment