Skip to content

Instantly share code, notes, and snippets.

@IUnknown68
IUnknown68 / createCollectionProxyFn.js
Created June 29, 2015 11:33
Collection function calling each item (Like `setModified()`, `getModified()`).
//------------------------------------------------------------------------------
// Create a function `collection.fnName`.
// The function applies `_.lodashFn` on `collection` and calls `fnName` on
// each item.
// @param collection Any collection lodash can process
// @param fnName Name of function generated.
// @param lodashFn Name of the lodash collection function to apply.
// Defaults to 'each'.
function createCollectionProxyFn(collection, fnName, lodashFn) {
lodashFn = lodashFn || 'each';
@IUnknown68
IUnknown68 / DependencyManager.js
Last active August 29, 2015 14:27
The DependencyManager handles asynchronous module initialization with dependencies.
//------------------------------------------------------------------------------
// DependencyManager
// The DependencyManager handles asynchronous module initialization with
// dependencies.
//
// Example:
// =======
// Modules A and B load asynchronously.
// Module C requires A and B and loads synchronously.
// Module D requires C and loads asynchronously.
@IUnknown68
IUnknown68 / MessageLoop.js
Created August 13, 2015 09:56
A hierarchical message loop
//==============================================================================
//==============================================================================
window.MessageLoop = (function(){
//============================================================================
var _Set;
if ('undefined' !== typeof Set) {
_Set = Set;
}
else {
@IUnknown68
IUnknown68 / HistoryManager.js
Created August 18, 2015 18:13
History manager.
//==============================================================================
(function(){
var _currentIndex = 0;
var _history = [{
url: location.hash.substr(1) || '/',
index: _currentIndex
}];
var _currentState = _history[_currentIndex];
var _ignorePopstate = false;
/**************************************************************************//**
* @file
* @brief SemVer.js: Semantic version implementation.
* @author Arne Seib <[email protected]>
* @copyright 2015 Arne Seib (http://www.seiberspace.de).
*****************************************************************************/
SemVer = (function() {
//------------------------------------------------------------------------------
// private function: validate `from[name]` with validator `type`.
@IUnknown68
IUnknown68 / one-two-five.js
Last active November 25, 2015 13:12
Generate a 1-2-5-10-20-50... scale
function createScale(vals, iterations) {
var m = vals.length * iterations;
for (var n = 0; n < m; n++) {
var x = vals[n % vals.length] * Math.pow(10, Math.floor(n / vals.length));
console.log(x);
}
}
createScale([1,2,5], 3);
@IUnknown68
IUnknown68 / doWhen.js
Last active January 27, 2016 09:15
Run fn when Promise.all on remaining arguments gets resolved.
// Sugar.
// Instead of
//
// Promise.all([2, calculateValB(3), calculateValC(4)]).then((result) => {
// var res = calculateResult(result[0], result[1], result[2]);
// /* process result */
// });
//
// write
//
function step1(result) {
console.log('call step1');
return new Promise(function(resolve, reject) {
console.log('resolve step1');
result['step1'] = 'Step 1';
return resolve(result);
});
}
function step2(result) {
@IUnknown68
IUnknown68 / MPromise.js
Last active September 28, 2018 01:39
Promise that can be resolved with multiple values.
class MPromise extends Promise {
constructor(executor) {
super((resolve, reject) =>
executor((...args) => resolve(args), (...args) => reject(args))
);
}
then(onFulfilled, onRejected) {
return super.then(
onFulfilled ? (args) => onFulfilled(...args) : undefined,
@IUnknown68
IUnknown68 / colorBrowserConsole.js
Created May 21, 2017 12:47
Adds color functions to the browser-console (e.g. console.green() or console.greenB() for bold-green. Simple and primitive.
(() => {
['white', 'silver', 'gray', 'black', 'red', 'maroon', 'yellow', 'olive', 'lime',
'green', 'aqua', 'teal', 'blue', 'navy', 'fuchsia', 'purple'
].forEach(color => {
console[color] = (...args) => {
console.log(`%c${args.join(' ')}`, `color:${color}`)
}
console[`${color}B`] = (...args) => {
console.log(`%c${args.join(' ')}`, `color:${color};font-weight:bold`)
}