@wopjs/cast 0.1.3 → 0.1.4
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 -5
- package/dist/index.d.ts +7 -5
- 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 +152 -0
- package/src/is-to-as.ts +8 -4
package/dist/index.d.mts
CHANGED
|
@@ -36,13 +36,15 @@ 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
|
-
declare const isArray: (
|
|
39
|
+
declare const isArray: <T>(x: T) => x is Extract<T, readonly unknown[]>;
|
|
40
40
|
/** Returns `x` if `x` is an array. */
|
|
41
|
-
declare const toArray: (x:
|
|
41
|
+
declare const toArray: <T>(x: T) => Extract<T, readonly unknown[]> | undefined;
|
|
42
|
+
/** Returns `true` if `x` is an array and has at least one element. */
|
|
43
|
+
declare const isNonEmptyArray: <T>(x: T) => x is Extract<T, readonly unknown[]>;
|
|
42
44
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
43
|
-
declare const toNonEmptyArray: <T>(x: T
|
|
45
|
+
declare const toNonEmptyArray: <T>(x: T) => Extract<T, readonly unknown[]> | _;
|
|
44
46
|
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
45
|
-
declare const asArray: (x:
|
|
47
|
+
declare const asArray: <T>(x: T) => Extract<T, readonly unknown[]>;
|
|
46
48
|
interface PlainObject {
|
|
47
49
|
[key: PropertyKey]: unknown;
|
|
48
50
|
}
|
|
@@ -89,4 +91,4 @@ declare const returnsFalse: () => false;
|
|
|
89
91
|
declare const returnsTrue: () => true;
|
|
90
92
|
declare const returnsEmptyString: () => string;
|
|
91
93
|
|
|
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 };
|
|
94
|
+
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,15 @@ 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
|
-
declare const isArray: (
|
|
39
|
+
declare const isArray: <T>(x: T) => x is Extract<T, readonly unknown[]>;
|
|
40
40
|
/** Returns `x` if `x` is an array. */
|
|
41
|
-
declare const toArray: (x:
|
|
41
|
+
declare const toArray: <T>(x: T) => Extract<T, readonly unknown[]> | undefined;
|
|
42
|
+
/** Returns `true` if `x` is an array and has at least one element. */
|
|
43
|
+
declare const isNonEmptyArray: <T>(x: T) => x is Extract<T, readonly unknown[]>;
|
|
42
44
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
43
|
-
declare const toNonEmptyArray: <T>(x: T
|
|
45
|
+
declare const toNonEmptyArray: <T>(x: T) => Extract<T, readonly unknown[]> | _;
|
|
44
46
|
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
45
|
-
declare const asArray: (x:
|
|
47
|
+
declare const asArray: <T>(x: T) => Extract<T, readonly unknown[]>;
|
|
46
48
|
interface PlainObject {
|
|
47
49
|
[key: PropertyKey]: unknown;
|
|
48
50
|
}
|
|
@@ -89,4 +91,4 @@ declare const returnsFalse: () => false;
|
|
|
89
91
|
declare const returnsTrue: () => true;
|
|
90
92
|
declare const returnsEmptyString: () => string;
|
|
91
93
|
|
|
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 };
|
|
94
|
+
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,C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,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;;;AClJO,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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AAA,OAAO,EAAE,QAAQ,MAAA,EAAQ;AACvB,MAAA,IAAI,GAAA,GAAM,MAAM,KAAK,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;;;AClJO,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,172 @@ 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
|
+
}
|
|
154
183
|
});
|
|
155
184
|
|
|
156
185
|
it("toArray", () => {
|
|
157
186
|
expect(toArray([])).toEqual([]);
|
|
158
187
|
expect(toArray({})).toBe(undefined);
|
|
188
|
+
|
|
189
|
+
// Type narrowing - preserves array type
|
|
190
|
+
const stringArr: string[] = ["a", "b"];
|
|
191
|
+
const result1 = toArray(stringArr);
|
|
192
|
+
if (result1) {
|
|
193
|
+
const _check: string[] = result1;
|
|
194
|
+
expect(_check).toBe(stringArr);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Type narrowing - extracts array from union
|
|
198
|
+
const unionValue: string | string[] = ["a"];
|
|
199
|
+
const result2 = toArray(unionValue);
|
|
200
|
+
if (result2) {
|
|
201
|
+
const _check: string[] = result2;
|
|
202
|
+
expect(_check).toBe(unionValue);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Type narrowing - preserves readonly array
|
|
206
|
+
const readonlyArr: readonly number[] = [1, 2];
|
|
207
|
+
const result3 = toArray(readonlyArr);
|
|
208
|
+
if (result3) {
|
|
209
|
+
const _check: readonly number[] = result3;
|
|
210
|
+
expect(_check).toBe(readonlyArr);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Type narrowing - preserves tuple
|
|
214
|
+
const tuple: [string, number] = ["a", 1];
|
|
215
|
+
const result4 = toArray(tuple);
|
|
216
|
+
if (result4) {
|
|
217
|
+
const _check: [string, number] = result4;
|
|
218
|
+
expect(_check).toBe(tuple);
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("isNonEmptyArray", () => {
|
|
223
|
+
// Runtime behavior
|
|
224
|
+
expect(isNonEmptyArray([1])).toBe(true);
|
|
225
|
+
expect(isNonEmptyArray([1, 2, 3])).toBe(true);
|
|
226
|
+
expect(isNonEmptyArray([])).toBe(false);
|
|
227
|
+
expect(isNonEmptyArray({})).toBe(false);
|
|
228
|
+
expect(isNonEmptyArray(null)).toBe(false);
|
|
229
|
+
expect(isNonEmptyArray(undefined)).toBe(false);
|
|
230
|
+
expect(isNonEmptyArray("string")).toBe(false);
|
|
231
|
+
|
|
232
|
+
// Type narrowing - preserves array type
|
|
233
|
+
const stringArr: string[] = ["a", "b"];
|
|
234
|
+
if (isNonEmptyArray(stringArr)) {
|
|
235
|
+
const _check: string[] = stringArr;
|
|
236
|
+
expect(_check).toBe(stringArr);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Type narrowing - extracts array from union
|
|
240
|
+
const unionValue: string | string[] = ["a"];
|
|
241
|
+
if (isNonEmptyArray(unionValue)) {
|
|
242
|
+
const _check: string[] = unionValue;
|
|
243
|
+
expect(_check).toBe(unionValue);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Type narrowing - preserves readonly array
|
|
247
|
+
const readonlyArr: readonly number[] = [1, 2];
|
|
248
|
+
if (isNonEmptyArray(readonlyArr)) {
|
|
249
|
+
const _check: readonly number[] = readonlyArr;
|
|
250
|
+
expect(_check).toBe(readonlyArr);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Type narrowing - preserves tuple
|
|
254
|
+
const tuple: [string, number] = ["a", 1];
|
|
255
|
+
if (isNonEmptyArray(tuple)) {
|
|
256
|
+
const _check: [string, number] = tuple;
|
|
257
|
+
expect(_check).toBe(tuple);
|
|
258
|
+
}
|
|
159
259
|
});
|
|
160
260
|
|
|
161
261
|
it("asArray", () => {
|
|
162
262
|
expect(asArray([])).toEqual([]);
|
|
163
263
|
expect(asArray({})).toEqual([]);
|
|
264
|
+
|
|
265
|
+
// Type narrowing - preserves array type
|
|
266
|
+
const stringArr: string[] = ["a", "b"];
|
|
267
|
+
const result1: string[] = asArray(stringArr);
|
|
268
|
+
expect(result1).toBe(stringArr);
|
|
269
|
+
|
|
270
|
+
// Type narrowing - extracts array from union
|
|
271
|
+
const unionValue: string | string[] = ["a"];
|
|
272
|
+
const result2: string[] = asArray(unionValue);
|
|
273
|
+
expect(result2).toEqual(["a"]);
|
|
274
|
+
|
|
275
|
+
// Type narrowing - preserves readonly array
|
|
276
|
+
const readonlyArr: readonly number[] = [1, 2];
|
|
277
|
+
const result3: readonly number[] = asArray(readonlyArr);
|
|
278
|
+
expect(result3).toBe(readonlyArr);
|
|
279
|
+
|
|
280
|
+
// Type narrowing - preserves tuple
|
|
281
|
+
const tuple: [string, number] = ["a", 1];
|
|
282
|
+
const result4: [string, number] = asArray(tuple);
|
|
283
|
+
expect(result4).toBe(tuple);
|
|
164
284
|
});
|
|
165
285
|
|
|
166
286
|
it("toNonEmptyArray", () => {
|
|
167
287
|
expect(toNonEmptyArray([1])).toEqual([1]);
|
|
168
288
|
expect(toNonEmptyArray([])).toBe(undefined);
|
|
289
|
+
|
|
290
|
+
// Type narrowing - preserves array type
|
|
291
|
+
const stringArr: string[] = ["a", "b"];
|
|
292
|
+
const result1 = toNonEmptyArray(stringArr);
|
|
293
|
+
if (result1) {
|
|
294
|
+
const _check: string[] = result1;
|
|
295
|
+
expect(_check).toBe(stringArr);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// Type narrowing - extracts array from union
|
|
299
|
+
const unionValue: string | string[] = ["a"];
|
|
300
|
+
const result2 = toNonEmptyArray(unionValue);
|
|
301
|
+
if (result2) {
|
|
302
|
+
const _check: string[] = result2;
|
|
303
|
+
expect(_check).toBe(unionValue);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Type narrowing - preserves readonly array
|
|
307
|
+
const readonlyArr: readonly number[] = [1, 2];
|
|
308
|
+
const result3 = toNonEmptyArray(readonlyArr);
|
|
309
|
+
if (result3) {
|
|
310
|
+
const _check: readonly number[] = result3;
|
|
311
|
+
expect(_check).toBe(readonlyArr);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Type narrowing - preserves tuple
|
|
315
|
+
const tuple: [string, number] = ["a", 1];
|
|
316
|
+
const result4 = toNonEmptyArray(tuple);
|
|
317
|
+
if (result4) {
|
|
318
|
+
const _check: [string, number] = result4;
|
|
319
|
+
expect(_check).toBe(tuple);
|
|
320
|
+
}
|
|
169
321
|
});
|
|
170
322
|
|
|
171
323
|
it("isObject", () => {
|
package/src/is-to-as.ts
CHANGED
|
@@ -57,16 +57,20 @@ 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
|
-
export const isArray = Array.isArray
|
|
60
|
+
export const isArray = Array.isArray as <T>(x: T) => x is Extract<T, readonly unknown[]>;
|
|
61
61
|
|
|
62
62
|
/** Returns `x` if `x` is an array. */
|
|
63
|
-
export const toArray = (x:
|
|
63
|
+
export const toArray = <T>(x: T): Extract<T, readonly unknown[]> | undefined => (isArray(x) ? x : _);
|
|
64
|
+
|
|
65
|
+
/** Returns `true` if `x` is an array and has at least one element. */
|
|
66
|
+
export const isNonEmptyArray = <T>(x: T): x is Extract<T, readonly unknown[]> => isArray(x) && x.length > 0;
|
|
64
67
|
|
|
65
68
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
66
|
-
export const toNonEmptyArray = <T>(x: T
|
|
69
|
+
export const toNonEmptyArray = <T>(x: T): Extract<T, readonly unknown[]> | _ => (isNonEmptyArray(x) ? x : _);
|
|
67
70
|
|
|
68
71
|
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
69
|
-
export const asArray = (x:
|
|
72
|
+
export const asArray = <T>(x: T): Extract<T, readonly unknown[]> =>
|
|
73
|
+
isArray(x) ? x : ([] as Extract<T, readonly unknown[]>);
|
|
70
74
|
|
|
71
75
|
export interface PlainObject {
|
|
72
76
|
[key: PropertyKey]: unknown;
|