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 template = function(str, obj) { | |
return str.replace(/\$\{([^}]+)\}/g, function(match, prop) { | |
return (prop in obj) ? obj[prop] : match; | |
}); | |
}; | |
var str = template('${greeting}, ${noun}.', {greeting:'Ciao',noun:'mondo'}); |
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
/** | |
* memoize | |
* http://unscriptable.com/index.php/2009/05/01/a-better-javascript-memoizer/ | |
*/ | |
var memoize = function (func, context) { | |
var memoizeArg = function (argPos) { | |
var cache = {}; |
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 trim(str){ | |
var start = -1, | |
end = str.length; | |
while(str.charCodeAt(--end) < 33); | |
while(str.charCodeAt(++start) < 33); | |
return str.slice(start, end + 1); | |
}; |
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
/** | |
* Adds a leading zero to numbers less than 10 | |
* | |
* @param {Number|String} val Numeric value | |
* @param {Number} len (optional, default = 2) String length | |
* @return {String} Modified value | |
*/ | |
var addLeadingZero = function(val /*, len */) { | |
var str = val.toString(); | |
var len = arguments[1] || 2; |
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 namespace = function (obj, ns) { | |
var props = ns.split('.'), | |
prop; | |
do { | |
prop = props.shift(); | |
obj = obj[prop] = obj[prop] || {}; | |
} while (props.length > 0); | |
return 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
function floorDate (floor /*, date, clone */) { | |
var clone = (arguments[2] === true), | |
date = (typeof arguments[1] !== 'undefined') ? arguments[1] : new Date(); | |
if (clone || !(date instanceof Date)) { | |
date = new Date(date); | |
} | |
switch(floor) { |
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
/** | |
* Returns a unique GMT timestamp at every specified interval. | |
* | |
* Examples: | |
* <ul> | |
* <!-- Returns a unique timestamp every 10 minutes --> | |
* <li>var t10m = modTimestamp(600);</li> | |
* <!-- Returns a unique timestamp every 30 seconds --> | |
* <li>var t30s = modTimestamp('30');</li> | |
* </ul> |
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 ordinal (n) { | |
return['th','st','nd','rd'][(n=~~(n<0?-n:n)%100)>10&&n<14||(n%=10)>3?0:n]; | |
} |
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 setDeepValue = function(obj, deepProp, val) { | |
var props = deepProp.split('.'), | |
root = obj, | |
i, n, p, t; | |
for (i = 0, n = props.length - 1; i < n; ++i) { | |
p = props[i]; | |
t = typeof obj[p]; | |
obj = obj[p] = (t === 'object' || t === 'function') ? obj[p] : {}; |
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 Transmogrifier (map) { | |
var EMPTY = '', | |
SOURCE = 's', | |
TARGET = 't', | |
DOT = '.', | |
EQUALS = '=', | |
OBJECT = '{}', | |
BREAK = ';\n', | |
NUMERIC_INDEX_REGEXP = /\.(\d+)/g, |