@wopjs/cast 0.1.11 → 0.1.13
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 +59 -19
- package/dist/index.d.ts +59 -19
- package/dist/index.js +52 -15
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +50 -16
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/array.test.ts +538 -0
- package/src/array.ts +129 -0
- package/src/index.ts +1 -0
- package/src/is-to-as.ts +24 -21
package/src/is-to-as.ts
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
const _ = undefined;
|
|
2
|
-
export type _ = undefined;
|
|
3
|
-
|
|
4
1
|
/** Returns `U` if `T` is `never` or `any`, otherwise returns `T`. */
|
|
5
2
|
export type SetDefaultType<T, U> = [T, U][T extends any ? (0 extends 1 & T ? 1 : 0) : 1];
|
|
6
3
|
|
|
4
|
+
export type Defined<T> = Exclude<T, undefined>;
|
|
5
|
+
|
|
7
6
|
/** Returns `true` if `x` is not `undefined`. */
|
|
8
|
-
export const isDefined = <T>(x: T |
|
|
7
|
+
export const isDefined = <T>(x: T | undefined): x is Defined<T> => x !== undefined;
|
|
9
8
|
|
|
10
9
|
export const isTrue = (x: unknown): x is true => x === true;
|
|
11
10
|
|
|
12
11
|
/** Returns `true` if `x` is `true`, otherwise returns `undefined`. */
|
|
13
|
-
export const toTrue = (x: unknown): true |
|
|
12
|
+
export const toTrue = (x: unknown): true | undefined => (x === true ? true : undefined);
|
|
14
13
|
|
|
15
14
|
/** Returns `true` if `x` is `true`, otherwise returns `false`. */
|
|
16
15
|
export const asTrue = (x: unknown): boolean => (x === true ? x : false);
|
|
@@ -23,24 +22,26 @@ export type ExtractFalsy<T> = SetDefaultType<Extract<T, Falsy>, Falsy>;
|
|
|
23
22
|
export const isFalsy = <T>(x: T): x is ExtractFalsy<T> => !x;
|
|
24
23
|
|
|
25
24
|
/** Returns `x` if `Boolean(x)` is `false`, otherwise returns `undefined`. */
|
|
26
|
-
export const toFalsy = <T>(x: T): ExtractFalsy<T> |
|
|
25
|
+
export const toFalsy = <T>(x: T): ExtractFalsy<T> | undefined => (isFalsy(x) ? x : undefined);
|
|
26
|
+
|
|
27
|
+
export type Truthy<T> = Exclude<T, Falsy>;
|
|
27
28
|
|
|
28
29
|
/** Returns `true` if `Boolean(x)` is `true`. */
|
|
29
|
-
export const isTruthy = <T>(x: T): x is
|
|
30
|
+
export const isTruthy = <T>(x: T): x is Truthy<T> => !!x;
|
|
30
31
|
|
|
31
32
|
/** Returns `x` if `Boolean(x)` is `true`, otherwise returns `undefined`. */
|
|
32
|
-
export const toTruthy = <T>(x: T):
|
|
33
|
+
export const toTruthy = <T>(x: T): Truthy<T> | undefined => (isTruthy(x) ? x : undefined);
|
|
33
34
|
|
|
34
35
|
export const isBoolean = (x: unknown): x is boolean => x === true || x === false;
|
|
35
36
|
|
|
36
37
|
/** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */
|
|
37
|
-
export const toBoolean = (x: unknown): boolean |
|
|
38
|
+
export const toBoolean = (x: unknown): boolean | undefined => (isBoolean(x) ? x : undefined);
|
|
38
39
|
|
|
39
40
|
/** Returns `true` if `x` is a number, returns `false` if `x` is `NaN` or other value. */
|
|
40
41
|
export const isNumber = (x: unknown): x is number => typeof x === "number" && x === x;
|
|
41
42
|
|
|
42
43
|
/** Returns `x` if `x` is a number, `NaN` and other value will be coerced to `undefined`. */
|
|
43
|
-
export const toNumber = (x: unknown): number |
|
|
44
|
+
export const toNumber = (x: unknown): number | undefined => (isNumber(x) ? x : undefined);
|
|
44
45
|
|
|
45
46
|
/** Returns `x` if `x` is a number, `NaN` and other value will be coerced to `0`. */
|
|
46
47
|
export const asNumber = (x: unknown): number => (isNumber(x) ? x : 0);
|
|
@@ -49,7 +50,7 @@ export const asNumber = (x: unknown): number => (isNumber(x) ? x : 0);
|
|
|
49
50
|
export const isString = (x: unknown): x is string => typeof x === "string";
|
|
50
51
|
|
|
51
52
|
/** Returns `x` if `x` is a string, otherwise returns `undefined`. */
|
|
52
|
-
export const toString = (x: unknown): string |
|
|
53
|
+
export const toString = (x: unknown): string | undefined => (isString(x) ? x : undefined);
|
|
53
54
|
|
|
54
55
|
/** Returns `x` if `x` is a string, otherwise returns `''` (empty string). */
|
|
55
56
|
export const asString = (x: unknown): string => (isString(x) ? x : "");
|
|
@@ -58,7 +59,7 @@ export const asString = (x: unknown): string => (isString(x) ? x : "");
|
|
|
58
59
|
export const isNonEmptyString = (x: unknown): x is string => isString(x) && x !== "";
|
|
59
60
|
|
|
60
61
|
/** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
|
|
61
|
-
export const toNonEmptyString = (x: unknown): string |
|
|
62
|
+
export const toNonEmptyString = (x: unknown): string | undefined => (isNonEmptyString(x) ? x : undefined);
|
|
62
63
|
|
|
63
64
|
/** Extracts array type from `T`, or `unknown[]` if no array type found. */
|
|
64
65
|
export type ExtractArray<T> = SetDefaultType<Extract<T, readonly unknown[]>, unknown[]>;
|
|
@@ -66,7 +67,7 @@ export type ExtractArray<T> = SetDefaultType<Extract<T, readonly unknown[]>, unk
|
|
|
66
67
|
export const isArray = Array.isArray as <T>(x: T) => x is ExtractArray<T>;
|
|
67
68
|
|
|
68
69
|
/** Returns `x` if `x` is an array. */
|
|
69
|
-
export const toArray = <T>(x: T): ExtractArray<T> |
|
|
70
|
+
export const toArray = <T>(x: T): ExtractArray<T> | undefined => (isArray(x) ? x : undefined);
|
|
70
71
|
|
|
71
72
|
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
72
73
|
export const asArray = <T>(x: T): ExtractArray<T> => (isArray(x) ? x : ([] as ExtractArray<T>));
|
|
@@ -75,7 +76,7 @@ export const asArray = <T>(x: T): ExtractArray<T> => (isArray(x) ? x : ([] as Ex
|
|
|
75
76
|
export const isNonEmptyArray = <T>(x: T): x is ExtractArray<T> => isArray(x) && x.length > 0;
|
|
76
77
|
|
|
77
78
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
78
|
-
export const toNonEmptyArray = <T>(x: T): ExtractArray<T> |
|
|
79
|
+
export const toNonEmptyArray = <T>(x: T): ExtractArray<T> | undefined => (isNonEmptyArray(x) ? x : undefined);
|
|
79
80
|
|
|
80
81
|
export type ExtractObject<T> = SetDefaultType<Extract<T, object>, object>;
|
|
81
82
|
|
|
@@ -83,7 +84,7 @@ export type ExtractObject<T> = SetDefaultType<Extract<T, object>, object>;
|
|
|
83
84
|
export const isObject = <T>(x: T): x is ExtractObject<T> => x !== null && typeof x === "object";
|
|
84
85
|
|
|
85
86
|
/** Returns `x` if `x` is an object (including array). */
|
|
86
|
-
export const toObject = <T>(x: T): ExtractObject<T> |
|
|
87
|
+
export const toObject = <T>(x: T): ExtractObject<T> | undefined => (isObject(x) ? x : undefined);
|
|
87
88
|
|
|
88
89
|
/** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */
|
|
89
90
|
export const asObject = <T>(x: T): ExtractObject<T> => (isObject(x) ? x : ({} as ExtractObject<T>));
|
|
@@ -98,7 +99,7 @@ export type ExtractPlainObject<T> = SetDefaultType<Exclude<Extract<T, object>, r
|
|
|
98
99
|
export const isPlainObject = <T>(x: T): x is ExtractPlainObject<T> => isObject(x) && !isArray(x);
|
|
99
100
|
|
|
100
101
|
/** Returns `x` if `x` is a plain object. */
|
|
101
|
-
export const toPlainObject = <T>(x: T): ExtractPlainObject<T> |
|
|
102
|
+
export const toPlainObject = <T>(x: T): ExtractPlainObject<T> | undefined => (isPlainObject(x) ? x : undefined);
|
|
102
103
|
|
|
103
104
|
/** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */
|
|
104
105
|
export const asPlainObject = <T>(x: T): ExtractPlainObject<T> => (isPlainObject(x) ? x : ({} as ExtractPlainObject<T>));
|
|
@@ -118,13 +119,15 @@ const walkPlainObjectValues = <T>(x: T, predicate?: (x: unknown) => boolean): bo
|
|
|
118
119
|
export const isNonEmptyPlainObject = <T>(x: T): x is ExtractPlainObject<T> => walkPlainObjectValues(x);
|
|
119
120
|
|
|
120
121
|
/** Returns `x` if `x` is a plain object and has at least one key. */
|
|
121
|
-
export const toNonEmptyPlainObject = <T>(x: T): ExtractPlainObject<T> |
|
|
122
|
+
export const toNonEmptyPlainObject = <T>(x: T): ExtractPlainObject<T> | undefined =>
|
|
123
|
+
isNonEmptyPlainObject(x) ? x : undefined;
|
|
122
124
|
|
|
123
125
|
/** Returns `true` if `x` is a plain object and has at least one key with non-undefined value. */
|
|
124
126
|
export const isNonEmptyJSONObject = <T>(x: T): x is ExtractPlainObject<T> => walkPlainObjectValues(x, isDefined);
|
|
125
127
|
|
|
126
128
|
/** Returns `x` if `x` is a plain object and has at least one key with non-undefined value, otherwise returns `undefined`. */
|
|
127
|
-
export const toNonEmptyJSONObject = <T>(x: T): ExtractPlainObject<T> |
|
|
129
|
+
export const toNonEmptyJSONObject = <T>(x: T): ExtractPlainObject<T> | undefined =>
|
|
130
|
+
isNonEmptyJSONObject(x) ? x : undefined;
|
|
128
131
|
|
|
129
132
|
type ExtractPlainObjectValue<T> = ExtractPlainObject<T>[keyof ExtractPlainObject<T>];
|
|
130
133
|
|
|
@@ -135,12 +138,12 @@ type ExtractPlainObjectValue<T> = ExtractPlainObject<T>[keyof ExtractPlainObject
|
|
|
135
138
|
export const toPlainObjectOf = <T, U extends ExtractPlainObjectValue<T>>(
|
|
136
139
|
x: T,
|
|
137
140
|
f: (v: ExtractPlainObjectValue<T>) => v is U
|
|
138
|
-
): Record<PropertyKey, U> |
|
|
141
|
+
): Record<PropertyKey, U> | undefined => {
|
|
139
142
|
if (isPlainObject(x)) {
|
|
140
143
|
let index = -1;
|
|
141
144
|
let props = Object.keys(x);
|
|
142
145
|
let length = props.length;
|
|
143
|
-
let result: Record<PropertyKey, U> |
|
|
146
|
+
let result: Record<PropertyKey, U> | undefined;
|
|
144
147
|
|
|
145
148
|
while (++index < length) {
|
|
146
149
|
let key = props[index] as keyof typeof x;
|
|
@@ -155,7 +158,7 @@ export const toPlainObjectOf = <T, U extends ExtractPlainObjectValue<T>>(
|
|
|
155
158
|
};
|
|
156
159
|
|
|
157
160
|
/** Filter props from object `x` whose values are `true`. */
|
|
158
|
-
export const toPlainObjectOfTrue = (x: unknown): Record<PropertyKey, true> |
|
|
161
|
+
export const toPlainObjectOfTrue = (x: unknown): Record<PropertyKey, true> | undefined => toPlainObjectOf(x, isTrue);
|
|
159
162
|
|
|
160
163
|
/**
|
|
161
164
|
* Returns `x` if `x` is string, otherwise returns `''` (empty string) if
|