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 runningOnNode(){ | |
return typeof module !== 'undefined' && module.exports; | |
} |
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
//inserts 1 to n items into an array arr starting at position pos | |
function insertAt(arr, pos /* , item1, item2, ...itemN */){ | |
switch (arguments.length){ | |
case 0: case 1: case 2: | |
break; | |
case 3: | |
//the default case would work here also, but it's faster it we avoid the extra concat and slice... | |
arr.splice(pos, 0, item); | |
break; | |
default: |
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
public static string Multiply(this string st, int times) | |
{ | |
//all merit for this so concise implementation goes to this guy: | |
//http://stackoverflow.com/a/8520023/169558 | |
return new StringBuilder().Insert(0, st, times).ToString(); | |
} |
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 Lazy(type){ | |
var _arguments = [].slice.call(arguments, 1); | |
this.create = function(){ | |
var inst = Object.create(type.prototype); | |
//need this extra inst2 to follow the correct behaviour in case the constructor returns something | |
var inst2 = type.apply(inst, _arguments); | |
return inst2 || inst; | |
}; | |
} |
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
/* | |
Polyfill for the Object.watch/Object.unwatch functions available in Mozilla browsers | |
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/watch | |
you have a test here: | |
http://www.telecable.es/personales/covam1/deployToNenyures/SourceCode/Object.watch.test.js | |
and can read more here: | |
http://deploytonenyures.blogspot.com.es/2013/02/objectwatch-polyfill.html | |
*/ |
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 getPrototypeChainOf(obj){ | |
var chain = []; | |
var __prototype__ = Object.getPrototypeOf(obj); | |
//any protype chain ends in Object.prototype.__proto__, that is null | |
while (__prototype__){ | |
chain.push(__prototype__); | |
__prototype__ = Object.getPrototypeOf(__prototype__); | |
} | |
return chain; | |
} |
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
// deploytonenyures.blogspot.com | |
var singletonizer = { | |
singletonize: function(initialFunc){ | |
if (typeof initialFunc != "function") | |
throw new TypeError("singletonizer.singletonize called on a non function"); | |
//desingletonize works also as a flag to know if the function has been singletonized | |
if (initialFunc.desingletonize) | |
return initialFunc; |
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
//decorates a given function with checking/assignment of default values to undefined parameters | |
function decorateWithDefaultsChecking(targetFunc /* ,default1, default2... */){ | |
var defaults = [].slice.call(arguments, 1) | |
if (!defaults) | |
return targetFunc; | |
var opStart = targetFunc.length - defaults.length; | |
return function(){ | |
for(i=0; i<defaults.length; i++){ | |
if (arguments[opStart + i] === undefined){ | |
arguments[opStart + i] = defaults[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
//gets an array with the names of the parameters to a function | |
//taken from: | |
//http://stackoverflow.com/questions/9091838/get-function-parameter-names-for-interface-purposes/9092085#9092085 | |
function getFnParamNames(fn){ | |
var fstr = fn.toString(); | |
return fstr.match(/\(.*?\)/)[0].replace(/[()]/gi,'').replace(/\s/gi,'').split(','); | |
} | |
//used to mark as such the support objects used to pass named parameters | |
function NamedParameters(obj){ |
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
//prompted by: http://ejohn.org/blog/javascript-method-overloading/ | |
//baseFn: function that will be used when the overloaded function is invoked with a non matching number of parameters | |
function overload(baseFn /*, overload1, overload2...*/){ | |
//returns an overload aware function | |
function buildOverload(baseFn){ | |
var overloads = { | |
"default": baseFn | |
}; | |
overloads[baseFn.length] = baseFn; |
OlderNewer