@mrmartineau/kit 0.1.0 → 1.1.0
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.
- package/README.md +325 -40
- package/package.json +46 -17
- package/src/biome/biome.json +37 -7
- package/src/biome/strict.json +108 -0
- package/src/eslint/index.ts +49 -13
- package/src/index.ts +2 -1
- package/src/oxfmt/.oxfmtrc.json +5 -1
- package/src/oxlint/.oxlintrc.json +12 -3
- package/src/oxlint/strict.json +61 -0
- package/src/prettier/astro.js +31 -0
- package/src/prettier/astro.test.ts +48 -0
- package/src/sounds/index.ts +156 -156
- package/src/utils/checks/checks.test.ts +162 -0
- package/src/utils/checks/collections.ts +63 -0
- package/src/utils/checks/index.ts +17 -0
- package/src/utils/checks/isEmpty.ts +15 -0
- package/src/utils/checks/nullish.ts +26 -0
- package/src/utils/checks/numbers.ts +31 -0
- package/src/utils/checks/objects.ts +38 -0
- package/src/utils/checks/primitives.ts +41 -0
- package/src/utils/formatters/constants.ts +1 -0
- package/src/utils/formatters/index.ts +24 -0
- package/src/utils/formatters/numbers/__tests__/formatNumberBase.test.ts +55 -0
- package/src/utils/formatters/numbers/__tests__/formatNumberCurrency.test.ts +55 -0
- package/src/utils/formatters/numbers/__tests__/formatNumberDecimal.test.ts +63 -0
- package/src/utils/formatters/numbers/__tests__/formatNumberDecimalForceDecimalPlaces.test.ts +29 -0
- package/src/utils/formatters/numbers/formatNumberAccounting.ts +27 -0
- package/src/utils/formatters/numbers/formatNumberBase.ts +21 -0
- package/src/utils/formatters/numbers/formatNumberBytes.ts +29 -0
- package/src/utils/formatters/numbers/formatNumberCompact.ts +19 -0
- package/src/utils/formatters/numbers/formatNumberCurrency.ts +25 -0
- package/src/utils/formatters/numbers/formatNumberDecimal.ts +21 -0
- package/src/utils/formatters/numbers/formatNumberDecimalForceDecimalPlaces.ts +23 -0
- package/src/utils/formatters/numbers/formatNumberOrdinal.ts +22 -0
- package/src/utils/formatters/numbers/formatNumberPercent.ts +21 -0
- package/src/utils/formatters/numbers/formatNumberRange.ts +22 -0
- package/src/utils/formatters/numbers/formatNumberSignDisplay.ts +24 -0
- package/src/utils/formatters/strings/formatInitials.ts +17 -0
- package/src/utils/formatters/strings/formatList.test.ts +48 -0
- package/src/utils/formatters/strings/formatList.ts +10 -0
- package/src/utils/formatters/strings/maskString.ts +27 -0
- package/src/utils/formatters/strings/normalizeWhitespace.ts +8 -0
- package/src/utils/formatters/strings/pluralize.ts +11 -0
- package/src/utils/formatters/strings/slugify.ts +12 -0
- package/src/utils/formatters/strings/splitWords.ts +12 -0
- package/src/utils/formatters/strings/stripDiacritics.ts +7 -0
- package/src/utils/formatters/strings/toCamelCase.ts +17 -0
- package/src/utils/formatters/strings/toKebabCase.ts +11 -0
- package/src/utils/formatters/strings/toPascalCase.ts +14 -0
- package/src/utils/formatters/strings/toSnakeCase.ts +11 -0
- package/src/utils/formatters/strings/toStartCase.test.ts +40 -0
- package/src/utils/formatters/strings/toStartCase.ts +21 -0
- package/src/utils/formatters/strings/truncate.ts +25 -0
- package/src/utils/index.ts +2 -0
package/src/sounds/index.ts
CHANGED
|
@@ -1,165 +1,165 @@
|
|
|
1
1
|
export const soundCategories = [
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
] as const
|
|
2
|
+
'back',
|
|
3
|
+
'bite',
|
|
4
|
+
'bong',
|
|
5
|
+
'boop',
|
|
6
|
+
'buy',
|
|
7
|
+
'click',
|
|
8
|
+
'close',
|
|
9
|
+
'confirmation',
|
|
10
|
+
'disable',
|
|
11
|
+
'drop',
|
|
12
|
+
'enable',
|
|
13
|
+
'error',
|
|
14
|
+
'glass',
|
|
15
|
+
'glitch',
|
|
16
|
+
'maximize',
|
|
17
|
+
'menu',
|
|
18
|
+
'minimize',
|
|
19
|
+
'open',
|
|
20
|
+
'pluck',
|
|
21
|
+
'plunger',
|
|
22
|
+
'pop',
|
|
23
|
+
'question',
|
|
24
|
+
'rising',
|
|
25
|
+
'scratch',
|
|
26
|
+
'scroll',
|
|
27
|
+
'select',
|
|
28
|
+
'switch',
|
|
29
|
+
'tick',
|
|
30
|
+
'toggle',
|
|
31
|
+
] as const
|
|
32
32
|
|
|
33
|
-
export type SoundCategory = (typeof soundCategories)[number]
|
|
33
|
+
export type SoundCategory = (typeof soundCategories)[number]
|
|
34
34
|
|
|
35
35
|
export const sounds = [
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
] as const
|
|
36
|
+
'back_001',
|
|
37
|
+
'back_002',
|
|
38
|
+
'back_003',
|
|
39
|
+
'back_004',
|
|
40
|
+
'bite',
|
|
41
|
+
'bong_001',
|
|
42
|
+
'boop',
|
|
43
|
+
'buy-01',
|
|
44
|
+
'buy-02',
|
|
45
|
+
'click_001',
|
|
46
|
+
'click_002',
|
|
47
|
+
'click_003',
|
|
48
|
+
'click_004',
|
|
49
|
+
'click_005',
|
|
50
|
+
'click-01',
|
|
51
|
+
'click-02',
|
|
52
|
+
'click-03',
|
|
53
|
+
'click-04',
|
|
54
|
+
'close_001',
|
|
55
|
+
'close_002',
|
|
56
|
+
'close_003',
|
|
57
|
+
'close_004',
|
|
58
|
+
'confirmation_001',
|
|
59
|
+
'confirmation_002',
|
|
60
|
+
'confirmation_003',
|
|
61
|
+
'confirmation_004',
|
|
62
|
+
'disable-sound',
|
|
63
|
+
'drop_001',
|
|
64
|
+
'drop_002',
|
|
65
|
+
'drop_003',
|
|
66
|
+
'drop_004',
|
|
67
|
+
'enable-sound',
|
|
68
|
+
'error_001',
|
|
69
|
+
'error_002',
|
|
70
|
+
'error_003',
|
|
71
|
+
'error_004',
|
|
72
|
+
'error_005',
|
|
73
|
+
'error_006',
|
|
74
|
+
'error_007',
|
|
75
|
+
'error_008',
|
|
76
|
+
'glass_001',
|
|
77
|
+
'glass_002',
|
|
78
|
+
'glass_003',
|
|
79
|
+
'glass_004',
|
|
80
|
+
'glass_005',
|
|
81
|
+
'glass_006',
|
|
82
|
+
'glitch_001',
|
|
83
|
+
'glitch_002',
|
|
84
|
+
'glitch_003',
|
|
85
|
+
'glitch_004',
|
|
86
|
+
'maximize_001',
|
|
87
|
+
'maximize_002',
|
|
88
|
+
'maximize_003',
|
|
89
|
+
'maximize_004',
|
|
90
|
+
'maximize_005',
|
|
91
|
+
'maximize_006',
|
|
92
|
+
'maximize_007',
|
|
93
|
+
'maximize_008',
|
|
94
|
+
'maximize_009',
|
|
95
|
+
'menu-open',
|
|
96
|
+
'minimize_001',
|
|
97
|
+
'minimize_002',
|
|
98
|
+
'minimize_003',
|
|
99
|
+
'minimize_004',
|
|
100
|
+
'minimize_005',
|
|
101
|
+
'minimize_006',
|
|
102
|
+
'minimize_007',
|
|
103
|
+
'minimize_008',
|
|
104
|
+
'minimize_009',
|
|
105
|
+
'open_001',
|
|
106
|
+
'open_002',
|
|
107
|
+
'open_003',
|
|
108
|
+
'open_004',
|
|
109
|
+
'pluck_001',
|
|
110
|
+
'pluck_002',
|
|
111
|
+
'plunger-immediate',
|
|
112
|
+
'pop',
|
|
113
|
+
'pop-down',
|
|
114
|
+
'pop-off',
|
|
115
|
+
'pop-on',
|
|
116
|
+
'pop-up-off',
|
|
117
|
+
'pop-up-on',
|
|
118
|
+
'question_001',
|
|
119
|
+
'question_002',
|
|
120
|
+
'question_003',
|
|
121
|
+
'question_004',
|
|
122
|
+
'rising-pops',
|
|
123
|
+
'scratch_001',
|
|
124
|
+
'scratch_002',
|
|
125
|
+
'scratch_003',
|
|
126
|
+
'scratch_004',
|
|
127
|
+
'scratch_005',
|
|
128
|
+
'scroll_001',
|
|
129
|
+
'scroll_002',
|
|
130
|
+
'scroll_003',
|
|
131
|
+
'scroll_004',
|
|
132
|
+
'scroll_005',
|
|
133
|
+
'select_001',
|
|
134
|
+
'select_002',
|
|
135
|
+
'select_003',
|
|
136
|
+
'select_004',
|
|
137
|
+
'select_005',
|
|
138
|
+
'select_006',
|
|
139
|
+
'select_007',
|
|
140
|
+
'select_008',
|
|
141
|
+
'switch_001',
|
|
142
|
+
'switch_002',
|
|
143
|
+
'switch_003',
|
|
144
|
+
'switch_004',
|
|
145
|
+
'switch_005',
|
|
146
|
+
'switch_006',
|
|
147
|
+
'switch_007',
|
|
148
|
+
'switch-off',
|
|
149
|
+
'switch-on',
|
|
150
|
+
'tick_001',
|
|
151
|
+
'tick_002',
|
|
152
|
+
'tick_004',
|
|
153
|
+
'toggle_001',
|
|
154
|
+
'toggle_002',
|
|
155
|
+
'toggle_003',
|
|
156
|
+
'toggle_004',
|
|
157
|
+
] as const
|
|
158
158
|
|
|
159
|
-
export type SoundName = (typeof sounds)[number]
|
|
159
|
+
export type SoundName = (typeof sounds)[number]
|
|
160
160
|
|
|
161
161
|
export function getSoundsByCategory(category: SoundCategory): SoundName[] {
|
|
162
|
-
return sounds.filter((s) => s.startsWith(category)) as SoundName[]
|
|
162
|
+
return sounds.filter((s) => s.startsWith(category)) as SoundName[]
|
|
163
163
|
}
|
|
164
164
|
|
|
165
165
|
/**
|
|
@@ -173,5 +173,5 @@ export function getSoundsByCategory(category: SoundCategory): SoundName[] {
|
|
|
173
173
|
* ```
|
|
174
174
|
*/
|
|
175
175
|
export function getSoundPath(name: SoundName): string {
|
|
176
|
-
return `@mrmartineau/kit/sounds/${name}.mp3
|
|
176
|
+
return `@mrmartineau/kit/sounds/${name}.mp3`
|
|
177
177
|
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import {
|
|
3
|
+
hasKey,
|
|
4
|
+
isArrayOf,
|
|
5
|
+
isDefined,
|
|
6
|
+
isEmpty,
|
|
7
|
+
isFiniteNumber,
|
|
8
|
+
isFunction,
|
|
9
|
+
isInteger,
|
|
10
|
+
isIterable,
|
|
11
|
+
isNil,
|
|
12
|
+
isNumber,
|
|
13
|
+
isNumericString,
|
|
14
|
+
isObject,
|
|
15
|
+
isObjectLike,
|
|
16
|
+
isPlainObject,
|
|
17
|
+
isPrimitive,
|
|
18
|
+
isPromiseLike,
|
|
19
|
+
isString,
|
|
20
|
+
isValidDate,
|
|
21
|
+
} from './index'
|
|
22
|
+
|
|
23
|
+
// oxlint-disable-next-line typescript/no-extraneous-class -- a class instance is what this tests
|
|
24
|
+
class Foo {}
|
|
25
|
+
|
|
26
|
+
describe('primitives', () => {
|
|
27
|
+
it('isString only matches strings', () => {
|
|
28
|
+
expect(isString('')).toBe(true)
|
|
29
|
+
expect(isString(1)).toBe(false)
|
|
30
|
+
expect(isString(null)).toBe(false)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('isFunction matches functions and classes', () => {
|
|
34
|
+
expect(isFunction(() => {})).toBe(true)
|
|
35
|
+
expect(isFunction(Foo)).toBe(true)
|
|
36
|
+
expect(isFunction({})).toBe(false)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('isPrimitive counts null but not objects or functions', () => {
|
|
40
|
+
expect(isPrimitive(null)).toBe(true)
|
|
41
|
+
expect(isPrimitive(undefined)).toBe(true)
|
|
42
|
+
expect(isPrimitive(1n)).toBe(true)
|
|
43
|
+
expect(isPrimitive({})).toBe(false)
|
|
44
|
+
expect(isPrimitive(() => {})).toBe(false)
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
describe('numbers', () => {
|
|
49
|
+
it('isNumber includes NaN, isFiniteNumber does not', () => {
|
|
50
|
+
expect(isNumber(Number.NaN)).toBe(true)
|
|
51
|
+
expect(isFiniteNumber(Number.NaN)).toBe(false)
|
|
52
|
+
expect(isFiniteNumber(Number.POSITIVE_INFINITY)).toBe(false)
|
|
53
|
+
expect(isFiniteNumber(0)).toBe(true)
|
|
54
|
+
expect(isFiniteNumber('1')).toBe(false)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('isInteger', () => {
|
|
58
|
+
expect(isInteger(4)).toBe(true)
|
|
59
|
+
expect(isInteger(4.5)).toBe(false)
|
|
60
|
+
expect(isInteger('4')).toBe(false)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('isNumericString', () => {
|
|
64
|
+
expect(isNumericString('42')).toBe(true)
|
|
65
|
+
expect(isNumericString('4.2e3')).toBe(true)
|
|
66
|
+
expect(isNumericString(' 7 ')).toBe(true)
|
|
67
|
+
expect(isNumericString('')).toBe(false)
|
|
68
|
+
expect(isNumericString(' ')).toBe(false)
|
|
69
|
+
expect(isNumericString('12px')).toBe(false)
|
|
70
|
+
expect(isNumericString('Infinity')).toBe(false)
|
|
71
|
+
expect(isNumericString(42)).toBe(false)
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
describe('nullish', () => {
|
|
76
|
+
it('isNil only matches null and undefined', () => {
|
|
77
|
+
expect(isNil(null)).toBe(true)
|
|
78
|
+
expect(isNil(undefined)).toBe(true)
|
|
79
|
+
expect(isNil(0)).toBe(false)
|
|
80
|
+
expect(isNil('')).toBe(false)
|
|
81
|
+
expect(isNil(false)).toBe(false)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('isDefined works as a filter callback', () => {
|
|
85
|
+
const ids: number[] = [1, null, 2, undefined, 3].filter(isDefined)
|
|
86
|
+
expect(ids).toEqual([1, 2, 3])
|
|
87
|
+
})
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
describe('objects', () => {
|
|
91
|
+
const cases: [string, unknown, boolean, boolean, boolean][] = [
|
|
92
|
+
// name, value, isObjectLike, isObject, isPlainObject
|
|
93
|
+
['object literal', { a: 1 }, true, true, true],
|
|
94
|
+
['null-prototype object', Object.create(null), true, true, true],
|
|
95
|
+
['array', [], true, false, false],
|
|
96
|
+
['date', new Date(), true, true, false],
|
|
97
|
+
['map', new Map(), true, true, false],
|
|
98
|
+
['class instance', new Foo(), true, true, false],
|
|
99
|
+
['null', null, false, false, false],
|
|
100
|
+
['string', 'a', false, false, false],
|
|
101
|
+
]
|
|
102
|
+
|
|
103
|
+
for (const [name, value, objectLike, object, plain] of cases) {
|
|
104
|
+
it(name, () => {
|
|
105
|
+
expect(isObjectLike(value)).toBe(objectLike)
|
|
106
|
+
expect(isObject(value)).toBe(object)
|
|
107
|
+
expect(isPlainObject(value)).toBe(plain)
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
it('hasKey ignores inherited keys', () => {
|
|
112
|
+
expect(hasKey({ id: 1 }, 'id')).toBe(true)
|
|
113
|
+
expect(hasKey({ id: 1 }, 'toString')).toBe(false)
|
|
114
|
+
expect(hasKey(null, 'id')).toBe(false)
|
|
115
|
+
})
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
describe('collections and built-ins', () => {
|
|
119
|
+
it('isArrayOf checks every item', () => {
|
|
120
|
+
expect(isArrayOf(['a', 'b'], isString)).toBe(true)
|
|
121
|
+
expect(isArrayOf(['a', 1], isString)).toBe(false)
|
|
122
|
+
expect(isArrayOf([], isString)).toBe(true)
|
|
123
|
+
expect(isArrayOf('ab', isString)).toBe(false)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('isValidDate rejects invalid dates', () => {
|
|
127
|
+
expect(isValidDate(new Date())).toBe(true)
|
|
128
|
+
expect(isValidDate(new Date('nonsense'))).toBe(false)
|
|
129
|
+
expect(isValidDate('2026-01-01')).toBe(false)
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
it('isPromiseLike matches promises and thenables', () => {
|
|
133
|
+
expect(isPromiseLike(Promise.resolve())).toBe(true)
|
|
134
|
+
// oxlint-disable-next-line unicorn/no-thenable -- a thenable is what this tests
|
|
135
|
+
expect(isPromiseLike({ then: () => {} })).toBe(true)
|
|
136
|
+
// oxlint-disable-next-line unicorn/no-thenable -- a thenable is what this tests
|
|
137
|
+
expect(isPromiseLike({ then: 1 })).toBe(false)
|
|
138
|
+
expect(isPromiseLike(null)).toBe(false)
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
it('isIterable', () => {
|
|
142
|
+
expect(isIterable([])).toBe(true)
|
|
143
|
+
expect(isIterable('abc')).toBe(true)
|
|
144
|
+
expect(isIterable(new Set())).toBe(true)
|
|
145
|
+
expect(isIterable({})).toBe(false)
|
|
146
|
+
expect(isIterable(null)).toBe(false)
|
|
147
|
+
})
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
describe('isEmpty', () => {
|
|
151
|
+
it('is true for empty values', () => {
|
|
152
|
+
for (const value of [null, undefined, '', [], {}, new Map(), new Set()]) {
|
|
153
|
+
expect(isEmpty(value)).toBe(true)
|
|
154
|
+
}
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('is false for everything else', () => {
|
|
158
|
+
for (const value of [0, false, ' ', [0], { a: 1 }, new Map([[1, 1]]), new Date()]) {
|
|
159
|
+
expect(isEmpty(value)).toBe(false)
|
|
160
|
+
}
|
|
161
|
+
})
|
|
162
|
+
})
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/** @example isArray([]) // true */
|
|
2
|
+
export function isArray(value: unknown): value is unknown[] {
|
|
3
|
+
return Array.isArray(value)
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `true` for an array where every item passes `check`.
|
|
8
|
+
* @example isArrayOf(['a', 'b'], isString) // true
|
|
9
|
+
*/
|
|
10
|
+
export function isArrayOf<T>(value: unknown, check: (item: unknown) => item is T): value is T[] {
|
|
11
|
+
return Array.isArray(value) && value.every(check)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* `true` for a `Date` that holds a real date. `new Date('nonsense')` is `false`.
|
|
16
|
+
* @example isValidDate(new Date('nonsense')) // false
|
|
17
|
+
*/
|
|
18
|
+
export function isValidDate(value: unknown): value is Date {
|
|
19
|
+
return value instanceof Date && !Number.isNaN(value.getTime())
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** @example isRegExp(/a/) // true */
|
|
23
|
+
export function isRegExp(value: unknown): value is RegExp {
|
|
24
|
+
return value instanceof RegExp
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** @example isMap(new Map()) // true */
|
|
28
|
+
export function isMap(value: unknown): value is Map<unknown, unknown> {
|
|
29
|
+
return value instanceof Map
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @example isSet(new Set()) // true */
|
|
33
|
+
export function isSet(value: unknown): value is Set<unknown> {
|
|
34
|
+
return value instanceof Set
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** @example isError(new TypeError('x')) // true */
|
|
38
|
+
export function isError(value: unknown): value is Error {
|
|
39
|
+
return value instanceof Error
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* `true` for anything with a `.then` method: real promises and "thenables".
|
|
44
|
+
* @example isPromiseLike(Promise.resolve()) // true
|
|
45
|
+
*/
|
|
46
|
+
export function isPromiseLike(value: unknown): value is PromiseLike<unknown> {
|
|
47
|
+
return (
|
|
48
|
+
typeof value === 'object' &&
|
|
49
|
+
value !== null &&
|
|
50
|
+
typeof (value as { then?: unknown }).then === 'function'
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* `true` for anything `for...of` can loop over. Strings count.
|
|
56
|
+
* @example isIterable(new Set()) // true
|
|
57
|
+
*/
|
|
58
|
+
export function isIterable(value: unknown): value is Iterable<unknown> {
|
|
59
|
+
return (
|
|
60
|
+
value != null &&
|
|
61
|
+
typeof (value as { [Symbol.iterator]?: unknown })[Symbol.iterator] === 'function'
|
|
62
|
+
)
|
|
63
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export {
|
|
2
|
+
isArray,
|
|
3
|
+
isArrayOf,
|
|
4
|
+
isError,
|
|
5
|
+
isIterable,
|
|
6
|
+
isMap,
|
|
7
|
+
isPromiseLike,
|
|
8
|
+
isRegExp,
|
|
9
|
+
isSet,
|
|
10
|
+
isValidDate,
|
|
11
|
+
} from './collections.js'
|
|
12
|
+
export { isEmpty } from './isEmpty.js'
|
|
13
|
+
export { isDefined, isNil, isNull, isUndefined } from './nullish.js'
|
|
14
|
+
export { isFiniteNumber, isInteger, isNumber, isNumericString } from './numbers.js'
|
|
15
|
+
export { hasKey, isObject, isObjectLike, isPlainObject } from './objects.js'
|
|
16
|
+
export type { Primitive } from './primitives.js'
|
|
17
|
+
export { isBigInt, isBoolean, isFunction, isPrimitive, isString, isSymbol } from './primitives.js'
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { isPlainObject } from './objects.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `true` for `null`, `undefined`, `''`, `[]`, `{}` and empty `Map`s and `Set`s.
|
|
5
|
+
* Everything else is `false`, including `0`, `false` and `' '`.
|
|
6
|
+
* @example isEmpty({}) // true
|
|
7
|
+
* @example isEmpty(0) // false
|
|
8
|
+
*/
|
|
9
|
+
export function isEmpty(value: unknown): boolean {
|
|
10
|
+
if (value == null) return true
|
|
11
|
+
if (typeof value === 'string' || Array.isArray(value)) return value.length === 0
|
|
12
|
+
if (value instanceof Map || value instanceof Set) return value.size === 0
|
|
13
|
+
if (isPlainObject(value)) return Object.keys(value).length === 0
|
|
14
|
+
return false
|
|
15
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** @example isNull(null) // true */
|
|
2
|
+
export function isNull(value: unknown): value is null {
|
|
3
|
+
return value === null
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/** @example isUndefined(undefined) // true */
|
|
7
|
+
export function isUndefined(value: unknown): value is undefined {
|
|
8
|
+
return value === undefined
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `true` for `null` or `undefined`, and nothing else.
|
|
13
|
+
* @example isNil(0) // false
|
|
14
|
+
*/
|
|
15
|
+
export function isNil(value: unknown): value is null | undefined {
|
|
16
|
+
return value == null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* `true` for anything except `null` and `undefined`. Keeps the rest of the
|
|
21
|
+
* type, so it works as a `filter` callback.
|
|
22
|
+
* @example [1, null, 2].filter(isDefined) // number[]
|
|
23
|
+
*/
|
|
24
|
+
export function isDefined<T>(value: T): value is NonNullable<T> {
|
|
25
|
+
return value != null
|
|
26
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `true` for any number, including `NaN` and `Infinity`. Use `isFiniteNumber`
|
|
3
|
+
* for a number you can do maths with.
|
|
4
|
+
* @example isNumber(Number.NaN) // true
|
|
5
|
+
*/
|
|
6
|
+
export function isNumber(value: unknown): value is number {
|
|
7
|
+
return typeof value === 'number'
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* `true` for a real, usable number: not `NaN`, not `Infinity`.
|
|
12
|
+
* @example isFiniteNumber(Number.NaN) // false
|
|
13
|
+
*/
|
|
14
|
+
export function isFiniteNumber(value: unknown): value is number {
|
|
15
|
+
return typeof value === 'number' && Number.isFinite(value)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** @example isInteger(4) // true */
|
|
19
|
+
export function isInteger(value: unknown): value is number {
|
|
20
|
+
return Number.isInteger(value)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* `true` for a string that holds a finite number, like a form field or URL
|
|
25
|
+
* parameter. Empty and whitespace-only strings are `false`.
|
|
26
|
+
* @example isNumericString('4.2e3') // true
|
|
27
|
+
* @example isNumericString('12px') // false
|
|
28
|
+
*/
|
|
29
|
+
export function isNumericString(value: unknown): value is string {
|
|
30
|
+
return typeof value === 'string' && value.trim() !== '' && Number.isFinite(Number(value))
|
|
31
|
+
}
|