1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | function polyfill (targetLength) {
|
11 | const length = this.length
|
12 |
|
13 | if (targetLength <= length) {
|
14 | return this
|
15 | }
|
16 |
|
17 | return `${this}${' '.repeat(targetLength - length)}`
|
18 | }
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function padEnd (string, targetLength) {
|
28 | if (typeof string !== 'string' ||
|
29 | typeof targetLength !== 'number') {
|
30 | return string
|
31 | }
|
32 |
|
33 |
|
34 | const fn = typeof String.prototype.padEnd === 'function'
|
35 | ? String.prototype.padEnd
|
36 | : polyfill
|
37 |
|
38 | return fn.call(string, targetLength)
|
39 | }
|
40 |
|
41 | module.exports = padEnd
|