This repository has been archived by the owner on Apr 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
polyfill.js
61 lines (59 loc) · 2.01 KB
/
polyfill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const RequireObjectCoercible = O => {
if (O === null || typeof O === 'undefined') {
throw new TypeError('"this" value must not be null or undefined');
}
return O;
};
const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;
const ToLength = argument => {
const len = Number(argument);
if (Number.isNaN(len) || len <= 0) { return 0; }
if (len > MAX_SAFE_INTEGER) { return MAX_SAFE_INTEGER; }
return len;
};
if (!String.prototype.padStart) {
String.prototype.padStart = function padStart(maxLength, fillString = ' ') {
const O = RequireObjectCoercible(this);
const S = String(O);
const intMaxLength = ToLength(maxLength);
const stringLength = ToLength(S.length);
if (intMaxLength <= stringLength) { return S; }
let filler = typeof fillString === 'undefined' ? ' ' : String(fillString);
if (filler === '') { return S; }
const fillLen = intMaxLength - stringLength;
while (filler.length < fillLen) {
const fLen = filler.length;
const remainingCodeUnits = fillLen - fLen;
if (fLen > remainingCodeUnits) {
filler += filler.slice(0, remainingCodeUnits);
} else {
filler += filler;
}
}
const truncatedStringFiller = filler.slice(0, fillLen);
return truncatedStringFiller + S;
};
}
if (!String.prototype.padEnd) {
String.prototype.padEnd = function padEnd(maxLength, fillString = ' ') {
const O = RequireObjectCoercible(this);
const S = String(O);
const intMaxLength = ToLength(maxLength);
const stringLength = ToLength(S.length);
if (intMaxLength <= stringLength) { return S; }
let filler = typeof fillString === 'undefined' ? ' ' : String(fillString);
if (filler === '') { return S; }
const fillLen = intMaxLength - stringLength;
while (filler.length < fillLen) {
const fLen = filler.length;
const remainingCodeUnits = fillLen - fLen;
if (fLen > remainingCodeUnits) {
filler += filler.slice(0, remainingCodeUnits);
} else {
filler += filler;
}
}
const truncatedStringFiller = filler.slice(0, fillLen);
return S + truncatedStringFiller;
};
}