Last active
August 29, 2015 14:09
-
-
Save lamchau/4a13ac6b2a8de348d820 to your computer and use it in GitHub Desktop.
underscore/lodash
This file contains hidden or 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((function() { | |
| function createPadding(value, pad, character) { | |
| character = _.isValue(character) ? String(character) : " "; | |
| var strPadded = ""; | |
| var length = pad - value.length; | |
| while (strPadded.length < length && character.length) { | |
| strPadded += character; | |
| } | |
| return strPadded.slice(0, length); | |
| } | |
| return { | |
| isValue: function(value) { | |
| return !_.isNull(value) && !_.isUndefined(value); | |
| }, | |
| padLeft: function(value, pad, character) { | |
| value = _.isValue(value) ? String(value) : ""; | |
| return createPadding(value, pad, character) + value; | |
| }, | |
| padRight: function(value, pad, character) { | |
| value = _.isValue(value) ? String(value) : ""; | |
| return value + createPadding(value, pad, character); | |
| }, | |
| }; | |
| })()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment