@wopjs/cast 0.1.3 → 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 +7 -4
- package/dist/index.d.ts +7 -4
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/is-to-as.test.ts +238 -0
- package/src/is-to-as.ts +8 -3
package/dist/index.d.mts
CHANGED
|
@@ -36,13 +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
|
+
type ExtractArray<T> = T extends readonly unknown[] ? T : unknown[];
|
|
39
40
|
declare const isArray: (arg: any) => arg is any[];
|
|
40
41
|
/** Returns `x` if `x` is an array. */
|
|
41
|
-
declare const toArray: (x:
|
|
42
|
+
declare const toArray: <T>(x: T) => ExtractArray<T> | _;
|
|
43
|
+
/** Returns `true` if `x` is an array and has at least one element. */
|
|
44
|
+
declare const isNonEmptyArray: (x: any) => x is any[];
|
|
42
45
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
43
|
-
declare const toNonEmptyArray: <T>(x: T
|
|
46
|
+
declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | _;
|
|
44
47
|
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
45
|
-
declare const asArray: (x:
|
|
48
|
+
declare const asArray: <T>(x: T) => ExtractArray<T>;
|
|
46
49
|
interface PlainObject {
|
|
47
50
|
[key: PropertyKey]: unknown;
|
|
48
51
|
}
|
|
@@ -89,4 +92,4 @@ declare const returnsFalse: () => false;
|
|
|
89
92
|
declare const returnsTrue: () => true;
|
|
90
93
|
declare const returnsEmptyString: () => string;
|
|
91
94
|
|
|
92
|
-
export { type Falsy, type IsFalsy, type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, 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 };
|
|
95
|
+
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 };
|
package/dist/index.d.ts
CHANGED
|
@@ -36,13 +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
|
+
type ExtractArray<T> = T extends readonly unknown[] ? T : unknown[];
|
|
39
40
|
declare const isArray: (arg: any) => arg is any[];
|
|
40
41
|
/** Returns `x` if `x` is an array. */
|
|
41
|
-
declare const toArray: (x:
|
|
42
|
+
declare const toArray: <T>(x: T) => ExtractArray<T> | _;
|
|
43
|
+
/** Returns `true` if `x` is an array and has at least one element. */
|
|
44
|
+
declare const isNonEmptyArray: (x: any) => x is any[];
|
|
42
45
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
43
|
-
declare const toNonEmptyArray: <T>(x: T
|
|
46
|
+
declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | _;
|
|
44
47
|
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
45
|
-
declare const asArray: (x:
|
|
48
|
+
declare const asArray: <T>(x: T) => ExtractArray<T>;
|
|
46
49
|
interface PlainObject {
|
|
47
50
|
[key: PropertyKey]: unknown;
|
|
48
51
|
}
|
|
@@ -89,4 +92,4 @@ declare const returnsFalse: () => false;
|
|
|
89
92
|
declare const returnsTrue: () => true;
|
|
90
93
|
declare const returnsEmptyString: () => string;
|
|
91
94
|
|
|
92
|
-
export { type Falsy, type IsFalsy, type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, 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 };
|
|
95
|
+
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 };
|
package/dist/index.js
CHANGED
|
@@ -21,7 +21,8 @@ var isNonEmptyString = (x) => isString(x) && x !== "";
|
|
|
21
21
|
var toNonEmptyString = (x) => isNonEmptyString(x) ? x : _;
|
|
22
22
|
var isArray = Array.isArray;
|
|
23
23
|
var toArray = (x) => isArray(x) ? x : _;
|
|
24
|
-
var
|
|
24
|
+
var isNonEmptyArray = (x) => isArray(x) && x.length > 0;
|
|
25
|
+
var toNonEmptyArray = (x) => isNonEmptyArray(x) ? x : _;
|
|
25
26
|
var asArray = (x) => isArray(x) ? x : [];
|
|
26
27
|
var isObject = (x) => x !== null && typeof x === "object";
|
|
27
28
|
var asObject = (x) => isObject(x) ? x : {};
|
|
@@ -78,6 +79,7 @@ exports.isArray = isArray;
|
|
|
78
79
|
exports.isBoolean = isBoolean;
|
|
79
80
|
exports.isDefined = isDefined;
|
|
80
81
|
exports.isFalsy = isFalsy;
|
|
82
|
+
exports.isNonEmptyArray = isNonEmptyArray;
|
|
81
83
|
exports.isNonEmptyJSONObject = isNonEmptyJSONObject;
|
|
82
84
|
exports.isNonEmptyPlainObject = isNonEmptyPlainObject;
|
|
83
85
|
exports.isNonEmptyString = isNonEmptyString;
|
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,IAA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,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.js"}
|
package/dist/index.mjs
CHANGED
|
@@ -19,7 +19,8 @@ var isNonEmptyString = (x) => isString(x) && x !== "";
|
|
|
19
19
|
var toNonEmptyString = (x) => isNonEmptyString(x) ? x : _;
|
|
20
20
|
var isArray = Array.isArray;
|
|
21
21
|
var toArray = (x) => isArray(x) ? x : _;
|
|
22
|
-
var
|
|
22
|
+
var isNonEmptyArray = (x) => isArray(x) && x.length > 0;
|
|
23
|
+
var toNonEmptyArray = (x) => isNonEmptyArray(x) ? x : _;
|
|
23
24
|
var asArray = (x) => isArray(x) ? x : [];
|
|
24
25
|
var isObject = (x) => x !== null && typeof x === "object";
|
|
25
26
|
var asObject = (x) => isObject(x) ? x : {};
|
|
@@ -66,6 +67,6 @@ var returnsFalse = () => false;
|
|
|
66
67
|
var returnsTrue = () => true;
|
|
67
68
|
var returnsEmptyString = () => "";
|
|
68
69
|
|
|
69
|
-
export { asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, 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 };
|
|
70
|
+
export { 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 };
|
|
70
71
|
//# sourceMappingURL=index.mjs.map
|
|
71
72
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.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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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
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
print,
|
|
21
21
|
isArray,
|
|
22
22
|
toArray,
|
|
23
|
+
isNonEmptyArray,
|
|
23
24
|
asArray,
|
|
24
25
|
toNonEmptyArray,
|
|
25
26
|
isObject,
|
|
@@ -151,21 +152,258 @@ describe("primitive.ts", () => {
|
|
|
151
152
|
it("isArray", () => {
|
|
152
153
|
expect(isArray([])).toBe(true);
|
|
153
154
|
expect(isArray({})).toBe(false);
|
|
155
|
+
|
|
156
|
+
// Type narrowing - preserves array type
|
|
157
|
+
const stringArr: string[] = ["a", "b"];
|
|
158
|
+
if (isArray(stringArr)) {
|
|
159
|
+
const _check: string[] = stringArr;
|
|
160
|
+
expect(_check).toBe(stringArr);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Type narrowing - extracts array from union
|
|
164
|
+
const unionValue: string | string[] = ["a"];
|
|
165
|
+
if (isArray(unionValue)) {
|
|
166
|
+
const _check: string[] = unionValue;
|
|
167
|
+
expect(_check).toBe(unionValue);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Type narrowing - preserves readonly array
|
|
171
|
+
const readonlyArr: readonly number[] = [1, 2];
|
|
172
|
+
if (isArray(readonlyArr)) {
|
|
173
|
+
const _check: readonly number[] = readonlyArr;
|
|
174
|
+
expect(_check).toBe(readonlyArr);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Type narrowing - preserves tuple
|
|
178
|
+
const tuple: [string, number] = ["a", 1];
|
|
179
|
+
if (isArray(tuple)) {
|
|
180
|
+
const _check: [string, number] = tuple;
|
|
181
|
+
expect(_check).toBe(tuple);
|
|
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
|
+
}
|
|
154
197
|
});
|
|
155
198
|
|
|
156
199
|
it("toArray", () => {
|
|
157
200
|
expect(toArray([])).toEqual([]);
|
|
158
201
|
expect(toArray({})).toBe(undefined);
|
|
202
|
+
|
|
203
|
+
// Type narrowing - preserves array type
|
|
204
|
+
const stringArr: string[] = ["a", "b"];
|
|
205
|
+
const result1 = toArray(stringArr);
|
|
206
|
+
if (result1) {
|
|
207
|
+
const _check: string[] = result1;
|
|
208
|
+
expect(_check).toBe(stringArr);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Type narrowing - extracts array from union
|
|
212
|
+
const unionValue: string | string[] = ["a"];
|
|
213
|
+
const result2 = toArray(unionValue);
|
|
214
|
+
if (result2) {
|
|
215
|
+
const _check: string[] = result2;
|
|
216
|
+
expect(_check).toBe(unionValue);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Type narrowing - preserves readonly array
|
|
220
|
+
const readonlyArr: readonly number[] = [1, 2];
|
|
221
|
+
const result3 = toArray(readonlyArr);
|
|
222
|
+
if (result3) {
|
|
223
|
+
const _check: readonly number[] = result3;
|
|
224
|
+
expect(_check).toBe(readonlyArr);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Type narrowing - preserves tuple
|
|
228
|
+
const tuple: [string, number] = ["a", 1];
|
|
229
|
+
const result4 = toArray(tuple);
|
|
230
|
+
if (result4) {
|
|
231
|
+
const _check: [string, number] = result4;
|
|
232
|
+
expect(_check).toBe(tuple);
|
|
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);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("isNonEmptyArray", () => {
|
|
247
|
+
// Runtime behavior
|
|
248
|
+
expect(isNonEmptyArray([1])).toBe(true);
|
|
249
|
+
expect(isNonEmptyArray([1, 2, 3])).toBe(true);
|
|
250
|
+
expect(isNonEmptyArray([])).toBe(false);
|
|
251
|
+
expect(isNonEmptyArray({})).toBe(false);
|
|
252
|
+
expect(isNonEmptyArray(null)).toBe(false);
|
|
253
|
+
expect(isNonEmptyArray(undefined)).toBe(false);
|
|
254
|
+
expect(isNonEmptyArray("string")).toBe(false);
|
|
255
|
+
|
|
256
|
+
// Type narrowing - preserves array type
|
|
257
|
+
const stringArr: string[] = ["a", "b"];
|
|
258
|
+
if (isNonEmptyArray(stringArr)) {
|
|
259
|
+
const _check: string[] = stringArr;
|
|
260
|
+
expect(_check).toBe(stringArr);
|
|
261
|
+
} else {
|
|
262
|
+
const _check: never = stringArr;
|
|
263
|
+
throw new Error("Unreachable");
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Type narrowing - extracts array from union
|
|
267
|
+
const unionValue: string | string[] = ["a"];
|
|
268
|
+
if (isNonEmptyArray(unionValue)) {
|
|
269
|
+
const _check: string[] = unionValue;
|
|
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);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Type narrowing - preserves readonly array
|
|
287
|
+
const readonlyArr: readonly number[] = [1, 2];
|
|
288
|
+
if (isNonEmptyArray(readonlyArr)) {
|
|
289
|
+
const _check: readonly number[] = readonlyArr;
|
|
290
|
+
expect(_check).toBe(readonlyArr);
|
|
291
|
+
} else {
|
|
292
|
+
const _check: readonly number[] = readonlyArr;
|
|
293
|
+
throw new Error("Unreachable");
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Type narrowing - preserves tuple
|
|
297
|
+
const tuple: [string, number] = ["a", 1];
|
|
298
|
+
if (isNonEmptyArray(tuple)) {
|
|
299
|
+
const _check: [string, number] = tuple;
|
|
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");
|
|
324
|
+
}
|
|
159
325
|
});
|
|
160
326
|
|
|
161
327
|
it("asArray", () => {
|
|
162
328
|
expect(asArray([])).toEqual([]);
|
|
163
329
|
expect(asArray({})).toEqual([]);
|
|
330
|
+
|
|
331
|
+
// Type narrowing - preserves array type
|
|
332
|
+
const stringArr: string[] = ["a", "b"];
|
|
333
|
+
const result1: string[] = asArray(stringArr);
|
|
334
|
+
expect(result1).toBe(stringArr);
|
|
335
|
+
|
|
336
|
+
// Type narrowing - extracts array from union
|
|
337
|
+
const unionValue: string | string[] = ["a"];
|
|
338
|
+
const result2: string[] = asArray(unionValue);
|
|
339
|
+
expect(result2).toEqual(["a"]);
|
|
340
|
+
|
|
341
|
+
// Type narrowing - preserves readonly array
|
|
342
|
+
const readonlyArr: readonly number[] = [1, 2];
|
|
343
|
+
const result3: readonly number[] = asArray(readonlyArr);
|
|
344
|
+
expect(result3).toBe(readonlyArr);
|
|
345
|
+
|
|
346
|
+
// Type narrowing - preserves tuple
|
|
347
|
+
const tuple: [string, number] = ["a", 1];
|
|
348
|
+
const result4: [string, number] = asArray(tuple);
|
|
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([]);
|
|
164
360
|
});
|
|
165
361
|
|
|
166
362
|
it("toNonEmptyArray", () => {
|
|
167
363
|
expect(toNonEmptyArray([1])).toEqual([1]);
|
|
168
364
|
expect(toNonEmptyArray([])).toBe(undefined);
|
|
365
|
+
|
|
366
|
+
// Type narrowing - preserves array type
|
|
367
|
+
const stringArr: string[] = ["a", "b"];
|
|
368
|
+
const result1 = toNonEmptyArray(stringArr);
|
|
369
|
+
if (result1) {
|
|
370
|
+
const _check: string[] = result1;
|
|
371
|
+
expect(_check).toBe(stringArr);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// Type narrowing - extracts array from union
|
|
375
|
+
const unionValue: string | string[] = ["a"];
|
|
376
|
+
const result2 = toNonEmptyArray(unionValue);
|
|
377
|
+
if (result2) {
|
|
378
|
+
const _check: string[] = result2;
|
|
379
|
+
expect(_check).toBe(unionValue);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Type narrowing - preserves readonly array
|
|
383
|
+
const readonlyArr: readonly number[] = [1, 2];
|
|
384
|
+
const result3 = toNonEmptyArray(readonlyArr);
|
|
385
|
+
if (result3) {
|
|
386
|
+
const _check: readonly number[] = result3;
|
|
387
|
+
expect(_check).toBe(readonlyArr);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// Type narrowing - preserves tuple
|
|
391
|
+
const tuple: [string, number] = ["a", 1];
|
|
392
|
+
const result4 = toNonEmptyArray(tuple);
|
|
393
|
+
if (result4) {
|
|
394
|
+
const _check: [string, number] = result4;
|
|
395
|
+
expect(_check).toBe(tuple);
|
|
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);
|
|
169
407
|
});
|
|
170
408
|
|
|
171
409
|
it("isObject", () => {
|
package/src/is-to-as.ts
CHANGED
|
@@ -57,16 +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
|
+
type ExtractArray<T> = T extends readonly unknown[] ? T : unknown[];
|
|
61
|
+
|
|
60
62
|
export const isArray = Array.isArray;
|
|
61
63
|
|
|
62
64
|
/** Returns `x` if `x` is an array. */
|
|
63
|
-
export const toArray = (x:
|
|
65
|
+
export const toArray = <T>(x: T): ExtractArray<T> | _ => (isArray(x) ? (x as ExtractArray<T>) : _);
|
|
66
|
+
|
|
67
|
+
/** Returns `true` if `x` is an array and has at least one element. */
|
|
68
|
+
export const isNonEmptyArray = (x: any): x is any[] => isArray(x) && x.length > 0;
|
|
64
69
|
|
|
65
70
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
66
|
-
export const toNonEmptyArray = <T>(x: T
|
|
71
|
+
export const toNonEmptyArray = <T>(x: T): ExtractArray<T> | _ => (isNonEmptyArray(x) ? (x as ExtractArray<T>) : _);
|
|
67
72
|
|
|
68
73
|
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
69
|
-
export const asArray = (x:
|
|
74
|
+
export const asArray = <T>(x: T): ExtractArray<T> => (isArray(x) ? (x as ExtractArray<T>) : ([] as ExtractArray<T>));
|
|
70
75
|
|
|
71
76
|
export interface PlainObject {
|
|
72
77
|
[key: PropertyKey]: unknown;
|