@wopjs/cast 0.1.2 → 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 +9 -5
- package/dist/index.d.ts +9 -5
- 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 +8 -8
- package/src/is-to-as.test.ts +169 -0
- package/src/is-to-as.ts +11 -4
package/dist/index.d.mts
CHANGED
|
@@ -15,6 +15,8 @@ interface IsFalsy {
|
|
|
15
15
|
declare const isFalsy: IsFalsy;
|
|
16
16
|
/** Returns `true` if `Boolean(x)` is `true`. */
|
|
17
17
|
declare const isTruthy: <T>(x: T) => x is Exclude<T, Falsy>;
|
|
18
|
+
/** Returns `x` if `Boolean(x)` is `true`, otherwise returns `undefined`. */
|
|
19
|
+
declare const toTruthy: <T>(x: T) => Exclude<T, Falsy> | _;
|
|
18
20
|
declare const isBoolean: (x: unknown) => x is boolean;
|
|
19
21
|
/** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */
|
|
20
22
|
declare const toBoolean: (x: unknown) => boolean | _;
|
|
@@ -34,13 +36,15 @@ declare const asString: (x: unknown) => string;
|
|
|
34
36
|
declare const isNonEmptyString: (x: unknown) => x is string;
|
|
35
37
|
/** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
|
|
36
38
|
declare const toNonEmptyString: (x: unknown) => string | _;
|
|
37
|
-
declare const isArray: (
|
|
39
|
+
declare const isArray: <T>(x: T) => x is Extract<T, readonly unknown[]>;
|
|
38
40
|
/** Returns `x` if `x` is an array. */
|
|
39
|
-
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[]>;
|
|
40
44
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
41
|
-
declare const toNonEmptyArray: <T>(x: T
|
|
45
|
+
declare const toNonEmptyArray: <T>(x: T) => Extract<T, readonly unknown[]> | _;
|
|
42
46
|
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
43
|
-
declare const asArray: (x:
|
|
47
|
+
declare const asArray: <T>(x: T) => Extract<T, readonly unknown[]>;
|
|
44
48
|
interface PlainObject {
|
|
45
49
|
[key: PropertyKey]: unknown;
|
|
46
50
|
}
|
|
@@ -87,4 +91,4 @@ declare const returnsFalse: () => false;
|
|
|
87
91
|
declare const returnsTrue: () => true;
|
|
88
92
|
declare const returnsEmptyString: () => string;
|
|
89
93
|
|
|
90
|
-
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 };
|
|
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
|
@@ -15,6 +15,8 @@ interface IsFalsy {
|
|
|
15
15
|
declare const isFalsy: IsFalsy;
|
|
16
16
|
/** Returns `true` if `Boolean(x)` is `true`. */
|
|
17
17
|
declare const isTruthy: <T>(x: T) => x is Exclude<T, Falsy>;
|
|
18
|
+
/** Returns `x` if `Boolean(x)` is `true`, otherwise returns `undefined`. */
|
|
19
|
+
declare const toTruthy: <T>(x: T) => Exclude<T, Falsy> | _;
|
|
18
20
|
declare const isBoolean: (x: unknown) => x is boolean;
|
|
19
21
|
/** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */
|
|
20
22
|
declare const toBoolean: (x: unknown) => boolean | _;
|
|
@@ -34,13 +36,15 @@ declare const asString: (x: unknown) => string;
|
|
|
34
36
|
declare const isNonEmptyString: (x: unknown) => x is string;
|
|
35
37
|
/** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
|
|
36
38
|
declare const toNonEmptyString: (x: unknown) => string | _;
|
|
37
|
-
declare const isArray: (
|
|
39
|
+
declare const isArray: <T>(x: T) => x is Extract<T, readonly unknown[]>;
|
|
38
40
|
/** Returns `x` if `x` is an array. */
|
|
39
|
-
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[]>;
|
|
40
44
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
41
|
-
declare const toNonEmptyArray: <T>(x: T
|
|
45
|
+
declare const toNonEmptyArray: <T>(x: T) => Extract<T, readonly unknown[]> | _;
|
|
42
46
|
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
43
|
-
declare const asArray: (x:
|
|
47
|
+
declare const asArray: <T>(x: T) => Extract<T, readonly unknown[]>;
|
|
44
48
|
interface PlainObject {
|
|
45
49
|
[key: PropertyKey]: unknown;
|
|
46
50
|
}
|
|
@@ -87,4 +91,4 @@ declare const returnsFalse: () => false;
|
|
|
87
91
|
declare const returnsTrue: () => true;
|
|
88
92
|
declare const returnsEmptyString: () => string;
|
|
89
93
|
|
|
90
|
-
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 };
|
|
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
|
@@ -8,6 +8,7 @@ var toTrue = (x) => x === true ? true : _;
|
|
|
8
8
|
var asTrue = (x) => x === true ? x : false;
|
|
9
9
|
var isFalsy = (x) => !x;
|
|
10
10
|
var isTruthy = (x) => !!x;
|
|
11
|
+
var toTruthy = (x) => isTruthy(x) ? x : _;
|
|
11
12
|
var isBoolean = (x) => x === true || x === false;
|
|
12
13
|
var toBoolean = (x) => isBoolean(x) ? x : _;
|
|
13
14
|
var isNumber = (x) => typeof x === "number" && x === x;
|
|
@@ -20,7 +21,8 @@ var isNonEmptyString = (x) => isString(x) && x !== "";
|
|
|
20
21
|
var toNonEmptyString = (x) => isNonEmptyString(x) ? x : _;
|
|
21
22
|
var isArray = Array.isArray;
|
|
22
23
|
var toArray = (x) => isArray(x) ? x : _;
|
|
23
|
-
var
|
|
24
|
+
var isNonEmptyArray = (x) => isArray(x) && x.length > 0;
|
|
25
|
+
var toNonEmptyArray = (x) => isNonEmptyArray(x) ? x : _;
|
|
24
26
|
var asArray = (x) => isArray(x) ? x : [];
|
|
25
27
|
var isObject = (x) => x !== null && typeof x === "object";
|
|
26
28
|
var asObject = (x) => isObject(x) ? x : {};
|
|
@@ -77,6 +79,7 @@ exports.isArray = isArray;
|
|
|
77
79
|
exports.isBoolean = isBoolean;
|
|
78
80
|
exports.isDefined = isDefined;
|
|
79
81
|
exports.isFalsy = isFalsy;
|
|
82
|
+
exports.isNonEmptyArray = isNonEmptyArray;
|
|
80
83
|
exports.isNonEmptyJSONObject = isNonEmptyJSONObject;
|
|
81
84
|
exports.isNonEmptyPlainObject = isNonEmptyPlainObject;
|
|
82
85
|
exports.isNonEmptyString = isNonEmptyString;
|
|
@@ -105,5 +108,6 @@ exports.toPlainObjectOf = toPlainObjectOf;
|
|
|
105
108
|
exports.toPlainObjectOfTrue = toPlainObjectOfTrue;
|
|
106
109
|
exports.toString = toString;
|
|
107
110
|
exports.toTrue = toTrue;
|
|
111
|
+
exports.toTruthy = toTruthy;
|
|
108
112
|
//# sourceMappingURL=index.js.map
|
|
109
113
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/is-to-as.ts","../src/returns.ts"],"names":[],"mappings":";;;AAAA,IAAM,
|
|
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,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I,MAAA;AAEJ,IAAA,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.js"}
|
package/dist/index.mjs
CHANGED
|
@@ -6,6 +6,7 @@ var toTrue = (x) => x === true ? true : _;
|
|
|
6
6
|
var asTrue = (x) => x === true ? x : false;
|
|
7
7
|
var isFalsy = (x) => !x;
|
|
8
8
|
var isTruthy = (x) => !!x;
|
|
9
|
+
var toTruthy = (x) => isTruthy(x) ? x : _;
|
|
9
10
|
var isBoolean = (x) => x === true || x === false;
|
|
10
11
|
var toBoolean = (x) => isBoolean(x) ? x : _;
|
|
11
12
|
var isNumber = (x) => typeof x === "number" && x === x;
|
|
@@ -18,7 +19,8 @@ var isNonEmptyString = (x) => isString(x) && x !== "";
|
|
|
18
19
|
var toNonEmptyString = (x) => isNonEmptyString(x) ? x : _;
|
|
19
20
|
var isArray = Array.isArray;
|
|
20
21
|
var toArray = (x) => isArray(x) ? x : _;
|
|
21
|
-
var
|
|
22
|
+
var isNonEmptyArray = (x) => isArray(x) && x.length > 0;
|
|
23
|
+
var toNonEmptyArray = (x) => isNonEmptyArray(x) ? x : _;
|
|
22
24
|
var asArray = (x) => isArray(x) ? x : [];
|
|
23
25
|
var isObject = (x) => x !== null && typeof x === "object";
|
|
24
26
|
var asObject = (x) => isObject(x) ? x : {};
|
|
@@ -65,6 +67,6 @@ var returnsFalse = () => false;
|
|
|
65
67
|
var returnsTrue = () => true;
|
|
66
68
|
var returnsEmptyString = () => "";
|
|
67
69
|
|
|
68
|
-
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 };
|
|
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 };
|
|
69
71
|
//# sourceMappingURL=index.mjs.map
|
|
70
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,
|
|
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,IAAI,CAAA,GAAI;AAExE,IAAM,SAAA,GAAY,CAAC,CAAA,KAA6B,CAAA,KAAM,QAAQ,CAAA,KAAM;AAGpE,IAAM,YAAY,CAAC,CAAA,KAA6B,SAAA,CAAU,CAAC,IAAI,CAAA,GAAI;AAGnE,IAAM,WAAW,CAAC,CAAA,KAA4B,OAAO,CAAA,KAAM,YAAY,CAAA,KAAM;AAG7E,IAAM,WAAW,CAAC,CAAA,KAA4B,QAAA,CAAS,CAAC,IAAI,CAAA,GAAI;AAGhE,IAAM,WAAW,CAAC,CAAA,KAAwB,QAAA,CAAS,CAAC,IAAI,CAAA,GAAI;AAG5D,IAAM,QAAA,GAAW,CAAC,CAAA,KAA4B,OAAO,CAAA,KAAM;AAG3D,IAAM,WAAW,CAAC,CAAA,KAA4B,QAAA,CAAS,CAAC,IAAI,CAAA,GAAI;AAGhE,IAAM,WAAW,CAAC,CAAA,KAAw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C,IAAM,cAAc,MAAY;AAEhC,IAAM,qBAAqB,MAAc","file":"index.mjs"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wopjs/cast",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Filter types from unknown",
|
|
5
5
|
"repository": "wopjs/cast",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -41,21 +41,21 @@
|
|
|
41
41
|
"license": "MIT",
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@eslint/js": "^9.15.0",
|
|
44
|
-
"@types/node": "^
|
|
45
|
-
"@vitest/coverage-v8": "^
|
|
44
|
+
"@types/node": "^24.9.1",
|
|
45
|
+
"@vitest/coverage-v8": "^4.0.4",
|
|
46
46
|
"commit-and-tag-version": "^12.5.0",
|
|
47
47
|
"eslint": "^9.15.0",
|
|
48
|
-
"eslint-config-prettier": "^
|
|
49
|
-
"eslint-import-resolver-typescript": "^
|
|
48
|
+
"eslint-config-prettier": "^10.1.8",
|
|
49
|
+
"eslint-import-resolver-typescript": "^4.4.4",
|
|
50
50
|
"eslint-plugin-import": "^2.31.0",
|
|
51
51
|
"gzip-size": "^7.0.0",
|
|
52
52
|
"prettier": "^3.3.3",
|
|
53
|
-
"pretty-bytes": "^
|
|
53
|
+
"pretty-bytes": "^7.1.0",
|
|
54
54
|
"tsup": "^8.3.5",
|
|
55
|
-
"typedoc": "^0.
|
|
55
|
+
"typedoc": "^0.28.14",
|
|
56
56
|
"typescript": "^5.6.3",
|
|
57
57
|
"typescript-eslint": "^8.15.0",
|
|
58
|
-
"vitest": "^
|
|
58
|
+
"vitest": "^4.0.4",
|
|
59
59
|
"yoctocolors": "^2.1.1"
|
|
60
60
|
}
|
|
61
61
|
}
|
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,
|
|
@@ -33,6 +34,7 @@ import {
|
|
|
33
34
|
toNonEmptyJSONObject,
|
|
34
35
|
toPlainObjectOf,
|
|
35
36
|
toPlainObjectOfTrue,
|
|
37
|
+
toTruthy,
|
|
36
38
|
} from ".";
|
|
37
39
|
|
|
38
40
|
describe("primitive.ts", () => {
|
|
@@ -65,6 +67,22 @@ describe("primitive.ts", () => {
|
|
|
65
67
|
expect(isTruthy(1)).toBe(true);
|
|
66
68
|
});
|
|
67
69
|
|
|
70
|
+
it("toTruthy", () => {
|
|
71
|
+
const expectTruthy = (o: unknown) => expect(toTruthy(o)).toBe(o);
|
|
72
|
+
expectTruthy({});
|
|
73
|
+
expectTruthy([]);
|
|
74
|
+
expectTruthy("string");
|
|
75
|
+
expectTruthy(Symbol());
|
|
76
|
+
expectTruthy(1);
|
|
77
|
+
expectTruthy(true);
|
|
78
|
+
|
|
79
|
+
expect(toTruthy(false)).toBe(undefined);
|
|
80
|
+
expect(toTruthy(0)).toBe(undefined);
|
|
81
|
+
expect(toTruthy(NaN)).toBe(undefined);
|
|
82
|
+
expect(toTruthy(null)).toBe(undefined);
|
|
83
|
+
expect(toTruthy(undefined)).toBe(undefined);
|
|
84
|
+
});
|
|
85
|
+
|
|
68
86
|
it("isFalsy", () => {
|
|
69
87
|
expect(isFalsy(true)).toBe(false);
|
|
70
88
|
expect(isFalsy(false)).toBe(true);
|
|
@@ -134,21 +152,172 @@ describe("primitive.ts", () => {
|
|
|
134
152
|
it("isArray", () => {
|
|
135
153
|
expect(isArray([])).toBe(true);
|
|
136
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
|
+
}
|
|
137
183
|
});
|
|
138
184
|
|
|
139
185
|
it("toArray", () => {
|
|
140
186
|
expect(toArray([])).toEqual([]);
|
|
141
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
|
+
}
|
|
142
259
|
});
|
|
143
260
|
|
|
144
261
|
it("asArray", () => {
|
|
145
262
|
expect(asArray([])).toEqual([]);
|
|
146
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);
|
|
147
284
|
});
|
|
148
285
|
|
|
149
286
|
it("toNonEmptyArray", () => {
|
|
150
287
|
expect(toNonEmptyArray([1])).toEqual([1]);
|
|
151
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
|
+
}
|
|
152
321
|
});
|
|
153
322
|
|
|
154
323
|
it("isObject", () => {
|
package/src/is-to-as.ts
CHANGED
|
@@ -25,6 +25,9 @@ export const isFalsy: IsFalsy = (x: unknown): x is Falsy => !x;
|
|
|
25
25
|
/** Returns `true` if `Boolean(x)` is `true`. */
|
|
26
26
|
export const isTruthy = <T>(x: T): x is Exclude<T, Falsy> => !!x;
|
|
27
27
|
|
|
28
|
+
/** Returns `x` if `Boolean(x)` is `true`, otherwise returns `undefined`. */
|
|
29
|
+
export const toTruthy = <T>(x: T): Exclude<T, Falsy> | _ => (isTruthy(x) ? x : _);
|
|
30
|
+
|
|
28
31
|
export const isBoolean = (x: unknown): x is boolean => x === true || x === false;
|
|
29
32
|
|
|
30
33
|
/** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */
|
|
@@ -54,16 +57,20 @@ export const isNonEmptyString = (x: unknown): x is string => isString(x) && x !=
|
|
|
54
57
|
/** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
|
|
55
58
|
export const toNonEmptyString = (x: unknown): string | _ => (isNonEmptyString(x) ? x : _);
|
|
56
59
|
|
|
57
|
-
export const isArray = Array.isArray
|
|
60
|
+
export const isArray = Array.isArray as <T>(x: T) => x is Extract<T, readonly unknown[]>;
|
|
58
61
|
|
|
59
62
|
/** Returns `x` if `x` is an array. */
|
|
60
|
-
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;
|
|
61
67
|
|
|
62
68
|
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
63
|
-
export const toNonEmptyArray = <T>(x: T
|
|
69
|
+
export const toNonEmptyArray = <T>(x: T): Extract<T, readonly unknown[]> | _ => (isNonEmptyArray(x) ? x : _);
|
|
64
70
|
|
|
65
71
|
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
66
|
-
export const asArray = (x:
|
|
72
|
+
export const asArray = <T>(x: T): Extract<T, readonly unknown[]> =>
|
|
73
|
+
isArray(x) ? x : ([] as Extract<T, readonly unknown[]>);
|
|
67
74
|
|
|
68
75
|
export interface PlainObject {
|
|
69
76
|
[key: PropertyKey]: unknown;
|