Skip to content

Commit 8c83b4e

Browse files
committed
feat: determineSpecificType
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 1e78899 commit 8c83b4e

File tree

7 files changed

+158
-6
lines changed

7 files changed

+158
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[![vitest](https://img.shields.io/badge/-vitest-6e9f18?style=flat&logo=vitest&logoColor=ffffff)](https://vitest.dev/)
88
[![yarn](https://img.shields.io/badge/-yarn-2c8ebb?style=flat&logo=yarn&logoColor=ffffff)](https://yarnpkg.com/)
99

10-
Create [Node.js errors][1]
10+
Utilities for creating [Node.js errors][1]
1111

1212
## Contents
1313

@@ -21,7 +21,7 @@ Create [Node.js errors][1]
2121

2222
## What is this?
2323

24-
This package is a utility for creating [Node.js errors][1].
24+
This package provides utilities for creating [Node.js errors][1].
2525

2626
## When should I use this?
2727

build.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import pkg from './package.json' assert { type: 'json' }
1212
* @const {Config} config
1313
*/
1414
const config: Config = defineBuildConfig({
15-
platform: 'node',
15+
sourcemap: true,
16+
sourcesContent: false,
1617
target: 'node' + pkg.engines.node.replace(/^\D+/, ''),
17-
treeShaking: true,
1818
tsconfig: 'tsconfig.build.json'
1919
})
2020

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@flex-development/create-node-error",
3-
"description": "Utility for creating Node.js errors",
3+
"description": "Utilities for creating Node.js errors",
44
"version": "0.0.0",
55
"keywords": [
66
"error",
@@ -63,6 +63,9 @@
6363
"typecheck": "vitest typecheck --run",
6464
"typecheck:watch": "vitest typecheck"
6565
},
66+
"dependencies": {
67+
"node-inspect-extracted": "2.0.0"
68+
},
6669
"devDependencies": {
6770
"@commitlint/cli": "17.3.0",
6871
"@commitlint/config-conventional": "17.3.0",
@@ -131,6 +134,9 @@
131134
"vitest-github-actions-reporter": "0.9.0",
132135
"yaml-eslint-parser": "1.1.0"
133136
},
137+
"bundleDependencies": [
138+
"node-inspect-extracted"
139+
],
134140
"resolutions": {
135141
"@ardatan/sync-fetch": "larsgw/sync-fetch#head=worker_threads",
136142
"@flex-development/tutils": "6.0.0-alpha.7",
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* @file Unit Tests - determineSpecificType
3+
* @module create-node-error/tests/unit/determineSpecificType
4+
*/
5+
6+
import { inspect } from 'node-inspect-extracted'
7+
import testSubject from '../determine-specific-type'
8+
9+
describe('unit:determineSpecificType', () => {
10+
it('should detect bigint', () => {
11+
// Arrange
12+
const value: bigint = faker.datatype.bigInt(13)
13+
const expected: string = `type bigint (${inspect(value, {
14+
colors: false
15+
})})`
16+
17+
// Act + Expect
18+
expect(testSubject(value)).to.equal(expected)
19+
})
20+
21+
it('should detect boolean', () => {
22+
// Arrange
23+
const value: unknown = faker.datatype.boolean()
24+
const expected: string = `type boolean (${value})`
25+
26+
// Act + Expect
27+
expect(testSubject(value)).to.equal(expected)
28+
})
29+
30+
it('should detect function', () => {
31+
// Arrange
32+
const value: unknown = vi.fn()
33+
const expected: string = 'function spy'
34+
35+
// Act + Expect
36+
expect(testSubject(value)).to.equal(expected)
37+
})
38+
39+
it('should detect instance object', () => {
40+
// Arrange
41+
const value: unknown = faker.datatype.datetime()
42+
const expected: string = 'an instance of Date'
43+
44+
// Act + Expect
45+
expect(testSubject(value)).to.equal(expected)
46+
})
47+
48+
it('should detect null', () => {
49+
expect(testSubject(null)).to.equal('null')
50+
})
51+
52+
it('should detect number', () => {
53+
// Arrange
54+
const value: unknown = faker.datatype.number(13)
55+
const expected: string = `type number (${value})`
56+
57+
// Act + Expect
58+
expect(testSubject(value)).to.equal(expected)
59+
})
60+
61+
it('should detect string', () => {
62+
// Arrange
63+
const value: unknown = faker.datatype.string(30)
64+
const inspected: string = inspect(value, { colors: false })
65+
const expected: string = `type string (${inspected.slice(0, 25)}...)`
66+
67+
// Act + Expect
68+
expect(testSubject(value)).to.equal(expected)
69+
})
70+
71+
it('should detect symbol', () => {
72+
// Arrange
73+
const value: unknown = Symbol('kIsNodeError')
74+
const expected: string = `type symbol (${inspect(value, {
75+
colors: false
76+
})})`
77+
78+
// Act + Expect
79+
expect(testSubject(value)).to.equal(expected)
80+
})
81+
82+
it('should detect undefined', () => {
83+
expect(testSubject(undefined)).to.equal('undefined')
84+
})
85+
})

src/determine-specific-type.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @file determineSpecificType
3+
* @module create-node-error/determineSpecificType
4+
* @see https://github.com/nodejs/node/blob/v19.3.0/lib/internal/errors.js#L878-L896
5+
*/
6+
7+
import { inspect } from 'node-inspect-extracted'
8+
9+
/**
10+
* Determines the specific type of a value for type-mismatch errors.
11+
*
12+
* @param {unknown} value - Value to detect type of
13+
* @return {string} Specific type of `value`
14+
*/
15+
function determineSpecificType(value: unknown): string {
16+
/**
17+
* Specific type of `value`.
18+
*
19+
* @var {string} type
20+
*/
21+
let type: string = ''
22+
23+
switch (true) {
24+
case typeof value === 'function':
25+
type = `function ${(value as FunctionConstructor).name}`
26+
break
27+
case typeof value === 'object':
28+
type = value?.constructor?.name
29+
? `an instance of ${value.constructor.name}`
30+
: inspect(value, { depth: -1 })
31+
break
32+
case typeof value === 'undefined':
33+
type = typeof value
34+
break
35+
default:
36+
/**
37+
* String representation of {@linkcode value}.
38+
*
39+
* @var {string} inspected
40+
*/
41+
let inspected: string = inspect(value, { colors: false })
42+
43+
// trim string representation of value
44+
if (inspected.length > 28) inspected = inspected.slice(0, 25) + '...'
45+
46+
type = `type ${typeof value} (${inspected})`
47+
break
48+
}
49+
50+
return type
51+
}
52+
53+
export default determineSpecificType

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
* @module create-node-error
44
*/
55

6-
export {}
6+
export { default as determineSpecificType } from './determine-specific-type'

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,7 @@ __metadata:
10431043
is-ci: "npm:3.0.1"
10441044
jsonc-eslint-parser: "npm:2.1.0"
10451045
lint-staged: "npm:13.1.0"
1046+
node-inspect-extracted: "npm:2.0.0"
10461047
node-notifier: "npm:10.0.1"
10471048
prettier: "npm:2.8.1"
10481049
prettier-plugin-sh: "npm:0.12.8"
@@ -6375,6 +6376,13 @@ __metadata:
63756376
languageName: node
63766377
linkType: hard
63776378

6379+
"node-inspect-extracted@npm:2.0.0":
6380+
version: 2.0.0
6381+
resolution: "node-inspect-extracted@npm:2.0.0"
6382+
checksum: d99c560d4687695f191d27585877893428101082d8ce1b52d79e968650b03c626cd3472f98cae98f17c98859ed0aa1e1a53954d82ba04659115ebb4e6e5565b4
6383+
languageName: node
6384+
linkType: hard
6385+
63786386
"node-notifier@npm:10.0.1":
63796387
version: 10.0.1
63806388
resolution: "node-notifier@npm:10.0.1"

0 commit comments

Comments
 (0)