es-to-primitive
Advanced tools
Comparing version 1.0.0 to 1.1.0
{ | ||
"es3": true, | ||
"additionalRules": [], | ||
"requireCurlyBraces": ["if", "else", "for", "while", "do", "try", "catch"], | ||
"requireSemicolons": true, | ||
"disallowMultipleSpaces": true, | ||
"disallowIdentifierNames": [], | ||
"requireCurlyBraces": { | ||
"allExcept": [], | ||
"keywords": ["if", "else", "for", "while", "do", "try", "catch"] | ||
}, | ||
"requireSpaceAfterKeywords": ["if", "else", "for", "while", "do", "switch", "return", "try", "catch", "function"], | ||
@@ -10,2 +21,17 @@ | ||
"disallowSpaceBeforeComma": true, | ||
"disallowSpaceAfterComma": false, | ||
"disallowSpaceBeforeSemicolon": true, | ||
"disallowNodeTypes": [ | ||
"DebuggerStatement", | ||
"ForInStatement", | ||
"LabeledStatement", | ||
"SwitchCase", | ||
"SwitchStatement", | ||
"WithStatement" | ||
], | ||
"requireObjectKeysOnNewLine": { "allExcept": ["sameLine"] }, | ||
"requireSpacesInAnonymousFunctionExpression": { "beforeOpeningRoundBrace": true, "beforeOpeningCurlyBrace": true }, | ||
@@ -58,3 +84,3 @@ "requireSpacesInNamedFunctionExpression": { "beforeOpeningCurlyBrace": true }, | ||
"requireDotNotation": true, | ||
"requireDotNotation": { "allExcept": ["keywords"] }, | ||
@@ -68,4 +94,65 @@ "requireParenthesesAroundIIFE": true, | ||
"mark": "'" | ||
} | ||
}, | ||
"disallowOperatorBeforeLineBreak": [], | ||
"requireSpaceBeforeKeywords": [ | ||
"do", | ||
"for", | ||
"if", | ||
"else", | ||
"switch", | ||
"case", | ||
"try", | ||
"catch", | ||
"finally", | ||
"while", | ||
"with", | ||
"return" | ||
], | ||
"validateAlignedFunctionParameters": { | ||
"lineBreakAfterOpeningBraces": true, | ||
"lineBreakBeforeClosingBraces": true | ||
}, | ||
"requirePaddingNewLinesBeforeExport": true, | ||
"validateNewlineAfterArrayElements": { | ||
"maximum": 12 | ||
}, | ||
"requirePaddingNewLinesAfterUseStrict": true, | ||
"disallowArrowFunctions": true, | ||
"disallowMultiLineTernary": true, | ||
"validateOrderInObjectKeys": false, | ||
"disallowIdenticalDestructuringNames": true, | ||
"disallowNestedTernaries": { "maxLevel": 1 }, | ||
"requireSpaceAfterComma": { "allExcept": ["trailing"] }, | ||
"requireAlignedMultilineParams": false, | ||
"requireSpacesInGenerator": { | ||
"afterStar": true | ||
}, | ||
"disallowSpacesInGenerator": { | ||
"beforeStar": true | ||
}, | ||
"disallowVar": false, | ||
"requireArrayDestructuring": false, | ||
"requireEnhancedObjectLiterals": false, | ||
"requireObjectDestructuring": false, | ||
"requireEarlyReturn": false | ||
} | ||
@@ -0,3 +1,14 @@ | ||
1.1.0 / 2015-12-27 | ||
================= | ||
* [New] add `Symbol.toPrimitive` support | ||
* [Deps] update `is-callable`, `is-date-object` | ||
* [Dev Deps] update `eslint`, `tape`, `semver`, `jscs`, `covert`, `nsp`, `@ljharb/eslint-config` | ||
* [Dev Deps] remove unused deps | ||
* [Tests] up to `node` `v5.3` | ||
* [Tests] fix npm upgrades on older node versions | ||
* [Tests] fix testling | ||
* [Docs] Switch from vb.teelaun.ch to versionbadg.es for the npm version badge SVG | ||
1.0.0 / 2015-03-19 | ||
================= | ||
* Initial release. |
17
es5.js
@@ -12,8 +12,6 @@ 'use strict'; | ||
'[[DefaultValue]]': function (O, hint) { | ||
if (!hint) { | ||
hint = toStr.call(O) === '[object Date]' ? String : Number; | ||
} | ||
var actualHint = hint || toStr.call(O) === '[object Date]' ? String : Number; | ||
if (hint === String || hint === Number) { | ||
var methods = hint === String ? ['toString', 'valueOf'] : ['valueOf', 'toString']; | ||
if (actualHint === String || actualHint === Number) { | ||
var methods = actualHint === String ? ['toString', 'valueOf'] : ['valueOf', 'toString']; | ||
var value, i; | ||
@@ -39,8 +37,9 @@ for (i = 0; i < methods.length; ++i) { | ||
} | ||
var preferredType = PreferredType; | ||
if (arguments.length < 2) { | ||
PreferredType = toStr.call(input) === '[object Date]' ? String : Number; | ||
preferredType = toStr.call(input) === '[object Date]' ? String : Number; | ||
} | ||
if (PreferredType === String) { | ||
if (preferredType === String) { | ||
return String(input); | ||
} else if (PreferredType === Number) { | ||
} else if (preferredType === Number) { | ||
return Number(input); | ||
@@ -50,3 +49,3 @@ } else { | ||
} | ||
return ES5internalSlots['[[DefaultValue]]'](input, PreferredType); | ||
return ES5internalSlots['[[DefaultValue]]'](input, preferredType); | ||
}; |
17
es6.js
@@ -11,3 +11,3 @@ 'use strict'; | ||
var ordinaryToPrimitive = function OrdinaryToPrimitive(O, hint) { | ||
if (O == null) { | ||
if (typeof O === 'undefined' || O === null) { | ||
throw new TypeError('Cannot call method on ' + O); | ||
@@ -32,3 +32,13 @@ } | ||
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toprimitive | ||
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) { | ||
@@ -50,4 +60,3 @@ if (isPrimitive(input)) { | ||
if (Symbol.toPrimitive) { | ||
throw new TypeError('Symbol.toPrimitive not supported yet'); | ||
// exoticToPrim = this.GetMethod(input, Symbol.toPrimitive); | ||
exoticToPrim = GetMethod(input, Symbol.toPrimitive); | ||
} else if (isSymbol(input)) { | ||
@@ -54,0 +63,0 @@ exoticToPrim = Symbol.prototype.valueOf; |
{ | ||
"name": "es-to-primitive", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"author": "Jordan Harband", | ||
@@ -9,3 +9,4 @@ "description": "ECMAScript “ToPrimitive” algorithm. Provides ES5 and ES6 versions.", | ||
"scripts": { | ||
"test": "npm run lint && node --harmony --es-staging test && npm run security", | ||
"test": "npm run lint && npm run tests-only && npm run security", | ||
"tests-only": "node --es-staging test", | ||
"coverage": "covert test/*.js", | ||
@@ -16,4 +17,3 @@ "coverage-quiet": "covert test/*.js --quiet", | ||
"eslint": "eslint test/*.js *.js", | ||
"eccheck": "editorconfig-tools check *.js **/*.js > /dev/null", | ||
"security": "nsp package" | ||
"security": "nsp check" | ||
}, | ||
@@ -36,22 +36,20 @@ "repository": { | ||
"dependencies": { | ||
"is-callable": "^1.0.4", | ||
"is-date-object": "^1.0.0", | ||
"is-callable": "^1.1.1", | ||
"is-date-object": "^1.0.1", | ||
"is-symbol": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"tape": "^3.5.0", | ||
"covert": "^1.0.1", | ||
"tape": "^4.4.0", | ||
"covert": "^1.1.0", | ||
"object-is": "^1.0.1", | ||
"foreach": "^2.0.5", | ||
"jscs": "^1.11.3", | ||
"editorconfig-tools": "^0.1.1", | ||
"nsp": "^1.0.1", | ||
"eslint": "^0.17.0", | ||
"make-arrow-function": "^1.0.0", | ||
"make-generator-function": "^1.1.0", | ||
"jscs": "^2.7.0", | ||
"nsp": "^2.2.0", | ||
"eslint": "^1.10.3", | ||
"@ljharb/eslint-config": "^1.6.0", | ||
"replace": "^0.3.0", | ||
"semver": "^4.3.1" | ||
"semver": "^5.1.0" | ||
}, | ||
"testling": { | ||
"files": "test.js", | ||
"files": "test", | ||
"browsers": [ | ||
@@ -58,0 +56,0 @@ "iexplore/6.0..latest", |
@@ -40,3 +40,3 @@ # es-to-primitive <sup>[![Version Badge][npm-version-svg]][package-url]</sup> | ||
[package-url]: https://npmjs.org/package/es-to-primitive | ||
[npm-version-svg]: http://vb.teelaun.ch/ljharb/es-to-primitive.svg | ||
[npm-version-svg]: http://versionbadg.es/ljharb/es-to-primitive.svg | ||
[travis-svg]: https://travis-ci.org/ljharb/es-to-primitive.svg | ||
@@ -43,0 +43,0 @@ [travis-url]: https://travis-ci.org/ljharb/es-to-primitive |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var test = require('tape'); | ||
@@ -66,10 +68,14 @@ var toPrimitive = require('../es5'); | ||
t.throws(function () { return toPrimitive(uncoercibleObject); }, TypeError, 'uncoercibleObject throws a TypeError'); | ||
t.throws(function () { return toPrimitive(uncoercibleObject, String); }, TypeError, 'uncoercibleObject with hint String throws a TypeError'); | ||
t.throws(function () { return toPrimitive(uncoercibleObject, Number); }, TypeError, 'uncoercibleObject with hint Number throws a TypeError'); | ||
t.test('exceptions', function (st) { | ||
st.throws(toPrimitive.bind(null, uncoercibleObject), TypeError, 'uncoercibleObject throws a TypeError'); | ||
st.throws(toPrimitive.bind(null, uncoercibleObject, String), TypeError, 'uncoercibleObject with hint String throws a TypeError'); | ||
st.throws(toPrimitive.bind(null, uncoercibleObject, Number), TypeError, 'uncoercibleObject with hint Number throws a TypeError'); | ||
t.throws(function () { return toPrimitive(uncoercibleFnObject); }, TypeError, 'uncoercibleFnObject throws a TypeError'); | ||
t.throws(function () { return toPrimitive(uncoercibleFnObject, String); }, TypeError, 'uncoercibleFnObject with hint String throws a TypeError'); | ||
t.throws(function () { return toPrimitive(uncoercibleFnObject, Number); }, TypeError, 'uncoercibleFnObject with hint Number throws a TypeError'); | ||
st.throws(toPrimitive.bind(null, uncoercibleFnObject), TypeError, 'uncoercibleFnObject throws a TypeError'); | ||
st.throws(toPrimitive.bind(null, uncoercibleFnObject, String), TypeError, 'uncoercibleFnObject with hint String throws a TypeError'); | ||
st.throws(toPrimitive.bind(null, uncoercibleFnObject, Number), TypeError, 'uncoercibleFnObject with hint Number throws a TypeError'); | ||
st.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var test = require('tape'); | ||
@@ -8,2 +10,3 @@ var toPrimitive = require('../es6'); | ||
var hasSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol'; | ||
var hasSymbolToPrimitive = hasSymbols && typeof Symbol.toPrimitive === 'symbol'; | ||
@@ -85,10 +88,46 @@ var primitives = [null, undefined, true, false, 0, -0, 42, NaN, Infinity, -Infinity, '', 'abc']; | ||
t.throws(function () { return toPrimitive(uncoercibleObject); }, TypeError, 'uncoercibleObject throws a TypeError'); | ||
t.throws(function () { return toPrimitive(uncoercibleObject, Number); }, TypeError, 'uncoercibleObject with hint Number throws a TypeError'); | ||
t.throws(function () { return toPrimitive(uncoercibleObject, String); }, TypeError, 'uncoercibleObject with hint String throws a TypeError'); | ||
t.test('Symbol.toPrimitive', { skip: !hasSymbolToPrimitive }, function (st) { | ||
var overriddenObject = { toString: st.fail, valueOf: st.fail }; | ||
overriddenObject[Symbol.toPrimitive] = function (hint) { return String(hint); }; | ||
t.throws(function () { return toPrimitive(uncoercibleFnObject); }, TypeError, 'uncoercibleFnObject throws a TypeError'); | ||
t.throws(function () { return toPrimitive(uncoercibleFnObject, Number); }, TypeError, 'uncoercibleFnObject with hint Number throws a TypeError'); | ||
t.throws(function () { return toPrimitive(uncoercibleFnObject, String); }, TypeError, 'uncoercibleFnObject with hint String throws a TypeError'); | ||
st.equal(toPrimitive(overriddenObject), 'default', 'object with Symbol.toPrimitive + no hint invokes that'); | ||
st.equal(toPrimitive(overriddenObject, Number), 'number', 'object with Symbol.toPrimitive + hint Number invokes that'); | ||
st.equal(toPrimitive(overriddenObject, String), 'string', 'object with Symbol.toPrimitive + hint String invokes that'); | ||
var nullToPrimitive = { toString: coercibleObject.toString, valueOf: coercibleObject.valueOf }; | ||
nullToPrimitive[Symbol.toPrimitive] = null; | ||
st.equal(toPrimitive(nullToPrimitive), toPrimitive(coercibleObject), 'object with no hint + null Symbol.toPrimitive ignores it'); | ||
st.equal(toPrimitive(nullToPrimitive, Number), toPrimitive(coercibleObject, Number), 'object with hint Number + null Symbol.toPrimitive ignores it'); | ||
st.equal(toPrimitive(nullToPrimitive, String), toPrimitive(coercibleObject, String), 'object with hint String + null Symbol.toPrimitive ignores it'); | ||
st.test('exceptions', function (sst) { | ||
var nonFunctionToPrimitive = { toString: sst.fail, valueOf: sst.fail }; | ||
nonFunctionToPrimitive[Symbol.toPrimitive] = {}; | ||
sst.throws(toPrimitive.bind(null, nonFunctionToPrimitive), TypeError, 'Symbol.toPrimitive returning a non-function throws'); | ||
var uncoercibleToPrimitive = { toString: sst.fail, valueOf: sst.fail }; | ||
uncoercibleToPrimitive[Symbol.toPrimitive] = function (hint) { return { toString: function () { return hint; } }; }; | ||
sst.throws(toPrimitive.bind(null, uncoercibleToPrimitive), TypeError, 'Symbol.toPrimitive returning an object throws'); | ||
var throwingToPrimitive = { toString: sst.fail, valueOf: sst.fail }; | ||
throwingToPrimitive[Symbol.toPrimitive] = function (hint) { throw new RangeError(hint); }; | ||
sst.throws(toPrimitive.bind(null, throwingToPrimitive), RangeError, 'Symbol.toPrimitive throwing throws'); | ||
sst.end(); | ||
}); | ||
st.end(); | ||
}); | ||
t.test('exceptions', function (st) { | ||
st.throws(toPrimitive.bind(null, uncoercibleObject), TypeError, 'uncoercibleObject throws a TypeError'); | ||
st.throws(toPrimitive.bind(null, uncoercibleObject, Number), TypeError, 'uncoercibleObject with hint Number throws a TypeError'); | ||
st.throws(toPrimitive.bind(null, uncoercibleObject, String), TypeError, 'uncoercibleObject with hint String throws a TypeError'); | ||
st.throws(toPrimitive.bind(null, uncoercibleFnObject), TypeError, 'uncoercibleFnObject throws a TypeError'); | ||
st.throws(toPrimitive.bind(null, uncoercibleFnObject, Number), TypeError, 'uncoercibleFnObject with hint Number throws a TypeError'); | ||
st.throws(toPrimitive.bind(null, uncoercibleFnObject, String), TypeError, 'uncoercibleFnObject with hint String throws a TypeError'); | ||
st.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var toPrimitive = require('../'); | ||
@@ -2,0 +4,0 @@ var ES5 = require('../es5'); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
32978
10
412
Updatedis-callable@^1.1.1
Updatedis-date-object@^1.0.1