Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50650 views
1
'use strict';
2
3
var toStr = Object.prototype.toString;
4
var hasSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol';
5
var symbolToStr = hasSymbols ? Symbol.prototype.toString : toStr;
6
7
var $isNaN = require('./helpers/isNaN');
8
var $isFinite = require('./helpers/isFinite');
9
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;
10
11
var assign = require('./helpers/assign');
12
var sign = require('./helpers/sign');
13
var mod = require('./helpers/mod');
14
var isPrimitive = require('./helpers/isPrimitive');
15
var toPrimitive = require('es-to-primitive/es6');
16
var parseInteger = parseInt;
17
var bind = require('function-bind');
18
var strSlice = bind.call(Function.call, String.prototype.slice);
19
var isBinary = bind.call(Function.call, RegExp.prototype.test, /^0b[01]+$/i);
20
var isOctal = bind.call(Function.call, RegExp.prototype.test, /^0o[0-7]+$/i);
21
var nonWS = ['\u0085', '\u200b', '\ufffe'].join('');
22
var nonWSregex = new RegExp('[' + nonWS + ']', 'g');
23
var hasNonWS = bind.call(Function.call, RegExp.prototype.test, nonWSregex);
24
var invalidHexLiteral = /^[-+]0x[0-9a-f]+$/i;
25
var isInvalidHexLiteral = bind.call(Function.call, RegExp.prototype.test, invalidHexLiteral);
26
27
// whitespace from: http://es5.github.io/#x15.5.4.20
28
// implementation from https://github.com/es-shims/es5-shim/blob/v3.4.0/es5-shim.js#L1304-L1324
29
var ws = [
30
'\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003',
31
'\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028',
32
'\u2029\uFEFF'
33
].join('');
34
var trimRegex = new RegExp('(^[' + ws + ']+)|([' + ws + ']+$)', 'g');
35
var replace = bind.call(Function.call, String.prototype.replace);
36
var trim = function (value) {
37
return replace(value, trimRegex, '');
38
};
39
40
var ES5 = require('./es5');
41
42
var hasRegExpMatcher = require('is-regex');
43
44
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-abstract-operations
45
var ES6 = assign(assign({}, ES5), {
46
47
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-call-f-v-args
48
Call: function Call(F, V) {
49
var args = arguments.length > 2 ? arguments[2] : [];
50
if (!this.IsCallable(F)) {
51
throw new TypeError(F + ' is not a function');
52
}
53
return F.apply(V, args);
54
},
55
56
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toprimitive
57
ToPrimitive: toPrimitive,
58
59
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toboolean
60
// ToBoolean: ES5.ToBoolean,
61
62
// http://www.ecma-international.org/ecma-262/6.0/#sec-tonumber
63
ToNumber: function ToNumber(argument) {
64
var value = isPrimitive(argument) ? argument : toPrimitive(argument, 'number');
65
if (typeof value === 'symbol') {
66
throw new TypeError('Cannot convert a Symbol value to a number');
67
}
68
if (typeof value === 'string') {
69
if (isBinary(value)) {
70
return this.ToNumber(parseInteger(strSlice(value, 2), 2));
71
} else if (isOctal(value)) {
72
return this.ToNumber(parseInteger(strSlice(value, 2), 8));
73
} else if (hasNonWS(value) || isInvalidHexLiteral(value)) {
74
return NaN;
75
} else {
76
var trimmed = trim(value);
77
if (trimmed !== value) {
78
return this.ToNumber(trimmed);
79
}
80
}
81
}
82
return Number(value);
83
},
84
85
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tointeger
86
// ToInteger: ES5.ToNumber,
87
88
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toint32
89
// ToInt32: ES5.ToInt32,
90
91
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-touint32
92
// ToUint32: ES5.ToUint32,
93
94
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toint16
95
ToInt16: function ToInt16(argument) {
96
var int16bit = this.ToUint16(argument);
97
return int16bit >= 0x8000 ? int16bit - 0x10000 : int16bit;
98
},
99
100
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-touint16
101
// ToUint16: ES5.ToUint16,
102
103
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toint8
104
ToInt8: function ToInt8(argument) {
105
var int8bit = this.ToUint8(argument);
106
return int8bit >= 0x80 ? int8bit - 0x100 : int8bit;
107
},
108
109
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-touint8
110
ToUint8: function ToUint8(argument) {
111
var number = this.ToNumber(argument);
112
if ($isNaN(number) || number === 0 || !$isFinite(number)) { return 0; }
113
var posInt = sign(number) * Math.floor(Math.abs(number));
114
return mod(posInt, 0x100);
115
},
116
117
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-touint8clamp
118
ToUint8Clamp: function ToUint8Clamp(argument) {
119
var number = this.ToNumber(argument);
120
if ($isNaN(number) || number <= 0) { return 0; }
121
if (number >= 0xFF) { return 0xFF; }
122
var f = Math.floor(argument);
123
if (f + 0.5 < number) { return f + 1; }
124
if (number < f + 0.5) { return f; }
125
if (f % 2 !== 0) { return f + 1; }
126
return f;
127
},
128
129
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tostring
130
ToString: function ToString(argument) {
131
if (typeof argument === 'symbol') {
132
throw new TypeError('Cannot convert a Symbol value to a string');
133
}
134
return String(argument);
135
},
136
137
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toobject
138
ToObject: function ToObject(value) {
139
this.RequireObjectCoercible(value);
140
return Object(value);
141
},
142
143
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-topropertykey
144
ToPropertyKey: function ToPropertyKey(argument) {
145
var key = this.ToPrimitive(argument, String);
146
return typeof key === 'symbol' ? symbolToStr.call(key) : this.ToString(key);
147
},
148
149
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength
150
ToLength: function ToLength(argument) {
151
var len = this.ToInteger(argument);
152
if (len <= 0) { return 0; } // includes converting -0 to +0
153
if (len > MAX_SAFE_INTEGER) { return MAX_SAFE_INTEGER; }
154
return len;
155
},
156
157
// http://www.ecma-international.org/ecma-262/6.0/#sec-canonicalnumericindexstring
158
CanonicalNumericIndexString: function CanonicalNumericIndexString(argument) {
159
if (toStr.call(argument) !== '[object String]') {
160
throw new TypeError('must be a string');
161
}
162
if (argument === '-0') { return -0; }
163
var n = this.ToNumber(argument);
164
if (this.SameValue(this.ToString(n), argument)) { return n; }
165
return void 0;
166
},
167
168
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-requireobjectcoercible
169
RequireObjectCoercible: ES5.CheckObjectCoercible,
170
171
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-isarray
172
IsArray: Array.isArray || function IsArray(argument) {
173
return toStr.call(argument) === '[object Array]';
174
},
175
176
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-iscallable
177
// IsCallable: ES5.IsCallable,
178
179
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-isconstructor
180
IsConstructor: function IsConstructor(argument) {
181
return typeof argument === 'function' && !!argument.prototype; // unfortunately there's no way to truly check this without try/catch `new argument`
182
},
183
184
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-isextensible-o
185
IsExtensible: function IsExtensible(obj) {
186
if (!Object.preventExtensions) { return true; }
187
if (isPrimitive(obj)) {
188
return false;
189
}
190
return Object.isExtensible(obj);
191
},
192
193
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-isinteger
194
IsInteger: function IsInteger(argument) {
195
if (typeof argument !== 'number' || $isNaN(argument) || !$isFinite(argument)) {
196
return false;
197
}
198
var abs = Math.abs(argument);
199
return Math.floor(abs) === abs;
200
},
201
202
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-ispropertykey
203
IsPropertyKey: function IsPropertyKey(argument) {
204
return typeof argument === 'string' || typeof argument === 'symbol';
205
},
206
207
// http://www.ecma-international.org/ecma-262/6.0/#sec-isregexp
208
IsRegExp: function IsRegExp(argument) {
209
if (!argument || typeof argument !== 'object') {
210
return false;
211
}
212
if (hasSymbols) {
213
var isRegExp = argument[Symbol.match];
214
if (typeof isRegExp !== 'undefined') {
215
return ES5.ToBoolean(isRegExp);
216
}
217
}
218
return hasRegExpMatcher(argument);
219
},
220
221
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevalue
222
// SameValue: ES5.SameValue,
223
224
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero
225
SameValueZero: function SameValueZero(x, y) {
226
return (x === y) || ($isNaN(x) && $isNaN(y));
227
},
228
229
/**
230
* 7.3.2 GetV (V, P)
231
* 1. Assert: IsPropertyKey(P) is true.
232
* 2. Let O be ToObject(V).
233
* 3. ReturnIfAbrupt(O).
234
* 4. Return O.[[Get]](P, V).
235
*/
236
GetV: function GetV(V, P) {
237
// 7.3.2.1
238
if (!this.IsPropertyKey(P)) {
239
throw new TypeError('Assertion failed: IsPropertyKey(P) is not true');
240
}
241
242
// 7.3.2.2-3
243
var O = this.ToObject(V);
244
245
// 7.3.2.4
246
return O[P];
247
},
248
249
/**
250
* 7.3.9 - http://www.ecma-international.org/ecma-262/6.0/#sec-getmethod
251
* 1. Assert: IsPropertyKey(P) is true.
252
* 2. Let func be GetV(O, P).
253
* 3. ReturnIfAbrupt(func).
254
* 4. If func is either undefined or null, return undefined.
255
* 5. If IsCallable(func) is false, throw a TypeError exception.
256
* 6. Return func.
257
*/
258
GetMethod: function GetMethod(O, P) {
259
// 7.3.9.1
260
if (!this.IsPropertyKey(P)) {
261
throw new TypeError('Assertion failed: IsPropertyKey(P) is not true');
262
}
263
264
// 7.3.9.2
265
var func = this.GetV(O, P);
266
267
// 7.3.9.4
268
if (func == null) {
269
return undefined;
270
}
271
272
// 7.3.9.5
273
if (!this.IsCallable(func)) {
274
throw new TypeError(P + 'is not a function');
275
}
276
277
// 7.3.9.6
278
return func;
279
},
280
281
/**
282
* 7.3.1 Get (O, P) - http://www.ecma-international.org/ecma-262/6.0/#sec-get-o-p
283
* 1. Assert: Type(O) is Object.
284
* 2. Assert: IsPropertyKey(P) is true.
285
* 3. Return O.[[Get]](P, O).
286
*/
287
Get: function Get(O, P) {
288
// 7.3.1.1
289
if (this.Type(O) !== 'Object') {
290
throw new TypeError('Assertion failed: Type(O) is not Object');
291
}
292
// 7.3.1.2
293
if (!this.IsPropertyKey(P)) {
294
throw new TypeError('Assertion failed: IsPropertyKey(P) is not true');
295
}
296
// 7.3.1.3
297
return O[P];
298
},
299
300
Type: function Type(x) {
301
if (typeof x === 'symbol') {
302
return 'Symbol';
303
}
304
return ES5.Type(x);
305
},
306
307
// http://www.ecma-international.org/ecma-262/6.0/#sec-speciesconstructor
308
SpeciesConstructor: function SpeciesConstructor(O, defaultConstructor) {
309
if (this.Type(O) !== 'Object') {
310
throw new TypeError('Assertion failed: Type(O) is not Object');
311
}
312
var C = O.constructor;
313
if (typeof C === 'undefined') {
314
return defaultConstructor;
315
}
316
if (this.Type(C) !== 'Object') {
317
throw new TypeError('O.constructor is not an Object');
318
}
319
var S = hasSymbols && Symbol.species ? C[Symbol.species] : undefined;
320
if (S == null) {
321
return defaultConstructor;
322
}
323
if (this.IsConstructor(S)) {
324
return S;
325
}
326
throw new TypeError('no constructor found');
327
}
328
});
329
330
delete ES6.CheckObjectCoercible; // renamed in ES6 to RequireObjectCoercible
331
332
module.exports = ES6;
333
334