@wopjs/cast 0.1.4 → 0.1.6
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/dist/index.d.mts +11 -6
- package/dist/index.d.ts +11 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -1
- package/src/is-to-as.test.ts +87 -0
- package/src/is-to-as.ts +12 -6
package/dist/index.d.mts
CHANGED
|
@@ -36,15 +36,20 @@ declare const asString: (x: unknown) => string;
|
|
|
36
36
|
declare const isNonEmptyString: (x: unknown) => x is string;
|
|
37
37
|
/** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
|
|
38
38
|
declare const toNonEmptyString: (x: unknown) => string | _;
|
|
39
|
-
|
|
39
|
+
type ExtractArray<T> = T extends readonly unknown[] ? T : unknown[];
|
|
40
|
+
interface IsArray {
|
|
41
|
+
(x: unknown): x is unknown[];
|
|
42
|
+
<T>(x: T): x is T extends readonly unknown[] ? T : never;
|
|
43
|
+
}
|
|
44
|
+
declare const isArray: IsArray;
|
|
40
45
|
/** Returns `x` if `x` is an array. */
|
|
41
|
-
declare const toArray: <T>(x: T) =>
|
|
46
|
+
declare const toArray: <T>(x: T) => ExtractArray<T> | _;
|
|
42
47
|
/** Returns `true` if `x` is an array and has at least one element. */
|
|
43
|
-
declare const isNonEmptyArray:
|
|
48
|
+
declare const isNonEmptyArray: IsArray;
|
|
44
49
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
45
|
-
declare const toNonEmptyArray: <T>(x: T) =>
|
|
50
|
+
declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | _;
|
|
46
51
|
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
47
|
-
declare const asArray: <T>(x: T) =>
|
|
52
|
+
declare const asArray: <T>(x: T) => ExtractArray<T>;
|
|
48
53
|
interface PlainObject {
|
|
49
54
|
[key: PropertyKey]: unknown;
|
|
50
55
|
}
|
|
@@ -91,4 +96,4 @@ declare const returnsFalse: () => false;
|
|
|
91
96
|
declare const returnsTrue: () => true;
|
|
92
97
|
declare const returnsEmptyString: () => string;
|
|
93
98
|
|
|
94
|
-
export { type Falsy, type IsFalsy, type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy };
|
|
99
|
+
export { type ExtractArray, type Falsy, type IsArray, type IsFalsy, type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy };
|
package/dist/index.d.ts
CHANGED
|
@@ -36,15 +36,20 @@ declare const asString: (x: unknown) => string;
|
|
|
36
36
|
declare const isNonEmptyString: (x: unknown) => x is string;
|
|
37
37
|
/** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
|
|
38
38
|
declare const toNonEmptyString: (x: unknown) => string | _;
|
|
39
|
-
|
|
39
|
+
type ExtractArray<T> = T extends readonly unknown[] ? T : unknown[];
|
|
40
|
+
interface IsArray {
|
|
41
|
+
(x: unknown): x is unknown[];
|
|
42
|
+
<T>(x: T): x is T extends readonly unknown[] ? T : never;
|
|
43
|
+
}
|
|
44
|
+
declare const isArray: IsArray;
|
|
40
45
|
/** Returns `x` if `x` is an array. */
|
|
41
|
-
declare const toArray: <T>(x: T) =>
|
|
46
|
+
declare const toArray: <T>(x: T) => ExtractArray<T> | _;
|
|
42
47
|
/** Returns `true` if `x` is an array and has at least one element. */
|
|
43
|
-
declare const isNonEmptyArray:
|
|
48
|
+
declare const isNonEmptyArray: IsArray;
|
|
44
49
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
45
|
-
declare const toNonEmptyArray: <T>(x: T) =>
|
|
50
|
+
declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | _;
|
|
46
51
|
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
47
|
-
declare const asArray: <T>(x: T) =>
|
|
52
|
+
declare const asArray: <T>(x: T) => ExtractArray<T>;
|
|
48
53
|
interface PlainObject {
|
|
49
54
|
[key: PropertyKey]: unknown;
|
|
50
55
|
}
|
|
@@ -91,4 +96,4 @@ declare const returnsFalse: () => false;
|
|
|
91
96
|
declare const returnsTrue: () => true;
|
|
92
97
|
declare const returnsEmptyString: () => string;
|
|
93
98
|
|
|
94
|
-
export { type Falsy, type IsFalsy, type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy };
|
|
99
|
+
export { type ExtractArray, type Falsy, type IsArray, type IsFalsy, type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy };
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/is-to-as.ts","../src/returns.ts"],"names":[],"mappings":";;;AAAA,IAAM,CAAA,GAAI,MAAA;AAIH,IAAM,SAAA,GAAY,CAAI,CAAA,KAA6B,CAAA,KAAM;AAEzD,IAAM,MAAA,GAAS,CAAC,CAAA,KAA0B,CAAA,KAAM;AAGhD,IAAM,MAAA,GAAS,CAAC,CAAA,KAA0B,CAAA,KAAM,OAAO,IAAA,GAAO;AAG9D,IAAM,MAAA,GAAS,CAAC,CAAA,KAAyB,CAAA,KAAM,OAAO,CAAA,GAAI;AAU1D,IAAM,OAAA,GAAmB,CAAC,CAAA,KAA2B,CAAC;AAGtD,IAAM,QAAA,GAAW,CAAI,CAAA,KAAiC,CAAC,CAAC;AAGxD,IAAM,WAAW,CAAI,CAAA,KAAiC,QAAA,CAAS,CAAC,IAAI,CAAA,GAAI;AAExE,IAAM,SAAA,GAAY,CAAC,CAAA,KAA6B,CAAA,KAAM,QAAQ,CAAA,KAAM;AAGpE,IAAM,YAAY,CAAC,CAAA,KAA6B,SAAA,CAAU,CAAC,IAAI,CAAA,GAAI;AAGnE,IAAM,WAAW,CAAC,CAAA,KAA4B,OAAO,CAAA,KAAM,YAAY,CAAA,KAAM;AAG7E,IAAM,WAAW,CAAC,CAAA,KAA4B,QAAA,CAAS,CAAC,IAAI,CAAA,GAAI;AAGhE,IAAM,WAAW,CAAC,CAAA,KAAwB,QAAA,CAAS,CAAC,IAAI,CAAA,GAAI;AAG5D,IAAM,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AA,EAAG,IAAA,EAAM,CAAC,CAAA;AAAA,EAClC,CAAA,CAAA,MAAQ;AAEN,IAAA,OAAO,CAAA,GAAI,EAAA;AAAA,EACb;AACF;;;ACxJO,IAAM,OAAO,MAAY;AAAC;AAE1B,IAAM,gBAAA,GAAmB;AAEzB,IAAM,cAAc,MAAY;AAEhC,IAAM,eAAe,MAAa;AAElC,IAAM,cAAc,MAAY;AAEhC,IAAM,qBAAqB,MAAc","file":"index.mjs"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wopjs/cast",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "Filter types from unknown",
|
|
5
5
|
"repository": "wopjs/cast",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"scripts": {
|
|
15
15
|
"prepublishOnly": "npm run build",
|
|
16
16
|
"lint": "eslint && prettier --check .",
|
|
17
|
+
"ts-check": "tsc --noEmit",
|
|
17
18
|
"docs": "typedoc --options typedoc.json",
|
|
18
19
|
"test": "vitest",
|
|
19
20
|
"test:coverage": "vitest --coverage --coverage.include=src/**",
|
package/src/is-to-as.test.ts
CHANGED
|
@@ -180,6 +180,20 @@ describe("primitive.ts", () => {
|
|
|
180
180
|
const _check: [string, number] = tuple;
|
|
181
181
|
expect(_check).toBe(tuple);
|
|
182
182
|
}
|
|
183
|
+
|
|
184
|
+
// Type narrowing - unknown input returns any[] | undefined
|
|
185
|
+
const unknownValue: unknown = ["a", "b"];
|
|
186
|
+
if (isArray(unknownValue)) {
|
|
187
|
+
const _check: unknown[] = unknownValue;
|
|
188
|
+
expect(_check).toEqual(["a", "b"]);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Type narrowing - non-array type (string) returns string & any[] | undefined
|
|
192
|
+
const stringValue: string = "hello";
|
|
193
|
+
if (isArray(stringValue)) {
|
|
194
|
+
const _check: unknown[] = stringValue;
|
|
195
|
+
expect(_check).toBe(undefined);
|
|
196
|
+
}
|
|
183
197
|
});
|
|
184
198
|
|
|
185
199
|
it("toArray", () => {
|
|
@@ -217,6 +231,16 @@ describe("primitive.ts", () => {
|
|
|
217
231
|
const _check: [string, number] = result4;
|
|
218
232
|
expect(_check).toBe(tuple);
|
|
219
233
|
}
|
|
234
|
+
|
|
235
|
+
// Type narrowing - unknown input returns unknown[] | undefined
|
|
236
|
+
const unknownValue: unknown = ["a", "b"];
|
|
237
|
+
const result5: unknown[] | undefined = toArray(unknownValue);
|
|
238
|
+
expect(result5).toEqual(["a", "b"]);
|
|
239
|
+
|
|
240
|
+
// Type narrowing - non-array type (string) returns unknown[] | undefined
|
|
241
|
+
const stringValue: string = "hello";
|
|
242
|
+
const result6: unknown[] | undefined = toArray(stringValue);
|
|
243
|
+
expect(result6).toBe(undefined);
|
|
220
244
|
});
|
|
221
245
|
|
|
222
246
|
it("isNonEmptyArray", () => {
|
|
@@ -234,6 +258,9 @@ describe("primitive.ts", () => {
|
|
|
234
258
|
if (isNonEmptyArray(stringArr)) {
|
|
235
259
|
const _check: string[] = stringArr;
|
|
236
260
|
expect(_check).toBe(stringArr);
|
|
261
|
+
} else {
|
|
262
|
+
const _check: never = stringArr;
|
|
263
|
+
throw new Error("Unreachable");
|
|
237
264
|
}
|
|
238
265
|
|
|
239
266
|
// Type narrowing - extracts array from union
|
|
@@ -241,6 +268,20 @@ describe("primitive.ts", () => {
|
|
|
241
268
|
if (isNonEmptyArray(unionValue)) {
|
|
242
269
|
const _check: string[] = unionValue;
|
|
243
270
|
expect(_check).toBe(unionValue);
|
|
271
|
+
} else {
|
|
272
|
+
const _check: string = unionValue;
|
|
273
|
+
throw new Error("Unreachable");
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// Type narrowing - extracts array from union
|
|
277
|
+
const stringLikeValue: string | string[] = "a";
|
|
278
|
+
if (isNonEmptyArray(stringLikeValue)) {
|
|
279
|
+
// @ts-expect-error Unreachable
|
|
280
|
+
const _check: string[] = stringLikeValue;
|
|
281
|
+
throw new Error("Unreachable");
|
|
282
|
+
} else {
|
|
283
|
+
const _check: string = stringLikeValue;
|
|
284
|
+
expect(_check).toBe(stringLikeValue);
|
|
244
285
|
}
|
|
245
286
|
|
|
246
287
|
// Type narrowing - preserves readonly array
|
|
@@ -248,6 +289,9 @@ describe("primitive.ts", () => {
|
|
|
248
289
|
if (isNonEmptyArray(readonlyArr)) {
|
|
249
290
|
const _check: readonly number[] = readonlyArr;
|
|
250
291
|
expect(_check).toBe(readonlyArr);
|
|
292
|
+
} else {
|
|
293
|
+
const _check: readonly number[] = readonlyArr;
|
|
294
|
+
throw new Error("Unreachable");
|
|
251
295
|
}
|
|
252
296
|
|
|
253
297
|
// Type narrowing - preserves tuple
|
|
@@ -255,6 +299,29 @@ describe("primitive.ts", () => {
|
|
|
255
299
|
if (isNonEmptyArray(tuple)) {
|
|
256
300
|
const _check: [string, number] = tuple;
|
|
257
301
|
expect(_check).toBe(tuple);
|
|
302
|
+
} else {
|
|
303
|
+
const _check: never = tuple;
|
|
304
|
+
throw new Error("Unreachable");
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Type narrowing - unknown input returns unknown[]
|
|
308
|
+
const unknownValue: unknown = ["a", "b"];
|
|
309
|
+
if (isNonEmptyArray(unknownValue)) {
|
|
310
|
+
const _check: unknown[] = unknownValue;
|
|
311
|
+
expect(_check).toEqual(["a", "b"]);
|
|
312
|
+
} else {
|
|
313
|
+
const _check: unknown = unknownValue;
|
|
314
|
+
throw new Error("Unreachable");
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Type narrowing - non-array type (string) returns never
|
|
318
|
+
const stringValue: string = "hello";
|
|
319
|
+
if (isNonEmptyArray(stringValue)) {
|
|
320
|
+
const _check: unknown[] = stringValue;
|
|
321
|
+
throw new Error("Unreachable");
|
|
322
|
+
} else {
|
|
323
|
+
const _check: string = stringValue;
|
|
324
|
+
expect(_check).toBe("hello");
|
|
258
325
|
}
|
|
259
326
|
});
|
|
260
327
|
|
|
@@ -281,6 +348,16 @@ describe("primitive.ts", () => {
|
|
|
281
348
|
const tuple: [string, number] = ["a", 1];
|
|
282
349
|
const result4: [string, number] = asArray(tuple);
|
|
283
350
|
expect(result4).toBe(tuple);
|
|
351
|
+
|
|
352
|
+
// Type narrowing - unknown input returns unknown[]
|
|
353
|
+
const unknownValue: unknown = ["a", "b"];
|
|
354
|
+
const result5: unknown[] = asArray(unknownValue);
|
|
355
|
+
expect(result5).toEqual(["a", "b"]);
|
|
356
|
+
|
|
357
|
+
// Type narrowing - non-array type (string) returns unknown[]
|
|
358
|
+
const stringValue: string = "hello";
|
|
359
|
+
const result6: unknown[] = asArray(stringValue);
|
|
360
|
+
expect(result6).toEqual([]);
|
|
284
361
|
});
|
|
285
362
|
|
|
286
363
|
it("toNonEmptyArray", () => {
|
|
@@ -318,6 +395,16 @@ describe("primitive.ts", () => {
|
|
|
318
395
|
const _check: [string, number] = result4;
|
|
319
396
|
expect(_check).toBe(tuple);
|
|
320
397
|
}
|
|
398
|
+
|
|
399
|
+
// Type narrowing - unknown input returns unknown[] | undefined
|
|
400
|
+
const unknownValue: unknown = ["a", "b"];
|
|
401
|
+
const result5: unknown[] | undefined = toNonEmptyArray(unknownValue);
|
|
402
|
+
expect(result5).toEqual(["a", "b"]);
|
|
403
|
+
|
|
404
|
+
// Type narrowing - non-array type (string) returns unknown[] | undefined
|
|
405
|
+
const stringValue: string = "hello";
|
|
406
|
+
const result6: unknown[] | undefined = toNonEmptyArray(stringValue);
|
|
407
|
+
expect(result6).toBe(undefined);
|
|
321
408
|
});
|
|
322
409
|
|
|
323
410
|
it("isObject", () => {
|
package/src/is-to-as.ts
CHANGED
|
@@ -57,20 +57,26 @@ export const isNonEmptyString = (x: unknown): x is string => isString(x) && x !=
|
|
|
57
57
|
/** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
|
|
58
58
|
export const toNonEmptyString = (x: unknown): string | _ => (isNonEmptyString(x) ? x : _);
|
|
59
59
|
|
|
60
|
-
export
|
|
60
|
+
export type ExtractArray<T> = T extends readonly unknown[] ? T : unknown[];
|
|
61
|
+
|
|
62
|
+
export interface IsArray {
|
|
63
|
+
(x: unknown): x is unknown[];
|
|
64
|
+
<T>(x: T): x is T extends readonly unknown[] ? T : never;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const isArray: IsArray = Array.isArray;
|
|
61
68
|
|
|
62
69
|
/** Returns `x` if `x` is an array. */
|
|
63
|
-
export const toArray = <T>(x: T):
|
|
70
|
+
export const toArray = <T>(x: T): ExtractArray<T> | _ => (isArray(x) ? (x as ExtractArray<T>) : _);
|
|
64
71
|
|
|
65
72
|
/** Returns `true` if `x` is an array and has at least one element. */
|
|
66
|
-
export const isNonEmptyArray =
|
|
73
|
+
export const isNonEmptyArray: IsArray = (x: unknown): x is unknown[] => isArray(x) && x.length > 0;
|
|
67
74
|
|
|
68
75
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
69
|
-
export const toNonEmptyArray = <T>(x: T):
|
|
76
|
+
export const toNonEmptyArray = <T>(x: T): ExtractArray<T> | _ => (isNonEmptyArray(x) ? (x as ExtractArray<T>) : _);
|
|
70
77
|
|
|
71
78
|
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
72
|
-
export const asArray = <T>(x: T):
|
|
73
|
-
isArray(x) ? x : ([] as Extract<T, readonly unknown[]>);
|
|
79
|
+
export const asArray = <T>(x: T): ExtractArray<T> => (isArray(x) ? (x as ExtractArray<T>) : ([] as ExtractArray<T>));
|
|
74
80
|
|
|
75
81
|
export interface PlainObject {
|
|
76
82
|
[key: PropertyKey]: unknown;
|