forked from inexorabletash/polyfill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes7.js
318 lines (279 loc) · 9.04 KB
/
es7.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//----------------------------------------------------------------------
//
// ECMAScript "Harmony" Polyfills
//
//----------------------------------------------------------------------
(function (global) {
"use strict";
var undefined = (void 0); // Paranoia
// Helpers
function assert(c, m) {
if (!c) throw Error("Internal assertion failure" + (m ? ': ' + m : ''));
}
function strict(o) {
return o === global ? undefined : o;
}
function hook(o, p, f) {
var op = o[p];
assert(typeof op === 'function', 'Hooking a non-function');
o[p] = function() {
var o = strict(this);
var r = f.apply(o, arguments);
return r !== undefined ? r : op.apply(o, arguments);
};
}
function define(o, p, v, override) {
if (p in o && !override)
return;
if (typeof v === 'function') {
// Sanity check that functions are appropriately named (where possible)
assert((global.Symbol && p instanceof global.Symbol) || !('name' in v) || v.name === p || v.name === p + '_', 'Expected function name "' + p + '", was "' + v.name + '"');
Object.defineProperty(o, p, {
value: v,
configurable: true,
enumerable: false,
writable: true
});
} else {
Object.defineProperty(o, p, {
value: v,
configurable: false,
enumerable: false,
writable: false
});
}
}
function set_internal(o, p, v) {
Object.defineProperty(o, p, {
value: v,
configurable: false,
enumerable: false,
writable: true
});
}
// Snapshot intrinsic functions
var $isNaN = global.isNaN,
$isFinite = global.isFinite,
$parseInt = global.parseInt,
$parseFloat = global.parseFloat;
var E = Math.E,
LOG10E = Math.LOG10E,
LOG2E = Math.LOG2E,
abs = Math.abs,
ceil = Math.ceil,
exp = Math.exp,
floor = Math.floor,
log = Math.log,
max = Math.max,
min = Math.min,
pow = Math.pow,
sqrt = Math.sqrt;
var empty = Object.create(null);
//----------------------------------------
// 6 ECMAScript Data Types and Values
//----------------------------------------
// "Type(x)" is used as shorthand for "the type of x"...
function Type(v) {
switch (typeof v) {
case 'undefined': return 'undefined';
case 'boolean': return 'boolean';
case 'number': return 'number';
case 'string': return 'string';
default:
if (v === null) return 'null';
if (v instanceof global.Symbol) return 'symbol';
return 'object';
}
}
// 6.1.7.4 Well-Known Symbols and Intrinsics
var $$iterator = global.Symbol.iterator,
$$toStringTag = global.Symbol.toStringTag;
//----------------------------------------
// 7 Abstract Operations
//----------------------------------------
// 7.1.4
function ToInteger(n) {
n = Number(n);
if ($isNaN(n)) return 0;
if (n === 0 || n === Infinity || n === -Infinity) return n;
return ((n < 0) ? -1 : 1) * floor(abs(n));
}
// 7.1.6
function ToUint32(v) { return v >>> 0; }
// 7.1.13 ToObject
function ToObject(v) {
if (v === null || v === undefined) throw TypeError();
return Object(v);
}
//----------------------------------------
// 7.2 Testing and Comparison Operations
//----------------------------------------
// 7.2.3
var SameValue = Object.is;
//----------------------------------------
// 9 ECMAScript Ordinary and Exotic Objects Behaviors
//----------------------------------------
// 9.1.13 ObjectCreate(proto, internalDataList)
function ObjectCreate(proto, internalDataList) {
return Object.create(proto, internalDataList);
}
//----------------------------------------------------------------------
//
// ECMAScript 7 Strawman Proposals
//
//----------------------------------------------------------------------
// http://wiki.ecmascript.org/doku.php?id=strawman:number_compare
define(
Number, 'compare',
function compare(first, second, tolerance) {
var difference = first - second;
return abs(difference) <= (tolerance || 0) ? 0 : difference < 0 ? -1 : 1;
});
// http://wiki.ecmascript.org/doku.php?id=harmony:extended_object_api
define(
Object, 'getPropertyDescriptor',
function getPropertyDescriptor(o, p) {
do {
var desc = Object.getOwnPropertyDescriptor(o, p);
if (desc) {
return desc;
}
o = Object.getPrototypeOf(o);
} while (o);
return undefined;
});
// http://wiki.ecmascript.org/doku.php?id=harmony:extended_object_api
define(
Object, 'getPropertyNames',
function getPropertyNames(o) {
var names = ObjectCreate(null);
do {
Object.getOwnPropertyNames(o).forEach(function(name) {
names[name] = true;
});
o = Object.getPrototypeOf(o);
} while (o);
return Object.keys(names);
});
// https://gist.github.com/WebReflection/9353781
define(
Object, 'getOwnPropertyDescriptors',
function getOwnPropertyDescriptors(o) {
var obj = ToObject(o);
var keys = Object.getOwnPropertyNames(obj);
var descriptors = {};
for (var i = 0; i < keys.length; ++i) {
var nextKey = keys[i];
var desc = Object.getOwnPropertyDescriptor(obj, nextKey);
descriptors[nextKey] = desc;
}
return descriptors;
});
// http://wiki.ecmascript.org/doku.php?id=strawman:array.prototype.pushall
define(
Array.prototype, 'pushAll',
function pushAll(other, start, end) {
other = ToObject(other);
if (start === undefined) {
start = 0;
}
start = ToUint32(start);
var otherLength = ToUint32(other.length);
if (end === undefined) {
end = otherLength;
}
end = ToUint32(end);
var self = ToObject(this);
var length = ToUint32(self.length);
for (var i = 0, j = length; i < end; i++, j++) {
self[j] = other[i];
}
self.length = j;
return;
});
// es-discuss: DOMStringList replacement; may rename to 'has'
define(
Array.prototype, 'contains',
function contains(target) {
if (this === undefined || this === null) throw TypeError();
var t = ToObject(this),
len = ToUint32(t.length),
i;
for (i = 0; i < len; i += 1) {
// eval('0 in [undefined]') == false in IE8-
if (/*i in t &&*/ SameValue(t[i], target)) {
return true;
}
}
return false;
});
define(
Object, 'values',
function values(o) {
return Object.keys(o).map(function(p) { return o[p]; });
});
define(
Object, 'entries',
function entries(o) {
return Object.keys(o).map(function(p) { return [p, o[p]]; });
});
// http://esdiscuss.org/topic/regexp-escape
define(
RegExp, 'escape',
function escape(s) {
return String(s).replace(/[^a-zA-Z0-9]/g, '\\$&');
});
// http://wiki.ecmascript.org/doku.php?id=strawman:string_at
define(
String.prototype, 'at',
function at(pos) {
var s = String(this);
var position = ToInteger(pos);
var size = s.length;
if (position < 0 || position >= size) return "";
var first = s.charAt(position);
var cuFirst = first.charCodeAt(0);
if (cuFirst < 0xD800 || cuFirst > 0xDBFF || position + 1 === size) return first;
var cuSecond = s.charCodeAt(position + 1);
if (cuSecond < 0xDC00 || cuSecond > 0xDFFF) return first;
var second = s.charAt(position + 1);
var cp = (first - 0xD800) * 0x400 + (second - 0xDC00) + 0x10000;
return String.fromCharCode(cuFirst, cuSecond);
});
// http://wiki.ecmascript.org/doku.php?id=strawman:string_padding
define(
String.prototype, 'lpad',
function lpad() {
var minLength = arguments[0];
var fillStr = arguments[1];
var s = String(this);
if (minLength === undefined) return s; // NOTE: Wiki is bogus here
var intMinLength = ToInteger(minLength);
var fillLen = intMinLength - s.length; // NOTE: Wiki is bogus here
if (fillLen < 0) throw RangeError();
if (fillLen === Infinity) throw RangeError();
var sFillStr = String(fillStr);
if (fillStr === undefined) sFillStr = ' '; // NOTE: Wiki is bogus here
var sFillVal = '';
while (sFillVal.length < fillLen) sFillVal += sFillStr;
return sFillVal + s;
});
// http://wiki.ecmascript.org/doku.php?id=strawman:string_padding
define(
String.prototype, 'rpad',
function rpad() {
var minLength = arguments[0];
var fillStr = arguments[1];
var s = String(this);
if (minLength === undefined) return s; // NOTE: Wiki is bogus here
var intMinLength = ToInteger(minLength);
var fillLen = intMinLength - s.length; // NOTE: Wiki is bogus here
if (fillLen < 0) throw RangeError();
if (fillLen === Infinity) throw RangeError();
var sFillStr = String(fillStr);
if (fillStr === undefined) sFillStr = ' '; // NOTE: Wiki is bogus here
var sFillVal = '';
while (sFillVal.length < fillLen) sFillVal += sFillStr;
return s + sFillVal;
});
}(this));