@wopjs/cast 0.1.0 → 0.1.1
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 +19 -1
- package/dist/index.d.mts +8 -2
- package/dist/index.d.ts +8 -2
- package/dist/index.js +8 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/primitive.test.ts +152 -0
- package/src/primitive.ts +10 -1
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
[](https://coveralls.io/github/wopjs/cast)
|
|
7
7
|
[](https://bundlephobia.com/package/@wopjs/cast)
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Filter types from unknown.
|
|
10
10
|
|
|
11
11
|
## Install
|
|
12
12
|
|
|
@@ -14,6 +14,24 @@ cast
|
|
|
14
14
|
npm add @wopjs/cast
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
import * as c from "@wopjs/cast";
|
|
21
|
+
import { Option } from "@wopjs/tsur";
|
|
22
|
+
|
|
23
|
+
const dataParser = {
|
|
24
|
+
width: c.toNumber,
|
|
25
|
+
position: a => Option.from(a, a => a && c.isNumber(a.x) && c.isNumber(a.y)),
|
|
26
|
+
metadata: c.toNonEmptyPlainObject,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function parseData(data) {
|
|
30
|
+
const d = c.asObject(data);
|
|
31
|
+
return Object.fromEntries(Object.keys(dataParser).map(k => [k, Option.unwrapOr(dataParser[k](d[k]))]));
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
17
35
|
## Publish New Version
|
|
18
36
|
|
|
19
37
|
You can use [npm version](https://docs.npmjs.com/cli/v10/commands/npm-version) to bump version.
|
package/dist/index.d.mts
CHANGED
|
@@ -24,13 +24,19 @@ declare const asString: (x: unknown) => string;
|
|
|
24
24
|
* `x` is `null` or `undefined`, otherwise returns `JSON.stringify(x)`.
|
|
25
25
|
* This is very useful to show a value inside a React component.
|
|
26
26
|
*/
|
|
27
|
-
declare const
|
|
27
|
+
declare const print: (x: unknown) => string;
|
|
28
28
|
interface PlainObject {
|
|
29
29
|
[key: PropertyKey]: unknown;
|
|
30
30
|
}
|
|
31
31
|
/** Returns `true` if `x` is an object (including array) and not null. */
|
|
32
32
|
declare const isObject: (x: unknown) => x is object;
|
|
33
|
+
/** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */
|
|
34
|
+
declare const asObject: (x: unknown) => object;
|
|
33
35
|
declare const isArray: (arg: any) => arg is any[];
|
|
36
|
+
/** Returns `true` if `x` is an array. */
|
|
37
|
+
declare const toArray: (x: unknown) => unknown[] | undefined;
|
|
38
|
+
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
39
|
+
declare const asArray: (x: unknown) => unknown[];
|
|
34
40
|
/** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
|
|
35
41
|
declare const isPlainObject: (x: unknown) => x is PlainObject;
|
|
36
42
|
/** Returns `x` if `x` is a plain object. */
|
|
@@ -51,4 +57,4 @@ declare const toPlainObjectOfTrue: (x: unknown) => {
|
|
|
51
57
|
[key: PropertyKey]: true;
|
|
52
58
|
} | _;
|
|
53
59
|
|
|
54
|
-
export { type PlainObject, type _, asNumber, asPlainObject, asString, asTrue, isArray, isBoolean, isNumber, isObject, isPlainObject, isString, isTrue,
|
|
60
|
+
export { type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isNumber, isObject, isPlainObject, isString, isTrue, print, toArray, toBoolean, toNonEmptyPlainObject, toNumber, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue };
|
package/dist/index.d.ts
CHANGED
|
@@ -24,13 +24,19 @@ declare const asString: (x: unknown) => string;
|
|
|
24
24
|
* `x` is `null` or `undefined`, otherwise returns `JSON.stringify(x)`.
|
|
25
25
|
* This is very useful to show a value inside a React component.
|
|
26
26
|
*/
|
|
27
|
-
declare const
|
|
27
|
+
declare const print: (x: unknown) => string;
|
|
28
28
|
interface PlainObject {
|
|
29
29
|
[key: PropertyKey]: unknown;
|
|
30
30
|
}
|
|
31
31
|
/** Returns `true` if `x` is an object (including array) and not null. */
|
|
32
32
|
declare const isObject: (x: unknown) => x is object;
|
|
33
|
+
/** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */
|
|
34
|
+
declare const asObject: (x: unknown) => object;
|
|
33
35
|
declare const isArray: (arg: any) => arg is any[];
|
|
36
|
+
/** Returns `true` if `x` is an array. */
|
|
37
|
+
declare const toArray: (x: unknown) => unknown[] | undefined;
|
|
38
|
+
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
39
|
+
declare const asArray: (x: unknown) => unknown[];
|
|
34
40
|
/** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
|
|
35
41
|
declare const isPlainObject: (x: unknown) => x is PlainObject;
|
|
36
42
|
/** Returns `x` if `x` is a plain object. */
|
|
@@ -51,4 +57,4 @@ declare const toPlainObjectOfTrue: (x: unknown) => {
|
|
|
51
57
|
[key: PropertyKey]: true;
|
|
52
58
|
} | _;
|
|
53
59
|
|
|
54
|
-
export { type PlainObject, type _, asNumber, asPlainObject, asString, asTrue, isArray, isBoolean, isNumber, isObject, isPlainObject, isString, isTrue,
|
|
60
|
+
export { type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isNumber, isObject, isPlainObject, isString, isTrue, print, toArray, toBoolean, toNonEmptyPlainObject, toNumber, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue };
|
package/dist/index.js
CHANGED
|
@@ -13,7 +13,7 @@ var asNumber = (x) => isNumber(x) ? x : 0;
|
|
|
13
13
|
var isString = (x) => typeof x === "string";
|
|
14
14
|
var toString = (x) => isString(x) ? x : _;
|
|
15
15
|
var asString = (x) => isString(x) ? x : "";
|
|
16
|
-
var
|
|
16
|
+
var print = (x) => {
|
|
17
17
|
if (isString(x)) return x;
|
|
18
18
|
if (x == null) return "";
|
|
19
19
|
try {
|
|
@@ -23,7 +23,10 @@ var show = (x) => {
|
|
|
23
23
|
}
|
|
24
24
|
};
|
|
25
25
|
var isObject = (x) => x !== null && typeof x === "object";
|
|
26
|
+
var asObject = (x) => isObject(x) ? x : {};
|
|
26
27
|
var isArray = Array.isArray;
|
|
28
|
+
var toArray = (x) => isArray(x) ? x : _;
|
|
29
|
+
var asArray = (x) => isArray(x) ? x : [];
|
|
27
30
|
var isPlainObject = (x) => isObject(x) && !isArray(x);
|
|
28
31
|
var toPlainObject = (x) => isPlainObject(x) ? x : _;
|
|
29
32
|
var asPlainObject = (x) => isPlainObject(x) ? x : {};
|
|
@@ -46,7 +49,9 @@ var toPlainObjectOf = (x, f) => {
|
|
|
46
49
|
};
|
|
47
50
|
var toPlainObjectOfTrue = (x) => toPlainObjectOf(x, isTrue);
|
|
48
51
|
|
|
52
|
+
exports.asArray = asArray;
|
|
49
53
|
exports.asNumber = asNumber;
|
|
54
|
+
exports.asObject = asObject;
|
|
50
55
|
exports.asPlainObject = asPlainObject;
|
|
51
56
|
exports.asString = asString;
|
|
52
57
|
exports.asTrue = asTrue;
|
|
@@ -57,7 +62,8 @@ exports.isObject = isObject;
|
|
|
57
62
|
exports.isPlainObject = isPlainObject;
|
|
58
63
|
exports.isString = isString;
|
|
59
64
|
exports.isTrue = isTrue;
|
|
60
|
-
exports.
|
|
65
|
+
exports.print = print;
|
|
66
|
+
exports.toArray = toArray;
|
|
61
67
|
exports.toBoolean = toBoolean;
|
|
62
68
|
exports.toNonEmptyPlainObject = toNonEmptyPlainObject;
|
|
63
69
|
exports.toNumber = toNumber;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/primitive.ts"],"names":[],"mappings":";;;AAAA,IAAM,CAAI,GAAA,KAAA,CAAA;AAGG,IAAA,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,CAAA,CAAC,IAAI,CAAI,GAAA;AAG/E,IAAM,gBAAgB,CAAC,CAAA,KAA6B,cAAc,CAAC,CAAA,GAAI,IAAI;AAG3E,IAAM,qBAAwB,GAAA,CAAwB,CAC3D,KAAA,aAAA,CAAc,CAAC,CAAA,IAAK,MAAO,CAAA,IAAA,CAAK,CAAC,CAAA,CAAE,MAAS,GAAA,CAAA,GAAI,CAAI,GAAA;AAMzC,IAAA,eAAA,GAAkB,CAAI,CAAA,EAAY,CAA6D,KAAA;AAC1G,EAAI,IAAA,aAAA,CAAc,CAAC,CAAG,EAAA;AACpB,IAAA,IAAI,KAAQ,GAAA,CAAA,CAAA;AACZ,IAAI,IAAA,KAAA,GAAQ,MAAO,CAAA,IAAA,CAAK,CAAC,CAAA;AACzB,IAAA,IAAI,SAAS,KAAM,CAAA,MAAA;AACnB,IAAI,IAAA,MAAA;AAEJ,IAAO,OAAA,EAAE,QAAQ,MAAQ,EAAA;AACvB,MAAI,IAAA,GAAA,GAAM,MAAM,KAAK,CAAA;AACrB,MAAI,IAAA,KAAA,GAAQ,EAAE,GAAG,CAAA;AACjB,MAAI,IAAA,CAAA,CAAE,KAAK,CAAG,EAAA;AACZ,QAAA,CAAC,MAAW,KAAA,EAAI,EAAA,GAAG,CAAI,GAAA,KAAA;AAAA;AACzB;AAGF,IAAO,OAAA,MAAA;AAAA;AAEX;AAGO,IAAM,mBAAsB,GAAA,CAAC,CAAiD,KAAA,eAAA,CAAgB,GAAG,MAAM","file":"index.js"}
|
package/dist/index.mjs
CHANGED
|
@@ -11,7 +11,7 @@ var asNumber = (x) => isNumber(x) ? x : 0;
|
|
|
11
11
|
var isString = (x) => typeof x === "string";
|
|
12
12
|
var toString = (x) => isString(x) ? x : _;
|
|
13
13
|
var asString = (x) => isString(x) ? x : "";
|
|
14
|
-
var
|
|
14
|
+
var print = (x) => {
|
|
15
15
|
if (isString(x)) return x;
|
|
16
16
|
if (x == null) return "";
|
|
17
17
|
try {
|
|
@@ -21,7 +21,10 @@ var show = (x) => {
|
|
|
21
21
|
}
|
|
22
22
|
};
|
|
23
23
|
var isObject = (x) => x !== null && typeof x === "object";
|
|
24
|
+
var asObject = (x) => isObject(x) ? x : {};
|
|
24
25
|
var isArray = Array.isArray;
|
|
26
|
+
var toArray = (x) => isArray(x) ? x : _;
|
|
27
|
+
var asArray = (x) => isArray(x) ? x : [];
|
|
25
28
|
var isPlainObject = (x) => isObject(x) && !isArray(x);
|
|
26
29
|
var toPlainObject = (x) => isPlainObject(x) ? x : _;
|
|
27
30
|
var asPlainObject = (x) => isPlainObject(x) ? x : {};
|
|
@@ -44,6 +47,6 @@ var toPlainObjectOf = (x, f) => {
|
|
|
44
47
|
};
|
|
45
48
|
var toPlainObjectOfTrue = (x) => toPlainObjectOf(x, isTrue);
|
|
46
49
|
|
|
47
|
-
export { asNumber, asPlainObject, asString, asTrue, isArray, isBoolean, isNumber, isObject, isPlainObject, isString, isTrue,
|
|
50
|
+
export { asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isNumber, isObject, isPlainObject, isString, isTrue, print, toArray, toBoolean, toNonEmptyPlainObject, toNumber, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue };
|
|
48
51
|
//# sourceMappingURL=index.mjs.map
|
|
49
52
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/primitive.ts"],"names":[],"mappings":";AAAA,IAAM,CAAI,GAAA,KAAA,CAAA;AAGG,IAAA,MAAA,GAAS,CAAC,CAAA,KAA0B,CAAM,KAAA;AAGhD,IAAM,MAAS,GAAA,CAAC,CAA0B,KAAA,CAAA,KAAM,OAAO,IAAO,GAAA;AAG9D,IAAM,MAAS,GAAA,CAAC,CAAyB,KAAA,CAAA,KAAM,OAAO,CAAI,GAAA;AAE1D,IAAM,SAAY,GAAA,CAAC,CAA6B,KAAA,CAAA,KAAM,QAAQ,CAAM,KAA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AA,eAAA,GAAkB,CAAI,CAAA,EAAY,CAA6D,KAAA;AAC1G,EAAI,IAAA,aAAA,CAAc,CAAC,CAAG,EAAA;AACpB,IAAA,IAAI,KAAQ,GAAA,CAAA,CAAA;AACZ,IAAI,IAAA,KAAA,GAAQ,MAAO,CAAA,IAAA,CAAK,CAAC,CAAA;AACzB,IAAA,IAAI,SAAS,KAAM,CAAA,MAAA;AACnB,IAAI,IAAA,MAAA;AAEJ,IAAO,OAAA,EAAE,QAAQ,MAAQ,EAAA;AACvB,MAAI,IAAA,GAAA,GAAM,MAAM,KAAK,CAAA;AACrB,MAAI,IAAA,KAAA,GAAQ,EAAE,GAAG,CAAA;AACjB,MAAI,IAAA,CAAA,CAAE,KAAK,CAAG,EAAA;AACZ,QAAA,CAAC,MAAW,KAAA,EAAI,EAAA,GAAG,CAAI,GAAA,KAAA;AAAA;AACzB;AAGF,IAAO,OAAA,MAAA;AAAA;AAEX;AAGO,IAAM,mBAAsB,GAAA,CAAC,CAAiD,KAAA,eAAA,CAAgB,GAAG,MAAM","file":"index.mjs"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
isTrue,
|
|
5
|
+
toTrue,
|
|
6
|
+
asTrue,
|
|
7
|
+
isBoolean,
|
|
8
|
+
toBoolean,
|
|
9
|
+
isNumber,
|
|
10
|
+
toNumber,
|
|
11
|
+
asNumber,
|
|
12
|
+
isString,
|
|
13
|
+
toString,
|
|
14
|
+
asString,
|
|
15
|
+
print,
|
|
16
|
+
isObject,
|
|
17
|
+
isArray,
|
|
18
|
+
isPlainObject,
|
|
19
|
+
toPlainObject,
|
|
20
|
+
asPlainObject,
|
|
21
|
+
toNonEmptyPlainObject,
|
|
22
|
+
toPlainObjectOf,
|
|
23
|
+
toPlainObjectOfTrue,
|
|
24
|
+
} from ".";
|
|
25
|
+
|
|
26
|
+
describe("primitive.ts", () => {
|
|
27
|
+
it("isTrue", () => {
|
|
28
|
+
expect(isTrue(true)).toBe(true);
|
|
29
|
+
expect(isTrue(false)).toBe(false);
|
|
30
|
+
expect(isTrue(1)).toBe(false);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("toTrue", () => {
|
|
34
|
+
expect(toTrue(true)).toBe(true);
|
|
35
|
+
expect(toTrue(false)).toBe(undefined);
|
|
36
|
+
expect(toTrue(1)).toBe(undefined);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("asTrue", () => {
|
|
40
|
+
expect(asTrue(true)).toBe(true);
|
|
41
|
+
expect(asTrue(false)).toBe(false);
|
|
42
|
+
expect(asTrue(1)).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("isBoolean", () => {
|
|
46
|
+
expect(isBoolean(true)).toBe(true);
|
|
47
|
+
expect(isBoolean(false)).toBe(true);
|
|
48
|
+
expect(isBoolean(1)).toBe(false);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("toBoolean", () => {
|
|
52
|
+
expect(toBoolean(true)).toBe(true);
|
|
53
|
+
expect(toBoolean(false)).toBe(false);
|
|
54
|
+
expect(toBoolean(1)).toBe(undefined);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("isNumber", () => {
|
|
58
|
+
expect(isNumber(1)).toBe(true);
|
|
59
|
+
expect(isNumber(NaN)).toBe(false);
|
|
60
|
+
expect(isNumber("1")).toBe(false);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("toNumber", () => {
|
|
64
|
+
expect(toNumber(1)).toBe(1);
|
|
65
|
+
expect(toNumber(NaN)).toBe(undefined);
|
|
66
|
+
expect(toNumber("1")).toBe(undefined);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("asNumber", () => {
|
|
70
|
+
expect(asNumber(1)).toBe(1);
|
|
71
|
+
expect(asNumber(NaN)).toBe(0);
|
|
72
|
+
expect(asNumber("1")).toBe(0);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("isString", () => {
|
|
76
|
+
expect(isString("hello")).toBe(true);
|
|
77
|
+
expect(isString(1)).toBe(false);
|
|
78
|
+
expect(isString(true)).toBe(false);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("toString", () => {
|
|
82
|
+
expect(toString("hello")).toBe("hello");
|
|
83
|
+
expect(toString(1)).toBe(undefined);
|
|
84
|
+
expect(toString(true)).toBe(undefined);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("asString", () => {
|
|
88
|
+
expect(asString("hello")).toBe("hello");
|
|
89
|
+
expect(asString(1)).toBe("");
|
|
90
|
+
expect(asString(true)).toBe("");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("show", () => {
|
|
94
|
+
expect(print("hello")).toBe("hello");
|
|
95
|
+
expect(print(null)).toBe("");
|
|
96
|
+
expect(print({ a: 1 })).toBe(JSON.stringify({ a: 1 }, null, 2));
|
|
97
|
+
expect(
|
|
98
|
+
print({
|
|
99
|
+
toJSON() {
|
|
100
|
+
throw new Error("x");
|
|
101
|
+
},
|
|
102
|
+
toString() {
|
|
103
|
+
return "str";
|
|
104
|
+
},
|
|
105
|
+
})
|
|
106
|
+
).toBe("str");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("isObject", () => {
|
|
110
|
+
expect(isObject({})).toBe(true);
|
|
111
|
+
expect(isObject([])).toBe(true);
|
|
112
|
+
expect(isObject(null)).toBe(false);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("isArray", () => {
|
|
116
|
+
expect(isArray([])).toBe(true);
|
|
117
|
+
expect(isArray({})).toBe(false);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("isPlainObject", () => {
|
|
121
|
+
expect(isPlainObject({})).toBe(true);
|
|
122
|
+
expect(isPlainObject([])).toBe(false);
|
|
123
|
+
expect(isPlainObject(null)).toBe(false);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("toPlainObject", () => {
|
|
127
|
+
expect(toPlainObject({})).toEqual({});
|
|
128
|
+
expect(toPlainObject([])).toBe(undefined);
|
|
129
|
+
expect(toPlainObject(null)).toBe(undefined);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("asPlainObject", () => {
|
|
133
|
+
expect(asPlainObject({})).toEqual({});
|
|
134
|
+
expect(asPlainObject([])).toEqual({});
|
|
135
|
+
expect(asPlainObject(null)).toEqual({});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("toNonEmptyPlainObject", () => {
|
|
139
|
+
expect(toNonEmptyPlainObject({ a: 1 })).toEqual({ a: 1 });
|
|
140
|
+
expect(toNonEmptyPlainObject({})).toBe(undefined);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("toPlainObjectOf", () => {
|
|
144
|
+
expect(toPlainObjectOf({ a: true, b: false }, isTrue)).toEqual({ a: true });
|
|
145
|
+
expect(toPlainObjectOf({ a: 1, b: 2 }, isTrue)).toBe(undefined);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("toPlainObjectOfTrue", () => {
|
|
149
|
+
expect(toPlainObjectOfTrue({ a: true, b: false })).toEqual({ a: true });
|
|
150
|
+
expect(toPlainObjectOfTrue({ a: 1, b: 2 })).toBe(undefined);
|
|
151
|
+
});
|
|
152
|
+
});
|
package/src/primitive.ts
CHANGED
|
@@ -37,7 +37,7 @@ export const asString = (x: unknown): string => (isString(x) ? x : "");
|
|
|
37
37
|
* `x` is `null` or `undefined`, otherwise returns `JSON.stringify(x)`.
|
|
38
38
|
* This is very useful to show a value inside a React component.
|
|
39
39
|
*/
|
|
40
|
-
export const
|
|
40
|
+
export const print = (x: unknown): string => {
|
|
41
41
|
if (isString(x)) return x;
|
|
42
42
|
if (x == null) return "";
|
|
43
43
|
try {
|
|
@@ -55,8 +55,17 @@ export interface PlainObject {
|
|
|
55
55
|
/** Returns `true` if `x` is an object (including array) and not null. */
|
|
56
56
|
export const isObject = (x: unknown): x is object => x !== null && typeof x === "object";
|
|
57
57
|
|
|
58
|
+
/** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */
|
|
59
|
+
export const asObject = (x: unknown): object => (isObject(x) ? x : {});
|
|
60
|
+
|
|
58
61
|
export const isArray = Array.isArray;
|
|
59
62
|
|
|
63
|
+
/** Returns `true` if `x` is an array. */
|
|
64
|
+
export const toArray = (x: unknown): unknown[] | undefined => (isArray(x) ? x : _);
|
|
65
|
+
|
|
66
|
+
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
67
|
+
export const asArray = (x: unknown): unknown[] => (isArray(x) ? x : []);
|
|
68
|
+
|
|
60
69
|
/** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
|
|
61
70
|
export const isPlainObject = (x: unknown): x is PlainObject => isObject(x) && !isArray(x);
|
|
62
71
|
|