Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
es-to-primitive
Advanced tools
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.
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);
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.
ECMAScript “ToPrimitive” algorithm. Provides ES5 and ES6 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
/es6
/es7
exported property if you require a specific version.
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);
Simply clone the repo, npm install
, and run npm test
FAQs
ECMAScript “ToPrimitive” algorithm. Provides ES5 and ES2015 versions.
The npm package es-to-primitive receives a total of 28,824,012 weekly downloads. As such, es-to-primitive popularity was classified as popular.
We found that es-to-primitive demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.