generated from tc39/template-for-proposals
-
Notifications
You must be signed in to change notification settings - Fork 5
/
polyfill.js
78 lines (76 loc) · 3.3 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// Proposal: String.cooked
// Repository: https://github.com/tc39/proposal-string-cooked
// Spec: https://tc39.es/proposal-string-cooked/
//
// ```
// String.cooked`mmm ... \u0064elicious cooked string` // 'mmm ... delicious cooked string'
// ```
//
if (!String.cooked) {
function ToObject(value) {
if (value == null) {
throw new TypeError("Cannot convert undefined or null to object");
}
return Object(value);
}
function ToString(value) {
if (typeof value === "symbol") {
throw new TypeError("Cannot convert a Symbol value to a string");
}
return String(value);
}
Object.defineProperty(String, "cooked", {
value: (template = {}, ...substitutions) => {
// 1.1 Let numberOfSubstitutions be the number of elements in substitutions.
let numberOfSubstitutions = substitutions.length;
// 1.2 Let cooked be ? ToObject(template).
let cooked = ToObject(template);
// 1.3. Let literalSegments be ? LengthOfArrayLike(cooked).
let literalSegments = cooked.length;
// 1.4 If literalSegments ≤ 0, return the empty String.
if (literalSegments <= 0) return "";
// 1.5 Let stringElements be a new empty List.
let stringElements = [];
// 1.6 Let nextIndex be 0.
let nextIndex = 0;
// 1.7 Repeat:
while (true) {
// 1.7a Let nextKey be ! ToString(𝔽(nextIndex)).
let nextKey = ToString(nextIndex);
// 1.7b Let nextVal be ? Get(cooked, nextKey).
let nextVal = cooked[nextKey];
// 1.7c If Type(nextVal) is Undefined, throw a TypeError exception.
if (typeof nextVal === "undefined") {
throw new TypeError("Template elements cannot be undefined");
}
// 1.7d Let nextSeg be ? ToString(nextVal).
let nextSeg = ToString(nextVal);
// 1.7e Append the code unit elements of nextSeg to the end of stringElements.
stringElements.push(nextSeg);
// 1.7f If nextIndex + 1 = literalSegments, then
if (nextIndex + 1 === literalSegments) {
// 1.7i Return the String value whose code units are the elements in the List stringElements. If stringElements has no elements, the empty String is returned.
return stringElements.join("");
}
// 1.7g If nextIndex < numberOfSubstitutions, let next be substitutions[nextIndex].
let next;
if (nextIndex < numberOfSubstitutions) {
next = substitutions[nextIndex];
} else {
// 1.7h Else, let next be the empty String.
next = "";
}
// 1.7i Let nextSub be ? ToString(next).
let nextSub = ToString(next);
// 1.7j Append the code unit elements of nextSub to the end of stringElements.
stringElements.push(nextSub);
// 1.7k Set nextIndex to nextIndex + 1.
nextIndex += 1;
}
},
configurable: true,
enumerable: false,
writable: true
})
}