oomph
Version:
Object Oriented javascript models for the client and the server
130 lines (123 loc) • 3.9 kB
JavaScript
// Generated by CoffeeScript 1.9.3
(function() {
var Promise, _, pluralize, utilities;
_ = require('lodash');
pluralize = require('pluralize');
Promise = require('promise');
utilities = {
randomString: function(length) {
var max, min;
max = parseInt(new Array(length + 1).join('z'), 36);
min = parseInt((Math.pow(10, length - 1)).toString(), 36);
return Math.floor(Math.random() * (max - min) + min).toString(36);
},
currency: function(symbol, number) {
return symbol + parseFloat(Math.round(number * 100) / 100).toFixed(2);
},
isBlank: function(string) {
return string === "" || string === void 0 || string === null;
},
isPresent: function(string) {
return !isBlank(string);
},
promiseWhile: function(conditionFn, fn) {
return new Promise(function(resolve, reject) {
var promiseLoop;
promiseLoop = function(output) {
if (!conditionFn()) {
return resolve(output);
}
return fn().then(promiseLoop);
};
return process.nextTick(promiseLoop);
});
},
promiseEachFn: function(promiseFnArray) {
var firstPromiseFn, promise, promiseReturnArray;
promiseReturnArray = [];
if (promiseFnArray.length > 0) {
firstPromiseFn = promiseFnArray.shift();
promise = firstPromiseFn();
promiseReturnArray.push(promise);
} else {
promise = new Promise(function(resolve) {
return resolve();
});
}
_.each(promiseFnArray, (function(_this) {
return function(fn) {
promise = promise.then(fn);
return promiseReturnArray.push(promise);
};
})(this));
return Promise.all(promiseReturnArray);
},
promiseEach: function(promiseArray) {
var promise, promiseReturnArray;
promiseReturnArray = [];
if (promiseArray.length > 0) {
promise = promiseArray.shift();
promiseReturnArray.push(promise);
} else {
promise = new Promise(function(resolve) {
return resolve();
});
}
_.each(promiseArray, (function(_this) {
return function(p) {
promise = promise.then(function() {
return p;
});
return promiseReturnArray.push(promise);
};
})(this));
return Promise.all(promiseReturnArray);
},
queryStringToObject: function(querystring) {
var pairs, result;
pairs = querystring.replace('?', '').split('&');
result = {};
pairs.forEach(function(pair) {
pair = pair.split('=');
return result[pair[0]] = decodeURIComponent(pair[1] || '');
});
return result;
},
objectToQueryString: function(obj, prefix) {
var k, p, str, v;
str = [];
for (p in obj) {
if (obj.hasOwnProperty(p)) {
k = prefix ? prefix + '[' + p + ']' : p;
v = obj[p];
str.push(typeof v === 'object' ? this.objectToQueryString(v, k) : encodeURIComponent(k) + '=' + encodeURIComponent(v));
}
}
return str.join('&');
},
camelCase: function(str) {
return str.replace(/(?:^\w|[A-Z]|\b\w|_\w)/g, function(letter, index) {
if (index === 0) {
return letter.toLowerCase();
} else {
return letter.toUpperCase();
}
}).replace(/\s+|_/g, '');
},
plural: function(str) {
return pluralize(str);
},
pluralKebabCase: function(str) {
var kebabCaseStr, lastWord, match;
kebabCaseStr = _.kebabCase(str);
match = kebabCaseStr.match(/-(\w+)$/);
if (match) {
lastWord = match[1];
} else {
lastWord = kebabCaseStr;
}
return kebabCaseStr.replace(new RegExp(lastWord + '$'), pluralize(lastWord));
}
};
module.exports = utilities;
}).call(this);