@wopjs/cast 0.1.8 → 0.1.9
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 +7 -7
- package/dist/index.d.ts +7 -7
- package/package.json +1 -1
- package/src/is-to-as.test.ts +21 -0
- package/src/is-to-as.ts +6 -6
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
type _ = undefined;
|
|
2
|
-
/**
|
|
3
|
-
type
|
|
2
|
+
/** Returns `U` if `T` is `never` or `any`, otherwise returns `T`. */
|
|
3
|
+
type SetDefaultType<T, U> = [T, U][T extends any ? (0 extends 1 & T ? 1 : 0) : 1];
|
|
4
4
|
/** Returns `true` if `x` is not `undefined`. */
|
|
5
5
|
declare const isDefined: <T>(x: T | undefined) => x is T;
|
|
6
6
|
declare const isTrue: (x: unknown) => x is true;
|
|
@@ -9,7 +9,7 @@ declare const toTrue: (x: unknown) => true | _;
|
|
|
9
9
|
/** Returns `true` if `x` is `true`, otherwise returns `false`. */
|
|
10
10
|
declare const asTrue: (x: unknown) => boolean;
|
|
11
11
|
type Falsy = false | null | undefined | 0 | "";
|
|
12
|
-
type ExtractFalsy<T> =
|
|
12
|
+
type ExtractFalsy<T> = SetDefaultType<Extract<T, Falsy>, Falsy>;
|
|
13
13
|
/** Returns `true` if `Boolean(x)` is `false`. */
|
|
14
14
|
declare const isFalsy: <T>(x: T) => x is ExtractFalsy<T>;
|
|
15
15
|
/** Returns `x` if `Boolean(x)` is `false`, otherwise returns `undefined`. */
|
|
@@ -37,7 +37,7 @@ declare const asString: (x: unknown) => string;
|
|
|
37
37
|
declare const isNonEmptyString: (x: unknown) => x is string;
|
|
38
38
|
/** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
|
|
39
39
|
declare const toNonEmptyString: (x: unknown) => string | _;
|
|
40
|
-
type ExtractArray<T> =
|
|
40
|
+
type ExtractArray<T> = SetDefaultType<Extract<T, readonly unknown[]>, unknown[]>;
|
|
41
41
|
declare const isArray: <T>(x: T) => x is ExtractArray<T>;
|
|
42
42
|
/** Returns `x` if `x` is an array. */
|
|
43
43
|
declare const toArray: <T>(x: T) => ExtractArray<T> | _;
|
|
@@ -47,7 +47,7 @@ declare const asArray: <T>(x: T) => ExtractArray<T>;
|
|
|
47
47
|
declare const isNonEmptyArray: <T>(x: T) => x is ExtractArray<T>;
|
|
48
48
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
49
49
|
declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | _;
|
|
50
|
-
type ExtractObject<T> =
|
|
50
|
+
type ExtractObject<T> = SetDefaultType<Extract<T, object>, object>;
|
|
51
51
|
/** Returns `true` if `x` is an object (including array) and not null. */
|
|
52
52
|
declare const isObject: <T>(x: T) => x is ExtractObject<T>;
|
|
53
53
|
/** Returns `x` if `x` is an object (including array). */
|
|
@@ -57,7 +57,7 @@ declare const asObject: <T>(x: T) => ExtractObject<T>;
|
|
|
57
57
|
interface PlainObject {
|
|
58
58
|
[key: PropertyKey]: unknown;
|
|
59
59
|
}
|
|
60
|
-
type ExtractPlainObject<T> =
|
|
60
|
+
type ExtractPlainObject<T> = SetDefaultType<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>;
|
|
61
61
|
/** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
|
|
62
62
|
declare const isPlainObject: <T>(x: T) => x is ExtractPlainObject<T>;
|
|
63
63
|
/** Returns `x` if `x` is a plain object. */
|
|
@@ -94,4 +94,4 @@ declare const returnsFalse: () => false;
|
|
|
94
94
|
declare const returnsTrue: () => true;
|
|
95
95
|
declare const returnsEmptyString: () => string;
|
|
96
96
|
|
|
97
|
-
export { type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type
|
|
97
|
+
export { type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type PlainObject, type SetDefaultType, 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, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
type _ = undefined;
|
|
2
|
-
/**
|
|
3
|
-
type
|
|
2
|
+
/** Returns `U` if `T` is `never` or `any`, otherwise returns `T`. */
|
|
3
|
+
type SetDefaultType<T, U> = [T, U][T extends any ? (0 extends 1 & T ? 1 : 0) : 1];
|
|
4
4
|
/** Returns `true` if `x` is not `undefined`. */
|
|
5
5
|
declare const isDefined: <T>(x: T | undefined) => x is T;
|
|
6
6
|
declare const isTrue: (x: unknown) => x is true;
|
|
@@ -9,7 +9,7 @@ declare const toTrue: (x: unknown) => true | _;
|
|
|
9
9
|
/** Returns `true` if `x` is `true`, otherwise returns `false`. */
|
|
10
10
|
declare const asTrue: (x: unknown) => boolean;
|
|
11
11
|
type Falsy = false | null | undefined | 0 | "";
|
|
12
|
-
type ExtractFalsy<T> =
|
|
12
|
+
type ExtractFalsy<T> = SetDefaultType<Extract<T, Falsy>, Falsy>;
|
|
13
13
|
/** Returns `true` if `Boolean(x)` is `false`. */
|
|
14
14
|
declare const isFalsy: <T>(x: T) => x is ExtractFalsy<T>;
|
|
15
15
|
/** Returns `x` if `Boolean(x)` is `false`, otherwise returns `undefined`. */
|
|
@@ -37,7 +37,7 @@ declare const asString: (x: unknown) => string;
|
|
|
37
37
|
declare const isNonEmptyString: (x: unknown) => x is string;
|
|
38
38
|
/** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
|
|
39
39
|
declare const toNonEmptyString: (x: unknown) => string | _;
|
|
40
|
-
type ExtractArray<T> =
|
|
40
|
+
type ExtractArray<T> = SetDefaultType<Extract<T, readonly unknown[]>, unknown[]>;
|
|
41
41
|
declare const isArray: <T>(x: T) => x is ExtractArray<T>;
|
|
42
42
|
/** Returns `x` if `x` is an array. */
|
|
43
43
|
declare const toArray: <T>(x: T) => ExtractArray<T> | _;
|
|
@@ -47,7 +47,7 @@ declare const asArray: <T>(x: T) => ExtractArray<T>;
|
|
|
47
47
|
declare const isNonEmptyArray: <T>(x: T) => x is ExtractArray<T>;
|
|
48
48
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
49
49
|
declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | _;
|
|
50
|
-
type ExtractObject<T> =
|
|
50
|
+
type ExtractObject<T> = SetDefaultType<Extract<T, object>, object>;
|
|
51
51
|
/** Returns `true` if `x` is an object (including array) and not null. */
|
|
52
52
|
declare const isObject: <T>(x: T) => x is ExtractObject<T>;
|
|
53
53
|
/** Returns `x` if `x` is an object (including array). */
|
|
@@ -57,7 +57,7 @@ declare const asObject: <T>(x: T) => ExtractObject<T>;
|
|
|
57
57
|
interface PlainObject {
|
|
58
58
|
[key: PropertyKey]: unknown;
|
|
59
59
|
}
|
|
60
|
-
type ExtractPlainObject<T> =
|
|
60
|
+
type ExtractPlainObject<T> = SetDefaultType<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>;
|
|
61
61
|
/** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
|
|
62
62
|
declare const isPlainObject: <T>(x: T) => x is ExtractPlainObject<T>;
|
|
63
63
|
/** Returns `x` if `x` is a plain object. */
|
|
@@ -94,4 +94,4 @@ declare const returnsFalse: () => false;
|
|
|
94
94
|
declare const returnsTrue: () => true;
|
|
95
95
|
declare const returnsEmptyString: () => string;
|
|
96
96
|
|
|
97
|
-
export { type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type
|
|
97
|
+
export { type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type PlainObject, type SetDefaultType, 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, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy };
|
package/package.json
CHANGED
package/src/is-to-as.test.ts
CHANGED
|
@@ -304,6 +304,16 @@ describe("primitive.ts", () => {
|
|
|
304
304
|
expect(check).toBe(undefined);
|
|
305
305
|
}
|
|
306
306
|
}
|
|
307
|
+
|
|
308
|
+
{
|
|
309
|
+
// Type narrowing - generic
|
|
310
|
+
const _fn = <T>(x: T): void => {
|
|
311
|
+
if (isArray(x)) {
|
|
312
|
+
const y: readonly any[] = x;
|
|
313
|
+
expect(Array.isArray(y)).toBe(true);
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
}
|
|
307
317
|
});
|
|
308
318
|
|
|
309
319
|
it("toArray", () => {
|
|
@@ -372,6 +382,15 @@ describe("primitive.ts", () => {
|
|
|
372
382
|
const arr = castType<string>("hello");
|
|
373
383
|
const result: unknown[] | undefined = toArray(arr);
|
|
374
384
|
expect(result).toBe(undefined);
|
|
385
|
+
toArray(arr)?.map(x => x);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
{
|
|
389
|
+
// Type narrowing - any returns any[]
|
|
390
|
+
const arr = castType<any>("hello");
|
|
391
|
+
const result: unknown[] | undefined = toArray(arr);
|
|
392
|
+
expect(result).toBe(undefined);
|
|
393
|
+
toArray(arr)?.map(x => x);
|
|
375
394
|
}
|
|
376
395
|
});
|
|
377
396
|
|
|
@@ -507,6 +526,7 @@ describe("primitive.ts", () => {
|
|
|
507
526
|
const arr = castType<unknown>(["a", "b"]);
|
|
508
527
|
const result: unknown[] = asArray(arr);
|
|
509
528
|
expect(result).toEqual(["a", "b"]);
|
|
529
|
+
asArray(arr).map(x => x);
|
|
510
530
|
}
|
|
511
531
|
|
|
512
532
|
{
|
|
@@ -514,6 +534,7 @@ describe("primitive.ts", () => {
|
|
|
514
534
|
const arr = castType<string>("hello");
|
|
515
535
|
const result: unknown[] = asArray(arr);
|
|
516
536
|
expect(result).toEqual([]);
|
|
537
|
+
asArray(arr).map(x => x);
|
|
517
538
|
}
|
|
518
539
|
});
|
|
519
540
|
|
package/src/is-to-as.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const _ = undefined;
|
|
2
2
|
export type _ = undefined;
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
export type
|
|
4
|
+
/** Returns `U` if `T` is `never` or `any`, otherwise returns `T`. */
|
|
5
|
+
export type SetDefaultType<T, U> = [T, U][T extends any ? (0 extends 1 & T ? 1 : 0) : 1];
|
|
6
6
|
|
|
7
7
|
/** Returns `true` if `x` is not `undefined`. */
|
|
8
8
|
export const isDefined = <T>(x: T | undefined): x is T => x !== _;
|
|
@@ -17,7 +17,7 @@ export const asTrue = (x: unknown): boolean => (x === true ? x : false);
|
|
|
17
17
|
|
|
18
18
|
export type Falsy = false | null | undefined | 0 | "";
|
|
19
19
|
|
|
20
|
-
export type ExtractFalsy<T> =
|
|
20
|
+
export type ExtractFalsy<T> = SetDefaultType<Extract<T, Falsy>, Falsy>;
|
|
21
21
|
|
|
22
22
|
/** Returns `true` if `Boolean(x)` is `false`. */
|
|
23
23
|
export const isFalsy = <T>(x: T): x is ExtractFalsy<T> => !x;
|
|
@@ -60,7 +60,7 @@ export const isNonEmptyString = (x: unknown): x is string => isString(x) && x !=
|
|
|
60
60
|
/** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
|
|
61
61
|
export const toNonEmptyString = (x: unknown): string | _ => (isNonEmptyString(x) ? x : _);
|
|
62
62
|
|
|
63
|
-
export type ExtractArray<T> =
|
|
63
|
+
export type ExtractArray<T> = SetDefaultType<Extract<T, readonly unknown[]>, unknown[]>;
|
|
64
64
|
|
|
65
65
|
export const isArray = Array.isArray as <T>(x: T) => x is ExtractArray<T>;
|
|
66
66
|
|
|
@@ -76,7 +76,7 @@ export const isNonEmptyArray = <T>(x: T): x is ExtractArray<T> => isArray(x) &&
|
|
|
76
76
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
77
77
|
export const toNonEmptyArray = <T>(x: T): ExtractArray<T> | _ => (isNonEmptyArray(x) ? x : _);
|
|
78
78
|
|
|
79
|
-
export type ExtractObject<T> =
|
|
79
|
+
export type ExtractObject<T> = SetDefaultType<Extract<T, object>, object>;
|
|
80
80
|
|
|
81
81
|
/** Returns `true` if `x` is an object (including array) and not null. */
|
|
82
82
|
export const isObject = <T>(x: T): x is ExtractObject<T> => x !== null && typeof x === "object";
|
|
@@ -91,7 +91,7 @@ export interface PlainObject {
|
|
|
91
91
|
[key: PropertyKey]: unknown;
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
export type ExtractPlainObject<T> =
|
|
94
|
+
export type ExtractPlainObject<T> = SetDefaultType<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>;
|
|
95
95
|
|
|
96
96
|
/** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
|
|
97
97
|
export const isPlainObject = <T>(x: T): x is ExtractPlainObject<T> => isObject(x) && !isArray(x);
|