awesome-string
Version:
The ultimate JavaScript string library
37 lines (36 loc) • 1.3 kB
JavaScript
import buildPadding from 'helper/string/build_padding';
import clipNumber from 'helper/number/clip_number';
import coerceToString from 'helper/string/coerce_to_string';
import isNil from 'helper/object/is_nil';
import { MAX_SAFE_INTEGER } from 'helper/number/const';
import toInteger from 'helper/number/to_integer';
/**
* Pads `subject` from left to a new `length`.
*
* @function padLeft
* @static
* @since 1.0.0
* @memberOf Manipulate
* @param {string} [subject=''] The string to pad.
* @param {int} [length=0] The length to left pad the string. No changes are made if `length` is less than `subject.length`.
* @param {string} [pad=' '] The string to be used for padding.
* @return {string} Returns the left padded string.
* @example
* as.padLeft('dog', 5);
* // => ' dog'
*
* as.padLeft('bird', 6, '-');
* // => '--bird'
*
* as.padLeft('cat', 6, '-=');
* // => '-=-cat'
*/
export default function padLeft(subject, length, pad) {
const subjectString = coerceToString(subject);
const lengthInt = isNil(length) ? 0 : clipNumber(toInteger(length), 0, MAX_SAFE_INTEGER);
const padString = coerceToString(pad, ' ');
if (lengthInt <= subjectString.length) {
return subjectString;
}
return buildPadding(padString, lengthInt - subjectString.length) + subjectString;
}