[EDIT] FILE: unbox-primitive.tar
test/index.js 0000755 00000002065 15075406671 0007211 0 ustar 00 'use strict'; var test = require('tape'); var inspect = require('object-inspect'); var is = require('object-is'); var forEach = require('for-each'); var hasSymbols = require('has-symbols')(); var hasBigInts = require('has-bigints')(); var unboxPrimitive = require('..'); var debug = function (v, m) { return inspect(v) + ' ' + m; }; test('primitives', function (t) { var primitives = [ true, false, '', 'foo', 42, NaN, Infinity, 0 ]; if (hasSymbols) { primitives.push(Symbol(), Symbol.iterator, Symbol('f')); } if (hasBigInts) { primitives.push(BigInt(42), BigInt(0)); } forEach(primitives, function (primitive) { var obj = Object(primitive); t.ok( is(unboxPrimitive(obj), primitive), debug(obj, 'unboxes to ' + inspect(primitive)) ); }); t.end(); }); test('objects', function (t) { var objects = [ {}, [], function () {}, /a/g, new Date() ]; forEach(objects, function (object) { t['throws']( function () { unboxPrimitive(object); }, TypeError, debug(object, 'is not a primitive') ); }); t.end(); }); .nycrc 0000755 00000000330 15075406671 0005675 0 ustar 00 { "all": true, "check-coverage": false, "reporter": ["text-summary", "text", "html", "json"], "lines": 86, "statements": 85.93, "functions": 82.43, "branches": 76.06, "exclude": [ "coverage", "test" ] } .github/FUNDING.yml 0000755 00000001112 15075406671 0007732 0 ustar 00 # These are supported funding model platforms github: [ljharb] patreon: # Replace with a single Patreon username open_collective: # Replace with a single Open Collective username ko_fi: # Replace with a single Ko-fi username tidelift: npm/unbox-primitive community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry liberapay: # Replace with a single Liberapay username issuehunt: # Replace with a single IssueHunt username otechie: # Replace with a single Otechie username custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] README.md 0000755 00000004134 15075406671 0006043 0 ustar 00 # unbox-primitive <sup>[![Version Badge][npm-version-svg]][package-url]</sup> [![github actions][actions-image]][actions-url] [![coverage][codecov-image]][codecov-url] [![dependency status][deps-svg]][deps-url] [![dev dependency status][dev-deps-svg]][dev-deps-url] [![License][license-image]][license-url] [![Downloads][downloads-image]][downloads-url] [![npm badge][npm-badge-png]][package-url] Unbox a boxed JS primitive value. This module works cross-realm/iframe, does not depend on `instanceof` or mutable properties, and works despite ES6 Symbol.toStringTag. ## Example ```js var unboxPrimitive = require('unbox-primitive'); var assert = require('assert'); assert.equal(unboxPrimitive(new Boolean(false)), false); assert.equal(unboxPrimitive(new String('f')), 'f'); assert.equal(unboxPrimitive(new Number(42)), 42); const s = Symbol(); assert.equal(unboxPrimitive(Object(s)), s); assert.equal(unboxPrimitive(new BigInt(42)), 42n); // any primitive, or non-boxed-primitive object, will throw ``` ## Tests Simply clone the repo, `npm install`, and run `npm test` [package-url]: https://npmjs.org/package/unbox-primitive [npm-version-svg]: https://versionbadg.es/ljharb/unbox-primitive.svg [deps-svg]: https://david-dm.org/ljharb/unbox-primitive.svg [deps-url]: https://david-dm.org/ljharb/unbox-primitive [dev-deps-svg]: https://david-dm.org/ljharb/unbox-primitive/dev-status.svg [dev-deps-url]: https://david-dm.org/ljharb/unbox-primitive#info=devDependencies [npm-badge-png]: https://nodei.co/npm/unbox-primitive.png?downloads=true&stars=true [license-image]: https://img.shields.io/npm/l/unbox-primitive.svg [license-url]: LICENSE [downloads-image]: https://img.shields.io/npm/dm/unbox-primitive.svg [downloads-url]: https://npm-stat.com/charts.html?package=unbox-primitive [codecov-image]: https://codecov.io/gh/ljharb/unbox-primitive/branch/main/graphs/badge.svg [codecov-url]: https://app.codecov.io/gh/ljharb/unbox-primitive/ [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/unbox-primitive [actions-url]: https://github.com/ljharb/unbox-primitive/actions LICENSE 0000755 00000002057 15075406671 0005573 0 ustar 00 MIT License Copyright (c) 2019 Jordan Harband Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CHANGELOG.md 0000755 00000014343 15075406671 0006400 0 ustar 00 # Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [v1.0.2](https://github.com/ljharb/unbox-primitive/compare/v1.0.1...v1.0.2) - 2022-04-24 ### Commits - [actions] reuse common workflows [`e6420b9`](https://github.com/ljharb/unbox-primitive/commit/e6420b94eed3558ef3ecddbd3d85fe2f400d1db1) - [actions] update codecov uploader [`b90aff2`](https://github.com/ljharb/unbox-primitive/commit/b90aff2b17b70c1a7200eb5faa3f9e64ebf055f9) - [readme] add github actions/codecov badges; update URLs [`bcc39b9`](https://github.com/ljharb/unbox-primitive/commit/bcc39b9d6d101bad199c95604f94999f80d6da97) - [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `object-inspect`, `safe-publish-latest`, `tape` [`a704a32`](https://github.com/ljharb/unbox-primitive/commit/a704a32ec4af43cd0631317ae3a537012adc1610) - [Refactor] use `call-bind` instead of `function-bind` [`0a609f1`](https://github.com/ljharb/unbox-primitive/commit/0a609f1be6269cbf501482aacd9a0a1bd8596108) - [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `aud`, `auto-changelog`, `object-inspect`, `safe-publish-latest`, `tape` [`6a45317`](https://github.com/ljharb/unbox-primitive/commit/6a4531757d74af97c9d0770b102f81ce6ff0a8dc) - [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `aud`, `auto-changelog`, `object-inspect`, `tape` [`795c76f`](https://github.com/ljharb/unbox-primitive/commit/795c76f14c4df3c7ae934f7b14a98c07951ea9c7) - [Deps] update `has-bigints`, `has-symbols` [`257a065`](https://github.com/ljharb/unbox-primitive/commit/257a065496b4190c296d674bff342ea40cf8e176) ## [v1.0.1](https://github.com/ljharb/unbox-primitive/compare/v1.0.0...v1.0.1) - 2021-03-25 ### Commits - [Tests] use shared travis-ci configs [`f977e5f`](https://github.com/ljharb/unbox-primitive/commit/f977e5f8fa532dbc519bd78a48cf4b81c14720fe) - [Tests] migrate tests to Github Actions [`b89def6`](https://github.com/ljharb/unbox-primitive/commit/b89def60908a236aa1b5c756426f7cc61cf458dd) - [meta] do not publish github action workflow files [`325d1f1`](https://github.com/ljharb/unbox-primitive/commit/325d1f1836cecbe57ee148545de5aefcbe7a7dce) - readme [`810cd70`](https://github.com/ljharb/unbox-primitive/commit/810cd70f7b3c670cd55eae64466c89595175ee2a) - [Tests] run `nyc` on all tests; use `tape` runner [`2f5fb08`](https://github.com/ljharb/unbox-primitive/commit/2f5fb08930c8f8e5e069ac61891dc9bd76cb762b) - [meta] add `auto-changelog` [`03ed375`](https://github.com/ljharb/unbox-primitive/commit/03ed3759284493f19323eb0500f726d0851fc085) - [actions] add automatic rebasing / merge commit blocking [`6dec48d`](https://github.com/ljharb/unbox-primitive/commit/6dec48daa357fa79a5cac1add9ca33f7b56276cc) - [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `aud`, `object-inspect`, `object-is`, `tape` [`528ed88`](https://github.com/ljharb/unbox-primitive/commit/528ed8826664b67f7eaf1fe7e2031c063b2d315f) - [actions] check out the entire repo [`5095b29`](https://github.com/ljharb/unbox-primitive/commit/5095b2981f44a78b3f9bfaa1a526f17a6823e383) - [actions] add "Allow Edits" workflow [`5aa26d7`](https://github.com/ljharb/unbox-primitive/commit/5aa26d7f0c32e0e78ba4bf3e5f9abb5478fd97fa) - [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `object-inspect`, `object-is`, `safe-publish-latest`, `tape` [`afc18c6`](https://github.com/ljharb/unbox-primitive/commit/afc18c6cb59cbb6b514e0d8004c6fd264e2a27eb) - [readme] remove travis badge [`a025899`](https://github.com/ljharb/unbox-primitive/commit/a0258997a21604e1266840e6d167f0a870966e9b) - [Dev Deps] update `auto-changelog` [`9219a32`](https://github.com/ljharb/unbox-primitive/commit/9219a32844b2ce3ed0a7ea12a5910a3e92424e4e) - [readme] Fix missing paren in example [`73f5a33`](https://github.com/ljharb/unbox-primitive/commit/73f5a3340ca1ab6c227ed4632117d816d5e35317) - [Dev Deps] update `@ljharb/eslint-config`, `tape` [`e450acc`](https://github.com/ljharb/unbox-primitive/commit/e450accb54ab452f240768a5f0a98e5887b0ba8c) - [Deps] update `has-bigints`, `has-symbols`, `which-boxed-primitive` [`a4279b5`](https://github.com/ljharb/unbox-primitive/commit/a4279b504732002074e5dcb9c5509038d605f563) - [Dev Deps] update `auto-changelog`, `in-publish`, `tape` [`b351548`](https://github.com/ljharb/unbox-primitive/commit/b351548d31789c0d0af4c3bce55c2bdefe51b40f) - [actions] switch Automatic Rebase workflow to `pull_request_target` event [`f600382`](https://github.com/ljharb/unbox-primitive/commit/f600382db83025270969354ac52a72aadb0a7ffa) - [readme] fix travis links [`4d02fa9`](https://github.com/ljharb/unbox-primitive/commit/4d02fa9a4990812b048f8aefe6e46be80b68beef) - [Dev Deps] update `auto-changelog`; add `aud` [`07e74a3`](https://github.com/ljharb/unbox-primitive/commit/07e74a3ca90688122593095849757e3c05c46db0) - [meta] add `funding` field [`7ca4bd7`](https://github.com/ljharb/unbox-primitive/commit/7ca4bd71196e90a2fc9c7cb0ef4e30f949d5a853) - [Tests] only audit prod deps [`47d8d5f`](https://github.com/ljharb/unbox-primitive/commit/47d8d5fbd58bf472e7e83f79ccef7e8379d06b35) - [Deps] update `has-symbols` [`c70c15e`](https://github.com/ljharb/unbox-primitive/commit/c70c15e924191d11a271cff25bde657b0c3c3016) ## v1.0.0 - 2019-08-10 ### Commits - [Tests] add `.travis.yml` [`8c9a5ef`](https://github.com/ljharb/unbox-primitive/commit/8c9a5efdb54be4866e2884bf32cbe830788b2c2a) - Initial commit [`feaff15`](https://github.com/ljharb/unbox-primitive/commit/feaff159eb999adc8763ff3e51d2d3d56d6164f8) - [Tests] add tests [`3dd18d6`](https://github.com/ljharb/unbox-primitive/commit/3dd18d65748efb4af9b8ca66f8d8c5521d8f2dec) - implementation [`472fb41`](https://github.com/ljharb/unbox-primitive/commit/472fb41d049ddee80ebf3219a5837e639a6e9341) - npm init [`e9e426f`](https://github.com/ljharb/unbox-primitive/commit/e9e426fc90b9a3f07ffc48db75f78c414f77bc2b) - [Tests] add linting [`139e74b`](https://github.com/ljharb/unbox-primitive/commit/139e74b94cdfd187b43b24de76c6d84af21ee467) - [meta] create FUNDING.yml [`a9509e1`](https://github.com/ljharb/unbox-primitive/commit/a9509e122163e2b9d98af421e5c0575df36e2310) - Only apps should have lockfiles [`b3d0834`](https://github.com/ljharb/unbox-primitive/commit/b3d0834d69dcbf4cbc1e61ccfaef05acf96cf630) .editorconfig 0000755 00000000436 15075406671 0007242 0 ustar 00 root = true [*] indent_style = tab indent_size = 4 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true max_line_length = 150 [CHANGELOG.md] indent_style = space indent_size = 2 [*.json] max_line_length = off [Makefile] max_line_length = off package.json 0000755 00000003323 15075406671 0007051 0 ustar 00 { "name": "unbox-primitive", "version": "1.0.2", "description": "Unbox a boxed JS primitive value.", "main": "index.js", "scripts": { "prepublish": "not-in-publish || npm run prepublishOnly", "prepublishOnly": "safe-publish-latest", "lint": "eslint --ext=js,mjs .", "pretest": "npm run lint", "tests-only": "nyc tape 'test/**/*.js'", "test": "npm run tests-only", "posttest": "aud --production", "version": "auto-changelog && git add CHANGELOG.md", "postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\"" }, "repository": { "type": "git", "url": "git+https://github.com/ljharb/unbox-primitive.git" }, "keywords": [ "unbox", "boxed", "primitive", "object", "javascript", "ecmascript" ], "author": "Jordan Harband <ljharb@gmail.com>", "funding": { "url": "https://github.com/sponsors/ljharb" }, "license": "MIT", "bugs": { "url": "https://github.com/ljharb/unbox-primitive/issues" }, "homepage": "https://github.com/ljharb/unbox-primitive#readme", "devDependencies": { "@ljharb/eslint-config": "^21.0.0", "aud": "^2.0.0", "auto-changelog": "^2.4.0", "eslint": "=8.8.0", "for-each": "^0.3.3", "in-publish": "^2.0.1", "nyc": "^10.3.2", "object-inspect": "^1.12.0", "object-is": "^1.1.5", "safe-publish-latest": "^2.0.0", "tape": "^5.5.3" }, "dependencies": { "call-bind": "^1.0.2", "has-bigints": "^1.0.2", "has-symbols": "^1.0.3", "which-boxed-primitive": "^1.0.2" }, "auto-changelog": { "output": "CHANGELOG.md", "template": "keepachangelog", "unreleased": false, "commitLimit": false, "backfillLimit": false, "hideCredit": true } } .eslintrc 0000755 00000000053 15075406671 0006404 0 ustar 00 { "root": true, "extends": "@ljharb", } index.js 0000755 00000002442 15075406671 0006231 0 ustar 00 'use strict'; var whichBoxedPrimitive = require('which-boxed-primitive'); var callBound = require('call-bind/callBound'); var hasSymbols = require('has-symbols')(); var hasBigInts = require('has-bigints')(); var stringToString = callBound('String.prototype.toString'); var numberValueOf = callBound('Number.prototype.valueOf'); var booleanValueOf = callBound('Boolean.prototype.valueOf'); var symbolValueOf = hasSymbols && callBound('Symbol.prototype.valueOf'); var bigIntValueOf = hasBigInts && callBound('BigInt.prototype.valueOf'); module.exports = function unboxPrimitive(value) { var which = whichBoxedPrimitive(value); if (typeof which !== 'string') { throw new TypeError(which === null ? 'value is an unboxed primitive' : 'value is a non-boxed-primitive object'); } if (which === 'String') { return stringToString(value); } if (which === 'Number') { return numberValueOf(value); } if (which === 'Boolean') { return booleanValueOf(value); } if (which === 'Symbol') { if (!hasSymbols) { throw new EvalError('somehow this environment does not have Symbols, but you have a boxed Symbol value. Please report this!'); } return symbolValueOf(value); } if (which === 'BigInt') { return bigIntValueOf(value); } throw new RangeError('unknown boxed primitive found: ' + which); };
SAVE
CANCEL