@wopjs/cast 0.1.6 → 0.1.8
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/README.md +152 -14
- package/dist/index.d.mts +30 -32
- package/dist/index.d.ts +30 -32
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +12 -5
- package/src/is-to-as.test.ts +720 -187
- package/src/is-to-as.ts +44 -33
package/src/is-to-as.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
const _ = undefined;
|
|
2
2
|
export type _ = undefined;
|
|
3
3
|
|
|
4
|
+
/** Map `never` to a different type */
|
|
5
|
+
export type MapNeverTo<T, U> = [T, U][T extends any ? 0 : 1];
|
|
6
|
+
|
|
4
7
|
/** Returns `true` if `x` is not `undefined`. */
|
|
5
8
|
export const isDefined = <T>(x: T | undefined): x is T => x !== _;
|
|
6
9
|
|
|
@@ -14,13 +17,13 @@ export const asTrue = (x: unknown): boolean => (x === true ? x : false);
|
|
|
14
17
|
|
|
15
18
|
export type Falsy = false | null | undefined | 0 | "";
|
|
16
19
|
|
|
17
|
-
export
|
|
18
|
-
(x: unknown): x is Falsy;
|
|
19
|
-
<T>(x: T): x is Extract<T, Falsy>;
|
|
20
|
-
}
|
|
20
|
+
export type ExtractFalsy<T> = MapNeverTo<Extract<T, Falsy>, Falsy>;
|
|
21
21
|
|
|
22
22
|
/** Returns `true` if `Boolean(x)` is `false`. */
|
|
23
|
-
export const isFalsy
|
|
23
|
+
export const isFalsy = <T>(x: T): x is ExtractFalsy<T> => !x;
|
|
24
|
+
|
|
25
|
+
/** Returns `x` if `Boolean(x)` is `false`, otherwise returns `undefined`. */
|
|
26
|
+
export const toFalsy = <T>(x: T): ExtractFalsy<T> | _ => (isFalsy(x) ? x : _);
|
|
24
27
|
|
|
25
28
|
/** Returns `true` if `Boolean(x)` is `true`. */
|
|
26
29
|
export const isTruthy = <T>(x: T): x is Exclude<T, Falsy> => !!x;
|
|
@@ -57,72 +60,80 @@ export const isNonEmptyString = (x: unknown): x is string => isString(x) && x !=
|
|
|
57
60
|
/** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
|
|
58
61
|
export const toNonEmptyString = (x: unknown): string | _ => (isNonEmptyString(x) ? x : _);
|
|
59
62
|
|
|
60
|
-
export type ExtractArray<T> = T
|
|
61
|
-
|
|
62
|
-
export interface IsArray {
|
|
63
|
-
(x: unknown): x is unknown[];
|
|
64
|
-
<T>(x: T): x is T extends readonly unknown[] ? T : never;
|
|
65
|
-
}
|
|
63
|
+
export type ExtractArray<T> = MapNeverTo<Extract<T, readonly unknown[]>, unknown[]>;
|
|
66
64
|
|
|
67
|
-
export const isArray
|
|
65
|
+
export const isArray = Array.isArray as <T>(x: T) => x is ExtractArray<T>;
|
|
68
66
|
|
|
69
67
|
/** Returns `x` if `x` is an array. */
|
|
70
|
-
export const toArray = <T>(x: T): ExtractArray<T> | _ => (isArray(x) ?
|
|
68
|
+
export const toArray = <T>(x: T): ExtractArray<T> | _ => (isArray(x) ? x : _);
|
|
69
|
+
|
|
70
|
+
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
71
|
+
export const asArray = <T>(x: T): ExtractArray<T> => (isArray(x) ? x : ([] as ExtractArray<T>));
|
|
71
72
|
|
|
72
73
|
/** Returns `true` if `x` is an array and has at least one element. */
|
|
73
|
-
export const isNonEmptyArray
|
|
74
|
+
export const isNonEmptyArray = <T>(x: T): x is ExtractArray<T> => isArray(x) && x.length > 0;
|
|
74
75
|
|
|
75
76
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
76
|
-
export const toNonEmptyArray = <T>(x: T): ExtractArray<T> | _ => (isNonEmptyArray(x) ?
|
|
77
|
+
export const toNonEmptyArray = <T>(x: T): ExtractArray<T> | _ => (isNonEmptyArray(x) ? x : _);
|
|
77
78
|
|
|
78
|
-
|
|
79
|
-
|
|
79
|
+
export type ExtractObject<T> = MapNeverTo<Extract<T, object>, object>;
|
|
80
|
+
|
|
81
|
+
/** Returns `true` if `x` is an object (including array) and not null. */
|
|
82
|
+
export const isObject = <T>(x: T): x is ExtractObject<T> => x !== null && typeof x === "object";
|
|
83
|
+
|
|
84
|
+
/** Returns `x` if `x` is an object (including array). */
|
|
85
|
+
export const toObject = <T>(x: T): ExtractObject<T> | _ => (isObject(x) ? x : _);
|
|
86
|
+
|
|
87
|
+
/** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */
|
|
88
|
+
export const asObject = <T>(x: T): ExtractObject<T> => (isObject(x) ? x : ({} as ExtractObject<T>));
|
|
80
89
|
|
|
81
90
|
export interface PlainObject {
|
|
82
91
|
[key: PropertyKey]: unknown;
|
|
83
92
|
}
|
|
84
93
|
|
|
85
|
-
|
|
86
|
-
export const isObject = (x: unknown): x is object => x !== null && typeof x === "object";
|
|
87
|
-
|
|
88
|
-
/** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */
|
|
89
|
-
export const asObject = (x: unknown): object => (isObject(x) ? x : {});
|
|
94
|
+
export type ExtractPlainObject<T> = MapNeverTo<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>;
|
|
90
95
|
|
|
91
96
|
/** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
|
|
92
|
-
export const isPlainObject = (x:
|
|
97
|
+
export const isPlainObject = <T>(x: T): x is ExtractPlainObject<T> => isObject(x) && !isArray(x);
|
|
93
98
|
|
|
94
99
|
/** Returns `x` if `x` is a plain object. */
|
|
95
|
-
export const toPlainObject = (x:
|
|
100
|
+
export const toPlainObject = <T>(x: T): ExtractPlainObject<T> | _ => (isPlainObject(x) ? x : _);
|
|
96
101
|
|
|
97
102
|
/** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */
|
|
98
|
-
export const asPlainObject = (x:
|
|
103
|
+
export const asPlainObject = <T>(x: T): ExtractPlainObject<T> => (isPlainObject(x) ? x : ({} as ExtractPlainObject<T>));
|
|
99
104
|
|
|
100
105
|
/** Returns `true` if `x` is a plain object and has at least one key. */
|
|
101
|
-
export const isNonEmptyPlainObject = (x:
|
|
106
|
+
export const isNonEmptyPlainObject = <T>(x: T): x is ExtractPlainObject<T> =>
|
|
107
|
+
isPlainObject(x) && Object.keys(x).length > 0;
|
|
102
108
|
|
|
103
109
|
/** Returns `x` if `x` is a plain object and has at least one key. */
|
|
104
|
-
export const toNonEmptyPlainObject = <T
|
|
110
|
+
export const toNonEmptyPlainObject = <T>(x: T): ExtractPlainObject<T> | _ => (isNonEmptyPlainObject(x) ? x : _);
|
|
105
111
|
|
|
106
112
|
/** Returns `true` if `x` is a plain object and has at least one key with non-undefined value. */
|
|
107
|
-
export const isNonEmptyJSONObject = (x:
|
|
113
|
+
export const isNonEmptyJSONObject = <T>(x: T): x is ExtractPlainObject<T> =>
|
|
108
114
|
isPlainObject(x) && Object.values(x).some(isDefined);
|
|
109
115
|
|
|
110
116
|
/** Returns `x` if `x` is a plain object and has at least one key with non-undefined value, otherwise returns `undefined`. */
|
|
111
|
-
export const toNonEmptyJSONObject = <T
|
|
117
|
+
export const toNonEmptyJSONObject = <T>(x: T): ExtractPlainObject<T> | _ => (isNonEmptyJSONObject(x) ? x : _);
|
|
118
|
+
|
|
119
|
+
type ExtractPlainObjectValue<T> = ExtractPlainObject<T>[keyof ExtractPlainObject<T>];
|
|
112
120
|
|
|
113
121
|
/**
|
|
114
122
|
* Creates an object from `x` with keys `k` if `f(x[k])` returns `true`.
|
|
115
123
|
* If `x` is not a plain object, or there's no passed props, returns `undefined`.
|
|
116
124
|
*/
|
|
117
|
-
export const toPlainObjectOf = <T
|
|
125
|
+
export const toPlainObjectOf = <T, U extends ExtractPlainObjectValue<T>>(
|
|
126
|
+
x: T,
|
|
127
|
+
f: (v: ExtractPlainObjectValue<T>) => v is U
|
|
128
|
+
): Record<PropertyKey, U> | _ => {
|
|
118
129
|
if (isPlainObject(x)) {
|
|
119
130
|
let index = -1;
|
|
120
131
|
let props = Object.keys(x);
|
|
121
132
|
let length = props.length;
|
|
122
|
-
let result:
|
|
133
|
+
let result: Record<PropertyKey, U> | _;
|
|
123
134
|
|
|
124
135
|
while (++index < length) {
|
|
125
|
-
let key = props[index];
|
|
136
|
+
let key = props[index] as keyof typeof x;
|
|
126
137
|
let value = x[key];
|
|
127
138
|
if (f(value)) {
|
|
128
139
|
(result ??= {})[key] = value;
|
|
@@ -134,7 +145,7 @@ export const toPlainObjectOf = <T>(x: unknown, f: (v: unknown) => v is T): { [ke
|
|
|
134
145
|
};
|
|
135
146
|
|
|
136
147
|
/** Filter props from object `x` whose values are `true`. */
|
|
137
|
-
export const toPlainObjectOfTrue = (x: unknown):
|
|
148
|
+
export const toPlainObjectOfTrue = (x: unknown): Record<PropertyKey, true> | _ => toPlainObjectOf(x, isTrue);
|
|
138
149
|
|
|
139
150
|
/**
|
|
140
151
|
* Returns `x` if `x` is string, otherwise returns `''` (empty string) if
|