Skip to content

Instantly share code, notes, and snippets.

View XoseLluis's full-sized avatar

XoseLluis XoseLluis

  • Xixón, Asturies
View GitHub Profile
@XoseLluis
XoseLluis / runningOnNode.js
Created December 24, 2012 16:36
Are we running on node.js?
function runningOnNode(){
return typeof module !== 'undefined' && module.exports;
}
@XoseLluis
XoseLluis / [Array].insertAt.js
Last active December 10, 2015 04:18
Insert 1 to N items into a JavaScript Array. The original array gets modified and is also returned to enable chaining
//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:
@XoseLluis
XoseLluis / MultiplyString.cs
Created January 16, 2013 15:21
C# Extension Method to multiply a string n times
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();
}
@XoseLluis
XoseLluis / LazyObjectConstruction.js
Created January 24, 2013 00:18
Lazy Object Construction in JavaScript. Create Lazy versions (unitialized) of your objects, and let the consumer initialize them right when he really needs them.
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;
};
}
@XoseLluis
XoseLluis / Object.watch.js
Last active May 15, 2016 02:41
Polyfill for the Object.watch/Object.unwatch functions available in Mozilla browsers
/*
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
*/
@XoseLluis
XoseLluis / getPrototypeChainOf.js
Created March 8, 2013 11:04
Get the chain of internal prototypes for a given object
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;
}
@XoseLluis
XoseLluis / Singletonizer.js
Created March 14, 2013 15:14
Turn your JavaScript constructor functions into Singletons.
// 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;
@XoseLluis
XoseLluis / decorateWithDefaultsChecking.js
Last active December 16, 2015 01:09
Decorates a given JavaScript function with checking/assignment of default values to undefined parameters
//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];
@XoseLluis
XoseLluis / enableNamedParameters.js
Last active December 17, 2015 17:59
Enabling a simple form of Named Parameters in JavaScript A function that receives another function and returns a new function where Named Parameters have been enabled. It has been updated so that a function accepts both unnamed and named parameters. Notice the Named parameters must be passed as the last ones.
//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){
@XoseLluis
XoseLluis / overload.js
Created July 27, 2013 14:06
JavaScript function overloading based on the number of parameters. http://deploytonenyures.blogspot.com.es/2013/07/javascript-function-overloading.html
//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;