@wopjs/cast 0.1.0 → 0.1.2
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 +44 -8
- package/dist/index.d.ts +44 -8
- package/dist/index.js +52 -13
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +34 -13
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +2 -1
- package/src/is-to-as.test.ts +231 -0
- package/src/{primitive.ts → is-to-as.ts} +61 -18
- package/src/returns.test.ts +29 -0
- package/src/returns.ts +11 -0
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
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
type _ = undefined;
|
|
2
|
+
/** Returns `true` if `x` is not `undefined`. */
|
|
3
|
+
declare const isDefined: <T>(x: T | undefined) => x is T;
|
|
2
4
|
declare const isTrue: (x: unknown) => x is true;
|
|
3
5
|
/** Returns `true` if `x` is `true`, otherwise returns `undefined`. */
|
|
4
6
|
declare const toTrue: (x: unknown) => true | _;
|
|
5
7
|
/** Returns `true` if `x` is `true`, otherwise returns `false`. */
|
|
6
8
|
declare const asTrue: (x: unknown) => boolean;
|
|
9
|
+
type Falsy = false | null | undefined | 0 | "";
|
|
10
|
+
interface IsFalsy {
|
|
11
|
+
(x: unknown): x is Falsy;
|
|
12
|
+
<T>(x: T): x is Extract<T, Falsy>;
|
|
13
|
+
}
|
|
14
|
+
/** Returns `true` if `Boolean(x)` is `false`. */
|
|
15
|
+
declare const isFalsy: IsFalsy;
|
|
16
|
+
/** Returns `true` if `Boolean(x)` is `true`. */
|
|
17
|
+
declare const isTruthy: <T>(x: T) => x is Exclude<T, Falsy>;
|
|
7
18
|
declare const isBoolean: (x: unknown) => x is boolean;
|
|
8
19
|
/** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */
|
|
9
20
|
declare const toBoolean: (x: unknown) => boolean | _;
|
|
@@ -19,26 +30,38 @@ declare const isString: (x: unknown) => x is string;
|
|
|
19
30
|
declare const toString: (x: unknown) => string | _;
|
|
20
31
|
/** Returns `x` if `x` is a string, otherwise returns `''` (empty string). */
|
|
21
32
|
declare const asString: (x: unknown) => string;
|
|
22
|
-
/**
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
33
|
+
/** Returns `true` if `x` is a string and not `''`. */
|
|
34
|
+
declare const isNonEmptyString: (x: unknown) => x is string;
|
|
35
|
+
/** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
|
|
36
|
+
declare const toNonEmptyString: (x: unknown) => string | _;
|
|
37
|
+
declare const isArray: (arg: any) => arg is any[];
|
|
38
|
+
/** Returns `x` if `x` is an array. */
|
|
39
|
+
declare const toArray: (x: unknown) => unknown[] | undefined;
|
|
40
|
+
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
41
|
+
declare const toNonEmptyArray: <T>(x: T[]) => T[] | _;
|
|
42
|
+
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
43
|
+
declare const asArray: (x: unknown) => unknown[];
|
|
28
44
|
interface PlainObject {
|
|
29
45
|
[key: PropertyKey]: unknown;
|
|
30
46
|
}
|
|
31
47
|
/** Returns `true` if `x` is an object (including array) and not null. */
|
|
32
48
|
declare const isObject: (x: unknown) => x is object;
|
|
33
|
-
|
|
49
|
+
/** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */
|
|
50
|
+
declare const asObject: (x: unknown) => object;
|
|
34
51
|
/** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
|
|
35
52
|
declare const isPlainObject: (x: unknown) => x is PlainObject;
|
|
36
53
|
/** Returns `x` if `x` is a plain object. */
|
|
37
54
|
declare const toPlainObject: (x: unknown) => PlainObject | _;
|
|
38
55
|
/** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */
|
|
39
56
|
declare const asPlainObject: (x: unknown) => PlainObject;
|
|
57
|
+
/** Returns `true` if `x` is a plain object and has at least one key. */
|
|
58
|
+
declare const isNonEmptyPlainObject: (x: unknown) => x is PlainObject;
|
|
40
59
|
/** Returns `x` if `x` is a plain object and has at least one key. */
|
|
41
60
|
declare const toNonEmptyPlainObject: <T extends PlainObject>(x: T) => T | _;
|
|
61
|
+
/** Returns `true` if `x` is a plain object and has at least one key with non-undefined value. */
|
|
62
|
+
declare const isNonEmptyJSONObject: (x: unknown) => x is PlainObject;
|
|
63
|
+
/** Returns `x` if `x` is a plain object and has at least one key with non-undefined value, otherwise returns `undefined`. */
|
|
64
|
+
declare const toNonEmptyJSONObject: <T extends PlainObject>(x: T) => T | _;
|
|
42
65
|
/**
|
|
43
66
|
* Creates an object from `x` with keys `k` if `f(x[k])` returns `true`.
|
|
44
67
|
* If `x` is not a plain object, or there's no passed props, returns `undefined`.
|
|
@@ -50,5 +73,18 @@ declare const toPlainObjectOf: <T>(x: unknown, f: (v: unknown) => v is T) => {
|
|
|
50
73
|
declare const toPlainObjectOfTrue: (x: unknown) => {
|
|
51
74
|
[key: PropertyKey]: true;
|
|
52
75
|
} | _;
|
|
76
|
+
/**
|
|
77
|
+
* Returns `x` if `x` is string, otherwise returns `''` (empty string) if
|
|
78
|
+
* `x` is `null` or `undefined`, otherwise returns `JSON.stringify(x)`.
|
|
79
|
+
* This is very useful to show a value inside a React component.
|
|
80
|
+
*/
|
|
81
|
+
declare const print: (x: unknown) => string;
|
|
82
|
+
|
|
83
|
+
declare const noop: () => void;
|
|
84
|
+
declare const returnsUndefined: () => undefined;
|
|
85
|
+
declare const returnsNull: () => null;
|
|
86
|
+
declare const returnsFalse: () => false;
|
|
87
|
+
declare const returnsTrue: () => true;
|
|
88
|
+
declare const returnsEmptyString: () => string;
|
|
53
89
|
|
|
54
|
-
export { type PlainObject, type _, asNumber, asPlainObject, asString, asTrue, isArray, isBoolean, isNumber, isObject, isPlainObject, isString, isTrue,
|
|
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 };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
type _ = undefined;
|
|
2
|
+
/** Returns `true` if `x` is not `undefined`. */
|
|
3
|
+
declare const isDefined: <T>(x: T | undefined) => x is T;
|
|
2
4
|
declare const isTrue: (x: unknown) => x is true;
|
|
3
5
|
/** Returns `true` if `x` is `true`, otherwise returns `undefined`. */
|
|
4
6
|
declare const toTrue: (x: unknown) => true | _;
|
|
5
7
|
/** Returns `true` if `x` is `true`, otherwise returns `false`. */
|
|
6
8
|
declare const asTrue: (x: unknown) => boolean;
|
|
9
|
+
type Falsy = false | null | undefined | 0 | "";
|
|
10
|
+
interface IsFalsy {
|
|
11
|
+
(x: unknown): x is Falsy;
|
|
12
|
+
<T>(x: T): x is Extract<T, Falsy>;
|
|
13
|
+
}
|
|
14
|
+
/** Returns `true` if `Boolean(x)` is `false`. */
|
|
15
|
+
declare const isFalsy: IsFalsy;
|
|
16
|
+
/** Returns `true` if `Boolean(x)` is `true`. */
|
|
17
|
+
declare const isTruthy: <T>(x: T) => x is Exclude<T, Falsy>;
|
|
7
18
|
declare const isBoolean: (x: unknown) => x is boolean;
|
|
8
19
|
/** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */
|
|
9
20
|
declare const toBoolean: (x: unknown) => boolean | _;
|
|
@@ -19,26 +30,38 @@ declare const isString: (x: unknown) => x is string;
|
|
|
19
30
|
declare const toString: (x: unknown) => string | _;
|
|
20
31
|
/** Returns `x` if `x` is a string, otherwise returns `''` (empty string). */
|
|
21
32
|
declare const asString: (x: unknown) => string;
|
|
22
|
-
/**
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
33
|
+
/** Returns `true` if `x` is a string and not `''`. */
|
|
34
|
+
declare const isNonEmptyString: (x: unknown) => x is string;
|
|
35
|
+
/** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
|
|
36
|
+
declare const toNonEmptyString: (x: unknown) => string | _;
|
|
37
|
+
declare const isArray: (arg: any) => arg is any[];
|
|
38
|
+
/** Returns `x` if `x` is an array. */
|
|
39
|
+
declare const toArray: (x: unknown) => unknown[] | undefined;
|
|
40
|
+
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
41
|
+
declare const toNonEmptyArray: <T>(x: T[]) => T[] | _;
|
|
42
|
+
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
43
|
+
declare const asArray: (x: unknown) => unknown[];
|
|
28
44
|
interface PlainObject {
|
|
29
45
|
[key: PropertyKey]: unknown;
|
|
30
46
|
}
|
|
31
47
|
/** Returns `true` if `x` is an object (including array) and not null. */
|
|
32
48
|
declare const isObject: (x: unknown) => x is object;
|
|
33
|
-
|
|
49
|
+
/** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */
|
|
50
|
+
declare const asObject: (x: unknown) => object;
|
|
34
51
|
/** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
|
|
35
52
|
declare const isPlainObject: (x: unknown) => x is PlainObject;
|
|
36
53
|
/** Returns `x` if `x` is a plain object. */
|
|
37
54
|
declare const toPlainObject: (x: unknown) => PlainObject | _;
|
|
38
55
|
/** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */
|
|
39
56
|
declare const asPlainObject: (x: unknown) => PlainObject;
|
|
57
|
+
/** Returns `true` if `x` is a plain object and has at least one key. */
|
|
58
|
+
declare const isNonEmptyPlainObject: (x: unknown) => x is PlainObject;
|
|
40
59
|
/** Returns `x` if `x` is a plain object and has at least one key. */
|
|
41
60
|
declare const toNonEmptyPlainObject: <T extends PlainObject>(x: T) => T | _;
|
|
61
|
+
/** Returns `true` if `x` is a plain object and has at least one key with non-undefined value. */
|
|
62
|
+
declare const isNonEmptyJSONObject: (x: unknown) => x is PlainObject;
|
|
63
|
+
/** Returns `x` if `x` is a plain object and has at least one key with non-undefined value, otherwise returns `undefined`. */
|
|
64
|
+
declare const toNonEmptyJSONObject: <T extends PlainObject>(x: T) => T | _;
|
|
42
65
|
/**
|
|
43
66
|
* Creates an object from `x` with keys `k` if `f(x[k])` returns `true`.
|
|
44
67
|
* If `x` is not a plain object, or there's no passed props, returns `undefined`.
|
|
@@ -50,5 +73,18 @@ declare const toPlainObjectOf: <T>(x: unknown, f: (v: unknown) => v is T) => {
|
|
|
50
73
|
declare const toPlainObjectOfTrue: (x: unknown) => {
|
|
51
74
|
[key: PropertyKey]: true;
|
|
52
75
|
} | _;
|
|
76
|
+
/**
|
|
77
|
+
* Returns `x` if `x` is string, otherwise returns `''` (empty string) if
|
|
78
|
+
* `x` is `null` or `undefined`, otherwise returns `JSON.stringify(x)`.
|
|
79
|
+
* This is very useful to show a value inside a React component.
|
|
80
|
+
*/
|
|
81
|
+
declare const print: (x: unknown) => string;
|
|
82
|
+
|
|
83
|
+
declare const noop: () => void;
|
|
84
|
+
declare const returnsUndefined: () => undefined;
|
|
85
|
+
declare const returnsNull: () => null;
|
|
86
|
+
declare const returnsFalse: () => false;
|
|
87
|
+
declare const returnsTrue: () => true;
|
|
88
|
+
declare const returnsEmptyString: () => string;
|
|
53
89
|
|
|
54
|
-
export { type PlainObject, type _, asNumber, asPlainObject, asString, asTrue, isArray, isBoolean, isNumber, isObject, isPlainObject, isString, isTrue,
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
// src/
|
|
3
|
+
// src/is-to-as.ts
|
|
4
4
|
var _ = void 0;
|
|
5
|
+
var isDefined = (x) => x !== _;
|
|
5
6
|
var isTrue = (x) => x === true;
|
|
6
7
|
var toTrue = (x) => x === true ? true : _;
|
|
7
8
|
var asTrue = (x) => x === true ? x : false;
|
|
9
|
+
var isFalsy = (x) => !x;
|
|
10
|
+
var isTruthy = (x) => !!x;
|
|
8
11
|
var isBoolean = (x) => x === true || x === false;
|
|
9
12
|
var toBoolean = (x) => isBoolean(x) ? x : _;
|
|
10
13
|
var isNumber = (x) => typeof x === "number" && x === x;
|
|
@@ -13,21 +16,21 @@ var asNumber = (x) => isNumber(x) ? x : 0;
|
|
|
13
16
|
var isString = (x) => typeof x === "string";
|
|
14
17
|
var toString = (x) => isString(x) ? x : _;
|
|
15
18
|
var asString = (x) => isString(x) ? x : "";
|
|
16
|
-
var
|
|
17
|
-
|
|
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";
|
|
19
|
+
var isNonEmptyString = (x) => isString(x) && x !== "";
|
|
20
|
+
var toNonEmptyString = (x) => isNonEmptyString(x) ? x : _;
|
|
26
21
|
var isArray = Array.isArray;
|
|
22
|
+
var toArray = (x) => isArray(x) ? x : _;
|
|
23
|
+
var toNonEmptyArray = (x) => x.length > 0 ? x : _;
|
|
24
|
+
var asArray = (x) => isArray(x) ? x : [];
|
|
25
|
+
var isObject = (x) => x !== null && typeof x === "object";
|
|
26
|
+
var asObject = (x) => isObject(x) ? x : {};
|
|
27
27
|
var isPlainObject = (x) => isObject(x) && !isArray(x);
|
|
28
28
|
var toPlainObject = (x) => isPlainObject(x) ? x : _;
|
|
29
29
|
var asPlainObject = (x) => isPlainObject(x) ? x : {};
|
|
30
|
-
var
|
|
30
|
+
var isNonEmptyPlainObject = (x) => isPlainObject(x) && Object.keys(x).length > 0;
|
|
31
|
+
var toNonEmptyPlainObject = (x) => isNonEmptyPlainObject(x) ? x : _;
|
|
32
|
+
var isNonEmptyJSONObject = (x) => isPlainObject(x) && Object.values(x).some(isDefined);
|
|
33
|
+
var toNonEmptyJSONObject = (x) => isNonEmptyJSONObject(x) ? x : _;
|
|
31
34
|
var toPlainObjectOf = (x, f) => {
|
|
32
35
|
if (isPlainObject(x)) {
|
|
33
36
|
let index = -1;
|
|
@@ -45,21 +48,57 @@ var toPlainObjectOf = (x, f) => {
|
|
|
45
48
|
}
|
|
46
49
|
};
|
|
47
50
|
var toPlainObjectOfTrue = (x) => toPlainObjectOf(x, isTrue);
|
|
51
|
+
var print = (x) => {
|
|
52
|
+
if (isString(x)) return x;
|
|
53
|
+
if (x == null) return "";
|
|
54
|
+
try {
|
|
55
|
+
return JSON.stringify(x, null, 2);
|
|
56
|
+
} catch {
|
|
57
|
+
return x + "";
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// src/returns.ts
|
|
62
|
+
var noop = () => {
|
|
63
|
+
};
|
|
64
|
+
var returnsUndefined = noop;
|
|
65
|
+
var returnsNull = () => null;
|
|
66
|
+
var returnsFalse = () => false;
|
|
67
|
+
var returnsTrue = () => true;
|
|
68
|
+
var returnsEmptyString = () => "";
|
|
48
69
|
|
|
70
|
+
exports.asArray = asArray;
|
|
49
71
|
exports.asNumber = asNumber;
|
|
72
|
+
exports.asObject = asObject;
|
|
50
73
|
exports.asPlainObject = asPlainObject;
|
|
51
74
|
exports.asString = asString;
|
|
52
75
|
exports.asTrue = asTrue;
|
|
53
76
|
exports.isArray = isArray;
|
|
54
77
|
exports.isBoolean = isBoolean;
|
|
78
|
+
exports.isDefined = isDefined;
|
|
79
|
+
exports.isFalsy = isFalsy;
|
|
80
|
+
exports.isNonEmptyJSONObject = isNonEmptyJSONObject;
|
|
81
|
+
exports.isNonEmptyPlainObject = isNonEmptyPlainObject;
|
|
82
|
+
exports.isNonEmptyString = isNonEmptyString;
|
|
55
83
|
exports.isNumber = isNumber;
|
|
56
84
|
exports.isObject = isObject;
|
|
57
85
|
exports.isPlainObject = isPlainObject;
|
|
58
86
|
exports.isString = isString;
|
|
59
87
|
exports.isTrue = isTrue;
|
|
60
|
-
exports.
|
|
88
|
+
exports.isTruthy = isTruthy;
|
|
89
|
+
exports.noop = noop;
|
|
90
|
+
exports.print = print;
|
|
91
|
+
exports.returnsEmptyString = returnsEmptyString;
|
|
92
|
+
exports.returnsFalse = returnsFalse;
|
|
93
|
+
exports.returnsNull = returnsNull;
|
|
94
|
+
exports.returnsTrue = returnsTrue;
|
|
95
|
+
exports.returnsUndefined = returnsUndefined;
|
|
96
|
+
exports.toArray = toArray;
|
|
61
97
|
exports.toBoolean = toBoolean;
|
|
98
|
+
exports.toNonEmptyArray = toNonEmptyArray;
|
|
99
|
+
exports.toNonEmptyJSONObject = toNonEmptyJSONObject;
|
|
62
100
|
exports.toNonEmptyPlainObject = toNonEmptyPlainObject;
|
|
101
|
+
exports.toNonEmptyString = toNonEmptyString;
|
|
63
102
|
exports.toNumber = toNumber;
|
|
64
103
|
exports.toPlainObject = toPlainObject;
|
|
65
104
|
exports.toPlainObjectOf = toPlainObjectOf;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/
|
|
1
|
+
{"version":3,"sources":["../src/is-to-as.ts","../src/returns.ts"],"names":[],"mappings":";;;AAAA,IAAM,CAAI,GAAA,KAAA,CAAA;AAIG,IAAA,SAAA,GAAY,CAAI,CAAA,KAA6B,CAAM,KAAA;AAEnD,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;AAUpD,IAAA,OAAA,GAAmB,CAAC,CAAA,KAA2B,CAAC;AAGtD,IAAM,QAAW,GAAA,CAAI,CAAiC,KAAA,CAAC,CAAC;AAExD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G,MAAM;AAOjG,IAAA,KAAA,GAAQ,CAAC,CAAuB,KAAA;AAC3C,EAAI,IAAA,QAAA,CAAS,CAAC,CAAA,EAAU,OAAA,CAAA;AACxB,EAAI,IAAA,CAAA,IAAK,MAAa,OAAA,EAAA;AACtB,EAAI,IAAA;AACF,IAAA,OAAO,IAAK,CAAA,SAAA,CAAU,CAAG,EAAA,IAAA,EAAM,CAAC,CAAA;AAAA,GAC1B,CAAA,MAAA;AAEN,IAAA,OAAO,CAAI,GAAA,EAAA;AAAA;AAEf;;;AC3IO,IAAM,OAAO,MAAY;AAAC;AAE1B,IAAM,gBAAmB,GAAA;AAEzB,IAAM,cAAc,MAAY;AAEhC,IAAM,eAAe,MAAa;AAElC,IAAM,cAAc,MAAY;AAEhC,IAAM,qBAAqB,MAAc","file":"index.js"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
// src/
|
|
1
|
+
// src/is-to-as.ts
|
|
2
2
|
var _ = void 0;
|
|
3
|
+
var isDefined = (x) => x !== _;
|
|
3
4
|
var isTrue = (x) => x === true;
|
|
4
5
|
var toTrue = (x) => x === true ? true : _;
|
|
5
6
|
var asTrue = (x) => x === true ? x : false;
|
|
7
|
+
var isFalsy = (x) => !x;
|
|
8
|
+
var isTruthy = (x) => !!x;
|
|
6
9
|
var isBoolean = (x) => x === true || x === false;
|
|
7
10
|
var toBoolean = (x) => isBoolean(x) ? x : _;
|
|
8
11
|
var isNumber = (x) => typeof x === "number" && x === x;
|
|
@@ -11,21 +14,21 @@ var asNumber = (x) => isNumber(x) ? x : 0;
|
|
|
11
14
|
var isString = (x) => typeof x === "string";
|
|
12
15
|
var toString = (x) => isString(x) ? x : _;
|
|
13
16
|
var asString = (x) => isString(x) ? x : "";
|
|
14
|
-
var
|
|
15
|
-
|
|
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";
|
|
17
|
+
var isNonEmptyString = (x) => isString(x) && x !== "";
|
|
18
|
+
var toNonEmptyString = (x) => isNonEmptyString(x) ? x : _;
|
|
24
19
|
var isArray = Array.isArray;
|
|
20
|
+
var toArray = (x) => isArray(x) ? x : _;
|
|
21
|
+
var toNonEmptyArray = (x) => x.length > 0 ? x : _;
|
|
22
|
+
var asArray = (x) => isArray(x) ? x : [];
|
|
23
|
+
var isObject = (x) => x !== null && typeof x === "object";
|
|
24
|
+
var asObject = (x) => isObject(x) ? x : {};
|
|
25
25
|
var isPlainObject = (x) => isObject(x) && !isArray(x);
|
|
26
26
|
var toPlainObject = (x) => isPlainObject(x) ? x : _;
|
|
27
27
|
var asPlainObject = (x) => isPlainObject(x) ? x : {};
|
|
28
|
-
var
|
|
28
|
+
var isNonEmptyPlainObject = (x) => isPlainObject(x) && Object.keys(x).length > 0;
|
|
29
|
+
var toNonEmptyPlainObject = (x) => isNonEmptyPlainObject(x) ? x : _;
|
|
30
|
+
var isNonEmptyJSONObject = (x) => isPlainObject(x) && Object.values(x).some(isDefined);
|
|
31
|
+
var toNonEmptyJSONObject = (x) => isNonEmptyJSONObject(x) ? x : _;
|
|
29
32
|
var toPlainObjectOf = (x, f) => {
|
|
30
33
|
if (isPlainObject(x)) {
|
|
31
34
|
let index = -1;
|
|
@@ -43,7 +46,25 @@ var toPlainObjectOf = (x, f) => {
|
|
|
43
46
|
}
|
|
44
47
|
};
|
|
45
48
|
var toPlainObjectOfTrue = (x) => toPlainObjectOf(x, isTrue);
|
|
49
|
+
var print = (x) => {
|
|
50
|
+
if (isString(x)) return x;
|
|
51
|
+
if (x == null) return "";
|
|
52
|
+
try {
|
|
53
|
+
return JSON.stringify(x, null, 2);
|
|
54
|
+
} catch {
|
|
55
|
+
return x + "";
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// src/returns.ts
|
|
60
|
+
var noop = () => {
|
|
61
|
+
};
|
|
62
|
+
var returnsUndefined = noop;
|
|
63
|
+
var returnsNull = () => null;
|
|
64
|
+
var returnsFalse = () => false;
|
|
65
|
+
var returnsTrue = () => true;
|
|
66
|
+
var returnsEmptyString = () => "";
|
|
46
67
|
|
|
47
|
-
export { asNumber, asPlainObject, asString, asTrue, isArray, isBoolean, isNumber, isObject, isPlainObject, isString, isTrue,
|
|
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 };
|
|
48
69
|
//# sourceMappingURL=index.mjs.map
|
|
49
70
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/
|
|
1
|
+
{"version":3,"sources":["../src/is-to-as.ts","../src/returns.ts"],"names":[],"mappings":";AAAA,IAAM,CAAI,GAAA,KAAA,CAAA;AAIG,IAAA,SAAA,GAAY,CAAI,CAAA,KAA6B,CAAM,KAAA;AAEnD,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;AAUpD,IAAA,OAAA,GAAmB,CAAC,CAAA,KAA2B,CAAC;AAGtD,IAAM,QAAW,GAAA,CAAI,CAAiC,KAAA,CAAC,CAAC;AAExD,IAAM,SAAY,GAAA,CAAC,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A,GAAQ,CAAC,CAAuB,KAAA;AAC3C,EAAI,IAAA,QAAA,CAAS,CAAC,CAAA,EAAU,OAAA,CAAA;AACxB,EAAI,IAAA,CAAA,IAAK,MAAa,OAAA,EAAA;AACtB,EAAI,IAAA;AACF,IAAA,OAAO,IAAK,CAAA,SAAA,CAAU,CAAG,EAAA,IAAA,EAAM,CAAC,CAAA;AAAA,GAC1B,CAAA,MAAA;AAEN,IAAA,OAAO,CAAI,GAAA,EAAA;AAAA;AAEf;;;AC3IO,IAAM,OAAO,MAAY;AAAC;AAE1B,IAAM,gBAAmB,GAAA;AAEzB,IAAM,cAAc,MAAY;AAEhC,IAAM,eAAe,MAAa;AAElC,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.2",
|
|
4
4
|
"description": "Filter types from unknown",
|
|
5
5
|
"repository": "wopjs/cast",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -52,9 +52,9 @@
|
|
|
52
52
|
"prettier": "^3.3.3",
|
|
53
53
|
"pretty-bytes": "^6.1.1",
|
|
54
54
|
"tsup": "^8.3.5",
|
|
55
|
-
"typedoc": "^0.
|
|
55
|
+
"typedoc": "^0.27.5",
|
|
56
56
|
"typescript": "^5.6.3",
|
|
57
|
-
"typescript-eslint": "^8.
|
|
57
|
+
"typescript-eslint": "^8.15.0",
|
|
58
58
|
"vitest": "^2.1.5",
|
|
59
59
|
"yoctocolors": "^2.1.1"
|
|
60
60
|
}
|
package/src/index.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export * from "./
|
|
1
|
+
export * from "./is-to-as";
|
|
2
|
+
export * from "./returns";
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
isDefined,
|
|
5
|
+
isTrue,
|
|
6
|
+
toTrue,
|
|
7
|
+
asTrue,
|
|
8
|
+
isTruthy,
|
|
9
|
+
isFalsy,
|
|
10
|
+
isBoolean,
|
|
11
|
+
toBoolean,
|
|
12
|
+
isNumber,
|
|
13
|
+
toNumber,
|
|
14
|
+
asNumber,
|
|
15
|
+
isString,
|
|
16
|
+
toString,
|
|
17
|
+
asString,
|
|
18
|
+
isNonEmptyString,
|
|
19
|
+
toNonEmptyString,
|
|
20
|
+
print,
|
|
21
|
+
isArray,
|
|
22
|
+
toArray,
|
|
23
|
+
asArray,
|
|
24
|
+
toNonEmptyArray,
|
|
25
|
+
isObject,
|
|
26
|
+
asObject,
|
|
27
|
+
isPlainObject,
|
|
28
|
+
toPlainObject,
|
|
29
|
+
asPlainObject,
|
|
30
|
+
isNonEmptyPlainObject,
|
|
31
|
+
toNonEmptyPlainObject,
|
|
32
|
+
isNonEmptyJSONObject,
|
|
33
|
+
toNonEmptyJSONObject,
|
|
34
|
+
toPlainObjectOf,
|
|
35
|
+
toPlainObjectOfTrue,
|
|
36
|
+
} from ".";
|
|
37
|
+
|
|
38
|
+
describe("primitive.ts", () => {
|
|
39
|
+
it("isDefined", () => {
|
|
40
|
+
expect(isDefined(1)).toBe(true);
|
|
41
|
+
expect(isDefined(undefined)).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("isTrue", () => {
|
|
45
|
+
expect(isTrue(true)).toBe(true);
|
|
46
|
+
expect(isTrue(false)).toBe(false);
|
|
47
|
+
expect(isTrue(1)).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("toTrue", () => {
|
|
51
|
+
expect(toTrue(true)).toBe(true);
|
|
52
|
+
expect(toTrue(false)).toBe(undefined);
|
|
53
|
+
expect(toTrue(1)).toBe(undefined);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("asTrue", () => {
|
|
57
|
+
expect(asTrue(true)).toBe(true);
|
|
58
|
+
expect(asTrue(false)).toBe(false);
|
|
59
|
+
expect(asTrue(1)).toBe(false);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("isTruthy", () => {
|
|
63
|
+
expect(isTruthy(true)).toBe(true);
|
|
64
|
+
expect(isTruthy(false)).toBe(false);
|
|
65
|
+
expect(isTruthy(1)).toBe(true);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("isFalsy", () => {
|
|
69
|
+
expect(isFalsy(true)).toBe(false);
|
|
70
|
+
expect(isFalsy(false)).toBe(true);
|
|
71
|
+
expect(isFalsy(1)).toBe(false);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("isBoolean", () => {
|
|
75
|
+
expect(isBoolean(true)).toBe(true);
|
|
76
|
+
expect(isBoolean(false)).toBe(true);
|
|
77
|
+
expect(isBoolean(1)).toBe(false);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("toBoolean", () => {
|
|
81
|
+
expect(toBoolean(true)).toBe(true);
|
|
82
|
+
expect(toBoolean(false)).toBe(false);
|
|
83
|
+
expect(toBoolean(1)).toBe(undefined);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("isNumber", () => {
|
|
87
|
+
expect(isNumber(1)).toBe(true);
|
|
88
|
+
expect(isNumber(NaN)).toBe(false);
|
|
89
|
+
expect(isNumber("1")).toBe(false);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("toNumber", () => {
|
|
93
|
+
expect(toNumber(1)).toBe(1);
|
|
94
|
+
expect(toNumber(NaN)).toBe(undefined);
|
|
95
|
+
expect(toNumber("1")).toBe(undefined);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("asNumber", () => {
|
|
99
|
+
expect(asNumber(1)).toBe(1);
|
|
100
|
+
expect(asNumber(NaN)).toBe(0);
|
|
101
|
+
expect(asNumber("1")).toBe(0);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("isString", () => {
|
|
105
|
+
expect(isString("hello")).toBe(true);
|
|
106
|
+
expect(isString(1)).toBe(false);
|
|
107
|
+
expect(isString(true)).toBe(false);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("toString", () => {
|
|
111
|
+
expect(toString("hello")).toBe("hello");
|
|
112
|
+
expect(toString(1)).toBe(undefined);
|
|
113
|
+
expect(toString(true)).toBe(undefined);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("asString", () => {
|
|
117
|
+
expect(asString("hello")).toBe("hello");
|
|
118
|
+
expect(asString(1)).toBe("");
|
|
119
|
+
expect(asString(true)).toBe("");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("isNonEmptyString", () => {
|
|
123
|
+
expect(isNonEmptyString("hello")).toBe(true);
|
|
124
|
+
expect(isNonEmptyString("")).toBe(false);
|
|
125
|
+
expect(isNonEmptyString(1)).toBe(false);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("toNonEmptyString", () => {
|
|
129
|
+
expect(toNonEmptyString("hello")).toBe("hello");
|
|
130
|
+
expect(toNonEmptyString("")).toBe(undefined);
|
|
131
|
+
expect(toNonEmptyString(1)).toBe(undefined);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("isArray", () => {
|
|
135
|
+
expect(isArray([])).toBe(true);
|
|
136
|
+
expect(isArray({})).toBe(false);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("toArray", () => {
|
|
140
|
+
expect(toArray([])).toEqual([]);
|
|
141
|
+
expect(toArray({})).toBe(undefined);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("asArray", () => {
|
|
145
|
+
expect(asArray([])).toEqual([]);
|
|
146
|
+
expect(asArray({})).toEqual([]);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("toNonEmptyArray", () => {
|
|
150
|
+
expect(toNonEmptyArray([1])).toEqual([1]);
|
|
151
|
+
expect(toNonEmptyArray([])).toBe(undefined);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("isObject", () => {
|
|
155
|
+
expect(isObject({})).toBe(true);
|
|
156
|
+
expect(isObject([])).toBe(true);
|
|
157
|
+
expect(isObject(null)).toBe(false);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("asObject", () => {
|
|
161
|
+
expect(asObject({})).toEqual({});
|
|
162
|
+
expect(asObject([])).toEqual([]);
|
|
163
|
+
expect(asObject(null)).toEqual({});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("isPlainObject", () => {
|
|
167
|
+
expect(isPlainObject({})).toBe(true);
|
|
168
|
+
expect(isPlainObject([])).toBe(false);
|
|
169
|
+
expect(isPlainObject(null)).toBe(false);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("toPlainObject", () => {
|
|
173
|
+
expect(toPlainObject({})).toEqual({});
|
|
174
|
+
expect(toPlainObject([])).toBe(undefined);
|
|
175
|
+
expect(toPlainObject(null)).toBe(undefined);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("asPlainObject", () => {
|
|
179
|
+
expect(asPlainObject({})).toEqual({});
|
|
180
|
+
expect(asPlainObject([])).toEqual({});
|
|
181
|
+
expect(asPlainObject(null)).toEqual({});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("isNonEmptyPlainObject", () => {
|
|
185
|
+
expect(isNonEmptyPlainObject({ a: 1 })).toBe(true);
|
|
186
|
+
expect(isNonEmptyPlainObject({})).toBe(false);
|
|
187
|
+
expect(isNonEmptyPlainObject([])).toBe(false);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("toNonEmptyPlainObject", () => {
|
|
191
|
+
expect(toNonEmptyPlainObject({ a: 1 })).toEqual({ a: 1 });
|
|
192
|
+
expect(toNonEmptyPlainObject({})).toBe(undefined);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("isNonEmptyJSONObject", () => {
|
|
196
|
+
expect(isNonEmptyJSONObject({ a: 1 })).toBe(true);
|
|
197
|
+
expect(isNonEmptyJSONObject({})).toBe(false);
|
|
198
|
+
expect(isNonEmptyJSONObject([])).toBe(false);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("toNonEmptyJSONObject", () => {
|
|
202
|
+
expect(toNonEmptyJSONObject({ a: 1 })).toEqual({ a: 1 });
|
|
203
|
+
expect(toNonEmptyJSONObject({})).toBe(undefined);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("toPlainObjectOf", () => {
|
|
207
|
+
expect(toPlainObjectOf({ a: true, b: false }, isTrue)).toEqual({ a: true });
|
|
208
|
+
expect(toPlainObjectOf({ a: 1, b: 2 }, isTrue)).toBe(undefined);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("toPlainObjectOfTrue", () => {
|
|
212
|
+
expect(toPlainObjectOfTrue({ a: true, b: false })).toEqual({ a: true });
|
|
213
|
+
expect(toPlainObjectOfTrue({ a: 1, b: 2 })).toBe(undefined);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("show", () => {
|
|
217
|
+
expect(print("hello")).toBe("hello");
|
|
218
|
+
expect(print(null)).toBe("");
|
|
219
|
+
expect(print({ a: 1 })).toBe(JSON.stringify({ a: 1 }, null, 2));
|
|
220
|
+
expect(
|
|
221
|
+
print({
|
|
222
|
+
toJSON() {
|
|
223
|
+
throw new Error("x");
|
|
224
|
+
},
|
|
225
|
+
toString() {
|
|
226
|
+
return "str";
|
|
227
|
+
},
|
|
228
|
+
})
|
|
229
|
+
).toBe("str");
|
|
230
|
+
});
|
|
231
|
+
});
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
const _ = undefined;
|
|
2
2
|
export type _ = undefined;
|
|
3
3
|
|
|
4
|
+
/** Returns `true` if `x` is not `undefined`. */
|
|
5
|
+
export const isDefined = <T>(x: T | undefined): x is T => x !== _;
|
|
6
|
+
|
|
4
7
|
export const isTrue = (x: unknown): x is true => x === true;
|
|
5
8
|
|
|
6
9
|
/** Returns `true` if `x` is `true`, otherwise returns `undefined`. */
|
|
@@ -9,6 +12,19 @@ export const toTrue = (x: unknown): true | _ => (x === true ? true : _);
|
|
|
9
12
|
/** Returns `true` if `x` is `true`, otherwise returns `false`. */
|
|
10
13
|
export const asTrue = (x: unknown): boolean => (x === true ? x : false);
|
|
11
14
|
|
|
15
|
+
export type Falsy = false | null | undefined | 0 | "";
|
|
16
|
+
|
|
17
|
+
export interface IsFalsy {
|
|
18
|
+
(x: unknown): x is Falsy;
|
|
19
|
+
<T>(x: T): x is Extract<T, Falsy>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Returns `true` if `Boolean(x)` is `false`. */
|
|
23
|
+
export const isFalsy: IsFalsy = (x: unknown): x is Falsy => !x;
|
|
24
|
+
|
|
25
|
+
/** Returns `true` if `Boolean(x)` is `true`. */
|
|
26
|
+
export const isTruthy = <T>(x: T): x is Exclude<T, Falsy> => !!x;
|
|
27
|
+
|
|
12
28
|
export const isBoolean = (x: unknown): x is boolean => x === true || x === false;
|
|
13
29
|
|
|
14
30
|
/** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */
|
|
@@ -32,21 +48,22 @@ export const toString = (x: unknown): string | _ => (isString(x) ? x : _);
|
|
|
32
48
|
/** Returns `x` if `x` is a string, otherwise returns `''` (empty string). */
|
|
33
49
|
export const asString = (x: unknown): string => (isString(x) ? x : "");
|
|
34
50
|
|
|
35
|
-
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
51
|
+
/** Returns `true` if `x` is a string and not `''`. */
|
|
52
|
+
export const isNonEmptyString = (x: unknown): x is string => isString(x) && x !== "";
|
|
53
|
+
|
|
54
|
+
/** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
|
|
55
|
+
export const toNonEmptyString = (x: unknown): string | _ => (isNonEmptyString(x) ? x : _);
|
|
56
|
+
|
|
57
|
+
export const isArray = Array.isArray;
|
|
58
|
+
|
|
59
|
+
/** Returns `x` if `x` is an array. */
|
|
60
|
+
export const toArray = (x: unknown): unknown[] | undefined => (isArray(x) ? x : _);
|
|
61
|
+
|
|
62
|
+
/** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
|
|
63
|
+
export const toNonEmptyArray = <T>(x: T[]): T[] | _ => (x.length > 0 ? x : _);
|
|
64
|
+
|
|
65
|
+
/** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
|
|
66
|
+
export const asArray = (x: unknown): unknown[] => (isArray(x) ? x : []);
|
|
50
67
|
|
|
51
68
|
export interface PlainObject {
|
|
52
69
|
[key: PropertyKey]: unknown;
|
|
@@ -55,7 +72,8 @@ export interface PlainObject {
|
|
|
55
72
|
/** Returns `true` if `x` is an object (including array) and not null. */
|
|
56
73
|
export const isObject = (x: unknown): x is object => x !== null && typeof x === "object";
|
|
57
74
|
|
|
58
|
-
|
|
75
|
+
/** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */
|
|
76
|
+
export const asObject = (x: unknown): object => (isObject(x) ? x : {});
|
|
59
77
|
|
|
60
78
|
/** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
|
|
61
79
|
export const isPlainObject = (x: unknown): x is PlainObject => isObject(x) && !isArray(x);
|
|
@@ -66,9 +84,18 @@ export const toPlainObject = (x: unknown): PlainObject | _ => (isPlainObject(x)
|
|
|
66
84
|
/** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */
|
|
67
85
|
export const asPlainObject = (x: unknown): PlainObject => (isPlainObject(x) ? x : {});
|
|
68
86
|
|
|
87
|
+
/** Returns `true` if `x` is a plain object and has at least one key. */
|
|
88
|
+
export const isNonEmptyPlainObject = (x: unknown): x is PlainObject => isPlainObject(x) && Object.keys(x).length > 0;
|
|
89
|
+
|
|
69
90
|
/** 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
|
-
|
|
91
|
+
export const toNonEmptyPlainObject = <T extends PlainObject>(x: T): T | _ => (isNonEmptyPlainObject(x) ? x : _);
|
|
92
|
+
|
|
93
|
+
/** Returns `true` if `x` is a plain object and has at least one key with non-undefined value. */
|
|
94
|
+
export const isNonEmptyJSONObject = (x: unknown): x is PlainObject =>
|
|
95
|
+
isPlainObject(x) && Object.values(x).some(isDefined);
|
|
96
|
+
|
|
97
|
+
/** Returns `x` if `x` is a plain object and has at least one key with non-undefined value, otherwise returns `undefined`. */
|
|
98
|
+
export const toNonEmptyJSONObject = <T extends PlainObject>(x: T): T | _ => (isNonEmptyJSONObject(x) ? x : _);
|
|
72
99
|
|
|
73
100
|
/**
|
|
74
101
|
* Creates an object from `x` with keys `k` if `f(x[k])` returns `true`.
|
|
@@ -95,3 +122,19 @@ export const toPlainObjectOf = <T>(x: unknown, f: (v: unknown) => v is T): { [ke
|
|
|
95
122
|
|
|
96
123
|
/** Filter props from object `x` whose values are `true`. */
|
|
97
124
|
export const toPlainObjectOfTrue = (x: unknown): { [key: PropertyKey]: true } | _ => toPlainObjectOf(x, isTrue);
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Returns `x` if `x` is string, otherwise returns `''` (empty string) if
|
|
128
|
+
* `x` is `null` or `undefined`, otherwise returns `JSON.stringify(x)`.
|
|
129
|
+
* This is very useful to show a value inside a React component.
|
|
130
|
+
*/
|
|
131
|
+
export const print = (x: unknown): string => {
|
|
132
|
+
if (isString(x)) return x;
|
|
133
|
+
if (x == null) return "";
|
|
134
|
+
try {
|
|
135
|
+
return JSON.stringify(x, null, 2);
|
|
136
|
+
} catch {
|
|
137
|
+
// Insane case is not handled: x = { toString: () => { throw x } }
|
|
138
|
+
return x + "";
|
|
139
|
+
}
|
|
140
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { noop, returnsUndefined, returnsNull, returnsFalse, returnsTrue, returnsEmptyString } from "./returns";
|
|
4
|
+
|
|
5
|
+
describe("returns functions", () => {
|
|
6
|
+
it("noop should return undefined", () => {
|
|
7
|
+
expect(noop()).toBeUndefined();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("returnsUndefined should return undefined", () => {
|
|
11
|
+
expect(returnsUndefined()).toBeUndefined();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("returnsNull should return null", () => {
|
|
15
|
+
expect(returnsNull()).toBeNull();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("returnsFalse should return false", () => {
|
|
19
|
+
expect(returnsFalse()).toBe(false);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("returnsTrue should return true", () => {
|
|
23
|
+
expect(returnsTrue()).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("returnsEmptyString should return an empty string", () => {
|
|
27
|
+
expect(returnsEmptyString()).toBe("");
|
|
28
|
+
});
|
|
29
|
+
});
|
package/src/returns.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const noop = (): void => {};
|
|
2
|
+
|
|
3
|
+
export const returnsUndefined = noop as () => undefined;
|
|
4
|
+
|
|
5
|
+
export const returnsNull = (): null => null;
|
|
6
|
+
|
|
7
|
+
export const returnsFalse = (): false => false;
|
|
8
|
+
|
|
9
|
+
export const returnsTrue = (): true => true;
|
|
10
|
+
|
|
11
|
+
export const returnsEmptyString = (): string => "";
|