diff options
Diffstat (limited to 'node_modules/locutus/php/strings/str_pad.js')
-rw-r--r-- | node_modules/locutus/php/strings/str_pad.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/node_modules/locutus/php/strings/str_pad.js b/node_modules/locutus/php/strings/str_pad.js new file mode 100644 index 0000000..a3afa23 --- /dev/null +++ b/node_modules/locutus/php/strings/str_pad.js @@ -0,0 +1,49 @@ +'use strict'; + +module.exports = function str_pad(input, padLength, padString, padType) { + // eslint-disable-line camelcase + // discuss at: http://locutus.io/php/str_pad/ + // original by: Kevin van Zonneveld (http://kvz.io) + // improved by: Michael White (http://getsprink.com) + // input by: Marco van Oort + // bugfixed by: Brett Zamir (http://brett-zamir.me) + // example 1: str_pad('Kevin van Zonneveld', 30, '-=', 'STR_PAD_LEFT') + // returns 1: '-=-=-=-=-=-Kevin van Zonneveld' + // example 2: str_pad('Kevin van Zonneveld', 30, '-', 'STR_PAD_BOTH') + // returns 2: '------Kevin van Zonneveld-----' + + var half = ''; + var padToGo; + + var _strPadRepeater = function _strPadRepeater(s, len) { + var collect = ''; + + while (collect.length < len) { + collect += s; + } + collect = collect.substr(0, len); + + return collect; + }; + + input += ''; + padString = padString !== undefined ? padString : ' '; + + if (padType !== 'STR_PAD_LEFT' && padType !== 'STR_PAD_RIGHT' && padType !== 'STR_PAD_BOTH') { + padType = 'STR_PAD_RIGHT'; + } + if ((padToGo = padLength - input.length) > 0) { + if (padType === 'STR_PAD_LEFT') { + input = _strPadRepeater(padString, padToGo) + input; + } else if (padType === 'STR_PAD_RIGHT') { + input = input + _strPadRepeater(padString, padToGo); + } else if (padType === 'STR_PAD_BOTH') { + half = _strPadRepeater(padString, Math.ceil(padToGo / 2)); + input = half + input + half; + input = input.substr(0, padLength); + } + } + + return input; +}; +//# sourceMappingURL=str_pad.js.map
\ No newline at end of file |