Socket
Socket
Sign inDemoInstall

es-to-primitive

Package Overview
Dependencies
5
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

es-to-primitive

ECMAScript “ToPrimitive” algorithm. Provides ES5 and ES2015 versions.


Version published
Maintainers
1
Weekly downloads
28,733,192
increased by0.99%

Weekly downloads

Package description

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

Readme

Source

es-to-primitive Version Badge

Build Status dependency status dev dependency status License Downloads

npm badge

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

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

Keywords

FAQs

Last updated on 09 Nov 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc