builtin-type 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ //#region src/index.d.ts
2
+ type BuiltinType = `undefined` | `null` | `boolean` | `number` | `bigint` | `string` | `symbol` | `Boolean` | `Number` | `String` | `Array` | `Map` | `Set` | `WeakMap` | `WeakSet` | `Function` | `GeneratorFunction` | `AsyncFunction` | `AsyncGeneratorFunction` | `Promise` | `Date` | `RegExp` | `URL` | `URLSearchParams` | `WeakRef` | `FinalizationRegistry` | `ArrayBuffer` | `SharedArrayBuffer` | `Buffer` | `Int8Array` | `Uint8Array` | `Uint8ClampedArray` | `Int16Array` | `Uint16Array` | `Int32Array` | `Uint32Array` | `BigInt64Array` | `BigUint64Array` | `Float16Array` | `Float32Array` | `Float64Array` | `DataView` | `Object`;
3
+ declare const builtinType: (value: unknown) => BuiltinType;
4
+ //#endregion
5
+ export { BuiltinType, builtinType as default };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ let e=e=>{if(e===null)return`null`;let n=typeof e;if(n==`object`){for(let n of t){let t=n(e);if(t)return t}return`Object`}if(n==`function`){let t=Function.prototype.toString.call(e),n=t.slice(0,t.indexOf(`(`));return`${n.includes(`async`)?`Async`:``}${n.includes(`*`)?`Generator`:``}Function`}return n},t=[...[[`Array`,`isArray`],[`Buffer`,`isBuffer`]].flatMap(([e,t])=>{let n=globalThis[e];if(!n)return[];let r=n[t];return t=>r(t)?e:void 0}),...[[`Boolean`,`valueOf`],[`Number`,`valueOf`],[`String`,`valueOf`],[`Map`,`has`],[`Set`,`has`],[`WeakMap`,`has`],[`WeakSet`,`has`],[`WeakRef`,`deref`],[`Promise`,`then`],[`Date`,`getDay`],[`RegExp`,`global`],[`URL`,`href`],[`URLSearchParams`,`size`],[`ArrayBuffer`,`byteLength`],[`SharedArrayBuffer`,`byteLength`],[`DataView`,`buffer`],[`FinalizationRegistry`,`unregister`,[{}]]].flatMap(([e,t,n=[]])=>{let r,i=globalThis[e]?.prototype;do{if(r=Object.getOwnPropertyDescriptor(i,t),r)break;i=Object.getPrototypeOf(i)}while(i);if(!r)return[];let a=r.value??r.get;return t=>{try{return a.apply(t,n)==null?void 0:e}catch{return}}})],n=Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype),Symbol.toStringTag)?.get;n&&t.push(e=>n.call(e));export{e as default};
package/license ADDED
@@ -0,0 +1,18 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Tomer Aberbach
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6
+ associated documentation files (the "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
9
+ following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all copies or substantial
12
+ portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15
+ LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
16
+ EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18
+ USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "builtin-type",
3
+ "version": "0.0.1",
4
+ "author": {
5
+ "name": "Tomer Aberbach",
6
+ "email": "tomer@aberba.ch",
7
+ "url": "https://tomeraberba.ch"
8
+ },
9
+ "description": "What is the type of this builtin JS value?",
10
+ "keywords": [
11
+ "builtin",
12
+ "detect",
13
+ "ecmascript",
14
+ "type",
15
+ "typeof",
16
+ "types"
17
+ ],
18
+ "homepage": "https://github.com/TomerAberbach/builtin-type",
19
+ "repository": "TomerAberbach/builtin-type",
20
+ "bugs": {
21
+ "url": "https://github.com/TomerAberbach/builtin-type/issues"
22
+ },
23
+ "funding": {
24
+ "url": "https://github.com/sponsors/TomerAberbach"
25
+ },
26
+ "license": "MIT",
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "type": "module",
31
+ "sideEffects": false,
32
+ "engines": {
33
+ "node": ">= 22"
34
+ },
35
+ "exports": {
36
+ ".": {
37
+ "types": "./dist/index.d.mts",
38
+ "default": "./dist/index.js"
39
+ },
40
+ "./package.json": "./package.json"
41
+ },
42
+ "prettier": "@tomer/prettier-config",
43
+ "devDependencies": {
44
+ "@fast-check/vitest": "^0.3.0",
45
+ "@rollup/plugin-terser": "^1.0.0",
46
+ "@tomer/eslint-config": "^4.6.1",
47
+ "@tomer/prettier-config": "^4.0.0",
48
+ "@vitest/coverage-v8": "^4.1.0",
49
+ "eslint": "^9.39.2",
50
+ "jsdom": "^29.0.1",
51
+ "magic-string": "^0.30.21",
52
+ "prettier": "^3.8.1",
53
+ "publint": "^0.3.18",
54
+ "tsdown": "^0.21.4",
55
+ "typescript": "^5.9.3",
56
+ "vitest": "^4.1.0",
57
+ "which-builtin-type": "^1.2.1"
58
+ },
59
+ "scripts": {
60
+ "format": "prettier --cache --write .",
61
+ "lint": "eslint --cache --cache-location node_modules/.cache/eslint/ --fix .",
62
+ "typecheck": "tsc --noEmit",
63
+ "test": "vitest",
64
+ "coverage": "vitest --coverage",
65
+ "bench": "vitest bench",
66
+ "build": "tsdown"
67
+ }
68
+ }
package/readme.md ADDED
@@ -0,0 +1,149 @@
1
+ <h1 align="center">
2
+ builtin-type
3
+ </h1>
4
+
5
+ <div align="center">
6
+ <a href="https://npmjs.org/package/builtin-type">
7
+ <img src="https://badgen.net/npm/v/builtin-type" alt="version" />
8
+ </a>
9
+ <a href="https://github.com/TomerAberbach/builtin-type/actions">
10
+ <img src="https://github.com/TomerAberbach/builtin-type/workflows/CI/badge.svg" alt="CI" />
11
+ </a>
12
+ <a href="https://unpkg.com/builtin-type/dist/index.js">
13
+ <img src="https://deno.bundlejs.com/?q=builtin-type&badge" alt="gzip size" />
14
+ </a>
15
+ <a href="https://unpkg.com/builtin-type/dist/index.js">
16
+ <img src="https://deno.bundlejs.com/?q=builtin-type&config={%22compression%22:{%22type%22:%22brotli%22}}&badge" alt="brotli size" />
17
+ </a>
18
+ <a href="https://github.com/sponsors/TomerAberbach">
19
+ <img src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86" alt="Sponsor" />
20
+ </a>
21
+ </div>
22
+
23
+ <div align="center">
24
+ What is the type of this builtin JS value?
25
+ </div>
26
+
27
+ ## Features
28
+
29
+ - **Comprehensive:** Supports a ton of builtin types
30
+ - **Robust:** Works
31
+ [cross-realm](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof#instanceof_and_multiple_realms)
32
+ - **Secure:** Resilient to spoofing
33
+ - **Tiny:** One 635 B gzipped package instead of
34
+ [`which-builtin-type`'s 10.5 kB gzipped total size](https://bundlejs.com/?q=which-builtin-type%401.2.1)
35
+ with [50 dependencies](https://npmgraph.js.org/?q=which-builtin-type#zoom=w)
36
+
37
+ ## Install
38
+
39
+ > [!NOTE]
40
+ >
41
+ > Determining the builtin type in a way that works cross-realm and is resilient
42
+ > to spoofing is significantly slower than plain
43
+ > [`instanceof`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof)
44
+ > or
45
+ > [`Object.prototype.toString.call(...)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString#using_tostring_to_detect_object_class).
46
+ > If you don't need those guarantees, then just use these simpler options
47
+ > directly.
48
+
49
+ ```sh
50
+ $ npm i builtin-type
51
+ ```
52
+
53
+ ## Usage
54
+
55
+ <!-- eslint-disable require-unicode-regexp, prefer-regex-literals, no-new-wrappers, unicorn/new-for-builtins, symbol-description -->
56
+
57
+ ```js
58
+ import assert from 'node:assert'
59
+ import builtinType from 'builtin-type'
60
+
61
+ assert.equal(builtinType(undefined), `undefined`)
62
+ assert.equal(builtinType(null), `null`)
63
+ assert.equal(builtinType(false), `boolean`)
64
+ assert.equal(builtinType(42), `number`)
65
+ assert.equal(builtinType(42n), `bigint`)
66
+ assert.equal(builtinType(`foo`), `string`)
67
+ assert.equal(builtinType(Symbol()), `symbol`)
68
+ assert.equal(builtinType(new Boolean(false)), `Boolean`)
69
+ assert.equal(builtinType(new Number(42)), `Number`)
70
+ assert.equal(builtinType(new String(`foo`)), `String`)
71
+ assert.equal(builtinType([]), `Array`)
72
+ assert.equal(builtinType(new Map()), `Map`)
73
+ assert.equal(builtinType(new Set()), `Set`)
74
+ assert.equal(builtinType(new WeakMap()), `WeakMap`)
75
+ assert.equal(builtinType(new WeakSet()), `WeakSet`)
76
+ assert.equal(
77
+ builtinType(() => {}),
78
+ `Function`,
79
+ )
80
+ assert.equal(
81
+ builtinType(function* () {}),
82
+ `GeneratorFunction`,
83
+ )
84
+ assert.equal(
85
+ builtinType(async () => {}),
86
+ `AsyncFunction`,
87
+ )
88
+ assert.equal(
89
+ builtinType(async function* () {}),
90
+ `AsyncGeneratorFunction`,
91
+ )
92
+ assert.equal(builtinType(Promise.resolve()), `Promise`)
93
+ assert.equal(builtinType(new Date()), `Date`)
94
+ assert.equal(builtinType(/a/g), `RegExp`)
95
+ assert.equal(builtinType(new URL(`https://example.com/`)), `URL`)
96
+ assert.equal(builtinType(new URLSearchParams()), `URLSearchParams`)
97
+ assert.equal(builtinType(new WeakRef({})), `WeakRef`)
98
+ assert.equal(
99
+ builtinType(new FinalizationRegistry(() => {})),
100
+ `FinalizationRegistry`,
101
+ )
102
+ assert.equal(builtinType(new ArrayBuffer()), `ArrayBuffer`)
103
+ assert.equal(builtinType(new SharedArrayBuffer()), `SharedArrayBuffer`)
104
+ assert.equal(builtinType(Buffer.from([])), `Buffer`)
105
+ assert.equal(builtinType(new Int8Array()), `Int8Array`)
106
+ assert.equal(builtinType(new Uint8Array()), `Uint8Array`)
107
+ assert.equal(builtinType(new Uint8ClampedArray()), `Uint8ClampedArray`)
108
+ assert.equal(builtinType(new Int16Array()), `Int16Array`)
109
+ assert.equal(builtinType(new Uint16Array()), `Uint16Array`)
110
+ assert.equal(builtinType(new Int32Array()), `Int32Array`)
111
+ assert.equal(builtinType(new Uint32Array()), `Uint32Array`)
112
+ assert.equal(builtinType(new BigInt64Array()), `BigInt64Array`)
113
+ assert.equal(builtinType(new BigUint64Array()), `BigUint64Array`)
114
+ assert.equal(builtinType(new Float16Array()), `Float16Array`)
115
+ assert.equal(builtinType(new Float32Array()), `Float32Array`)
116
+ assert.equal(builtinType(new Float64Array()), `Float64Array`)
117
+ assert.equal(builtinType(new DataView(new ArrayBuffer())), `DataView`)
118
+ assert.equal(builtinType({}), `Object`)
119
+ ```
120
+
121
+ ## Benchmarks
122
+
123
+ `builtin-type` is at least as fast as
124
+ [`which-builtin-type`](https://www.npmjs.com/package/which-builtin-type). It may
125
+ be slightly faster, but that could just be noise in the benchmarks.
126
+
127
+ Here's an example run of [the benchmark](./src/index.bench.ts):
128
+
129
+ ```
130
+ ✓ src/index.bench.ts 3333ms
131
+ name hz min max mean p75 p99 p995 p999 rme samples
132
+ · whichBuiltinType 66.9824 14.6727 15.4056 14.9293 15.0297 15.4056 15.4056 15.4056 ±0.43% 34
133
+ · builtinType 71.4251 13.7500 14.6090 14.0007 14.0046 14.6090 14.6090 14.6090 ±0.41% 36
134
+
135
+ builtinType - src/index.bench.ts
136
+ 1.07x faster than whichBuiltinType
137
+ ```
138
+
139
+ ## Contributing
140
+
141
+ Stars are always welcome!
142
+
143
+ For bugs and feature requests,
144
+ [please create an issue](https://github.com/TomerAberbach/builtin-type/issues/new).
145
+
146
+ ## License
147
+
148
+ [MIT](https://github.com/TomerAberbach/builtin-type/blob/main/license) ©
149
+ [Tomer Aberbach](https://github.com/TomerAberbach)