Socket
Socket
Sign inDemoInstall

which-boxed-primitive

Package Overview
Dependencies
19
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    which-boxed-primitive

Which kind of boxed JS primitive is this?


Version published
Maintainers
1
Install size
210 kB
Created

Package description

What is which-boxed-primitive?

The `which-boxed-primitive` npm package is designed to help developers identify the type of boxed primitives (objects that encapsulate primitive values) in JavaScript. This can be particularly useful when working with code that needs to distinguish between different types of object wrappers around primitive values, such as `Number`, `String`, `Boolean`, `Symbol`, `BigInt`, etc.

What are which-boxed-primitive's main functionalities?

Identifying boxed primitives

This feature allows developers to pass an object to the `whichBoxedPrimitive` function and receive a string indicating the type of boxed primitive, or `undefined` if the object is not a boxed primitive. This can be useful for type checking and ensuring that certain operations are only performed on the expected types of objects.

"use strict";\nconst whichBoxedPrimitive = require('which-boxed-primitive');\n\nconsole.log(whichBoxedPrimitive(new String('hello'))); // 'String'\nconsole.log(whichBoxedPrimitive(new Number(123))); // 'Number'\nconsole.log(whichBoxedPrimitive(new Boolean(true))); // 'Boolean'\nconsole.log(whichBoxedPrimitive(Object(Symbol('sym')))); // 'Symbol'\nconsole.log(whichBoxedPrimitive(Object(BigInt(10)))); // 'BigInt'\nconsole.log(whichBoxedPrimitive([])); // undefined (not a boxed primitive)"

Other packages similar to which-boxed-primitive

Changelog

Source

v1.0.2 - 2020-12-14

Commits

  • [Tests] use shared travis-ci configs 8674582
  • [Tests] migrate tests to Github Actions dff6643
  • [meta] do not publish github action workflow files b26112a
  • [meta] make auto-changelog config consistent 8d10175
  • [readme] fix repo URLs, remove defunct badges ab8db24
  • [Tests] run nyc on all tests; use tape runner 7d084df
  • [Dev Deps] update eslint, @ljharb/eslint-config, aud, auto-changelog, object-inspect, tape 576f6f3
  • [actions] add automatic rebasing / merge commit blocking 97efa53
  • [actions] add "Allow Edits" workflow fb1b4f7
  • [Dev Deps] update eslint, @ljharb/eslint-config, has-symbols, object-inspect, safe-publish-latest 1e03c61
  • [Deps] update is-boolean-object, is-number-object, is-string, is-symbol 13673df
  • [Dev Deps] update auto-changelog, in-publish, tape 65a0e15
  • [Dev Deps] update eslint, @ljharb/eslint-config, tape f8a0afe
  • [Deps] update is-bigint, is-boolean-object e7a1ce2
  • [actions] switch Automatic Rebase workflow to pull_request_target event e46f193
  • [Dev Deps] update @ljharb/eslint-config, tape df3da14
  • [Dev Deps] update auto-changelog; add aud e2e8a12
  • [meta] add funding field 7df404b
  • [Dev Deps] update auto-changelog 0d6b76d
  • [Tests] only audit prod deps 246151c
  • [meta] fix changelog c2d1685
  • [readme] Fix spelling error 25fb2b5

Readme

Source

which-boxed-primitive Version Badge

dependency status dev dependency status License Downloads

npm badge

Which kind of boxed JS primitive is this? This module works cross-realm/iframe, does not depend on instanceof or mutable properties, and works despite ES6 Symbol.toStringTag.

Example

var whichBoxedPrimitive = require('which-boxed-primitive');
var assert = require('assert');

// unboxed primitives return `null`
// boxed primitives return the builtin constructor name

assert.equal(whichBoxedPrimitive(undefined), null);
assert.equal(whichBoxedPrimitive(null), null);

assert.equal(whichBoxedPrimitive(false), null);
assert.equal(whichBoxedPrimitive(true), null);
assert.equal(whichBoxedPrimitive(new Boolean(false)), 'Boolean');
assert.equal(whichBoxedPrimitive(new Boolean(true)), 'Boolean');

assert.equal(whichBoxedPrimitive(42), null);
assert.equal(whichBoxedPrimitive(NaN), null);
assert.equal(whichBoxedPrimitive(Infinity), null);
assert.equal(whichBoxedPrimitive(new Number(42)), 'Number');
assert.equal(whichBoxedPrimitive(new Number(NaN)), 'Number');
assert.equal(whichBoxedPrimitive(new Number(Infinity)), 'Number');

assert.equal(whichBoxedPrimitive(''), null);
assert.equal(whichBoxedPrimitive('foo'), null);
assert.equal(whichBoxedPrimitive(new String('')), 'String');
assert.equal(whichBoxedPrimitive(new String('foo')), 'String');

assert.equal(whichBoxedPrimitive(Symbol()), null);
assert.equal(whichBoxedPrimitive(Object(Symbol()), 'Symbol');

assert.equal(whichBoxedPrimitive(42n), null);
assert.equal(whichBoxedPrimitive(Object(42n), 'BigInt');

// non-boxed-primitive objects return `undefined`
assert.equal(whichBoxedPrimitive([]), undefined);
assert.equal(whichBoxedPrimitive({}), undefined);
assert.equal(whichBoxedPrimitive(/a/g), undefined);
assert.equal(whichBoxedPrimitive(new RegExp('a', 'g')), undefined);
assert.equal(whichBoxedPrimitive(new Date()), undefined);
assert.equal(whichBoxedPrimitive(function () {}), undefined);
assert.equal(whichBoxedPrimitive(function* () {}), undefined);
assert.equal(whichBoxedPrimitive(x => x * x), undefined);
assert.equal(whichBoxedPrimitive([]), undefined);

Tests

Simply clone the repo, npm install, and run npm test

Keywords

FAQs

Last updated on 14 Dec 2020

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