1
|
# es-to-primitive <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
|
2
|
|
3
|
[![Build Status][travis-svg]][travis-url]
|
4
|
[![dependency status][deps-svg]][deps-url]
|
5
|
[![dev dependency status][dev-deps-svg]][dev-deps-url]
|
6
|
[![License][license-image]][license-url]
|
7
|
[![Downloads][downloads-image]][downloads-url]
|
8
|
|
9
|
[![npm badge][npm-badge-png]][package-url]
|
10
|
|
11
|
ECMAScript “ToPrimitive” algorithm. Provides ES5 and ES2015 versions.
|
12
|
When different versions of the spec conflict, the default export will be the latest version of the abstract operation.
|
13
|
Alternative versions will also be available under an `es5`/`es2015` exported property if you require a specific version.
|
14
|
|
15
|
## Example
|
16
|
|
17
|
```js
|
18
|
var toPrimitive = require('es-to-primitive');
|
19
|
var assert = require('assert');
|
20
|
|
21
|
assert(toPrimitive(function () {}) === String(function () {}));
|
22
|
|
23
|
var date = new Date();
|
24
|
assert(toPrimitive(date) === String(date));
|
25
|
|
26
|
assert(toPrimitive({ valueOf: function () { return 3; } }) === 3);
|
27
|
|
28
|
assert(toPrimitive(['a', 'b', 3]) === String(['a', 'b', 3]));
|
29
|
|
30
|
var sym = Symbol();
|
31
|
assert(toPrimitive(Object(sym)) === sym);
|
32
|
```
|
33
|
|
34
|
## Tests
|
35
|
Simply clone the repo, `npm install`, and run `npm test`
|
36
|
|
37
|
[package-url]: https://npmjs.org/package/es-to-primitive
|
38
|
[npm-version-svg]: http://versionbadg.es/ljharb/es-to-primitive.svg
|
39
|
[travis-svg]: https://travis-ci.org/ljharb/es-to-primitive.svg
|
40
|
[travis-url]: https://travis-ci.org/ljharb/es-to-primitive
|
41
|
[deps-svg]: https://david-dm.org/ljharb/es-to-primitive.svg
|
42
|
[deps-url]: https://david-dm.org/ljharb/es-to-primitive
|
43
|
[dev-deps-svg]: https://david-dm.org/ljharb/es-to-primitive/dev-status.svg
|
44
|
[dev-deps-url]: https://david-dm.org/ljharb/es-to-primitive#info=devDependencies
|
45
|
[npm-badge-png]: https://nodei.co/npm/es-to-primitive.png?downloads=true&stars=true
|
46
|
[license-image]: http://img.shields.io/npm/l/es-to-primitive.svg
|
47
|
[license-url]: LICENSE
|
48
|
[downloads-image]: http://img.shields.io/npm/dm/es-to-primitive.svg
|
49
|
[downloads-url]: http://npm-stat.com/charts.html?package=es-to-primitive
|