pax_global_header00006660000000000000000000000064135614555300014521gustar00rootroot0000000000000052 comment=fc864b766689e70707a0b86a136b0ec0021e4d17 es-to-primitive-1.2.1/000077500000000000000000000000001356145553000145575ustar00rootroot00000000000000es-to-primitive-1.2.1/.eslintrc000066400000000000000000000006051356145553000164040ustar00rootroot00000000000000{ "root": true, "extends": "@ljharb", "rules": { "complexity": [2, 14], "func-name-matching": 0, "id-length": [2, { "min": 1, "max": 24, "properties": "never" }], "max-statements": [2, 20], "new-cap": [2, { "capIsNewExceptions": ["GetMethod"] }] }, "overrides": [ { "files": "test/**", "rules": { "max-lines-per-function": [2, { "max": 68 }], }, } ], } es-to-primitive-1.2.1/.github/000077500000000000000000000000001356145553000161175ustar00rootroot00000000000000es-to-primitive-1.2.1/.github/FUNDING.yml000066400000000000000000000011121356145553000177270ustar00rootroot00000000000000# These are supported funding model platforms github: [ljharb] patreon: # Replace with a single Patreon username open_collective: # Replace with a single Open Collective username ko_fi: # Replace with a single Ko-fi username tidelift: npm/es-to-primitive community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry liberapay: # Replace with a single Liberapay username issuehunt: # Replace with a single IssueHunt username otechie: # Replace with a single Otechie username custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] es-to-primitive-1.2.1/.gitignore000066400000000000000000000012361356145553000165510ustar00rootroot00000000000000# Logs logs *.log # Runtime data pids *.pid *.seed # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release # Dependency directory # Commenting this out is preferred by some people, see # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- node_modules # Users Environment Variables .lock-wscript # Only apps should have lockfiles yarn.lock package-lock.json npm-shrinkwrap.json es-to-primitive-1.2.1/.npmrc000066400000000000000000000000231356145553000156720ustar00rootroot00000000000000package-lock=false es-to-primitive-1.2.1/.travis.yml000066400000000000000000000004531356145553000166720ustar00rootroot00000000000000version: ~> 1.0 language: node_js cache: directories: - "$(nvm cache dir)" os: - linux import: - ljharb/travis-ci:node/all.yml - ljharb/travis-ci:node/pretest.yml - ljharb/travis-ci:node/posttest.yml - ljharb/travis-ci:node/coverage.yml matrix: allow_failures: - env: COVERAGE=true es-to-primitive-1.2.1/CHANGELOG.md000066400000000000000000000041001356145553000163630ustar00rootroot000000000000001.2.1 / 2019-11-08 ================= * [readme] remove testling URLs * [meta] add `funding` field * [meta] create FUNDING.yml * [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `covert`, `replace`, `semver`, `tape`, `function.prototype.name` * [Tests] use shared travis-ci configs * [Tests] Add es5 tests for `symbol` types (#45) * [Tests] use `npx aud` instead of `nsp` or `npm audit` with hoops * [Tests] remove `jscs` 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 ================= * [Fix: ES5] fix coercion logic: ES5’s ToPrimitive does not coerce any primitive value, regardless of hint (#2) 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.1 / 2016-01-03 ================= * [Fix: ES5] fix coercion logic: ES5’s ToPrimitive does not coerce any primitive value, regardless of hint (#2) * [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. es-to-primitive-1.2.1/LICENSE000066400000000000000000000020721356145553000155650ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Jordan Harband Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. es-to-primitive-1.2.1/Makefile000066400000000000000000000073721356145553000162300ustar00rootroot00000000000000# Since we rely on paths relative to the makefile location, abort if make isn't being run from there. $(if $(findstring /,$(MAKEFILE_LIST)),$(error Please only invoke this makefile from the directory it resides in)) # The files that need updating when incrementing the version number. VERSIONED_FILES := *.js *.json README* # Add the local npm packages' bin folder to the PATH, so that `make` can find them, when invoked directly. # Note that rather than using `$(npm bin)` the 'node_modules/.bin' path component is hard-coded, so that invocation works even from an environment # where npm is (temporarily) unavailable due to having deactivated an nvm instance loaded into the calling shell in order to avoid interference with tests. export PATH := $(shell printf '%s' "$$PWD/node_modules/.bin:$$PATH") UTILS := semver # Make sure that all required utilities can be located. UTIL_CHECK := $(or $(shell PATH="$(PATH)" which $(UTILS) >/dev/null && echo 'ok'),$(error Did you forget to run `npm install` after cloning the repo? At least one of the required supporting utilities not found: $(UTILS))) # Default target (by virtue of being the first non '.'-prefixed in the file). .PHONY: _no-target-specified _no-target-specified: $(error Please specify the target to make - `make list` shows targets. Alternatively, use `npm test` to run the default tests; `npm run` shows all tests) # Lists all targets defined in this makefile. .PHONY: list list: @$(MAKE) -pRrn : -f $(MAKEFILE_LIST) 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | command grep -v -e '^[^[:alnum:]]' -e '^$@$$command ' | sort # All-tests target: invokes the specified test suites for ALL shells defined in $(SHELLS). .PHONY: test test: @npm test .PHONY: _ensure-tag _ensure-tag: ifndef TAG $(error Please invoke with `make TAG= release`, where is either an increment specifier (patch, minor, major, prepatch, preminor, premajor, prerelease), or an explicit major.minor.patch version number) endif CHANGELOG_ERROR = $(error No CHANGELOG specified) .PHONY: _ensure-changelog _ensure-changelog: @ (git status -sb --porcelain | command grep -E '^( M|[MA] ) CHANGELOG.md' > /dev/null) || (echo no CHANGELOG.md specified && exit 2) # Ensures that the git workspace is clean. .PHONY: _ensure-clean _ensure-clean: @[ -z "$$((git status --porcelain --untracked-files=no || echo err) | command grep -v 'CHANGELOG.md')" ] || { echo "Workspace is not clean; please commit changes first." >&2; exit 2; } # Makes a release; invoke with `make TAG= release`. .PHONY: release release: _ensure-tag _ensure-changelog _ensure-clean @old_ver=`git describe --abbrev=0 --tags --match 'v[0-9]*.[0-9]*.[0-9]*'` || { echo "Failed to determine current version." >&2; exit 1; }; old_ver=$${old_ver#v}; \ new_ver=`echo "$(TAG)" | sed 's/^v//'`; new_ver=$${new_ver:-patch}; \ if printf "$$new_ver" | command grep -q '^[0-9]'; then \ semver "$$new_ver" >/dev/null || { echo 'Invalid version number specified: $(TAG) - must be major.minor.patch' >&2; exit 2; }; \ semver -r "> $$old_ver" "$$new_ver" >/dev/null || { echo 'Invalid version number specified: $(TAG) - must be HIGHER than current one.' >&2; exit 2; } \ else \ new_ver=`semver -i "$$new_ver" "$$old_ver"` || { echo 'Invalid version-increment specifier: $(TAG)' >&2; exit 2; } \ fi; \ printf "=== Bumping version **$$old_ver** to **$$new_ver** before committing and tagging:\n=== TYPE 'proceed' TO PROCEED, anything else to abort: " && read response && [ "$$response" = 'proceed' ] || { echo 'Aborted.' >&2; exit 2; }; \ replace "$$old_ver" "$$new_ver" -- $(VERSIONED_FILES) && \ git commit -m "v$$new_ver" $(VERSIONED_FILES) CHANGELOG.md && \ git tag -a -m "v$$new_ver" "v$$new_ver" es-to-primitive-1.2.1/README.md000066400000000000000000000037121356145553000160410ustar00rootroot00000000000000# es-to-primitive [![Version Badge][npm-version-svg]][package-url] [![Build Status][travis-svg]][travis-url] [![dependency status][deps-svg]][deps-url] [![dev dependency status][dev-deps-svg]][dev-deps-url] [![License][license-image]][license-url] [![Downloads][downloads-image]][downloads-url] [![npm badge][npm-badge-png]][package-url] 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`/`es2015` exported property if you require a specific version. ## Example ```js var toPrimitive = require('es-to-primitive'); var assert = require('assert'); assert(toPrimitive(function () {}) === String(function () {})); var date = new Date(); assert(toPrimitive(date) === String(date)); assert(toPrimitive({ valueOf: function () { return 3; } }) === 3); assert(toPrimitive(['a', 'b', 3]) === String(['a', 'b', 3])); var sym = Symbol(); assert(toPrimitive(Object(sym)) === sym); ``` ## Tests Simply clone the repo, `npm install`, and run `npm test` [package-url]: https://npmjs.org/package/es-to-primitive [npm-version-svg]: http://versionbadg.es/ljharb/es-to-primitive.svg [travis-svg]: https://travis-ci.org/ljharb/es-to-primitive.svg [travis-url]: https://travis-ci.org/ljharb/es-to-primitive [deps-svg]: https://david-dm.org/ljharb/es-to-primitive.svg [deps-url]: https://david-dm.org/ljharb/es-to-primitive [dev-deps-svg]: https://david-dm.org/ljharb/es-to-primitive/dev-status.svg [dev-deps-url]: https://david-dm.org/ljharb/es-to-primitive#info=devDependencies [npm-badge-png]: https://nodei.co/npm/es-to-primitive.png?downloads=true&stars=true [license-image]: http://img.shields.io/npm/l/es-to-primitive.svg [license-url]: LICENSE [downloads-image]: http://img.shields.io/npm/dm/es-to-primitive.svg [downloads-url]: http://npm-stat.com/charts.html?package=es-to-primitive es-to-primitive-1.2.1/es2015.js000066400000000000000000000041331356145553000160350ustar00rootroot00000000000000'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; } return void 0; }; // http://www.ecma-international.org/ecma-262/6.0/#sec-toprimitive module.exports = function ToPrimitive(input) { if (isPrimitive(input)) { return input; } var hint = 'default'; if (arguments.length > 1) { if (arguments[1] === String) { hint = 'string'; } else if (arguments[1] === 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); }; es-to-primitive-1.2.1/es5.js000066400000000000000000000022571356145553000156170ustar00rootroot00000000000000'use strict'; var toStr = Object.prototype.toString; var isPrimitive = require('./helpers/isPrimitive'); var isCallable = require('is-callable'); // http://ecma-international.org/ecma-262/5.1/#sec-8.12.8 var ES5internalSlots = { '[[DefaultValue]]': function (O) { var actualHint; if (arguments.length > 1) { actualHint = arguments[1]; } else { actualHint = toStr.call(O) === '[object Date]' ? String : Number; } if (actualHint === String || actualHint === Number) { var methods = actualHint === String ? ['toString', 'valueOf'] : ['valueOf', 'toString']; var value, i; for (i = 0; i < methods.length; ++i) { if (isCallable(O[methods[i]])) { value = O[methods[i]](); if (isPrimitive(value)) { return value; } } } throw new TypeError('No default value'); } throw new TypeError('invalid [[DefaultValue]] hint supplied'); } }; // http://ecma-international.org/ecma-262/5.1/#sec-9.1 module.exports = function ToPrimitive(input) { if (isPrimitive(input)) { return input; } if (arguments.length > 1) { return ES5internalSlots['[[DefaultValue]]'](input, arguments[1]); } return ES5internalSlots['[[DefaultValue]]'](input); }; es-to-primitive-1.2.1/es6.js000066400000000000000000000000651356145553000156130ustar00rootroot00000000000000'use strict'; module.exports = require('./es2015'); es-to-primitive-1.2.1/helpers/000077500000000000000000000000001356145553000162215ustar00rootroot00000000000000es-to-primitive-1.2.1/helpers/isPrimitive.js000066400000000000000000000002271356145553000210640ustar00rootroot00000000000000'use strict'; module.exports = function isPrimitive(value) { return value === null || (typeof value !== 'function' && typeof value !== 'object'); }; es-to-primitive-1.2.1/index.js000066400000000000000000000007061356145553000162270ustar00rootroot00000000000000'use strict'; var ES5 = require('./es5'); var ES6 = require('./es6'); var ES2015 = require('./es2015'); if (Object.defineProperty) { 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 = ES2015; es-to-primitive-1.2.1/package.json000066400000000000000000000032431356145553000170470ustar00rootroot00000000000000{ "name": "es-to-primitive", "version": "1.2.1", "author": "Jordan Harband ", "funding": { "url": "https://github.com/sponsors/ljharb" }, "description": "ECMAScript “ToPrimitive” algorithm. Provides ES5 and ES2015 versions.", "license": "MIT", "main": "index.js", "scripts": { "pretest": "npm run --silent lint", "test": "npm run --silent tests-only", "posttest": "npx aud", "tests-only": "node --es-staging test", "coverage": "covert test/*.js", "coverage-quiet": "covert test/*.js --quiet", "lint": "eslint ." }, "repository": { "type": "git", "url": "git://github.com/ljharb/es-to-primitive.git" }, "keywords": [ "primitive", "abstract", "ecmascript", "es5", "es6", "es2015", "toPrimitive", "coerce", "type", "object", "string", "number", "boolean", "symbol", "null", "undefined" ], "dependencies": { "is-callable": "^1.1.4", "is-date-object": "^1.0.1", "is-symbol": "^1.0.2" }, "devDependencies": { "@ljharb/eslint-config": "^15.0.0", "covert": "^1.1.1", "eslint": "^6.6.0", "foreach": "^2.0.5", "function.prototype.name": "^1.1.1", "has-symbols": "^1.0.0", "object-inspect": "^1.6.0", "object-is": "^1.0.1", "replace": "^1.1.1", "semver": "^6.3.0", "tape": "^4.11.0" }, "testling": { "files": "test", "browsers": [ "iexplore/6.0..latest", "firefox/3.0..6.0", "firefox/15.0..latest", "firefox/nightly", "chrome/4.0..10.0", "chrome/20.0..latest", "chrome/canary", "opera/10.0..latest", "opera/next", "safari/4.0..latest", "ipad/6.0..latest", "iphone/6.0..latest", "android-browser/4.2" ] }, "engines": { "node": ">= 0.4" } } es-to-primitive-1.2.1/test/000077500000000000000000000000001356145553000155365ustar00rootroot00000000000000es-to-primitive-1.2.1/test/es2015.js000066400000000000000000000210341356145553000170130ustar00rootroot00000000000000'use strict'; var test = require('tape'); var toPrimitive = require('../es2015'); var is = require('object-is'); var forEach = require('foreach'); var functionName = require('function.prototype.name'); var debug = require('object-inspect'); var hasSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol'; var hasSymbolToPrimitive = hasSymbols && typeof Symbol.toPrimitive === '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']; test('primitives', function (t) { forEach(primitives, function (i) { t.ok(is(toPrimitive(i), i), 'toPrimitive(' + debug(i) + ') returns the same value'); t.ok(is(toPrimitive(i, String), i), 'toPrimitive(' + debug(i) + ', String) returns the same value'); t.ok(is(toPrimitive(i, Number), i), 'toPrimitive(' + debug(i) + ', Number) returns the same value'); }); t.end(); }); test('Symbols', { skip: !hasSymbols }, function (t) { var symbols = [ Symbol('foo'), Symbol.iterator, Symbol['for']('foo') // eslint-disable-line no-restricted-properties ]; forEach(symbols, function (sym) { t.equal(toPrimitive(sym), sym, 'toPrimitive(' + debug(sym) + ') returns the same value'); t.equal(toPrimitive(sym, String), sym, 'toPrimitive(' + debug(sym) + ', String) returns the same value'); t.equal(toPrimitive(sym, Number), sym, 'toPrimitive(' + debug(sym) + ', Number) returns the same value'); }); var primitiveSym = Symbol('primitiveSym'); var objectSym = Object(primitiveSym); t.equal(toPrimitive(objectSym), primitiveSym, 'toPrimitive(' + debug(objectSym) + ') returns ' + debug(primitiveSym)); t.equal(toPrimitive(objectSym, String), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', String) returns ' + debug(primitiveSym)); t.equal(toPrimitive(objectSym, Number), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', Number) returns ' + debug(primitiveSym)); t.end(); }); test('Arrays', function (t) { var arrays = [[], ['a', 'b'], [1, 2]]; forEach(arrays, function (arr) { t.equal(toPrimitive(arr), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array'); t.equal(toPrimitive(arr, String), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array'); t.equal(toPrimitive(arr, Number), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array'); }); t.end(); }); test('Dates', function (t) { var dates = [new Date(), new Date(0), new Date(NaN)]; forEach(dates, function (date) { t.equal(toPrimitive(date), String(date), 'toPrimitive(' + debug(date) + ') returns the string version of the date'); t.equal(toPrimitive(date, String), String(date), 'toPrimitive(' + debug(date) + ') returns the string version of the date'); t.ok(is(toPrimitive(date, Number), Number(date)), 'toPrimitive(' + debug(date) + ') returns the number version of the date'); }); t.end(); }); var coercibleObject = { valueOf: function () { return 3; }, toString: function () { return 42; } }; var valueOfOnlyObject = { valueOf: function () { return 4; }, toString: function () { return {}; } }; var toStringOnlyObject = { valueOf: function () { return {}; }, toString: function () { return 7; } }; 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() {}; } }; test('Objects', function (t) { t.equal(toPrimitive(coercibleObject), coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf'); t.equal(toPrimitive(coercibleObject, Number), coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf'); t.equal(toPrimitive(coercibleObject, String), coercibleObject.toString(), 'coercibleObject with hint String coerces to non-stringified toString'); t.equal(toPrimitive(coercibleFnObject), coercibleFnObject.toString(), 'coercibleFnObject coerces to non-stringified toString'); t.equal(toPrimitive(coercibleFnObject, Number), coercibleFnObject.toString(), 'coercibleFnObject with hint Number coerces to non-stringified toString'); t.equal(toPrimitive(coercibleFnObject, String), coercibleFnObject.toString(), 'coercibleFnObject with hint String coerces to non-stringified toString'); t.equal(toPrimitive({}), '[object Object]', '{} with no hint coerces to Object#toString'); t.equal(toPrimitive({}, Number), '[object Object]', '{} with hint Number coerces to Object#toString'); t.equal(toPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString'); t.equal(toPrimitive(toStringOnlyObject), toStringOnlyObject.toString(), 'toStringOnlyObject returns non-stringified toString'); t.equal(toPrimitive(toStringOnlyObject, Number), toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns non-stringified toString'); t.equal(toPrimitive(toStringOnlyObject, String), toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns non-stringified toString'); t.equal(toPrimitive(valueOfOnlyObject), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf'); t.equal(toPrimitive(valueOfOnlyObject, Number), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf'); t.equal(toPrimitive(valueOfOnlyObject, String), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns non-stringified valueOf'); t.test('Symbol.toPrimitive', { skip: !hasSymbolToPrimitive }, function (st) { var overriddenObject = { toString: st.fail, valueOf: st.fail }; overriddenObject[Symbol.toPrimitive] = function (hint) { return String(hint); }; 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(); }); es-to-primitive-1.2.1/test/es5.js000066400000000000000000000145231356145553000165750ustar00rootroot00000000000000'use strict'; var test = require('tape'); var toPrimitive = require('../es5'); var is = require('object-is'); var forEach = require('foreach'); var functionName = require('function.prototype.name'); var debug = require('object-inspect'); var hasSymbols = require('has-symbols')(); 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']; test('primitives', function (t) { forEach(primitives, function (i) { t.ok(is(toPrimitive(i), i), 'toPrimitive(' + debug(i) + ') returns the same value'); t.ok(is(toPrimitive(i, String), i), 'toPrimitive(' + debug(i) + ', String) returns the same value'); t.ok(is(toPrimitive(i, Number), i), 'toPrimitive(' + debug(i) + ', Number) returns the same value'); }); t.end(); }); test('Symbols', { skip: !hasSymbols }, function (t) { var symbols = [ Symbol('foo'), Symbol.iterator, Symbol['for']('foo') // eslint-disable-line no-restricted-properties ]; forEach(symbols, function (sym) { t.equal(toPrimitive(sym), sym, 'toPrimitive(' + debug(sym) + ') returns the same value'); t.equal(toPrimitive(sym, String), sym, 'toPrimitive(' + debug(sym) + ', String) returns the same value'); t.equal(toPrimitive(sym, Number), sym, 'toPrimitive(' + debug(sym) + ', Number) returns the same value'); }); var primitiveSym = Symbol('primitiveSym'); var stringSym = Symbol.prototype.toString.call(primitiveSym); var objectSym = Object(primitiveSym); t.equal(toPrimitive(objectSym), primitiveSym, 'toPrimitive(' + debug(objectSym) + ') returns ' + debug(primitiveSym)); // This is different from ES2015, as the ES5 algorithm doesn't account for the existence of Symbols: t.equal(toPrimitive(objectSym, String), stringSym, 'toPrimitive(' + debug(objectSym) + ', String) returns ' + debug(stringSym)); t.equal(toPrimitive(objectSym, Number), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', Number) returns ' + debug(primitiveSym)); t.end(); }); test('Arrays', function (t) { var arrays = [[], ['a', 'b'], [1, 2]]; forEach(arrays, function (arr) { t.ok(is(toPrimitive(arr), arr.toString()), 'toPrimitive(' + debug(arr) + ') returns toString of the array'); t.equal(toPrimitive(arr, String), arr.toString(), 'toPrimitive(' + debug(arr) + ') returns toString of the array'); t.ok(is(toPrimitive(arr, Number), arr.toString()), 'toPrimitive(' + debug(arr) + ') returns toString of the array'); }); t.end(); }); test('Dates', function (t) { var dates = [new Date(), new Date(0), new Date(NaN)]; forEach(dates, function (date) { t.equal(toPrimitive(date), date.toString(), 'toPrimitive(' + debug(date) + ') returns toString of the date'); t.equal(toPrimitive(date, String), date.toString(), 'toPrimitive(' + debug(date) + ') returns toString of the date'); t.ok(is(toPrimitive(date, Number), date.valueOf()), 'toPrimitive(' + debug(date) + ') returns valueOf of the date'); }); t.end(); }); var coercibleObject = { valueOf: function () { return 3; }, toString: function () { return 42; } }; var valueOfOnlyObject = { valueOf: function () { return 4; }, toString: function () { return {}; } }; var toStringOnlyObject = { valueOf: function () { return {}; }, toString: function () { return 7; } }; 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() {}; } }; test('Objects', function (t) { t.equal(toPrimitive(coercibleObject), coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf'); t.equal(toPrimitive(coercibleObject, String), coercibleObject.toString(), 'coercibleObject with hint String coerces to toString'); t.equal(toPrimitive(coercibleObject, Number), coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf'); t.equal(toPrimitive(coercibleFnObject), coercibleFnObject.toString(), 'coercibleFnObject coerces to toString'); t.equal(toPrimitive(coercibleFnObject, String), coercibleFnObject.toString(), 'coercibleFnObject with hint String coerces to toString'); t.equal(toPrimitive(coercibleFnObject, Number), coercibleFnObject.toString(), 'coercibleFnObject with hint Number coerces to toString'); t.ok(is(toPrimitive({}), '[object Object]'), '{} with no hint coerces to Object#toString'); t.equal(toPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString'); t.ok(is(toPrimitive({}, Number), '[object Object]'), '{} with hint Number coerces to Object#toString'); t.equal(toPrimitive(toStringOnlyObject), toStringOnlyObject.toString(), 'toStringOnlyObject returns toString'); t.equal(toPrimitive(toStringOnlyObject, String), toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns toString'); t.equal(toPrimitive(toStringOnlyObject, Number), toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns toString'); t.equal(toPrimitive(valueOfOnlyObject), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf'); t.equal(toPrimitive(valueOfOnlyObject, String), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns valueOf'); t.equal(toPrimitive(valueOfOnlyObject, Number), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf'); 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'); 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(); }); es-to-primitive-1.2.1/test/es6.js000066400000000000000000000207561356145553000166030ustar00rootroot00000000000000'use strict'; var test = require('tape'); var toPrimitive = require('../es6'); var is = require('object-is'); var forEach = require('foreach'); var functionName = require('function.prototype.name'); var debug = require('object-inspect'); var hasSymbols = require('has-symbols')(); var hasSymbolToPrimitive = hasSymbols && typeof Symbol.toPrimitive === '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']; test('primitives', function (t) { forEach(primitives, function (i) { t.ok(is(toPrimitive(i), i), 'toPrimitive(' + debug(i) + ') returns the same value'); t.ok(is(toPrimitive(i, String), i), 'toPrimitive(' + debug(i) + ', String) returns the same value'); t.ok(is(toPrimitive(i, Number), i), 'toPrimitive(' + debug(i) + ', Number) returns the same value'); }); t.end(); }); test('Symbols', { skip: !hasSymbols }, function (t) { var symbols = [ Symbol('foo'), Symbol.iterator, Symbol['for']('foo') // eslint-disable-line no-restricted-properties ]; forEach(symbols, function (sym) { t.equal(toPrimitive(sym), sym, 'toPrimitive(' + debug(sym) + ') returns the same value'); t.equal(toPrimitive(sym, String), sym, 'toPrimitive(' + debug(sym) + ', String) returns the same value'); t.equal(toPrimitive(sym, Number), sym, 'toPrimitive(' + debug(sym) + ', Number) returns the same value'); }); var primitiveSym = Symbol('primitiveSym'); var objectSym = Object(primitiveSym); t.equal(toPrimitive(objectSym), primitiveSym, 'toPrimitive(' + debug(objectSym) + ') returns ' + debug(primitiveSym)); t.equal(toPrimitive(objectSym, String), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', String) returns ' + debug(primitiveSym)); t.equal(toPrimitive(objectSym, Number), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', Number) returns ' + debug(primitiveSym)); t.end(); }); test('Arrays', function (t) { var arrays = [[], ['a', 'b'], [1, 2]]; forEach(arrays, function (arr) { t.equal(toPrimitive(arr), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array'); t.equal(toPrimitive(arr, String), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array'); t.equal(toPrimitive(arr, Number), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array'); }); t.end(); }); test('Dates', function (t) { var dates = [new Date(), new Date(0), new Date(NaN)]; forEach(dates, function (date) { t.equal(toPrimitive(date), String(date), 'toPrimitive(' + debug(date) + ') returns the string version of the date'); t.equal(toPrimitive(date, String), String(date), 'toPrimitive(' + debug(date) + ') returns the string version of the date'); t.ok(is(toPrimitive(date, Number), Number(date)), 'toPrimitive(' + debug(date) + ') returns the number version of the date'); }); t.end(); }); var coercibleObject = { valueOf: function () { return 3; }, toString: function () { return 42; } }; var valueOfOnlyObject = { valueOf: function () { return 4; }, toString: function () { return {}; } }; var toStringOnlyObject = { valueOf: function () { return {}; }, toString: function () { return 7; } }; 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() {}; } }; test('Objects', function (t) { t.equal(toPrimitive(coercibleObject), coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf'); t.equal(toPrimitive(coercibleObject, Number), coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf'); t.equal(toPrimitive(coercibleObject, String), coercibleObject.toString(), 'coercibleObject with hint String coerces to non-stringified toString'); t.equal(toPrimitive(coercibleFnObject), coercibleFnObject.toString(), 'coercibleFnObject coerces to non-stringified toString'); t.equal(toPrimitive(coercibleFnObject, Number), coercibleFnObject.toString(), 'coercibleFnObject with hint Number coerces to non-stringified toString'); t.equal(toPrimitive(coercibleFnObject, String), coercibleFnObject.toString(), 'coercibleFnObject with hint String coerces to non-stringified toString'); t.equal(toPrimitive({}), '[object Object]', '{} with no hint coerces to Object#toString'); t.equal(toPrimitive({}, Number), '[object Object]', '{} with hint Number coerces to Object#toString'); t.equal(toPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString'); t.equal(toPrimitive(toStringOnlyObject), toStringOnlyObject.toString(), 'toStringOnlyObject returns non-stringified toString'); t.equal(toPrimitive(toStringOnlyObject, Number), toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns non-stringified toString'); t.equal(toPrimitive(toStringOnlyObject, String), toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns non-stringified toString'); t.equal(toPrimitive(valueOfOnlyObject), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf'); t.equal(toPrimitive(valueOfOnlyObject, Number), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf'); t.equal(toPrimitive(valueOfOnlyObject, String), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns non-stringified valueOf'); t.test('Symbol.toPrimitive', { skip: !hasSymbolToPrimitive }, function (st) { var overriddenObject = { toString: st.fail, valueOf: st.fail }; overriddenObject[Symbol.toPrimitive] = function (hint) { return String(hint); }; 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(); }); es-to-primitive-1.2.1/test/index.js000066400000000000000000000010361356145553000172030ustar00rootroot00000000000000'use strict'; var toPrimitive = require('../'); var ES5 = require('../es5'); var ES6 = require('../es6'); var ES2015 = require('../es2015'); var test = require('tape'); test('default export', function (t) { 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(); }); require('./es5'); require('./es6'); require('./es2015');