Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

es-to-primitive

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

es-to-primitive

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

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
37M
increased by7.03%
Maintainers
1
Weekly downloads
 
Created

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

Keywords

FAQs

Package last updated on 26 Nov 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc