main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Line | |
---|
1 | import createPadding from './_createPadding.js';
|
---|
2 | import stringSize from './_stringSize.js';
|
---|
3 | import toInteger from './toInteger.js';
|
---|
4 | import toString from './toString.js';
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * Pads `string` on the left side if it's shorter than `length`. Padding
|
---|
8 | * characters are truncated if they exceed `length`.
|
---|
9 | *
|
---|
10 | * @static
|
---|
11 | * @memberOf _
|
---|
12 | * @since 4.0.0
|
---|
13 | * @category String
|
---|
14 | * @param {string} [string=''] The string to pad.
|
---|
15 | * @param {number} [length=0] The padding length.
|
---|
16 | * @param {string} [chars=' '] The string used as padding.
|
---|
17 | * @returns {string} Returns the padded string.
|
---|
18 | * @example
|
---|
19 | *
|
---|
20 | * _.padStart('abc', 6);
|
---|
21 | * // => ' abc'
|
---|
22 | *
|
---|
23 | * _.padStart('abc', 6, '_-');
|
---|
24 | * // => '_-_abc'
|
---|
25 | *
|
---|
26 | * _.padStart('abc', 3);
|
---|
27 | * // => 'abc'
|
---|
28 | */
|
---|
29 | function padStart(string, length, chars) {
|
---|
30 | string = toString(string);
|
---|
31 | length = toInteger(length);
|
---|
32 |
|
---|
33 | var strLength = length ? stringSize(string) : 0;
|
---|
34 | return (length && strLength < length)
|
---|
35 | ? (createPadding(length - strLength, chars) + string)
|
---|
36 | : string;
|
---|
37 | }
|
---|
38 |
|
---|
39 | export default padStart;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.