Skip to content

Commit

Permalink
[New] move es6+ to es2015+; leave es6/es7 as aliases.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 30, 2017
1 parent 37c5272 commit 99d9096
Show file tree
Hide file tree
Showing 11 changed files with 1,374 additions and 1,328 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

ECMAScript spec abstract operations.
When different versions of the spec conflict, the default export will be the latest version of the abstract operation.
All abstract operations will also be available under an `es5`/`es6`/`es7` exported property if you require a specific version.
All abstract operations will also be available under an `es5`/`es2015`/`es2016` entry point, and exported property, if you require a specific version.

## Example

Expand Down
332 changes: 332 additions & 0 deletions es2015.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,332 @@
'use strict';

var toStr = Object.prototype.toString;
var hasSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol';
var symbolToStr = hasSymbols ? Symbol.prototype.toString : toStr;

var $isNaN = require('./helpers/isNaN');
var $isFinite = require('./helpers/isFinite');
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;

var assign = require('./helpers/assign');
var sign = require('./helpers/sign');
var mod = require('./helpers/mod');
var isPrimitive = require('./helpers/isPrimitive');
var toPrimitive = require('es-to-primitive/es6');
var parseInteger = parseInt;
var bind = require('function-bind');
var strSlice = bind.call(Function.call, String.prototype.slice);
var isBinary = bind.call(Function.call, RegExp.prototype.test, /^0b[01]+$/i);
var isOctal = bind.call(Function.call, RegExp.prototype.test, /^0o[0-7]+$/i);
var nonWS = ['\u0085', '\u200b', '\ufffe'].join('');
var nonWSregex = new RegExp('[' + nonWS + ']', 'g');
var hasNonWS = bind.call(Function.call, RegExp.prototype.test, nonWSregex);
var invalidHexLiteral = /^[-+]0x[0-9a-f]+$/i;
var isInvalidHexLiteral = bind.call(Function.call, RegExp.prototype.test, invalidHexLiteral);

// whitespace from: http://es5.github.io/#x15.5.4.20
// implementation from https://github.com/es-shims/es5-shim/blob/v3.4.0/es5-shim.js#L1304-L1324
var ws = [
'\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003',
'\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028',
'\u2029\uFEFF'
].join('');
var trimRegex = new RegExp('(^[' + ws + ']+)|([' + ws + ']+$)', 'g');
var replace = bind.call(Function.call, String.prototype.replace);
var trim = function (value) {
return replace(value, trimRegex, '');
};

var ES5 = require('./es5');

