es-to-primitive
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -48,3 +48,3 @@ { | ||
"disallowQuotedKeysInObjects": "allButReserved", | ||
"disallowQuotedKeysInObjects": { "allExcept": ["reserved"] }, | ||
@@ -154,4 +154,25 @@ "disallowSpaceAfterObjectKeys": true, | ||
"requireEarlyReturn": false | ||
"requireEarlyReturn": false, | ||
"requireCapitalizedConstructorsNew": { | ||
"allExcept": ["Function", "String", "Object", "Symbol", "Number", "Date", "RegExp", "Error", "Boolean", "Array", "GetMethod"] | ||
}, | ||
"requireImportAlphabetized": false, | ||
"requireSpaceBeforeObjectValues": true, | ||
"requireSpaceBeforeDestructuredValues": true, | ||
"disallowSpacesInsideTemplateStringPlaceholders": true, | ||
"disallowArrayDestructuringReturn": false, | ||
"requireNewlineBeforeSingleStatementsInIf": false, | ||
"disallowUnusedVariables": true, | ||
"requireSpacesInsideImportedObjectBraces": true, | ||
"requireUseStrict": true | ||
} | ||
@@ -0,1 +1,10 @@ | ||
1.2.0 / 2018-09-27 | ||
================= | ||
* [New] create ES2015 entry point/property, to replace ES6 | ||
* [Fix] Ensure optional arguments are not part of the length (#29) | ||
* [Deps] update `is-callable` | ||
* [Dev Deps] update `tape`, `jscs`, `nsp`, `eslint`, `@ljharb/eslint-config`, `semver`, `object-inspect`, `replace` | ||
* [Tests] avoid util.inspect bug with `new Date(NaN)` on node v6.0 and v6.1. | ||
* [Tests] up to `node` `v10.11`, `v9.11`, `v8.12`, `v6.14`, `v4.9` | ||
1.1.1 / 2016-01-03 | ||
@@ -2,0 +11,0 @@ ================= |
20
es5.js
@@ -9,6 +9,11 @@ 'use strict'; | ||
// https://es5.github.io/#x8.12 | ||
// http://ecma-international.org/ecma-262/5.1/#sec-8.12.8 | ||
var ES5internalSlots = { | ||
'[[DefaultValue]]': function (O, hint) { | ||
var actualHint = hint || (toStr.call(O) === '[object Date]' ? String : Number); | ||
'[[DefaultValue]]': function (O) { | ||
var actualHint; | ||
if (arguments.length > 1) { | ||
actualHint = arguments[1]; | ||
} else { | ||
actualHint = toStr.call(O) === '[object Date]' ? String : Number; | ||
} | ||
@@ -32,8 +37,11 @@ if (actualHint === String || actualHint === Number) { | ||
// https://es5.github.io/#x9 | ||
module.exports = function ToPrimitive(input, PreferredType) { | ||
// http://ecma-international.org/ecma-262/5.1/#sec-9.1 | ||
module.exports = function ToPrimitive(input) { | ||
if (isPrimitive(input)) { | ||
return input; | ||
} | ||
return ES5internalSlots['[[DefaultValue]]'](input, PreferredType); | ||
if (arguments.length > 1) { | ||
return ES5internalSlots['[[DefaultValue]]'](input, arguments[1]); | ||
} | ||
return ES5internalSlots['[[DefaultValue]]'](input); | ||
}; |
73
es6.js
'use strict'; | ||
var hasSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol'; | ||
var isPrimitive = require('./helpers/isPrimitive'); | ||
var isCallable = require('is-callable'); | ||
var isDate = require('is-date-object'); | ||
var isSymbol = require('is-symbol'); | ||
var ordinaryToPrimitive = function OrdinaryToPrimitive(O, hint) { | ||
if (typeof O === 'undefined' || O === null) { | ||
throw new TypeError('Cannot call method on ' + O); | ||
} | ||
if (typeof hint !== 'string' || (hint !== 'number' && hint !== 'string')) { | ||
throw new TypeError('hint must be "string" or "number"'); | ||
} | ||
var methodNames = hint === 'string' ? ['toString', 'valueOf'] : ['valueOf', 'toString']; | ||
var method, result, i; | ||
for (i = 0; i < methodNames.length; ++i) { | ||
method = O[methodNames[i]]; | ||
if (isCallable(method)) { | ||
result = method.call(O); | ||
if (isPrimitive(result)) { | ||
return result; | ||
} | ||
} | ||
} | ||
throw new TypeError('No default value'); | ||
}; | ||
var GetMethod = function GetMethod(O, P) { | ||
var func = O[P]; | ||
if (func !== null && typeof func !== 'undefined') { | ||
if (!isCallable(func)) { | ||
throw new TypeError(func + ' returned for property ' + P + ' of object ' + O + ' is not a function'); | ||
} | ||
return func; | ||
} | ||
}; | ||
// http://www.ecma-international.org/ecma-262/6.0/#sec-toprimitive | ||
module.exports = function ToPrimitive(input, PreferredType) { | ||
if (isPrimitive(input)) { | ||
return input; | ||
} | ||
var hint = 'default'; | ||
if (arguments.length > 1) { | ||
if (PreferredType === String) { | ||
hint = 'string'; | ||
} else if (PreferredType === Number) { | ||
hint = 'number'; | ||
} | ||
} | ||
var exoticToPrim; | ||
if (hasSymbols) { | ||
if (Symbol.toPrimitive) { | ||
exoticToPrim = GetMethod(input, Symbol.toPrimitive); | ||
} else if (isSymbol(input)) { | ||
exoticToPrim = Symbol.prototype.valueOf; | ||
} | ||
} | ||
if (typeof exoticToPrim !== 'undefined') { | ||
var result = exoticToPrim.call(input, hint); | ||
if (isPrimitive(result)) { | ||
return result; | ||
} | ||
throw new TypeError('unable to convert exotic object to primitive'); | ||
} | ||
if (hint === 'default' && (isDate(input) || isSymbol(input))) { | ||
hint = 'string'; | ||
} | ||
return ordinaryToPrimitive(input, hint === 'default' ? 'number' : hint); | ||
}; | ||
module.exports = require('./es2015'); |
@@ -5,11 +5,14 @@ 'use strict'; | ||
var ES6 = require('./es6'); | ||
var ES2015 = require('./es2015'); | ||
if (Object.defineProperty) { | ||
Object.defineProperty(ES6, 'ES5', { enumerable: false, value: ES5 }); | ||
Object.defineProperty(ES6, 'ES6', { enumerable: false, value: ES6 }); | ||
Object.defineProperty(ES2015, 'ES5', { enumerable: false, value: ES5 }); | ||
Object.defineProperty(ES2015, 'ES6', { enumerable: false, value: ES6 }); | ||
Object.defineProperty(ES2015, 'ES2015', { enumerable: false, value: ES2015 }); | ||
} else { | ||
ES6.ES5 = ES5; | ||
ES6.ES6 = ES6; | ||
ES6.ES2015 = ES2015; | ||
} | ||
module.exports = ES6; | ||
module.exports = ES2015; |
{ | ||
"name": "es-to-primitive", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"author": "Jordan Harband", | ||
"description": "ECMAScript “ToPrimitive” algorithm. Provides ES5 and ES6 versions.", | ||
"description": "ECMAScript “ToPrimitive” algorithm. Provides ES5 and ES2015 versions.", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "npm run lint && npm run tests-only && npm run security", | ||
"pretest": "npm run --silent lint", | ||
"test": "npm run --silent tests-only", | ||
"posttest": "npm run --silent security", | ||
"tests-only": "node --es-staging test", | ||
"coverage": "covert test/*.js", | ||
"coverage-quiet": "covert test/*.js --quiet", | ||
"lint": "npm run jscs && npm run eslint", | ||
"lint": "npm run --silent jscs && npm run --silent eslint", | ||
"jscs": "jscs test/*.js *.js", | ||
@@ -28,23 +30,32 @@ "eslint": "eslint test/*.js *.js", | ||
"es6", | ||
"es2015", | ||
"toPrimitive", | ||
"coerce", | ||
"type", | ||
"object" | ||
"object", | ||
"string", | ||
"number", | ||
"boolean", | ||
"symbol", | ||
"null", | ||
"undefined" | ||
], | ||
"dependencies": { | ||
"is-callable": "^1.1.1", | ||
"is-callable": "^1.1.4", | ||
"is-date-object": "^1.0.1", | ||
"is-symbol": "^1.0.1" | ||
"is-symbol": "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
"tape": "^4.4.0", | ||
"@ljharb/eslint-config": "^13.0.0", | ||
"covert": "^1.1.0", | ||
"eslint": "^5.6.0", | ||
"foreach": "^2.0.5", | ||
"function.prototype.name": "^1.1.0", | ||
"jscs": "^3.0.7", | ||
"nsp": "^3.2.1", | ||
"object-inspect": "^1.6.0", | ||
"object-is": "^1.0.1", | ||
"foreach": "^2.0.5", | ||
"jscs": "^2.7.0", | ||
"nsp": "^2.2.0", | ||
"eslint": "^1.10.3", | ||
"@ljharb/eslint-config": "^1.6.1", | ||
"replace": "^0.3.0", | ||
"semver": "^5.1.0" | ||
"replace": "^1.0.0", | ||
"semver": "^5.5.1", | ||
"tape": "^4.9.1" | ||
}, | ||
@@ -51,0 +62,0 @@ "testling": { |
@@ -11,7 +11,5 @@ # es-to-primitive <sup>[![Version Badge][npm-version-svg]][package-url]</sup> | ||
[![browser support][testling-svg]][testling-url] | ||
ECMAScript “ToPrimitive” algorithm. Provides ES5 and ES6 versions. | ||
ECMAScript “ToPrimitive” algorithm. Provides ES5 and ES2015 versions. | ||
When different versions of the spec conflict, the default export will be the latest version of the abstract operation. | ||
Alternative versions will also be available under an `es5`/`es6`/`es7` exported property if you require a specific version. | ||
Alternative versions will also be available under an `es5`/`es2015` exported property if you require a specific version. | ||
@@ -18,0 +16,0 @@ ## Example |
@@ -7,4 +7,12 @@ 'use strict'; | ||
var forEach = require('foreach'); | ||
var debug = require('util').inspect; | ||
var functionName = require('function.prototype.name'); | ||
var debug = require('object-inspect'); | ||
test('function properties', function (t) { | ||
t.equal(toPrimitive.length, 1, 'length is 1'); | ||
t.equal(functionName(toPrimitive), 'ToPrimitive', 'name is ToPrimitive'); | ||
t.end(); | ||
}); | ||
var primitives = [null, undefined, true, false, 0, -0, 42, NaN, Infinity, -Infinity, '', 'abc']; | ||
@@ -44,5 +52,11 @@ | ||
var toStringOnlyObject = { valueOf: function () { return {}; }, toString: function () { return 7; } }; | ||
var coercibleFnObject = { valueOf: function () { return function valueOfFn() {}; }, toString: function () { return 42; } }; | ||
var coercibleFnObject = { | ||
valueOf: function () { return function valueOfFn() {}; }, | ||
toString: function () { return 42; } | ||
}; | ||
var uncoercibleObject = { valueOf: function () { return {}; }, toString: function () { return {}; } }; | ||
var uncoercibleFnObject = { valueOf: function () { return function valueOfFn() {}; }, toString: function () { return function toStrFn() {}; } }; | ||
var uncoercibleFnObject = { | ||
valueOf: function () { return function valueOfFn() {}; }, | ||
toString: function () { return function toStrFn() {}; } | ||
}; | ||
@@ -49,0 +63,0 @@ test('Objects', function (t) { |
@@ -7,3 +7,4 @@ 'use strict'; | ||
var forEach = require('foreach'); | ||
var debug = require('util').inspect; | ||
var functionName = require('function.prototype.name'); | ||
var debug = require('object-inspect'); | ||
@@ -13,2 +14,9 @@ var hasSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol'; | ||
test('function properties', function (t) { | ||
t.equal(toPrimitive.length, 1, 'length is 1'); | ||
t.equal(functionName(toPrimitive), 'ToPrimitive', 'name is ToPrimitive'); | ||
t.end(); | ||
}); | ||
var primitives = [null, undefined, true, false, 0, -0, 42, NaN, Infinity, -Infinity, '', 'abc']; | ||
@@ -26,3 +34,7 @@ | ||
test('Symbols', { skip: !hasSymbols }, function (t) { | ||
var symbols = [Symbol(), Symbol.iterator, Symbol.for('foo')]; | ||
var symbols = [ | ||
Symbol('foo'), | ||
Symbol.iterator, | ||
Symbol['for']('foo') // eslint-disable-line no-restricted-properties | ||
]; | ||
forEach(symbols, function (sym) { | ||
@@ -65,5 +77,11 @@ t.equal(toPrimitive(sym), sym, 'toPrimitive(' + debug(sym) + ') returns the same value'); | ||
var toStringOnlyObject = { valueOf: function () { return {}; }, toString: function () { return 7; } }; | ||
var coercibleFnObject = { valueOf: function () { return function valueOfFn() {}; }, toString: function () { return 42; } }; | ||
var coercibleFnObject = { | ||
valueOf: function () { return function valueOfFn() {}; }, | ||
toString: function () { return 42; } | ||
}; | ||
var uncoercibleObject = { valueOf: function () { return {}; }, toString: function () { return {}; } }; | ||
var uncoercibleFnObject = { valueOf: function () { return function valueOfFn() {}; }, toString: function () { return function toStrFn() {}; } }; | ||
var uncoercibleFnObject = { | ||
valueOf: function () { return function valueOfFn() {}; }, | ||
toString: function () { return function toStrFn() {}; } | ||
}; | ||
@@ -111,3 +129,5 @@ test('Objects', function (t) { | ||
var uncoercibleToPrimitive = { toString: sst.fail, valueOf: sst.fail }; | ||
uncoercibleToPrimitive[Symbol.toPrimitive] = function (hint) { return { toString: function () { return hint; } }; }; | ||
uncoercibleToPrimitive[Symbol.toPrimitive] = function (hint) { | ||
return { toString: function () { return hint; } }; | ||
}; | ||
sst['throws'](toPrimitive.bind(null, uncoercibleToPrimitive), TypeError, 'Symbol.toPrimitive returning an object throws'); | ||
@@ -114,0 +134,0 @@ |
@@ -6,2 +6,3 @@ 'use strict'; | ||
var ES6 = require('../es6'); | ||
var ES2015 = require('../es2015'); | ||
@@ -11,5 +12,6 @@ var test = require('tape'); | ||
test('default export', function (t) { | ||
t.equal(toPrimitive, ES6, 'default export is ES6'); | ||
t.equal(toPrimitive, ES2015, 'default export is ES2015'); | ||
t.equal(toPrimitive.ES5, ES5, 'ES5 property has ES5 method'); | ||
t.equal(toPrimitive.ES6, ES6, 'ES6 property has ES6 method'); | ||
t.equal(toPrimitive.ES2015, ES2015, 'ES2015 property has ES2015 method'); | ||
t.end(); | ||
@@ -20,1 +22,2 @@ }); | ||
require('./es6'); | ||
require('./es2015'); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49837
19
585
12
52
Updatedis-callable@^1.1.4
Updatedis-symbol@^1.0.2