UNPKG

825 BJavaScriptView Raw
1'use strict'
2
3/**
4 * polyfill String.prototype.padEnd
5 *
6 * @param {number} targetLength padding length
7 * @returns {string} string
8 */
9/* istanbul ignore next */
10function 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 * String.prototype.padEnd
22 *
23 * @param {string} string string
24 * @param {number} targetLength padding length
25 * @returns {string} string
26 */
27function padEnd (string, targetLength) {
28 if (typeof string !== 'string' ||
29 typeof targetLength !== 'number') {
30 return string
31 }
32
33 /* istanbul ignore next */
34 const fn = typeof String.prototype.padEnd === 'function'
35 ? String.prototype.padEnd
36 : polyfill
37
38 return fn.call(string, targetLength)
39}
40
41module.exports = padEnd