var hasRegExpMatcher = require('is-regex');

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-abstract-operations
var ES6 = assign(assign({}, ES5), {

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-call-f-v-args
Call: function Call(F, V) {
var args = arguments.length > 2 ? arguments[2] : [];
if (!this.IsCallable(F)) {
throw new TypeError(F + ' is not a function');
}
return F.apply(V, args);
},

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toprimitive
ToPrimitive: toPrimitive,

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toboolean
// ToBoolean: ES5.ToBoolean,

// http://www.ecma-international.org/ecma-262/6.0/#sec-tonumber
ToNumber: function ToNumber(argument) {
var value = isPrimitive(argument) ? argument : toPrimitive(argument, 'number');
if (typeof value === 'symbol') {
throw new TypeError('Cannot convert a Symbol value to a number');
}
if (typeof value === 'string') {
if (isBinary(value)) {
return this.ToNumber(parseInteger(strSlice(value, 2), 2));
} else if (isOctal(value)) {
return this.ToNumber(parseInteger(strSlice(value, 2), 8));
} else if (hasNonWS(value) || isInvalidHexLiteral(value)) {
return NaN;
} else {
var trimmed = trim(value);
if (trimmed !== value) {
return this.ToNumber(trimmed);
}
}
}
return Number(value);
},

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tointeger
// ToInteger: ES5.ToNumber,

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toint32
// ToInt32: ES5.ToInt32,

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-touint32
// ToUint32: ES5.ToUint32,

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toint16
ToInt16: function ToInt16(argument) {
var int16bit = this.ToUint16(argument);
return int16bit >= 0x8000 ? int16bit - 0x10000 : int16bit;
},

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-touint16
// ToUint16: ES5.ToUint16,

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toint8
ToInt8: function ToInt8(argument) {
var int8bit = this.ToUint8(argument);
return int8bit >= 0x80 ? int8bit - 0x100 : int8bit;
},

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-touint8
ToUint8: function ToUint8(argument) {
var number = this.ToNumber(argument);
if ($isNaN(number) || number === 0 || !$isFinite(number)) { return 0; }
var posInt = sign(number) * Math.floor(Math.abs(number));
return mod(posInt, 0x100);
},

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-touint8clamp
ToUint8Clamp: function ToUint8Clamp(argument) {
var number = this.ToNumber(argument);
if ($isNaN(number) || number <= 0) { return 0; }
if (number >= 0xFF) { return 0xFF; }
var f = Math.floor(argument);
if (f + 0.5 < number) { return f + 1; }
if (number < f + 0.5) { return f; }
if (f % 2 !== 0) { return f + 1; }
return f;
},

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tostring
ToString: function ToString(argument) {
if (typeof argument === 'symbol') {
throw new TypeError('Cannot convert a Symbol value to a string');
}
return String(argument);
},

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toobject
ToObject: function ToObject(value) {
this.RequireObjectCoercible(value);
return Object(value);
},

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-topropertykey
ToPropertyKey: function ToPropertyKey(argument) {
var key = this.ToPrimitive(argument, String);
return typeof key === 'symbol' ? symbolToStr.call(key) : this.ToString(key);
},

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength
ToLength: function ToLength(argument) {
var len = this.ToInteger(argument);
if (len <= 0) { return 0; } // includes converting -0 to +0
if (len > MAX_SAFE_INTEGER) { return MAX_SAFE_INTEGER; }
return len;
},

// http://www.ecma-international.org/ecma-262/6.0/#sec-canonicalnumericindexstring
CanonicalNumericIndexString: function CanonicalNumericIndexString(argument) {
if (toStr.call(argument) !== '[object String]') {
throw new TypeError('must be a string');
}
if (argument === '-0') { return -0; }
var n = this.ToNumber(argument);
if (this.SameValue(this.ToString(n), argument)) { return n; }
return void 0;
},

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-requireobjectcoercible
RequireObjectCoercible: ES5.CheckObjectCoercible,

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-isarray
IsArray: Array.isArray || function IsArray(argument) {
return toStr.call(argument) === '[object Array]';
},

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-iscallable
// IsCallable: ES5.IsCallable,

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-isconstructor
IsConstructor: function IsConstructor(argument) {
return typeof argument === 'function' && !!argument.prototype; // unfortunately there's no way to truly check this without try/catch `new argument`
},

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-isextensible-o
IsExtensible: function IsExtensible(obj) {
if (!Object.preventExtensions) { return true; }
if (isPrimitive(obj)) {
return false;
}
return Object.isExtensible(obj);
},

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-isinteger
IsInteger: function IsInteger(argument) {
if (typeof argument !== 'number' || $isNaN(argument) || !$isFinite(argument)) {
return false;
}
var abs = Math.abs(argument);
return Math.floor(abs) === abs;
},

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-ispropertykey
IsPropertyKey: function IsPropertyKey(argument) {
return typeof argument === 'string' || typeof argument === 'symbol';
},

// http://www.ecma-international.org/ecma-262/6.0/#sec-isregexp
IsRegExp: function IsRegExp(argument) {
if (!argument || typeof argument !== 'object') {
return false;
}
if (hasSymbols) {
var isRegExp = argument[Symbol.match];
if (typeof isRegExp !== 'undefined') {
return ES5.ToBoolean(isRegExp);
}
}
return hasRegExpMatcher(argument);
},

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevalue
// SameValue: ES5.SameValue,

// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero
SameValueZero: function SameValueZero(x, y) {
return (x === y) || ($isNaN(x) && $isNaN(y));
},

/**
* 7.3.2 GetV (V, P)
* 1. Assert: IsPropertyKey(P) is true.
* 2. Let O be ToObject(V).
* 3. ReturnIfAbrupt(O).
* 4. Return O.[[Get]](P, V).
*/
GetV: function GetV(V, P) {
// 7.3.2.1
if (!this.IsPropertyKey(P)) {
throw new TypeError('Assertion failed: IsPropertyKey(P) is not true');
}

// 7.3.2.2-3
var O = this.ToObject(V);

// 7.3.2.4
return O[P];
},

/**
* 7.3.9 - http://www.ecma-international.org/ecma-262/6.0/#sec-getmethod
* 1. Assert: IsPropertyKey(P) is true.
* 2. Let func be GetV(O, P).
* 3. ReturnIfAbrupt(func).
* 4. If func is either undefined or null, return undefined.
* 5. If IsCallable(func) is false, throw a TypeError exception.
* 6. Return func.
*/
GetMethod: function GetMethod(O, P) {
// 7.3.9.1
if (!this.IsPropertyKey(P)) {
throw new TypeError('Assertion failed: IsPropertyKey(P) is not true');
}

// 7.3.9.2
var func = this.GetV(O, P);

// 7.3.9.4
if (func == null) {
return undefined;
}

// 7.3.9.5
if (!this.IsCallable(func)) {
throw new TypeError(P + 'is not a function');
}

// 7.3.9.6
return func;
},

/**
* 7.3.1 Get (O, P) - http://www.ecma-international.org/ecma-262/6.0/#sec-get-o-p
* 1. Assert: Type(O) is Object.
* 2. Assert: IsPropertyKey(P) is true.
* 3. Return O.[[Get]](P, O).
*/
Get: function Get(O, P) {
// 7.3.1.1
if (this.Type(O) !== 'Object') {
throw new TypeError('Assertion failed: Type(O) is not Object');
}
// 7.3.1.2
if (!this.IsPropertyKey(P)) {
throw new TypeError('Assertion failed: IsPropertyKey(P) is not true');
}
// 7.3.1.3
return O[P];
},

Type: function Type(x) {
if (typeof x === 'symbol') {
return 'Symbol';
}
return ES5.Type(x);
},

// http://www.ecma-international.org/ecma-262/6.0/#sec-speciesconstructor
SpeciesConstructor: function SpeciesConstructor(O, defaultConstructor) {
if (this.Type(O) !== 'Object') {
throw new TypeError('Assertion failed: Type(O) is not Object');
}
var C = O.constructor;
if (typeof C === 'undefined') {
return defaultConstructor;
}
if (this.Type(C) !== 'Object') {
throw new TypeError('O.constructor is not an Object');
}
var S = hasSymbols && Symbol.species ? C[Symbol.species] : undefined;
if (S == null) {
return defaultConstructor;
}
if (this.IsConstructor(S)) {
return S;
}
throw new TypeError('no constructor found');
}
});

delete ES6.CheckObjectCoercible; // renamed in ES6 to RequireObjectCoercible

module.exports = ES6;
16 changes: 16 additions & 0 deletions es2016.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

var ES2015 = require('./es2015');
var assign = require('./helpers/assign');

var ES2016 = assign(ES2015, {
// https://github.com/tc39/ecma262/pull/60
SameValueNonNumber: function SameValueNonNumber(x, y) {
if (typeof x === 'number' || typeof x !== typeof y) {
throw new TypeError('SameValueNonNumber requires two non-number values of the same type.');
}
return this.SameValue(x, y);
}
});

module.exports = ES2016;
Loading

0 comments on commit 99d9096

Please sign in to comment.