@wopjs/cast 0.1.4 → 0.1.5
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 +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/is-to-as.test.ts +86 -0
- package/src/is-to-as.ts +7 -6
package/dist/index.d.mts
CHANGED
|
@@ -36,15 +36,16 @@ 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
|
+
declare const isArray: (arg: any) => arg is any[];
|
|
40
41
|
/** Returns `x` if `x` is an array. */
|
|
41
|
-
declare const toArray: <T>(x: T) =>
|
|
42
|
+
declare const toArray: <T>(x: T) => ExtractArray<T> | _;
|
|
42
43
|
/** Returns `true` if `x` is an array and has at least one element. */
|
|
43
|
-
declare const isNonEmptyArray:
|
|
44
|
+
declare const isNonEmptyArray: (x: any) => x is any[];
|
|
44
45
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
45
|
-
declare const toNonEmptyArray: <T>(x: T) =>
|
|
46
|
+
declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | _;
|
|
46
47
|
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
47
|
-
declare const asArray: <T>(x: T) =>
|
|
48
|
+
declare const asArray: <T>(x: T) => ExtractArray<T>;
|
|
48
49
|
interface PlainObject {
|
|
49
50
|
[key: PropertyKey]: unknown;
|
|
50
51
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -36,15 +36,16 @@ 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
|
+
declare const isArray: (arg: any) => arg is any[];
|
|
40
41
|
/** Returns `x` if `x` is an array. */
|
|
41
|
-
declare const toArray: <T>(x: T) =>
|
|
42
|
+
declare const toArray: <T>(x: T) => ExtractArray<T> | _;
|
|
42
43
|
/** Returns `true` if `x` is an array and has at least one element. */
|
|
43
|
-
declare const isNonEmptyArray:
|
|
44
|
+
declare const isNonEmptyArray: (x: any) => x is any[];
|
|
44
45
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
45
|
-
declare const toNonEmptyArray: <T>(x: T) =>
|
|
46
|
+
declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | _;
|
|
46
47
|
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
47
|
-
declare const asArray: <T>(x: T) =>
|
|
48
|
+
declare const asArray: <T>(x: T) => ExtractArray<T>;
|
|
48
49
|
interface PlainObject {
|
|
49
50
|
[key: PropertyKey]: unknown;
|
|
50
51
|
}
|
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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AAK,CAAA;AACrB,MAAA,IAAI,KAAA,GAAQ,EAAE,GAAG,CAAA;AACjB,MAAA,IAAI,CAAA,CAAE,KAAK,CAAA,EAAG;AACZ,QAAA,CAAC,MAAA,KAAW,EAAC,EAAG,GAAG,CAAA,GAAI,KAAA;AAAA,MACzB;AAAA,IACF;AAEA,IAAA,OAAO,MAAA;AAAA,EACT;AACF;AAGO,IAAM,mBAAA,GAAsB,CAAC,CAAA,KAAiD,eAAA,CAAgB,GAAG,MAAM;AAOvG,IAAM,KAAA,GAAQ,CAAC,CAAA,KAAuB;AAC3C,EAAA,IAAI,QAAA,CAAS,CAAC,CAAA,EAAG,OAAO,CAAA;AACxB,EAAA,IAAI,CAAA,IAAK,MAAM,OAAO,EAAA;AACtB,EAAA,IAAI;AACF,IAAA,OAAO,IAAA,CAAK,SAAA,CAAU,CAAA,EAAG,IAAA,EAAM,CAAC,CAAA;AAAA,EAClC,CAAA,CAAA,MAAQ;AAEN,IAAA,OAAO,CAAA,GAAI,EAAA;AAAA,EACb;AACF;;;ACnJO,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
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,19 @@ 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
|
+
const _check: string[] = stringLikeValue;
|
|
280
|
+
throw new Error("Unreachable");
|
|
281
|
+
} else {
|
|
282
|
+
const _check: string = stringLikeValue;
|
|
283
|
+
expect(_check).toBe(stringLikeValue);
|
|
244
284
|
}
|
|
245
285
|
|
|
246
286
|
// Type narrowing - preserves readonly array
|
|
@@ -248,6 +288,9 @@ describe("primitive.ts", () => {
|
|
|
248
288
|
if (isNonEmptyArray(readonlyArr)) {
|
|
249
289
|
const _check: readonly number[] = readonlyArr;
|
|
250
290
|
expect(_check).toBe(readonlyArr);
|
|
291
|
+
} else {
|
|
292
|
+
const _check: readonly number[] = readonlyArr;
|
|
293
|
+
throw new Error("Unreachable");
|
|
251
294
|
}
|
|
252
295
|
|
|
253
296
|
// Type narrowing - preserves tuple
|
|
@@ -255,6 +298,29 @@ describe("primitive.ts", () => {
|
|
|
255
298
|
if (isNonEmptyArray(tuple)) {
|
|
256
299
|
const _check: [string, number] = tuple;
|
|
257
300
|
expect(_check).toBe(tuple);
|
|
301
|
+
} else {
|
|
302
|
+
const _check: never = tuple;
|
|
303
|
+
throw new Error("Unreachable");
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Type narrowing - unknown input returns unknown[]
|
|
307
|
+
const unknownValue: unknown = ["a", "b"];
|
|
308
|
+
if (isNonEmptyArray(unknownValue)) {
|
|
309
|
+
const _check: unknown[] = unknownValue;
|
|
310
|
+
expect(_check).toEqual(["a", "b"]);
|
|
311
|
+
} else {
|
|
312
|
+
const _check: unknown = unknownValue;
|
|
313
|
+
throw new Error("Unreachable");
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Type narrowing - non-array type (string) returns never
|
|
317
|
+
const stringValue: string = "hello";
|
|
318
|
+
if (isNonEmptyArray(stringValue)) {
|
|
319
|
+
const _check: unknown[] = stringValue;
|
|
320
|
+
throw new Error("Unreachable");
|
|
321
|
+
} else {
|
|
322
|
+
const _check: string = stringValue;
|
|
323
|
+
expect(_check).toBe("hello");
|
|
258
324
|
}
|
|
259
325
|
});
|
|
260
326
|
|
|
@@ -281,6 +347,16 @@ describe("primitive.ts", () => {
|
|
|
281
347
|
const tuple: [string, number] = ["a", 1];
|
|
282
348
|
const result4: [string, number] = asArray(tuple);
|
|
283
349
|
expect(result4).toBe(tuple);
|
|
350
|
+
|
|
351
|
+
// Type narrowing - unknown input returns unknown[]
|
|
352
|
+
const unknownValue: unknown = ["a", "b"];
|
|
353
|
+
const result5: unknown[] = asArray(unknownValue);
|
|
354
|
+
expect(result5).toEqual(["a", "b"]);
|
|
355
|
+
|
|
356
|
+
// Type narrowing - non-array type (string) returns unknown[]
|
|
357
|
+
const stringValue: string = "hello";
|
|
358
|
+
const result6: unknown[] = asArray(stringValue);
|
|
359
|
+
expect(result6).toEqual([]);
|
|
284
360
|
});
|
|
285
361
|
|
|
286
362
|
it("toNonEmptyArray", () => {
|
|
@@ -318,6 +394,16 @@ describe("primitive.ts", () => {
|
|
|
318
394
|
const _check: [string, number] = result4;
|
|
319
395
|
expect(_check).toBe(tuple);
|
|
320
396
|
}
|
|
397
|
+
|
|
398
|
+
// Type narrowing - unknown input returns unknown[] | undefined
|
|
399
|
+
const unknownValue: unknown = ["a", "b"];
|
|
400
|
+
const result5: unknown[] | undefined = toNonEmptyArray(unknownValue);
|
|
401
|
+
expect(result5).toEqual(["a", "b"]);
|
|
402
|
+
|
|
403
|
+
// Type narrowing - non-array type (string) returns unknown[] | undefined
|
|
404
|
+
const stringValue: string = "hello";
|
|
405
|
+
const result6: unknown[] | undefined = toNonEmptyArray(stringValue);
|
|
406
|
+
expect(result6).toBe(undefined);
|
|
321
407
|
});
|
|
322
408
|
|
|
323
409
|
it("isObject", () => {
|
package/src/is-to-as.ts
CHANGED
|
@@ -57,20 +57,21 @@ 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
|
-
|
|
60
|
+
type ExtractArray<T> = T extends readonly unknown[] ? T : unknown[];
|
|
61
|
+
|
|
62
|
+
export const isArray = Array.isArray;
|
|
61
63
|
|
|
62
64
|
/** Returns `x` if `x` is an array. */
|
|
63
|
-
export const toArray = <T>(x: T):
|
|
65
|
+
export const toArray = <T>(x: T): ExtractArray<T> | _ => (isArray(x) ? (x as ExtractArray<T>) : _);
|
|
64
66
|
|
|
65
67
|
/** Returns `true` if `x` is an array and has at least one element. */
|
|
66
|
-
export const isNonEmptyArray =
|
|
68
|
+
export const isNonEmptyArray = (x: any): x is any[] => isArray(x) && x.length > 0;
|
|
67
69
|
|
|
68
70
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
69
|
-
export const toNonEmptyArray = <T>(x: T):
|
|
71
|
+
export const toNonEmptyArray = <T>(x: T): ExtractArray<T> | _ => (isNonEmptyArray(x) ? (x as ExtractArray<T>) : _);
|
|
70
72
|
|
|
71
73
|
/** 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[]>);
|
|
74
|
+
export const asArray = <T>(x: T): ExtractArray<T> => (isArray(x) ? (x as ExtractArray<T>) : ([] as ExtractArray<T>));
|
|
74
75
|
|
|
75
76
|
export interface PlainObject {
|
|
76
77
|
[key: PropertyKey]: unknown;
|