Last active
August 29, 2015 13:57
-
-
Save b-ma/9703863 to your computer and use it in GitHub Desktop.
underscore mixins
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
_.mixin({ | |
// from underscore's examples [http://underscorejs.org/#mixin](http://underscorejs.org/#mixin) | |
capitalize: function(string) { | |
return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase(); | |
}, | |
ensureApi: function(obj, api) {}, | |
/** | |
* add sep betwen 3 numbers: | |
* ex: formatNumber(1000000, ',') return 1,000,000 | |
*/ | |
formatNumber: function(nbr, sep) { | |
nbr.toString().replace(/\B(?=(\d{3})+(?!\d))/g, sep); | |
} | |
// return an number formated with '0' to match `length` length | |
// default length : 3 | |
// _.padLeft(2) === '002'; | |
padLeft: function(number, length) { | |
length = (length || 3); | |
number = number.toString(); | |
if (number.length < length) { | |
return _.padLeft.call(_, '0' + number, length); | |
} | |
return number; | |
}, | |
// create an array populated with values from `low` to `high` | |
arrayRange: function(low, high) { | |
var arr = []; | |
while (low <= high) { | |
arr.push(low); | |
low += 1; | |
} | |
return arr; | |
}, | |
constrain: function(value, min, max) { | |
if (value > max) { value = max; } | |
if (value < min) { value = min; } | |
return value; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment