@wopjs/cast 0.1.0

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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 wopjs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # @wopjs/cast
2
+
3
+ [![Docs](https://img.shields.io/badge/Docs-read-%23fdf9f5)](https://wopjs.github.io/cast)
4
+ [![Build Status](https://github.com/wopjs/cast/actions/workflows/build.yml/badge.svg)](https://github.com/wopjs/cast/actions/workflows/build.yml)
5
+ [![npm-version](https://img.shields.io/npm/v/@wopjs/cast.svg)](https://www.npmjs.com/package/@wopjs/cast)
6
+ [![Coverage Status](https://img.shields.io/coverallsCoverage/github/wopjs/cast)](https://coveralls.io/github/wopjs/cast)
7
+ [![minified-size](https://img.shields.io/bundlephobia/minzip/@wopjs/cast)](https://bundlephobia.com/package/@wopjs/cast)
8
+
9
+ cast
10
+
11
+ ## Install
12
+
13
+ ```
14
+ npm add @wopjs/cast
15
+ ```
16
+
17
+ ## Publish New Version
18
+
19
+ You can use [npm version](https://docs.npmjs.com/cli/v10/commands/npm-version) to bump version.
20
+
21
+ ```
22
+ npm version patch
23
+ ```
24
+
25
+ Push the tag to remote and CI will publish the new version to npm.
26
+
27
+ ```
28
+ git push --follow-tags
29
+ ```
30
+
31
+ ### CI Publish
32
+
33
+ If you want to publish the package in CI, you need to set the `NPM_TOKEN` secrets [in GitHub repository settings](https://docs.github.com/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). See how to [create a NPM access token](https://docs.npmjs.com/creating-and-viewing-access-tokens).
@@ -0,0 +1,54 @@
1
+ type _ = undefined;
2
+ declare const isTrue: (x: unknown) => x is true;
3
+ /** Returns `true` if `x` is `true`, otherwise returns `undefined`. */
4
+ declare const toTrue: (x: unknown) => true | _;
5
+ /** Returns `true` if `x` is `true`, otherwise returns `false`. */
6
+ declare const asTrue: (x: unknown) => boolean;
7
+ declare const isBoolean: (x: unknown) => x is boolean;
8
+ /** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */
9
+ declare const toBoolean: (x: unknown) => boolean | _;
10
+ /** Returns `true` if `x` is a number, returns `false` if `x` is `NaN` or other value. */
11
+ declare const isNumber: (x: unknown) => x is number;
12
+ /** Returns `x` if `x` is a number, `NaN` and other value will be coerced to `undefined`. */
13
+ declare const toNumber: (x: unknown) => number | _;
14
+ /** Returns `x` if `x` is a number, `NaN` and other value will be coerced to `0`. */
15
+ declare const asNumber: (x: unknown) => number;
16
+ /** Returns `true` if `x` is a string. */
17
+ declare const isString: (x: unknown) => x is string;
18
+ /** Returns `x` if `x` is a string, otherwise returns `undefined`. */
19
+ declare const toString: (x: unknown) => string | _;
20
+ /** Returns `x` if `x` is a string, otherwise returns `''` (empty string). */
21
+ declare const asString: (x: unknown) => string;
22
+ /**
23
+ * Returns `x` if `x` is string, otherwise returns `''` (empty string) if
24
+ * `x` is `null` or `undefined`, otherwise returns `JSON.stringify(x)`.
25
+ * This is very useful to show a value inside a React component.
26
+ */
27
+ declare const show: (x: unknown) => string;
28
+ interface PlainObject {
29
+ [key: PropertyKey]: unknown;
30
+ }
31
+ /** Returns `true` if `x` is an object (including array) and not null. */
32
+ declare const isObject: (x: unknown) => x is object;
33
+ declare const isArray: (arg: any) => arg is any[];
34
+ /** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
35
+ declare const isPlainObject: (x: unknown) => x is PlainObject;
36
+ /** Returns `x` if `x` is a plain object. */
37
+ declare const toPlainObject: (x: unknown) => PlainObject | _;
38
+ /** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */
39
+ declare const asPlainObject: (x: unknown) => PlainObject;
40
+ /** Returns `x` if `x` is a plain object and has at least one key. */
41
+ declare const toNonEmptyPlainObject: <T extends PlainObject>(x: T) => T | _;
42
+ /**
43
+ * Creates an object from `x` with keys `k` if `f(x[k])` returns `true`.
44
+ * If `x` is not a plain object, or there's no passed props, returns `undefined`.
45
+ */
46
+ declare const toPlainObjectOf: <T>(x: unknown, f: (v: unknown) => v is T) => {
47
+ [key: PropertyKey]: T;
48
+ } | _;
49
+ /** Filter props from object `x` whose values are `true`. */
50
+ declare const toPlainObjectOfTrue: (x: unknown) => {
51
+ [key: PropertyKey]: true;
52
+ } | _;
53
+
54
+ export { type PlainObject, type _, asNumber, asPlainObject, asString, asTrue, isArray, isBoolean, isNumber, isObject, isPlainObject, isString, isTrue, show, toBoolean, toNonEmptyPlainObject, toNumber, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue };
@@ -0,0 +1,54 @@
1
+ type _ = undefined;
2
+ declare const isTrue: (x: unknown) => x is true;
3
+ /** Returns `true` if `x` is `true`, otherwise returns `undefined`. */
4
+ declare const toTrue: (x: unknown) => true | _;
5
+ /** Returns `true` if `x` is `true`, otherwise returns `false`. */
6
+ declare const asTrue: (x: unknown) => boolean;
7
+ declare const isBoolean: (x: unknown) => x is boolean;
8
+ /** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */
9
+ declare const toBoolean: (x: unknown) => boolean | _;
10
+ /** Returns `true` if `x` is a number, returns `false` if `x` is `NaN` or other value. */
11
+ declare const isNumber: (x: unknown) => x is number;
12
+ /** Returns `x` if `x` is a number, `NaN` and other value will be coerced to `undefined`. */
13
+ declare const toNumber: (x: unknown) => number | _;
14
+ /** Returns `x` if `x` is a number, `NaN` and other value will be coerced to `0`. */
15
+ declare const asNumber: (x: unknown) => number;
16
+ /** Returns `true` if `x` is a string. */
17
+ declare const isString: (x: unknown) => x is string;
18
+ /** Returns `x` if `x` is a string, otherwise returns `undefined`. */
19
+ declare const toString: (x: unknown) => string | _;
20
+ /** Returns `x` if `x` is a string, otherwise returns `''` (empty string). */
21
+ declare const asString: (x: unknown) => string;
22
+ /**
23
+ * Returns `x` if `x` is string, otherwise returns `''` (empty string) if
24
+ * `x` is `null` or `undefined`, otherwise returns `JSON.stringify(x)`.
25
+ * This is very useful to show a value inside a React component.
26
+ */
27
+ declare const show: (x: unknown) => string;
28
+ interface PlainObject {
29
+ [key: PropertyKey]: unknown;
30
+ }
31
+ /** Returns `true` if `x` is an object (including array) and not null. */
32
+ declare const isObject: (x: unknown) => x is object;
33
+ declare const isArray: (arg: any) => arg is any[];
34
+ /** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
35
+ declare const isPlainObject: (x: unknown) => x is PlainObject;
36
+ /** Returns `x` if `x` is a plain object. */
37
+ declare const toPlainObject: (x: unknown) => PlainObject | _;
38
+ /** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */
39
+ declare const asPlainObject: (x: unknown) => PlainObject;
40
+ /** Returns `x` if `x` is a plain object and has at least one key. */
41
+ declare const toNonEmptyPlainObject: <T extends PlainObject>(x: T) => T | _;
42
+ /**
43
+ * Creates an object from `x` with keys `k` if `f(x[k])` returns `true`.
44
+ * If `x` is not a plain object, or there's no passed props, returns `undefined`.
45
+ */
46
+ declare const toPlainObjectOf: <T>(x: unknown, f: (v: unknown) => v is T) => {
47
+ [key: PropertyKey]: T;
48
+ } | _;
49
+ /** Filter props from object `x` whose values are `true`. */
50
+ declare const toPlainObjectOfTrue: (x: unknown) => {
51
+ [key: PropertyKey]: true;
52
+ } | _;
53
+
54
+ export { type PlainObject, type _, asNumber, asPlainObject, asString, asTrue, isArray, isBoolean, isNumber, isObject, isPlainObject, isString, isTrue, show, toBoolean, toNonEmptyPlainObject, toNumber, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue };
package/dist/index.js ADDED
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ // src/primitive.ts
4
+ var _ = void 0;
5
+ var isTrue = (x) => x === true;
6
+ var toTrue = (x) => x === true ? true : _;
7
+ var asTrue = (x) => x === true ? x : false;
8
+ var isBoolean = (x) => x === true || x === false;
9
+ var toBoolean = (x) => isBoolean(x) ? x : _;
10
+ var isNumber = (x) => typeof x === "number" && x === x;
11
+ var toNumber = (x) => isNumber(x) ? x : _;
12
+ var asNumber = (x) => isNumber(x) ? x : 0;
13
+ var isString = (x) => typeof x === "string";
14
+ var toString = (x) => isString(x) ? x : _;
15
+ var asString = (x) => isString(x) ? x : "";
16
+ var show = (x) => {
17
+ if (isString(x)) return x;
18
+ if (x == null) return "";
19
+ try {
20
+ return JSON.stringify(x, null, 2);
21
+ } catch {
22
+ return x + "";
23
+ }
24
+ };
25
+ var isObject = (x) => x !== null && typeof x === "object";
26
+ var isArray = Array.isArray;
27
+ var isPlainObject = (x) => isObject(x) && !isArray(x);
28
+ var toPlainObject = (x) => isPlainObject(x) ? x : _;
29
+ var asPlainObject = (x) => isPlainObject(x) ? x : {};
30
+ var toNonEmptyPlainObject = (x) => isPlainObject(x) && Object.keys(x).length > 0 ? x : _;
31
+ var toPlainObjectOf = (x, f) => {
32
+ if (isPlainObject(x)) {
33
+ let index = -1;
34
+ let props = Object.keys(x);
35
+ let length = props.length;
36
+ let result;
37
+ while (++index < length) {
38
+ let key = props[index];
39
+ let value = x[key];
40
+ if (f(value)) {
41
+ (result ??= {})[key] = value;
42
+ }
43
+ }
44
+ return result;
45
+ }
46
+ };
47
+ var toPlainObjectOfTrue = (x) => toPlainObjectOf(x, isTrue);
48
+
49
+ exports.asNumber = asNumber;
50
+ exports.asPlainObject = asPlainObject;
51
+ exports.asString = asString;
52
+ exports.asTrue = asTrue;
53
+ exports.isArray = isArray;
54
+ exports.isBoolean = isBoolean;
55
+ exports.isNumber = isNumber;
56
+ exports.isObject = isObject;
57
+ exports.isPlainObject = isPlainObject;
58
+ exports.isString = isString;
59
+ exports.isTrue = isTrue;
60
+ exports.show = show;
61
+ exports.toBoolean = toBoolean;
62
+ exports.toNonEmptyPlainObject = toNonEmptyPlainObject;
63
+ exports.toNumber = toNumber;
64
+ exports.toPlainObject = toPlainObject;
65
+ exports.toPlainObjectOf = toPlainObjectOf;
66
+ exports.toPlainObjectOfTrue = toPlainObjectOfTrue;
67
+ exports.toString = toString;
68
+ exports.toTrue = toTrue;
69
+ //# sourceMappingURL=index.js.map
70
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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,KAAA;AAGpE,IAAM,YAAY,CAAC,CAAA,KAA6B,SAAU,CAAA,CAAC,IAAI,CAAI,GAAA;AAGnE,IAAM,WAAW,CAAC,CA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AI,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 ADDED
@@ -0,0 +1,49 @@
1
+ // src/primitive.ts
2
+ var _ = void 0;
3
+ var isTrue = (x) => x === true;
4
+ var toTrue = (x) => x === true ? true : _;
5
+ var asTrue = (x) => x === true ? x : false;
6
+ var isBoolean = (x) => x === true || x === false;
7
+ var toBoolean = (x) => isBoolean(x) ? x : _;
8
+ var isNumber = (x) => typeof x === "number" && x === x;
9
+ var toNumber = (x) => isNumber(x) ? x : _;
10
+ var asNumber = (x) => isNumber(x) ? x : 0;
11
+ var isString = (x) => typeof x === "string";
12
+ var toString = (x) => isString(x) ? x : _;
13
+ var asString = (x) => isString(x) ? x : "";
14
+ var show = (x) => {
15
+ if (isString(x)) return x;
16
+ if (x == null) return "";
17
+ try {
18
+ return JSON.stringify(x, null, 2);
19
+ } catch {
20
+ return x + "";
21
+ }
22
+ };
23
+ var isObject = (x) => x !== null && typeof x === "object";
24
+ var isArray = Array.isArray;
25
+ var isPlainObject = (x) => isObject(x) && !isArray(x);
26
+ var toPlainObject = (x) => isPlainObject(x) ? x : _;
27
+ var asPlainObject = (x) => isPlainObject(x) ? x : {};
28
+ var toNonEmptyPlainObject = (x) => isPlainObject(x) && Object.keys(x).length > 0 ? x : _;
29
+ var toPlainObjectOf = (x, f) => {
30
+ if (isPlainObject(x)) {
31
+ let index = -1;
32
+ let props = Object.keys(x);
33
+ let length = props.length;
34
+ let result;
35
+ while (++index < length) {
36
+ let key = props[index];
37
+ let value = x[key];
38
+ if (f(value)) {
39
+ (result ??= {})[key] = value;
40
+ }
41
+ }
42
+ return result;
43
+ }
44
+ };
45
+ var toPlainObjectOfTrue = (x) => toPlainObjectOf(x, isTrue);
46
+
47
+ export { asNumber, asPlainObject, asString, asTrue, isArray, isBoolean, isNumber, isObject, isPlainObject, isString, isTrue, show, toBoolean, toNonEmptyPlainObject, toNumber, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue };
48
+ //# sourceMappingURL=index.mjs.map
49
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +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,KAAA;AAGpE,IAAM,YAAY,CAAC,CAAA,KAA6B,SAAU,CAAA,CAAC,IAAI,CAAI,GAAA;AAGnE,IAAM,WAAW,CAAC,CAAA,KAA4B,OAAO,CAAA,KAAM,YAAY,CAAM,KAAA;AAG7E,IAAM,WAAW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A;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 ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@wopjs/cast",
3
+ "version": "0.1.0",
4
+ "description": "Filter types from unknown",
5
+ "repository": "wopjs/cast",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.mjs",
8
+ "types": "./dist/index.d.ts",
9
+ "sideEffects": false,
10
+ "files": [
11
+ "src",
12
+ "dist"
13
+ ],
14
+ "scripts": {
15
+ "prepublishOnly": "npm run build",
16
+ "lint": "eslint && prettier --check .",
17
+ "docs": "typedoc --options typedoc.json",
18
+ "test": "vitest",
19
+ "test:coverage": "vitest --coverage --coverage.include=src/**",
20
+ "test:ci": "tsc --noEmit && vitest --coverage --coverage.reporter=lcov --coverage.include=src/**",
21
+ "build": "tsup",
22
+ "build:min": "MINIFY=true tsup && node scripts/gzip.mjs",
23
+ "release": "commit-and-tag-version"
24
+ },
25
+ "keywords": [
26
+ "typescript",
27
+ "coerce"
28
+ ],
29
+ "maintainers": [
30
+ {
31
+ "name": "CRIMX",
32
+ "email": "straybugs@gmail.com",
33
+ "url": "https://github.com/crimx/"
34
+ },
35
+ {
36
+ "name": "hyrious",
37
+ "email": "hyrious@outlook.com",
38
+ "url": "https://github.com/hyrious/"
39
+ }
40
+ ],
41
+ "license": "MIT",
42
+ "devDependencies": {
43
+ "@eslint/js": "^9.15.0",
44
+ "@types/node": "^22.9.0",
45
+ "@vitest/coverage-v8": "^2.1.5",
46
+ "commit-and-tag-version": "^12.5.0",
47
+ "eslint": "^9.15.0",
48
+ "eslint-config-prettier": "^9.1.0",
49
+ "eslint-import-resolver-typescript": "^3.6.3",
50
+ "eslint-plugin-import": "^2.31.0",
51
+ "gzip-size": "^7.0.0",
52
+ "prettier": "^3.3.3",
53
+ "pretty-bytes": "^6.1.1",
54
+ "tsup": "^8.3.5",
55
+ "typedoc": "^0.26.11",
56
+ "typescript": "^5.6.3",
57
+ "typescript-eslint": "^8.14.0",
58
+ "vitest": "^2.1.5",
59
+ "yoctocolors": "^2.1.1"
60
+ }
61
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./primitive";
@@ -0,0 +1,97 @@
1
+ const _ = undefined;
2
+ export type _ = undefined;
3
+
4
+ export const isTrue = (x: unknown): x is true => x === true;
5
+
6
+ /** Returns `true` if `x` is `true`, otherwise returns `undefined`. */
7
+ export const toTrue = (x: unknown): true | _ => (x === true ? true : _);
8
+
9
+ /** Returns `true` if `x` is `true`, otherwise returns `false`. */
10
+ export const asTrue = (x: unknown): boolean => (x === true ? x : false);
11
+
12
+ export const isBoolean = (x: unknown): x is boolean => x === true || x === false;
13
+
14
+ /** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */
15
+ export const toBoolean = (x: unknown): boolean | _ => (isBoolean(x) ? x : _);
16
+
17
+ /** Returns `true` if `x` is a number, returns `false` if `x` is `NaN` or other value. */
18
+ export const isNumber = (x: unknown): x is number => typeof x === "number" && x === x;
19
+
20
+ /** Returns `x` if `x` is a number, `NaN` and other value will be coerced to `undefined`. */
21
+ export const toNumber = (x: unknown): number | _ => (isNumber(x) ? x : _);
22
+
23
+ /** Returns `x` if `x` is a number, `NaN` and other value will be coerced to `0`. */
24
+ export const asNumber = (x: unknown): number => (isNumber(x) ? x : 0);
25
+
26
+ /** Returns `true` if `x` is a string. */
27
+ export const isString = (x: unknown): x is string => typeof x === "string";
28
+
29
+ /** Returns `x` if `x` is a string, otherwise returns `undefined`. */
30
+ export const toString = (x: unknown): string | _ => (isString(x) ? x : _);
31
+
32
+ /** Returns `x` if `x` is a string, otherwise returns `''` (empty string). */
33
+ export const asString = (x: unknown): string => (isString(x) ? x : "");
34
+
35
+ /**
36
+ * Returns `x` if `x` is string, otherwise returns `''` (empty string) if
37
+ * `x` is `null` or `undefined`, otherwise returns `JSON.stringify(x)`.
38
+ * This is very useful to show a value inside a React component.
39
+ */
40
+ export const show = (x: unknown): string => {
41
+ if (isString(x)) return x;
42
+ if (x == null) return "";
43
+ try {
44
+ return JSON.stringify(x, null, 2);
45
+ } catch {
46
+ // Insane case is not handled: x = { toString: () => { throw x } }
47
+ return x + "";
48
+ }
49
+ };
50
+
51
+ export interface PlainObject {
52
+ [key: PropertyKey]: unknown;
53
+ }
54
+
55
+ /** Returns `true` if `x` is an object (including array) and not null. */
56
+ export const isObject = (x: unknown): x is object => x !== null && typeof x === "object";
57
+
58
+ export const isArray = Array.isArray;
59
+
60
+ /** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
61
+ export const isPlainObject = (x: unknown): x is PlainObject => isObject(x) && !isArray(x);
62
+
63
+ /** Returns `x` if `x` is a plain object. */
64
+ export const toPlainObject = (x: unknown): PlainObject | _ => (isPlainObject(x) ? x : _);
65
+
66
+ /** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */
67
+ export const asPlainObject = (x: unknown): PlainObject => (isPlainObject(x) ? x : {});
68
+
69
+ /** Returns `x` if `x` is a plain object and has at least one key. */
70
+ export const toNonEmptyPlainObject = <T extends PlainObject>(x: T): T | _ =>
71
+ isPlainObject(x) && Object.keys(x).length > 0 ? x : _;
72
+
73
+ /**
74
+ * Creates an object from `x` with keys `k` if `f(x[k])` returns `true`.
75
+ * If `x` is not a plain object, or there's no passed props, returns `undefined`.
76
+ */
77
+ export const toPlainObjectOf = <T>(x: unknown, f: (v: unknown) => v is T): { [key: PropertyKey]: T } | _ => {
78
+ if (isPlainObject(x)) {
79
+ let index = -1;
80
+ let props = Object.keys(x);
81
+ let length = props.length;
82
+ let result: { [key: PropertyKey]: T } | _;
83
+
84
+ while (++index < length) {
85
+ let key = props[index];
86
+ let value = x[key];
87
+ if (f(value)) {
88
+ (result ??= {})[key] = value;
89
+ }
90
+ }
91
+
92
+ return result;
93
+ }
94
+ };
95
+
96
+ /** Filter props from object `x` whose values are `true`. */
97
+ export const toPlainObjectOfTrue = (x: unknown): { [key: PropertyKey]: true } | _ => toPlainObjectOf(x, isTrue);