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
function clone(obj, deep) { | |
if (!(obj instanceof Object)) { | |
return obj; | |
} | |
var descriptors = {}; | |
Object.getOwnPropertyNames(obj).forEach(function(name) { | |
var prop = Object.getOwnPropertyDescriptor(obj, name); |
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
// array-like only | |
function get(arr, i) { | |
var len = arr.length; | |
i = i % len; | |
if (i < 0) { | |
i += len; | |
} | |
return arr[i]; | |
} |
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
// I want to add multiple properties to a target object in one statement without adding extra variables to the current scope | |
// This can be done in ES6 with Object.assign: | |
var targetObj = {}; | |
Object.assign({ | |
a: 1, | |
b: 2 | |
}, targetObj); |
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
(function() { | |
var highlight = require('cardinal').highlight; | |
var beautify = require('js-beautify').js_beautify; | |
var log = console.log; | |
console.log = function() { | |
log.apply(console, Array.prototype.map.call(arguments, function(x) { | |
return highlight('(' + beautify(JSON.stringify(x)) + ')'); | |
})); | |
}; |
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
REPORTER ?= nyan | |
TESTS = test/*.js | |
test: ; @NODE_ENV=test ./node_modules/.bin/mocha \ | |
--timeout 1000 --reporter $(REPORTER) $(TESTS) | |
.PHONY: test |
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
var pattern = /^(\d+?)(0+)$/; | |
function ne(n) { | |
'use strict'; | |
var str = String(n); | |
var match = str.match(pattern); | |
var len; | |
if (!match) { |
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
'use strict'; | |
function d2r(d) { | |
return d * Math.PI / 180; | |
} | |
function r2d(r) { | |
return r * 180 / Math.PI; | |
} |
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
function reload(url, delay) { | |
url = url || '/'; | |
delay = delay || 10; | |
var req = new XMLHttpRequest(); | |
req.onload = window.location.reload.bind(window.location); | |
req.onerror = setTimeout.bind(null, reload.bind(null, url, delay), delay); | |
req.open('head', url, true); |
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
function extend(target, source) { | |
Object.keys(source).forEach(function(key) { | |
var sourceItem = source[key]; | |
var targetItem = target[key]; | |
if (sourceItem instanceof Object && targetItem !== undefined) { | |
extend(targetItem, sourceItem); | |
} else { | |
target[key] = sourceItem; | |
} |
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
var listeners = { | |
a: function(done) { | |
// do something | |
done(); | |
}, | |
b: function(done) { | |
// do something | |
done(); | |
} | |
}; |
OlderNewer