@wistia/utils 0.0.0-stage → 0.17.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/LICENSE +22 -0
- package/README.md +237 -2
- package/dist/index.cjs +1921 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1560 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +1560 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +1833 -0
- package/dist/index.mjs.map +1 -0
- package/llms-full.txt +1195 -0
- package/llms.txt +158 -0
- package/package.json +81 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Wistia, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
CHANGED
|
@@ -1,3 +1,238 @@
|
|
|
1
|
-
#
|
|
1
|
+
# utils
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
This library provides a set of type guards that help safely check and narrow down types at runtime, making it easier to handle data validation and type checking in a type-safe manner. It also ships a set of general-purpose utility functions and utility types.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @wistia/utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
yarn add @wistia/utils
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
pnpm add @wistia/utils
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
Every guard is a `value is T` predicate, so TypeScript narrows the value inside the `if` block:
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { isNonEmptyString, isNotNil } from '@wistia/utils';
|
|
25
|
+
|
|
26
|
+
function greet(value: unknown): string {
|
|
27
|
+
if (isNonEmptyString(value)) {
|
|
28
|
+
return `Hello, ${value}`;
|
|
29
|
+
}
|
|
30
|
+
return 'Hello, stranger';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const names: (string | null | undefined)[] = ['Alice', null, 'Bob'];
|
|
34
|
+
const cleaned: string[] = names.filter(isNotNil); // ['Alice', 'Bob']
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Guards
|
|
38
|
+
|
|
39
|
+
<!-- AUTOGEN:README-GUARDS:START -->
|
|
40
|
+
|
|
41
|
+
### Primitives
|
|
42
|
+
|
|
43
|
+
| Guard | Narrows to |
|
|
44
|
+
| ------------------------ | ------------------------------------------ |
|
|
45
|
+
| `isAsyncFunction(value)` | `(...args: unknown[]) => Promise<unknown>` |
|
|
46
|
+
| `isBigInt(value)` | `bigint` |
|
|
47
|
+
| `isBoolean(value)` | `boolean` |
|
|
48
|
+
| `isDate(value)` | `Date` |
|
|
49
|
+
| `isError(value)` | `Error` |
|
|
50
|
+
| `isFiniteNumber(value)` | `number` |
|
|
51
|
+
| `isFunction(value)` | `(...args: unknown[]) => unknown` |
|
|
52
|
+
| `isInteger(value)` | `number` |
|
|
53
|
+
| `isIterable(value)` | `Iterable<unknown>` |
|
|
54
|
+
| `isMap(value)` | `Map<unknown, unknown>` |
|
|
55
|
+
| `isNaN(value)` | `number` |
|
|
56
|
+
| `isNil(value)` | `Nil` |
|
|
57
|
+
| `isNonNaNNumber(value)` | `number` |
|
|
58
|
+
| `isNull(value)` | `null` |
|
|
59
|
+
| `isNumber(value)` | `number` |
|
|
60
|
+
| `isPromise(value)` | `Promise<unknown>` |
|
|
61
|
+
| `isRegExp(value)` | `RegExp` |
|
|
62
|
+
| `isSet(value)` | `Set<unknown>` |
|
|
63
|
+
| `isString(value)` | `string` |
|
|
64
|
+
| `isSymbol(value)` | `symbol` |
|
|
65
|
+
| `isUndefined(value)` | `undefined` |
|
|
66
|
+
| `isVoid(value)` | `void` |
|
|
67
|
+
| `isWeakMap(value)` | `WeakMap<WeakKey, unknown>` |
|
|
68
|
+
| `isWeakSet(value)` | `WeakSet<WeakKey>` |
|
|
69
|
+
|
|
70
|
+
### Arrays
|
|
71
|
+
|
|
72
|
+
| Guard | Narrows to |
|
|
73
|
+
| ---------------------------------- | ------------------ |
|
|
74
|
+
| `isArray(value)` | `unknown[]` |
|
|
75
|
+
| `isEmptyArray(value)` | `never[]` |
|
|
76
|
+
| `isNonEmptyArray(value)` | `NonEmptyArray<T>` |
|
|
77
|
+
| `readonlyArrayIncludes(arr, elem)` | `T` |
|
|
78
|
+
|
|
79
|
+
### Records (plain objects, not arrays)
|
|
80
|
+
|
|
81
|
+
| Guard | Narrows to |
|
|
82
|
+
| ------------------------- | ------------------------- |
|
|
83
|
+
| `hasKey(value, key)` | `Record<Key, Value>` |
|
|
84
|
+
| `isEmptyRecord(value)` | `EmptyObject` |
|
|
85
|
+
| `isNonEmptyRecord(value)` | `Record<string, unknown>` |
|
|
86
|
+
| `isPlainObject(value)` | `Record<string, unknown>` |
|
|
87
|
+
| `isRecord(value)` | `Record<string, unknown>` |
|
|
88
|
+
|
|
89
|
+
### Strings
|
|
90
|
+
|
|
91
|
+
| Guard | Narrows to |
|
|
92
|
+
| ------------------------- | ---------- |
|
|
93
|
+
| `isEmptyString(value)` | `''` |
|
|
94
|
+
| `isNonBlankString(value)` | `string` |
|
|
95
|
+
| `isNonEmptyString(value)` | `string` |
|
|
96
|
+
|
|
97
|
+
### Truthy / falsy
|
|
98
|
+
|
|
99
|
+
| Guard | Narrows to |
|
|
100
|
+
| ----------------- | ---------- |
|
|
101
|
+
| `isFalsy(value)` | `Falsy` |
|
|
102
|
+
| `isTruthy(value)` | `boolean` |
|
|
103
|
+
|
|
104
|
+
### Negated
|
|
105
|
+
|
|
106
|
+
The `isNot*` variants are useful as `.filter` predicates to drop the unwanted type:
|
|
107
|
+
|
|
108
|
+
| Guard | Narrows to |
|
|
109
|
+
| ----------------------- | --------------------------------------------------- |
|
|
110
|
+
| `isNotArray(value)` | `Exclude<unknown, unknown[]>` |
|
|
111
|
+
| `isNotBoolean(value)` | `Exclude<unknown, boolean>` |
|
|
112
|
+
| `isNotFunction(value)` | `Exclude<unknown, (...args: unknown[]) => unknown>` |
|
|
113
|
+
| `isNotNil(value)` | `T` |
|
|
114
|
+
| `isNotNull(value)` | `Exclude<T, null>` |
|
|
115
|
+
| `isNotNumber(value)` | `Exclude<unknown, number>` |
|
|
116
|
+
| `isNotRecord(value)` | `Exclude<unknown, Record<string, unknown>>` |
|
|
117
|
+
| `isNotString(value)` | `Exclude<unknown, string>` |
|
|
118
|
+
| `isNotUndefined(value)` | `Exclude<T, undefined>` |
|
|
119
|
+
| `isNotVoid(value)` | `Exclude<unknown, void>` |
|
|
120
|
+
|
|
121
|
+
### DOM
|
|
122
|
+
|
|
123
|
+
| Guard | Narrows to |
|
|
124
|
+
| ---------------------------- | ------------------- |
|
|
125
|
+
| `isHtmlButtonElement(value)` | `HTMLButtonElement` |
|
|
126
|
+
| `isHtmlElement(value)` | `HTMLElement` |
|
|
127
|
+
| `isHtmlInputElement(value)` | `HTMLInputElement` |
|
|
128
|
+
| `isHtmlVideoElement(value)` | `HTMLVideoElement` |
|
|
129
|
+
| `isMouseEvent(value)` | `MouseEvent` |
|
|
130
|
+
| `isSvgElement(value)` | `SVGElement` |
|
|
131
|
+
| `isTextNode(value)` | `Text` |
|
|
132
|
+
|
|
133
|
+
<!-- AUTOGEN:README-GUARDS:END -->
|
|
134
|
+
|
|
135
|
+
## Types
|
|
136
|
+
|
|
137
|
+
<!-- AUTOGEN:README-TYPES:START -->
|
|
138
|
+
|
|
139
|
+
| Type | Resolves to |
|
|
140
|
+
| --------------------------------------------------- | ------------------------------------------------------------- |
|
|
141
|
+
| `Arrayable<T>` | `T \| T[]` |
|
|
142
|
+
| `Falsy` | `typeof Number.NaN \| '' \| 0n \| false \| null \| undefined` |
|
|
143
|
+
| `NestedNonNullable<T, P extends Paths<T> & string>` | `NonNullable<Get<T, P>>` |
|
|
144
|
+
| `Nil` | `null \| undefined` |
|
|
145
|
+
| `Nilable<A>` | `A \| Nil` |
|
|
146
|
+
| `NilableArray<T>` | `Nilable<Nilable<T>[]>` |
|
|
147
|
+
| `NonEmptyArray<T>` | `[T, ...T[]]` |
|
|
148
|
+
| `NotNilable<A>` | `Exclude<A, Nil>` |
|
|
149
|
+
| `Nullable<A>` | `A \| null` |
|
|
150
|
+
| `NullableProperties<T extends UnknownRecord>` | `{ [Key in keyof T]: Nullable<T[Key]> }` |
|
|
151
|
+
| `Truthy<T>` | `Exclude<T, Falsy>` |
|
|
152
|
+
| `Undefinable<A>` | `A \| undefined` |
|
|
153
|
+
|
|
154
|
+
<!-- AUTOGEN:README-TYPES:END -->
|
|
155
|
+
|
|
156
|
+
## Utilities
|
|
157
|
+
|
|
158
|
+
General-purpose helper functions that complement the guards. Unlike guards, these are not type predicates.
|
|
159
|
+
|
|
160
|
+
<!-- AUTOGEN:README-UTILITIES:START -->
|
|
161
|
+
|
|
162
|
+
| Utility | Returns |
|
|
163
|
+
| ------------------------------------------------------------------------ | ------------------------------ |
|
|
164
|
+
| `asyncNoOpFn()` | `Promise<void>` |
|
|
165
|
+
| `buildTimeDuration(numberOfMilliseconds)` | `TimeDuration` |
|
|
166
|
+
| `coerceToBoolean(value)` | `boolean` |
|
|
167
|
+
| `dateOnlyISOString(date, { timeZone = defaultTimeZone })` | `string` |
|
|
168
|
+
| `dateOnlyString(date, { timeZone = defaultTimeZone, omitYear = false })` | `string` |
|
|
169
|
+
| `dateOnlyStringForSentence(date, { timeZone = defaultTimeZone })` | `string` |
|
|
170
|
+
| `dateOnlyStringNumeric(date, { timeZone = defaultTimeZone })` | `string` |
|
|
171
|
+
| `dateTimeRounded(dateTime, toISOString = true)` | `string \| Date \| null` |
|
|
172
|
+
| `dateTimeString(date, { timeZone = defaultTimeZone, omitYear = false })` | `string` |
|
|
173
|
+
| `dateTimeStringForSentence(date, { timeZone = defaultTimeZone })` | `string` |
|
|
174
|
+
| `dateTimeToDate(dateTime)` | `Date \| null` |
|
|
175
|
+
| `dateTimeToISO(dateTime)` | `string \| null` |
|
|
176
|
+
| `dateToDateTime(date)` | `WistiaDateTimeObject \| null` |
|
|
177
|
+
| `dateUTCOffset(date)` | `string` |
|
|
178
|
+
| `dayOfWeekString(date, { timeZone = defaultTimeZone })` | `string` |
|
|
179
|
+
| `deepMerge(...objects)` | `DeepMergeResult<T>` |
|
|
180
|
+
| `getObjectEntries(obj)` | `[keyof T, T[keyof T]][]` |
|
|
181
|
+
| `getObjectKeys(obj)` | `(keyof T)[]` |
|
|
182
|
+
| `getObjectValues(obj)` | `T[keyof T][]` |
|
|
183
|
+
| `isEmail(value)` | `boolean` |
|
|
184
|
+
| `isUrl(value)` | `boolean` |
|
|
185
|
+
| `mediaDurationString(numberOfMilliseconds)` | `string` |
|
|
186
|
+
| `millisecondsToDurationISOString(numberOfMilliseconds)` | `string` |
|
|
187
|
+
| `monthDayStringNumeric(date, { timeZone = defaultTimeZone })` | `string` |
|
|
188
|
+
| `noOpFn()` | `void` |
|
|
189
|
+
| `noOpFnReturningFalse()` | `false` |
|
|
190
|
+
| `noOpFnReturningNull()` | `null` |
|
|
191
|
+
| `parseDateString(str)` | `Date \| null` |
|
|
192
|
+
| `sessionDurationString(numberOfMilliseconds)` | `string` |
|
|
193
|
+
| `stripExtension(str)` | `string` |
|
|
194
|
+
| `timeAgoString(date, { nowAnchor = new Date(), includeTime = true })` | `string` |
|
|
195
|
+
| `timeOnlyString(date, { timeZone = defaultTimeZone })` | `string` |
|
|
196
|
+
| `tupleIncludes(tuple, item)` | `item is TTuple[number]` |
|
|
197
|
+
|
|
198
|
+
<!-- AUTOGEN:README-UTILITIES:END -->
|
|
199
|
+
|
|
200
|
+
## Patterns
|
|
201
|
+
|
|
202
|
+
Filter nullish values from a list, with narrowing:
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
import { isNotNil } from '@wistia/utils';
|
|
206
|
+
|
|
207
|
+
const raw: (string | null | undefined)[] = ['a', null, 'b', undefined];
|
|
208
|
+
const clean: string[] = raw.filter(isNotNil); // ['a', 'b']
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Safely access a key on an unknown value:
|
|
212
|
+
|
|
213
|
+
```ts
|
|
214
|
+
import { hasKey } from '@wistia/utils';
|
|
215
|
+
|
|
216
|
+
function readName(value: unknown): string | undefined {
|
|
217
|
+
if (hasKey<'name', string>(value, 'name') && typeof value.name === 'string') {
|
|
218
|
+
return value.name;
|
|
219
|
+
}
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Combine guards to handle a discriminated input:
|
|
225
|
+
|
|
226
|
+
```ts
|
|
227
|
+
import { isString, isNonEmptyArray } from '@wistia/utils';
|
|
228
|
+
|
|
229
|
+
function describe(value: unknown): string {
|
|
230
|
+
if (isString(value)) return value.toUpperCase();
|
|
231
|
+
if (isNonEmptyArray<unknown>(value)) return `${value.length} items`;
|
|
232
|
+
return 'other';
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## Agent-friendly docs
|
|
237
|
+
|
|
238
|
+
Every export has a TSDoc `@example` block, so IDEs and LLM agents see usage at the call site via the bundled `.d.ts`. The package also ships `llms.txt` (compact index) and `llms-full.txt` (every signature plus an example) at the package root, following the [llmstxt.org](https://llmstxt.org) convention.
|