Phase 1 PWA: prayer times, qibla, quran, hijri, tasbih, 99 names

This commit is contained in:
Hermes Bot
2026-08-06 11:55:47 +08:00
parent 4def05d0ef
commit 0d37e7ec1e
10869 changed files with 959465 additions and 18 deletions
+12
View File
@@ -0,0 +1,12 @@
# 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']
+9
View File
@@ -0,0 +1,9 @@
{
"all": true,
"check-coverage": false,
"reporter": ["text-summary", "text", "html", "json"],
"exclude": [
"coverage",
"test"
]
}
+135
View File
@@ -0,0 +1,135 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [v1.3.4](https://github.com/ljharb/es-to-primitive/compare/v1.3.3...v1.3.4) - 2026-06-25
### Commits
- [types] expose `ES5`/`ES6`/`ES2015` as statically-detectable named exports, to fix `attw` [`477f235`](https://github.com/ljharb/es-to-primitive/commit/477f235b81269b6195ff849ee33efe6eed1f7fd9)
- [Robustness] use `es-define-property` [`73d7fa3`](https://github.com/ljharb/es-to-primitive/commit/73d7fa3a67241152b5602f45e5f14fc5c3d72fdf)
## [v1.3.3](https://github.com/ljharb/es-to-primitive/compare/v1.3.2...v1.3.3) - 2026-06-25
### Commits
- [types] further improve types [`f92b77a`](https://github.com/ljharb/es-to-primitive/commit/f92b77a6a54594b39e2520a14836088072ba4bce)
## [v1.3.2](https://github.com/ljharb/es-to-primitive/compare/v1.3.1...v1.3.2) - 2026-06-25
### Commits
- [types] add ES6 (non-bigint) primitive types [`c221148`](https://github.com/ljharb/es-to-primitive/commit/c22114883f40be2cb67bb7099162478642021fbd)
- [Dev Deps] update `@arethetypeswrong/ci`, `tape` [`dc78129`](https://github.com/ljharb/es-to-primitive/commit/dc781292022a3534b13b1889236b8e02de767cc6)
## [v1.3.1](https://github.com/ljharb/es-to-primitive/compare/v1.3.0...v1.3.1) - 2026-06-15
### Commits
- [types] improve types [`b3857bd`](https://github.com/ljharb/es-to-primitive/commit/b3857bd9480c9372a191ee8a998fb78d522ff908)
- [Dev Deps] update `eslint` [`85aa673`](https://github.com/ljharb/es-to-primitive/commit/85aa673c0916ab498a2c57805267879cf9cd0495)
- [Robustness] use `es-errors` [`f91017d`](https://github.com/ljharb/es-to-primitive/commit/f91017d4812099dab3a8709360717fc4f0dbb36e)
- [Dev Deps] update `@arethetypeswrong/cli`, `@ljharb/eslint-config`, `@ljharb/tsconfig`, `@types/function.prototype.name`, `@types/is-date-object`, `@types/is-symbol`, `auto-changelog`, `es-value-fixtures`, `eslint`, `for-each`, `function.prototype.name`, `npmignore`, `object-inspect`, `tape` [`db8d4d8`](https://github.com/ljharb/es-to-primitive/commit/db8d4d8ca4d69facc6f3711cca121be79363050c)
- [Refactor] use `es-abstract-get`s GetMethod implementation [`ee426a3`](https://github.com/ljharb/es-to-primitive/commit/ee426a393f02f633b3d3ea3589bb2b5061b00311)
- [eslint] simplify config [`81c38c3`](https://github.com/ljharb/es-to-primitive/commit/81c38c3a7845a3ce65e62e46fd3e47315f3273fe)
- [Dev Deps] remove no-longer-needed DT packages [`fa1348c`](https://github.com/ljharb/es-to-primitive/commit/fa1348c87e011096ce99ff8616372d09f621e429)
- [Deps] update `is-date-object`, `is-symbol` [`4f07bad`](https://github.com/ljharb/es-to-primitive/commit/4f07bad5c7bf6e42cdbf5adee8578d24c51d2037)
- [readme] replace runkit CI badge with shields.io check-runs badge [`f5b8ae6`](https://github.com/ljharb/es-to-primitive/commit/f5b8ae69e2c0ca688f91e04517905a29941e2357)
## [v1.3.0](https://github.com/ljharb/es-to-primitive/compare/v1.2.1...v1.3.0) - 2024-11-26
### Commits
- [actions] reuse common workflows [`bb72efc`](https://github.com/ljharb/es-to-primitive/commit/bb72efc7e04ae11b84e4aecf120a4e9063e34428)
- [Tests] use `es-value-fixtures` [`a912f7b`](https://github.com/ljharb/es-to-primitive/commit/a912f7b675333735c1c980cda88772ac1870395b)
- [Tests] migrate tests to Github Actions [`510baf0`](https://github.com/ljharb/es-to-primitive/commit/510baf092633a62d59866fbf56836ce42c717c70)
- [New] add types [`69ba1fd`](https://github.com/ljharb/es-to-primitive/commit/69ba1fdcac834b03698739990ba98fe6007024dc)
- [meta] remove unused Makefile [`4ea66e6`](https://github.com/ljharb/es-to-primitive/commit/4ea66e62ef4afa0102eb8335ba3e003e8332f664)
- [actions] use `node/install` instead of `node/run`; use `codecov` action [`3c31937`](https://github.com/ljharb/es-to-primitive/commit/3c31937119ca24fd6d00e362d6435a28cfe9e91c)
- [meta] do not publish github action workflow files [`389567e`](https://github.com/ljharb/es-to-primitive/commit/389567e8523b65b90b529f1029d215fd4f12ac14)
- [meta] use `npmignore` to autogenerate an npmignore file [`9f3aa76`](https://github.com/ljharb/es-to-primitive/commit/9f3aa7651791ab9386408035491a1ba4fec4c432)
- [actions] split out node 10-20, and 20+ [`c60d7d8`](https://github.com/ljharb/es-to-primitive/commit/c60d7d822a36880bce0535335c70fdc2a8da232d)
- [Tests] run `nyc` on all tests; use `tape` runner [`29cbb89`](https://github.com/ljharb/es-to-primitive/commit/29cbb89800b5cfef9bef3ae7e0e779c782e1bbb9)
- [meta] add `auto-changelog` [`ea744b2`](https://github.com/ljharb/es-to-primitive/commit/ea744b2a0bda788b0d957c1787e41434e98b0155)
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `aud`, `function.prototype.name`, `has-symbols`, `object-inspect`, `object-is`, `tape` [`e5c3c79`](https://github.com/ljharb/es-to-primitive/commit/e5c3c792f67685a9647e817e7582d4c76a876f69)
- [actions] add automatic rebasing / merge commit blocking [`a5a6f00`](https://github.com/ljharb/es-to-primitive/commit/a5a6f0066540c91c8aa45a4921f1cd2349f435ba)
- [Dev Deps] update `@ljharb/eslint-config`, `es-value-fixtures`, `function.prototype.name`, `npmignore`, `object-inspect`, `object-is`, `tape` [`7941fd5`](https://github.com/ljharb/es-to-primitive/commit/7941fd530fb3a73f923b76c739335ffc21793ad6)
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `aud`, `es-value-fixtures`, `foreach`, `object-inspect`, `tape` [`eb1c79c`](https://github.com/ljharb/es-to-primitive/commit/eb1c79c288c89154014634b94f64308344901eaf)
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `function.prototype.name`, `object-inspect`, `safe-publish-latest`, `tape` [`249b42f`](https://github.com/ljharb/es-to-primitive/commit/249b42f1ce069ea78a032f10414d1c1c0b6c6345)
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `aud`, `function.prototype.name`, `object-inspect`, `object-is`, `tape` [`d57d5e9`](https://github.com/ljharb/es-to-primitive/commit/d57d5e9ea5ea4778f383e2f1aa637be0be80dd78)
- [actions] update codecov uploader [`003b62c`](https://github.com/ljharb/es-to-primitive/commit/003b62c483372d5eac38f51925b6cbdf5d7a0665)
- [actions] add "Allow Edits" workflow [`75ee990`](https://github.com/ljharb/es-to-primitive/commit/75ee99083626dc14558ae294c127e4aaf925d214)
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `tape`, `object-is`; add `safe-publish-latest` [`ba5da7b`](https://github.com/ljharb/es-to-primitive/commit/ba5da7bffd93c3cc2e079ad751a3e678333a973e)
- [readme] remove travis badge [`6f7aec7`](https://github.com/ljharb/es-to-primitive/commit/6f7aec78e4f1ebfca74c384a837063f4099e7b9b)
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `aud`, `object-inspect`, `tape` [`3291fd5`](https://github.com/ljharb/es-to-primitive/commit/3291fd567695b45bddc58e5ec3da2dcce0e5ccc7)
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `function.prototype.name`, `has-symbols`, `object-inspect` [`53007f2`](https://github.com/ljharb/es-to-primitive/commit/53007f25d1f26e301b4f41d070c423723bed1690)
- [actions] update checkout action [`69640db`](https://github.com/ljharb/es-to-primitive/commit/69640dbb9ddafe05527388fe72bda1aca08d07b5)
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `object-is`, `tape`; add `aud` [`c9d644e`](https://github.com/ljharb/es-to-primitive/commit/c9d644ef3c6b2210e86ce2d3aa8e8b1668f6801d)
- [Tests] use `for-each` instead of `foreach` [`e9117bb`](https://github.com/ljharb/es-to-primitive/commit/e9117bb055417cb721dbf5dbe1d23b058a8241f2)
- [readme] add github actions/codecov badges [`53cd375`](https://github.com/ljharb/es-to-primitive/commit/53cd375ab22a25d4bada35000473e30c22ee2028)
- [Deps] update `is-callable`, `is-date-object`, `is-symbol` [`8116c68`](https://github.com/ljharb/es-to-primitive/commit/8116c68a8ba555f8daaf1d71a60c974d3439c94b)
- [Tests] fix test skipping for `Symbol.toPrimitive` [`e6268ef`](https://github.com/ljharb/es-to-primitive/commit/e6268ef31b34cb5263501ba9735ccce78a07e504)
- [actions] switch Automatic Rebase workflow to `pull_request_target` event [`da41c40`](https://github.com/ljharb/es-to-primitive/commit/da41c40399c2a574f74a59b85800d9934b91d49a)
- [Deps] update `is-callable`, `is-date-object` [`96fe13f`](https://github.com/ljharb/es-to-primitive/commit/96fe13ff3c486c7857c2ca69ac70161ef0e5b4a1)
- [Tests] replace `aud` with `npm audit` [`0b53154`](https://github.com/ljharb/es-to-primitive/commit/0b531546081427cb8a4fc06fde5540ba0b287b5b)
- [meta] use `prepublishOnly` script for npm 7+ [`9d7d485`](https://github.com/ljharb/es-to-primitive/commit/9d7d4856d4b5f28c68de2aba068522b9a85ee669)
- [Deps] update `is-callable` [`3c990b6`](https://github.com/ljharb/es-to-primitive/commit/3c990b646813e2470b19460e32801113f9acc13b)
- [Deps] update `is-callable` [`9bcfff2`](https://github.com/ljharb/es-to-primitive/commit/9bcfff276ce078034404b6b27e4f74beb530002c)
- [Deps] update `is-callable` [`1eb5478`](https://github.com/ljharb/es-to-primitive/commit/1eb5478e0c93b230b7bc67f9fef963d94a391117)
- [meta] only run `aud` on prod deps [`1fcd896`](https://github.com/ljharb/es-to-primitive/commit/1fcd89684a4351c15fec2cb289ecc331f917b80e)
- [Deps] update `is-symbol` [`7174a47`](https://github.com/ljharb/es-to-primitive/commit/7174a474f4f9f07319c81f046b10446caf9b3af0)
<!-- auto-changelog-above -->
1.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: ES5s 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: ES5s 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.
+22
View File
@@ -0,0 +1,22 @@
The 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.
+52
View File
@@ -0,0 +1,52 @@
# es-to-primitive <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
[![github actions][actions-image]][actions-url]
[![coverage][codecov-image]][codecov-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]: https://versionbadg.es/ljharb/es-to-primitive.svg
[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]: https://img.shields.io/npm/l/es-to-primitive.svg
[license-url]: LICENSE
[downloads-image]: https://img.shields.io/npm/dm/es-to-primitive.svg
[downloads-url]: https://npm-stat.com/charts.html?package=es-to-primitive
[codecov-image]: https://codecov.io/gh/ljharb/es-to-primitive/branch/main/graphs/badge.svg
[codecov-url]: https://app.codecov.io/gh/ljharb/es-to-primitive/
[actions-image]: https://img.shields.io/github/check-runs/ljharb/es-to-primitive/main
[actions-url]: https://github.com/ljharb/es-to-primitive/actions
+14
View File
@@ -0,0 +1,14 @@
import type { primitiveES5 } from './es5';
declare function ToPrimitive(
input: unknown,
hint?: StringConstructor | NumberConstructor,
): ToPrimitive.primitiveES6;
declare namespace ToPrimitive {
export type primitiveES6 = primitiveES5 | symbol;
export type unknownES6 = primitiveES6 | object;
}
export = ToPrimitive;
+80
View File
@@ -0,0 +1,80 @@
'use strict';
var hasSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol';
var isCallable = require('is-callable');
var isDate = require('is-date-object');
var isSymbol = require('is-symbol');
var $TypeError = require('es-errors/type');
var isPrimitive = require('./helpers/isPrimitive');
/** @import { primitiveES6 } from './es2015' */
/** @type {(O: { valueOf?: () => unknown, toString?: () => unknown }, hint: 'number' | 'string') => primitiveES6} */
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"');
}
/** @type {('toString' | 'valueOf')[]} */
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 /** @type {primitiveES6} */ (result);
}
}
}
throw new $TypeError('No default value');
}
var GetMethod = require('es-abstract-get/GetMethod');
/** @type {import('./es2015')} */
// http://www.ecma-international.org/ecma-262/6.0/#sec-toprimitive
module.exports = function ToPrimitive(input) {
if (isPrimitive(input)) {
return /** @type {primitiveES6} */ (input);
}
/** @type {'default' | 'string' | 'number'} */
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(
/** @type {{ [k in SymbolConstructor['toPrimitive']]?: Function }} */
(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 /** @type {primitiveES6} */ (result);
}
throw new $TypeError('unable to convert exotic object to primitive');
}
if (hint === 'default' && (isDate(input) || isSymbol(input))) {
hint = /** @type {const} */ ('string');
}
return OrdinaryToPrimitive(input, hint === 'default' ? 'number' : hint);
};
+17
View File
@@ -0,0 +1,17 @@
declare function ToPrimitive(
input: ToPrimitive.unknownES5,
hint?: StringConstructor | NumberConstructor,
): ToPrimitive.primitiveES5;
declare namespace ToPrimitive {
export type primitiveES5 =
| null
| undefined
| string
| number
| boolean
;
export type unknownES5 = primitiveES5 | object;
}
export = ToPrimitive;
+56
View File
@@ -0,0 +1,56 @@
'use strict';
var toStr = Object.prototype.toString;
var isPrimitive = require('./helpers/isPrimitive');
var isCallable = require('is-callable');
var $TypeError = require('es-errors/type');
/** @import { unknownES5 } from './es5' */
// http://ecma-international.org/ecma-262/5.1/#sec-8.12.8
var ES5internalSlots = /** @type {const} */ ({
'[[DefaultValue]]': /** @param {object} O */ 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
? /** @type {const} */ (['toString', 'valueOf'])
: /** @type {const} */ (['valueOf', 'toString']);
/** @type {unknownES5} */
var value;
for (var i = 0; i < methods.length; ++i) {
var method = methods[i];
if (isCallable(O[method])) {
value = O[method]();
if (isPrimitive(value)) {
return value;
}
}
}
throw new $TypeError('No default value');
}
throw new $TypeError('invalid [[DefaultValue]] hint supplied');
}
});
/** @type {import('./es5')} */
// 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);
};
+3
View File
@@ -0,0 +1,3 @@
import ES2015 from './es2015';
export = ES2015;
+4
View File
@@ -0,0 +1,4 @@
'use strict';
/** @type {import('./es2015')} */
module.exports = require('./es2015');
+29
View File
@@ -0,0 +1,29 @@
import ljharb from '@ljharb/eslint-config/flat';
export default [
...ljharb,
{
rules: {
'func-style': [
'error',
'declaration',
],
'id-length': [
'error', {
max: 24,
min: 1,
properties: 'never',
},
],
'new-cap': [
'error', {
capIsNewExceptions: [
'GetMethod',
'OrdinaryToPrimitive',
],
},
],
'no-extra-parens': 'off',
},
},
];
+9
View File
@@ -0,0 +1,9 @@
'use strict';
/** @import { primitive } from '../' */
/** @import { primitiveES5 } from '../es5' */
/** @type {<T extends primitive | primitiveES5>(value: unknown) => value is T} */
module.exports = function isPrimitive(value) {
return value === null || (typeof value !== 'function' && typeof value !== 'object');
};
+26
View File
@@ -0,0 +1,26 @@
import type { primitiveES6 } from './es2015';
import es5 from './es5';
import es6 from './es6';
import es2015 from './es2015';
declare function ToPrimitive(
input: unknown,
hint?: StringConstructor | NumberConstructor,
): ToPrimitive.primitive;
declare namespace ToPrimitive {
export type ES5 = typeof es5;
export const ES5: typeof es5;
/** @deprecated */
export type ES6 = typeof es6;
/** @deprecated */
export const ES6: typeof es6;
export type ES2015 = typeof es2015;
export const ES2015: typeof es2015;
export type primitive = primitiveES6 | bigint;
}
export = ToPrimitive;
+21
View File
@@ -0,0 +1,21 @@
'use strict';
var defineProperty = require('es-define-property');
var ES5 = require('./es5');
var ES6 = require('./es6');
var ES2015 = require('./es2015');
module.exports = ES2015;
// `module.exports.x =` form lets `cjs-module-lexer` statically detect these as named exports (ESM interop / attw)
module.exports.ES5 = ES5;
module.exports.ES6 = ES6;
module.exports.ES2015 = ES2015;
if (defineProperty) {
// `ES2015` is `module.exports`; defining here rather than on `module.exports` keeps the lexer from de-listing the above
defineProperty(ES2015, 'ES5', { enumerable: false });
defineProperty(ES2015, 'ES6', { enumerable: false });
defineProperty(ES2015, 'ES2015', { enumerable: false });
}
+96
View File
@@ -0,0 +1,96 @@
{
"name": "es-to-primitive",
"version": "1.3.4",
"author": "Jordan Harband <ljharb@gmail.com>",
"funding": {
"url": "https://github.com/sponsors/ljharb"
},
"description": "ECMAScript “ToPrimitive” algorithm. Provides ES5 and ES2015 versions.",
"license": "MIT",
"main": "index.js",
"scripts": {
"prepack": "npmignore --auto --commentLines=autogenerated",
"prepublish": "not-in-publish || npm run prepublishOnly",
"prepublishOnly": "safe-publish-latest",
"pretest": "npm run lint",
"test": "npm run tests-only",
"posttest": "npx npm@'>=10.2' audit --production",
"tests-only": "nyc tape 'test/**/*.js'",
"lint": "eslint .",
"postlint": "tsc && attw -P",
"version": "auto-changelog && git add CHANGELOG.md",
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
},
"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": {
"es-abstract-get": "^1.0.0",
"es-define-property": "^1.0.1",
"es-errors": "^1.3.0",
"is-callable": "^1.2.7",
"is-date-object": "^1.1.0",
"is-symbol": "^1.1.1"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.18.4",
"@ljharb/eslint-config": "^22.2.3",
"@ljharb/tsconfig": "^0.3.2",
"@types/is-callable": "^1.1.2",
"@types/node": "^12.20.55",
"@types/object-inspect": "^1.13.0",
"@types/object-is": "^1.1.0",
"auto-changelog": "^2.6.0",
"es-value-fixtures": "^1.7.1",
"eslint": "^10.5.0",
"for-each": "^0.3.5",
"function.prototype.name": "^1.2.0",
"jiti": "^0.0.0",
"npmignore": "^0.3.5",
"nyc": "^10.3.2",
"object-inspect": "^1.13.4",
"object-is": "^1.1.6",
"safe-publish-latest": "^2.0.0",
"tape": "^5.10.2",
"typescript": "next"
},
"testling": {
"files": "test/index.js"
},
"engines": {
"node": ">= 0.4"
},
"auto-changelog": {
"output": "CHANGELOG.md",
"template": "keepachangelog",
"unreleased": false,
"commitLimit": false,
"backfillLimit": false,
"hideCredit": true,
"startingVersion": "1.2.2"
},
"publishConfig": {
"ignore": [
".github/workflows"
]
}
}
+140
View File
@@ -0,0 +1,140 @@
'use strict';
var test = require('tape');
var toPrimitive = require('../es2015');
var is = require('object-is');
var forEach = require('for-each');
var functionName = require('function.prototype.name');
var debug = require('object-inspect');
var v = require('es-value-fixtures');
/** @typedef {{ toString?: unknown, valueOf?: unknown, [Symbol.toPrimitive]?: unknown }} Coercible */
test('function properties', function (t) {
t.equal(toPrimitive.length, 1, 'length is 1');
t.equal(functionName(toPrimitive), 'ToPrimitive', 'name is ToPrimitive');
t.end();
});
test('primitives', function (t) {
forEach(v.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: !v.hasSymbols }, function (t) {
forEach(v.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();
});
test('Objects', function (t) {
t.equal(toPrimitive(v.coercibleObject), v.coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf');
t.equal(toPrimitive(v.coercibleObject, Number), v.coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf');
t.equal(toPrimitive(v.coercibleObject, String), v.coercibleObject.toString(), 'coercibleObject with hint String coerces to non-stringified toString');
t.equal(toPrimitive(v.coercibleFnObject), v.coercibleFnObject.toString(), 'coercibleFnObject coerces to non-stringified toString');
t.equal(toPrimitive(v.coercibleFnObject, Number), v.coercibleFnObject.toString(), 'coercibleFnObject with hint Number coerces to non-stringified toString');
t.equal(toPrimitive(v.coercibleFnObject, String), v.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(v.toStringOnlyObject), v.toStringOnlyObject.toString(), 'toStringOnlyObject returns non-stringified toString');
t.equal(toPrimitive(v.toStringOnlyObject, Number), v.toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns non-stringified toString');
t.equal(toPrimitive(v.toStringOnlyObject, String), v.toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns non-stringified toString');
t.equal(toPrimitive(v.valueOfOnlyObject), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf');
t.equal(toPrimitive(v.valueOfOnlyObject, Number), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf');
t.equal(toPrimitive(v.valueOfOnlyObject, String), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns non-stringified valueOf');
t.test('Symbol.toPrimitive', { skip: !v.hasSymbols || !Symbol.toPrimitive }, function (st) {
/** @type {Coercible} */
var overriddenObject = { toString: st.fail, valueOf: st.fail };
overriddenObject[Symbol.toPrimitive] = /** @type {(hint: string) => unknown} */ 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');
/** @type {Coercible} */
var nullToPrimitive = { toString: v.coercibleObject.toString, valueOf: v.coercibleObject.valueOf };
nullToPrimitive[Symbol.toPrimitive] = null;
st.equal(toPrimitive(nullToPrimitive), toPrimitive(v.coercibleObject), 'object with no hint + null Symbol.toPrimitive ignores it');
st.equal(toPrimitive(nullToPrimitive, Number), toPrimitive(v.coercibleObject, Number), 'object with hint Number + null Symbol.toPrimitive ignores it');
st.equal(toPrimitive(nullToPrimitive, String), toPrimitive(v.coercibleObject, String), 'object with hint String + null Symbol.toPrimitive ignores it');
st.test('exceptions', function (sst) {
/** @type {Coercible} */
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');
/** @type {Coercible} */
var uncoercibleToPrimitive = { toString: sst.fail, valueOf: sst.fail };
uncoercibleToPrimitive[Symbol.toPrimitive] = /** @type {(hint: string) => unknown} */ function (hint) {
return { toString: function () { return hint; } };
};
sst['throws'](toPrimitive.bind(null, uncoercibleToPrimitive), TypeError, 'Symbol.toPrimitive returning an object throws');
/** @type {Coercible} */
var throwingToPrimitive = { toString: sst.fail, valueOf: sst.fail };
throwingToPrimitive[Symbol.toPrimitive] = /** @type {(hint: string) => unknown} */ 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, v.uncoercibleObject), TypeError, 'uncoercibleObject throws a TypeError');
st['throws'](toPrimitive.bind(null, v.uncoercibleObject, Number), TypeError, 'uncoercibleObject with hint Number throws a TypeError');
st['throws'](toPrimitive.bind(null, v.uncoercibleObject, String), TypeError, 'uncoercibleObject with hint String throws a TypeError');
st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject), TypeError, 'uncoercibleFnObject throws a TypeError');
st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject, Number), TypeError, 'uncoercibleFnObject with hint Number throws a TypeError');
st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject, String), TypeError, 'uncoercibleFnObject with hint String throws a TypeError');
st.end();
});
t.end();
});
+103
View File
@@ -0,0 +1,103 @@
'use strict';
var test = require('tape');
var toPrimitive = require('../es5');
var is = require('object-is');
var forEach = require('for-each');
var functionName = require('function.prototype.name');
var debug = require('object-inspect');
var v = require('es-value-fixtures');
test('function properties', function (t) {
t.equal(toPrimitive.length, 1, 'length is 1');
t.equal(functionName(toPrimitive), 'ToPrimitive', 'name is ToPrimitive');
t.end();
});
test('primitives', function (t) {
forEach(v.primitives, function (i) {
if (typeof i !== 'symbol' && typeof i !== 'bigint') {
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: !v.hasSymbols }, function (t) {
forEach(v.symbols, function (sym) {
// @ts-expect-error no symbols in ES5
t.equal(toPrimitive(sym), sym, 'toPrimitive(' + debug(sym) + ') returns the same value');
// @ts-expect-error no symbols in ES5
t.equal(toPrimitive(sym, String), sym, 'toPrimitive(' + debug(sym) + ', String) returns the same value');
// @ts-expect-error no symbols in ES5
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();
});
test('Objects', function (t) {
t.equal(toPrimitive(v.coercibleObject), v.coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf');
t.equal(toPrimitive(v.coercibleObject, String), v.coercibleObject.toString(), 'coercibleObject with hint String coerces to toString');
t.equal(toPrimitive(v.coercibleObject, Number), v.coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf');
t.equal(toPrimitive(v.coercibleFnObject), v.coercibleFnObject.toString(), 'coercibleFnObject coerces to toString');
t.equal(toPrimitive(v.coercibleFnObject, String), v.coercibleFnObject.toString(), 'coercibleFnObject with hint String coerces to toString');
t.equal(toPrimitive(v.coercibleFnObject, Number), v.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(v.toStringOnlyObject), v.toStringOnlyObject.toString(), 'toStringOnlyObject returns toString');
t.equal(toPrimitive(v.toStringOnlyObject, String), v.toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns toString');
t.equal(toPrimitive(v.toStringOnlyObject, Number), v.toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns toString');
t.equal(toPrimitive(v.valueOfOnlyObject), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf');
t.equal(toPrimitive(v.valueOfOnlyObject, String), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns valueOf');
t.equal(toPrimitive(v.valueOfOnlyObject, Number), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf');
t.test('exceptions', function (st) {
st['throws'](toPrimitive.bind(null, v.uncoercibleObject), TypeError, 'uncoercibleObject throws a TypeError');
st['throws'](toPrimitive.bind(null, v.uncoercibleObject, String), TypeError, 'uncoercibleObject with hint String throws a TypeError');
st['throws'](toPrimitive.bind(null, v.uncoercibleObject, Number), TypeError, 'uncoercibleObject with hint Number throws a TypeError');
st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject), TypeError, 'uncoercibleFnObject throws a TypeError');
st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject, String), TypeError, 'uncoercibleFnObject with hint String throws a TypeError');
st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject, Number), TypeError, 'uncoercibleFnObject with hint Number throws a TypeError');
st.end();
});
t.end();
});
+140
View File
@@ -0,0 +1,140 @@
'use strict';
var test = require('tape');
var toPrimitive = require('../es6');
var is = require('object-is');
var forEach = require('for-each');
var functionName = require('function.prototype.name');
var debug = require('object-inspect');
var v = require('es-value-fixtures');
/** @typedef {{ toString?: unknown, valueOf?: unknown, [Symbol.toPrimitive]?: unknown }} Coercible */
test('function properties', function (t) {
t.equal(toPrimitive.length, 1, 'length is 1');
t.equal(functionName(toPrimitive), 'ToPrimitive', 'name is ToPrimitive');
t.end();
});
test('primitives', function (t) {
forEach(v.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: !v.hasSymbols }, function (t) {
forEach(v.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();
});
test('Objects', function (t) {
t.equal(toPrimitive(v.coercibleObject), v.coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf');
t.equal(toPrimitive(v.coercibleObject, Number), v.coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf');
t.equal(toPrimitive(v.coercibleObject, String), v.coercibleObject.toString(), 'coercibleObject with hint String coerces to non-stringified toString');
t.equal(toPrimitive(v.coercibleFnObject), v.coercibleFnObject.toString(), 'coercibleFnObject coerces to non-stringified toString');
t.equal(toPrimitive(v.coercibleFnObject, Number), v.coercibleFnObject.toString(), 'coercibleFnObject with hint Number coerces to non-stringified toString');
t.equal(toPrimitive(v.coercibleFnObject, String), v.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(v.toStringOnlyObject), v.toStringOnlyObject.toString(), 'toStringOnlyObject returns non-stringified toString');
t.equal(toPrimitive(v.toStringOnlyObject, Number), v.toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns non-stringified toString');
t.equal(toPrimitive(v.toStringOnlyObject, String), v.toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns non-stringified toString');
t.equal(toPrimitive(v.valueOfOnlyObject), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf');
t.equal(toPrimitive(v.valueOfOnlyObject, Number), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf');
t.equal(toPrimitive(v.valueOfOnlyObject, String), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns non-stringified valueOf');
t.test('Symbol.toPrimitive', { skip: !v.hasSymbols || !Symbol.toPrimitive }, function (st) {
/** @type {Coercible} */
var overriddenObject = { toString: st.fail, valueOf: st.fail };
overriddenObject[Symbol.toPrimitive] = /** @type {(hint: string) => unknown} */ 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');
/** @type {Coercible} */
var nullToPrimitive = { toString: v.coercibleObject.toString, valueOf: v.coercibleObject.valueOf };
nullToPrimitive[Symbol.toPrimitive] = null;
st.equal(toPrimitive(nullToPrimitive), toPrimitive(v.coercibleObject), 'object with no hint + null Symbol.toPrimitive ignores it');
st.equal(toPrimitive(nullToPrimitive, Number), toPrimitive(v.coercibleObject, Number), 'object with hint Number + null Symbol.toPrimitive ignores it');
st.equal(toPrimitive(nullToPrimitive, String), toPrimitive(v.coercibleObject, String), 'object with hint String + null Symbol.toPrimitive ignores it');
st.test('exceptions', function (sst) {
/** @type {Coercible} */
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');
/** @type {Coercible} */
var uncoercibleToPrimitive = { toString: sst.fail, valueOf: sst.fail };
uncoercibleToPrimitive[Symbol.toPrimitive] = /** @type {(hint: string) => unknown} */ function (hint) {
return { toString: function () { return hint; } };
};
sst['throws'](toPrimitive.bind(null, uncoercibleToPrimitive), TypeError, 'Symbol.toPrimitive returning an object throws');
/** @type {Coercible} */
var throwingToPrimitive = { toString: sst.fail, valueOf: sst.fail };
throwingToPrimitive[Symbol.toPrimitive] = /** @type {(hint: string) => unknown} */ 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, v.uncoercibleObject), TypeError, 'uncoercibleObject throws a TypeError');
st['throws'](toPrimitive.bind(null, v.uncoercibleObject, Number), TypeError, 'uncoercibleObject with hint Number throws a TypeError');
st['throws'](toPrimitive.bind(null, v.uncoercibleObject, String), TypeError, 'uncoercibleObject with hint String throws a TypeError');
st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject), TypeError, 'uncoercibleFnObject throws a TypeError');
st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject, Number), TypeError, 'uncoercibleFnObject with hint Number throws a TypeError');
st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject, String), TypeError, 'uncoercibleFnObject with hint String throws a TypeError');
st.end();
});
t.end();
});
+20
View File
@@ -0,0 +1,20 @@
'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');
+9
View File
@@ -0,0 +1,9 @@
{
"extends": "@ljharb/tsconfig",
"compilerOptions": {
"target": "es2021",
},
"exclude": [
"coverage",
],
}