Security News

### OpenJS: “XZ Utils Cyberattack Likely Not an Isolated Incident”

OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.

which-boxed-primitive

Advanced tools

- Version published

- Maintainers
- 1

- Weekly downloads
- 23,078,796
- decreased by-16.47%

- Install size
- 210 kB

Weekly downloads

Package description

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.

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)"`

The `kind-of` package is similar to `which-boxed-primitive` in that it provides a way to check the type of a value in JavaScript. However, `kind-of` goes beyond just boxed primitives and can identify many other types, including arrays, regular expressions, dates, and more. This makes `kind-of` more versatile for general type checking, but potentially less focused if the specific need is to identify boxed primitives.

The `is` package offers a collection of type-check functions, such as `is.string`, `is.number`, and so on, which can be used to determine the type of a given value. While it includes functions for checking boxed types (e.g., `is.object` and specific checks for boxed types), it is broader in scope and includes checks for many other types of values. Compared to `which-boxed-primitive`, `is` provides a more granular approach to type checking at the cost of requiring more specific function calls for each type.

Readme

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.

```
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);
```

Simply clone the repo, `npm install`

, and run `npm test`

FAQs

Which kind of boxed JS primitive is this?

The npm package which-boxed-primitive receives a total of **17,868,494** weekly downloads. As such, which-boxed-primitive popularity was classified as **popular**.

We found that which-boxed-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

OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.

By Sarah Gooding - Apr 17, 2024

Company News

Come meet the Socket team at BSidesSF and RSA! We're sponsoring several fun networking events and we would love to see you there.

By Sarah Gooding - Apr 15, 2024

Security News

OSI is starting a conversation aimed at removing the excuse of the SaaS loophole for companies navigating licensing and the complexities of doing business with open source.

By Sarah Gooding - Apr 12, 2024