Skip to content

Instantly share code, notes, and snippets.

View furf's full-sized avatar
🎯
Focusing

Dave Furfero furf

🎯
Focusing
View GitHub Profile
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'});
/**
* memoize
* http://unscriptable.com/index.php/2009/05/01/a-better-javascript-memoizer/
*/
var memoize = function (func, context) {
var memoizeArg = function (argPos) {
var cache = {};
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);
};
/**
* 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;
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;
};
@furf
furf / floorDate.js
Created June 4, 2009 16:00
Utility for flooring a date to a specified level
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) {
/**
* 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>
function ordinal (n) {
return['th','st','nd','rd'][(n=~~(n<0?-n:n)%100)>10&&n<14||(n%=10)>3?0:n];
}
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] : {};
function Transmogrifier (map) {
var EMPTY = '',
SOURCE = 's',
TARGET = 't',
DOT = '.',
EQUALS = '=',
OBJECT = '{}',
BREAK = ';\n',
NUMERIC_INDEX_REGEXP = /\.(\d+)/g,