What is es-to-primitive?
The es-to-primitive npm package provides functionality to convert a JavaScript value to its primitive equivalent, following the ECMAScript specification for ToPrimitive abstract operation. It handles the conversion according to the preferred type hint, either Number or String.
What are es-to-primitive's main functionalities?
Converting to primitive with no hint
This code converts a Date object to its primitive value without specifying a type hint. The default hint will be applied based on the object type.
var toPrimitive = require('es-to-primitive');
var obj = new Date();
var primitiveValue = toPrimitive(obj);
Converting to primitive with a hint of Number
This code converts a Date object to its primitive value with a type hint of Number, which means it will prefer the numeric value of the Date (timestamp).
var toPrimitive = require('es-to-primitive');
var obj = new Date();
var primitiveValue = toPrimitive(obj, Number);
Converting to primitive with a hint of String
This code converts an object with both toString and valueOf methods to its primitive value with a type hint of String, which means it will prefer the string representation 'foo'.
var toPrimitive = require('es-to-primitive');
var obj = { toString: function() { return 'foo'; }, valueOf: function() { return 42; } };
var primitiveValue = toPrimitive(obj, String);
Other packages similar to es-to-primitive
to-primitive
The to-primitive package is another alternative that offers similar functionality to es-to-primitive. It aims to provide a reliable way to get the primitive value of objects but may have different internal implementations or API design.
v1.3.0 - 2024-11-26
Commits
- [actions] reuse common workflows
bb72efc
- [Tests] use
es-value-fixtures
a912f7b
- [Tests] migrate tests to Github Actions
510baf0
- [New] add types
69ba1fd
- [meta] remove unused Makefile
4ea66e6
- [actions] use
node/install
instead of node/run
; use codecov
action 3c31937
- [meta] do not publish github action workflow files
389567e
- [meta] use
npmignore
to autogenerate an npmignore file 9f3aa76
- [actions] split out node 10-20, and 20+
c60d7d8
- [Tests] run
nyc
on all tests; use tape
runner 29cbb89
- [meta] add
auto-changelog
ea744b2
- [Dev Deps] update
eslint
, @ljharb/eslint-config
, aud
, function.prototype.name
, has-symbols
, object-inspect
, object-is
, tape
e5c3c79
- [actions] add automatic rebasing / merge commit blocking
a5a6f00
- [Dev Deps] update
@ljharb/eslint-config
, es-value-fixtures
, function.prototype.name
, npmignore
, object-inspect
, object-is
, tape
7941fd5
- [Dev Deps] update
eslint
, @ljharb/eslint-config
, aud
, es-value-fixtures
, foreach
, object-inspect
, tape
eb1c79c
- [Dev Deps] update
eslint
, @ljharb/eslint-config
, function.prototype.name
, object-inspect
, safe-publish-latest
, tape
249b42f
- [Dev Deps] update
eslint
, @ljharb/eslint-config
, aud
, function.prototype.name
, object-inspect
, object-is
, tape
d57d5e9
- [actions] update codecov uploader
003b62c
- [actions] add "Allow Edits" workflow
75ee990
- [Dev Deps] update
eslint
, @ljharb/eslint-config
, tape
, object-is
; add safe-publish-latest
ba5da7b
- [readme] remove travis badge
6f7aec7
- [Dev Deps] update
eslint
, @ljharb/eslint-config
, aud
, object-inspect
, tape
3291fd5
- [Dev Deps] update
eslint
, @ljharb/eslint-config
, function.prototype.name
, has-symbols
, object-inspect
53007f2
- [actions] update checkout action
69640db
- [Dev Deps] update
eslint
, @ljharb/eslint-config
, object-is
, tape
; add aud
c9d644e
- [Tests] use
for-each
instead of foreach
e9117bb
- [readme] add github actions/codecov badges
53cd375
- [Deps] update
is-callable
, is-date-object
, is-symbol
8116c68
- [Tests] fix test skipping for
Symbol.toPrimitive
e6268ef
- [actions] switch Automatic Rebase workflow to
pull_request_target
event da41c40
- [Deps] update
is-callable
, is-date-object
96fe13f
- [Tests] replace
aud
with npm audit
0b53154
- [meta] use
prepublishOnly
script for npm 7+ 9d7d485
- [Deps] update
is-callable
3c990b6
- [Deps] update
is-callable
9bcfff2
- [Deps] update
is-callable
1eb5478
- [meta] only run
aud
on prod deps 1fcd896
- [Deps] update
is-symbol
7174a47
<!-- 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: 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