@wopjs/cast 0.1.1 → 0.1.3

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 CHANGED
@@ -1,9 +1,22 @@
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>;
18
+ /** Returns `x` if `Boolean(x)` is `true`, otherwise returns `undefined`. */
19
+ declare const toTruthy: <T>(x: T) => Exclude<T, Falsy> | _;
7
20
  declare const isBoolean: (x: unknown) => x is boolean;
8
21
  /** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */
9
22
  declare const toBoolean: (x: unknown) => boolean | _;
@@ -19,12 +32,17 @@ declare const isString: (x: unknown) => x is string;
19
32
  declare const toString: (x: unknown) => string | _;
20
33
  /** Returns `x` if `x` is a string, otherwise returns `''` (empty string). */
21
34
  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 print: (x: unknown) => string;
35
+ /** Returns `true` if `x` is a string and not `''`. */
36
+ declare const isNonEmptyString: (x: unknown) => x is string;
37
+ /** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
38
+ declare const toNonEmptyString: (x: unknown) => string | _;
39
+ declare const isArray: (arg: any) => arg is any[];
40
+ /** Returns `x` if `x` is an array. */
41
+ declare const toArray: (x: unknown) => unknown[] | undefined;
42
+ /** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
43
+ declare const toNonEmptyArray: <T>(x: T[]) => T[] | _;
44
+ /** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
45
+ declare const asArray: (x: unknown) => unknown[];
28
46
  interface PlainObject {
29
47
  [key: PropertyKey]: unknown;
30
48
  }
@@ -32,19 +50,20 @@ interface PlainObject {
32
50
  declare const isObject: (x: unknown) => x is object;
33
51
  /** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */
34
52
  declare const asObject: (x: unknown) => object;
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[];
40
53
  /** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
41
54
  declare const isPlainObject: (x: unknown) => x is PlainObject;
42
55
  /** Returns `x` if `x` is a plain object. */
43
56
  declare const toPlainObject: (x: unknown) => PlainObject | _;
44
57
  /** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */
45
58
  declare const asPlainObject: (x: unknown) => PlainObject;
59
+ /** Returns `true` if `x` is a plain object and has at least one key. */
60
+ declare const isNonEmptyPlainObject: (x: unknown) => x is PlainObject;
46
61
  /** Returns `x` if `x` is a plain object and has at least one key. */
47
62
  declare const toNonEmptyPlainObject: <T extends PlainObject>(x: T) => T | _;
63
+ /** Returns `true` if `x` is a plain object and has at least one key with non-undefined value. */
64
+ declare const isNonEmptyJSONObject: (x: unknown) => x is PlainObject;
65
+ /** Returns `x` if `x` is a plain object and has at least one key with non-undefined value, otherwise returns `undefined`. */
66
+ declare const toNonEmptyJSONObject: <T extends PlainObject>(x: T) => T | _;
48
67
  /**
49
68
  * Creates an object from `x` with keys `k` if `f(x[k])` returns `true`.
50
69
  * If `x` is not a plain object, or there's no passed props, returns `undefined`.
@@ -56,5 +75,18 @@ declare const toPlainObjectOf: <T>(x: unknown, f: (v: unknown) => v is T) => {
56
75
  declare const toPlainObjectOfTrue: (x: unknown) => {
57
76
  [key: PropertyKey]: true;
58
77
  } | _;
78
+ /**
79
+ * Returns `x` if `x` is string, otherwise returns `''` (empty string) if
80
+ * `x` is `null` or `undefined`, otherwise returns `JSON.stringify(x)`.
81
+ * This is very useful to show a value inside a React component.
82
+ */
83
+ declare const print: (x: unknown) => string;
84
+
85
+ declare const noop: () => void;
86
+ declare const returnsUndefined: () => undefined;
87
+ declare const returnsNull: () => null;
88
+ declare const returnsFalse: () => false;
89
+ declare const returnsTrue: () => true;
90
+ declare const returnsEmptyString: () => string;
59
91
 
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 };
92
+ export { type Falsy, type IsFalsy, type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,22 @@
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>;
18
+ /** Returns `x` if `Boolean(x)` is `true`, otherwise returns `undefined`. */
19
+ declare const toTruthy: <T>(x: T) => Exclude<T, Falsy> | _;
7
20
  declare const isBoolean: (x: unknown) => x is boolean;
8
21
  /** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */
9
22
  declare const toBoolean: (x: unknown) => boolean | _;
@@ -19,12 +32,17 @@ declare const isString: (x: unknown) => x is string;
19
32
  declare const toString: (x: unknown) => string | _;
20
33
  /** Returns `x` if `x` is a string, otherwise returns `''` (empty string). */
21
34
  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 print: (x: unknown) => string;
35
+ /** Returns `true` if `x` is a string and not `''`. */
36
+ declare const isNonEmptyString: (x: unknown) => x is string;
37
+ /** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
38
+ declare const toNonEmptyString: (x: unknown) => string | _;
39
+ declare const isArray: (arg: any) => arg is any[];
40
+ /** Returns `x` if `x` is an array. */
41
+ declare const toArray: (x: unknown) => unknown[] | undefined;
42
+ /** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
43
+ declare const toNonEmptyArray: <T>(x: T[]) => T[] | _;
44
+ /** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
45
+ declare const asArray: (x: unknown) => unknown[];
28
46
  interface PlainObject {
29
47
  [key: PropertyKey]: unknown;
30
48
  }
@@ -32,19 +50,20 @@ interface PlainObject {
32
50
  declare const isObject: (x: unknown) => x is object;
33
51
  /** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */
34
52
  declare const asObject: (x: unknown) => object;
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[];
40
53
  /** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
41
54
  declare const isPlainObject: (x: unknown) => x is PlainObject;
42
55
  /** Returns `x` if `x` is a plain object. */
43
56
  declare const toPlainObject: (x: unknown) => PlainObject | _;
44
57
  /** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */
45
58
  declare const asPlainObject: (x: unknown) => PlainObject;
59
+ /** Returns `true` if `x` is a plain object and has at least one key. */
60
+ declare const isNonEmptyPlainObject: (x: unknown) => x is PlainObject;
46
61
  /** Returns `x` if `x` is a plain object and has at least one key. */
47
62
  declare const toNonEmptyPlainObject: <T extends PlainObject>(x: T) => T | _;
63
+ /** Returns `true` if `x` is a plain object and has at least one key with non-undefined value. */
64
+ declare const isNonEmptyJSONObject: (x: unknown) => x is PlainObject;
65
+ /** Returns `x` if `x` is a plain object and has at least one key with non-undefined value, otherwise returns `undefined`. */
66
+ declare const toNonEmptyJSONObject: <T extends PlainObject>(x: T) => T | _;
48
67
  /**
49
68
  * Creates an object from `x` with keys `k` if `f(x[k])` returns `true`.
50
69
  * If `x` is not a plain object, or there's no passed props, returns `undefined`.
@@ -56,5 +75,18 @@ declare const toPlainObjectOf: <T>(x: unknown, f: (v: unknown) => v is T) => {
56
75
  declare const toPlainObjectOfTrue: (x: unknown) => {
57
76
  [key: PropertyKey]: true;
58
77
  } | _;
78
+ /**
79
+ * Returns `x` if `x` is string, otherwise returns `''` (empty string) if
80
+ * `x` is `null` or `undefined`, otherwise returns `JSON.stringify(x)`.
81
+ * This is very useful to show a value inside a React component.
82
+ */
83
+ declare const print: (x: unknown) => string;
84
+
85
+ declare const noop: () => void;
86
+ declare const returnsUndefined: () => undefined;
87
+ declare const returnsNull: () => null;
88
+ declare const returnsFalse: () => false;
89
+ declare const returnsTrue: () => true;
90
+ declare const returnsEmptyString: () => string;
59
91
 
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 };
92
+ export { type Falsy, type IsFalsy, type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy };
package/dist/index.js CHANGED
@@ -1,10 +1,14 @@
1
1
  'use strict';
2
2
 
3
- // src/primitive.ts
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;
11
+ var toTruthy = (x) => isTruthy(x) ? x : _;
8
12
  var isBoolean = (x) => x === true || x === false;
9
13
  var toBoolean = (x) => isBoolean(x) ? x : _;
10
14
  var isNumber = (x) => typeof x === "number" && x === x;
@@ -13,24 +17,21 @@ var asNumber = (x) => isNumber(x) ? x : 0;
13
17
  var isString = (x) => typeof x === "string";
14
18
  var toString = (x) => isString(x) ? x : _;
15
19
  var asString = (x) => isString(x) ? x : "";
16
- var print = (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 asObject = (x) => isObject(x) ? x : {};
20
+ var isNonEmptyString = (x) => isString(x) && x !== "";
21
+ var toNonEmptyString = (x) => isNonEmptyString(x) ? x : _;
27
22
  var isArray = Array.isArray;
28
23
  var toArray = (x) => isArray(x) ? x : _;
24
+ var toNonEmptyArray = (x) => x.length > 0 ? x : _;
29
25
  var asArray = (x) => isArray(x) ? x : [];
26
+ var isObject = (x) => x !== null && typeof x === "object";
27
+ var asObject = (x) => isObject(x) ? x : {};
30
28
  var isPlainObject = (x) => isObject(x) && !isArray(x);
31
29
  var toPlainObject = (x) => isPlainObject(x) ? x : _;
32
30
  var asPlainObject = (x) => isPlainObject(x) ? x : {};
33
- var toNonEmptyPlainObject = (x) => isPlainObject(x) && Object.keys(x).length > 0 ? x : _;
31
+ var isNonEmptyPlainObject = (x) => isPlainObject(x) && Object.keys(x).length > 0;
32
+ var toNonEmptyPlainObject = (x) => isNonEmptyPlainObject(x) ? x : _;
33
+ var isNonEmptyJSONObject = (x) => isPlainObject(x) && Object.values(x).some(isDefined);
34
+ var toNonEmptyJSONObject = (x) => isNonEmptyJSONObject(x) ? x : _;
34
35
  var toPlainObjectOf = (x, f) => {
35
36
  if (isPlainObject(x)) {
36
37
  let index = -1;
@@ -48,6 +49,24 @@ var toPlainObjectOf = (x, f) => {
48
49
  }
49
50
  };
50
51
  var toPlainObjectOfTrue = (x) => toPlainObjectOf(x, isTrue);
52
+ var print = (x) => {
53
+ if (isString(x)) return x;
54
+ if (x == null) return "";
55
+ try {
56
+ return JSON.stringify(x, null, 2);
57
+ } catch {
58
+ return x + "";
59
+ }
60
+ };
61
+
62
+ // src/returns.ts
63
+ var noop = () => {
64
+ };
65
+ var returnsUndefined = noop;
66
+ var returnsNull = () => null;
67
+ var returnsFalse = () => false;
68
+ var returnsTrue = () => true;
69
+ var returnsEmptyString = () => "";
51
70
 
52
71
  exports.asArray = asArray;
53
72
  exports.asNumber = asNumber;
@@ -57,20 +76,36 @@ exports.asString = asString;
57
76
  exports.asTrue = asTrue;
58
77
  exports.isArray = isArray;
59
78
  exports.isBoolean = isBoolean;
79
+ exports.isDefined = isDefined;
80
+ exports.isFalsy = isFalsy;
81
+ exports.isNonEmptyJSONObject = isNonEmptyJSONObject;
82
+ exports.isNonEmptyPlainObject = isNonEmptyPlainObject;
83
+ exports.isNonEmptyString = isNonEmptyString;
60
84
  exports.isNumber = isNumber;
61
85
  exports.isObject = isObject;
62
86
  exports.isPlainObject = isPlainObject;
63
87
  exports.isString = isString;
64
88
  exports.isTrue = isTrue;
89
+ exports.isTruthy = isTruthy;
90
+ exports.noop = noop;
65
91
  exports.print = print;
92
+ exports.returnsEmptyString = returnsEmptyString;
93
+ exports.returnsFalse = returnsFalse;
94
+ exports.returnsNull = returnsNull;
95
+ exports.returnsTrue = returnsTrue;
96
+ exports.returnsUndefined = returnsUndefined;
66
97
  exports.toArray = toArray;
67
98
  exports.toBoolean = toBoolean;
99
+ exports.toNonEmptyArray = toNonEmptyArray;
100
+ exports.toNonEmptyJSONObject = toNonEmptyJSONObject;
68
101
  exports.toNonEmptyPlainObject = toNonEmptyPlainObject;
102
+ exports.toNonEmptyString = toNonEmptyString;
69
103
  exports.toNumber = toNumber;
70
104
  exports.toPlainObject = toPlainObject;
71
105
  exports.toPlainObjectOf = toPlainObjectOf;
72
106
  exports.toPlainObjectOfTrue = toPlainObjectOfTrue;
73
107
  exports.toString = toString;
74
108
  exports.toTrue = toTrue;
109
+ exports.toTruthy = toTruthy;
75
110
  //# sourceMappingURL=index.js.map
76
111
  //# sourceMappingURL=index.js.map
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,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,CAAC,CAAA,KAA4B,QAAS,CAAA,CAAC,IAAI,CAAI,GAAA;AAGhE,IAAM,WAAW,CAAC,CAAA,KAAwB,QAAS,CAAA,CAAC,IAAI,CAAI,GAAA;AAG5D,IAAM,QAAW,GAAA,CAAC,CAA4B,KAAA,OAAO,CAAM,KAAA;AAG3D,IAAM,WAAW,CAAC,CAAA,KAA4B,QAAS,CAAA,CAAC,IAAI,CAAI,GAAA;AAGhE,IAAM,WAAW,CAAC,CAAA,KAAwB,QAAS,CAAA,CAAC,IAAI,CAAI,GAAA;AAOtD,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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a;AAElC,IAAM,cAAc,MAAY;AAEhC,IAAM,qBAAqB,MAAc","file":"index.js"}
package/dist/index.mjs CHANGED
@@ -1,8 +1,12 @@
1
- // src/primitive.ts
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;
9
+ var toTruthy = (x) => isTruthy(x) ? x : _;
6
10
  var isBoolean = (x) => x === true || x === false;
7
11
  var toBoolean = (x) => isBoolean(x) ? x : _;
8
12
  var isNumber = (x) => typeof x === "number" && x === x;
@@ -11,24 +15,21 @@ var asNumber = (x) => isNumber(x) ? x : 0;
11
15
  var isString = (x) => typeof x === "string";
12
16
  var toString = (x) => isString(x) ? x : _;
13
17
  var asString = (x) => isString(x) ? x : "";
14
- var print = (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 asObject = (x) => isObject(x) ? x : {};
18
+ var isNonEmptyString = (x) => isString(x) && x !== "";
19
+ var toNonEmptyString = (x) => isNonEmptyString(x) ? x : _;
25
20
  var isArray = Array.isArray;
26
21
  var toArray = (x) => isArray(x) ? x : _;
22
+ var toNonEmptyArray = (x) => x.length > 0 ? x : _;
27
23
  var asArray = (x) => isArray(x) ? x : [];
24
+ var isObject = (x) => x !== null && typeof x === "object";
25
+ var asObject = (x) => isObject(x) ? x : {};
28
26
  var isPlainObject = (x) => isObject(x) && !isArray(x);
29
27
  var toPlainObject = (x) => isPlainObject(x) ? x : _;
30
28
  var asPlainObject = (x) => isPlainObject(x) ? x : {};
31
- var toNonEmptyPlainObject = (x) => isPlainObject(x) && Object.keys(x).length > 0 ? x : _;
29
+ var isNonEmptyPlainObject = (x) => isPlainObject(x) && Object.keys(x).length > 0;
30
+ var toNonEmptyPlainObject = (x) => isNonEmptyPlainObject(x) ? x : _;
31
+ var isNonEmptyJSONObject = (x) => isPlainObject(x) && Object.values(x).some(isDefined);
32
+ var toNonEmptyJSONObject = (x) => isNonEmptyJSONObject(x) ? x : _;
32
33
  var toPlainObjectOf = (x, f) => {
33
34
  if (isPlainObject(x)) {
34
35
  let index = -1;
@@ -46,7 +47,25 @@ var toPlainObjectOf = (x, f) => {
46
47
  }
47
48
  };
48
49
  var toPlainObjectOfTrue = (x) => toPlainObjectOf(x, isTrue);
50
+ var print = (x) => {
51
+ if (isString(x)) return x;
52
+ if (x == null) return "";
53
+ try {
54
+ return JSON.stringify(x, null, 2);
55
+ } catch {
56
+ return x + "";
57
+ }
58
+ };
59
+
60
+ // src/returns.ts
61
+ var noop = () => {
62
+ };
63
+ var returnsUndefined = noop;
64
+ var returnsNull = () => null;
65
+ var returnsFalse = () => false;
66
+ var returnsTrue = () => true;
67
+ var returnsEmptyString = () => "";
49
68
 
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 };
69
+ export { asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy };
51
70
  //# sourceMappingURL=index.mjs.map
52
71
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/primitive.ts"],"names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0.1.1",
3
+ "version": "0.1.3",
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": "^22.9.0",
45
- "@vitest/coverage-v8": "^2.1.5",
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": "^9.1.0",
49
- "eslint-import-resolver-typescript": "^3.6.3",
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": "^6.1.1",
53
+ "pretty-bytes": "^7.1.0",
54
54
  "tsup": "^8.3.5",
55
- "typedoc": "^0.26.11",
55
+ "typedoc": "^0.28.14",
56
56
  "typescript": "^5.6.3",
57
- "typescript-eslint": "^8.14.0",
58
- "vitest": "^2.1.5",
57
+ "typescript-eslint": "^8.15.0",
58
+ "vitest": "^4.0.4",
59
59
  "yoctocolors": "^2.1.1"
60
60
  }
61
61
  }
package/src/index.ts CHANGED
@@ -1 +1,2 @@
1
- export * from "./primitive";
1
+ export * from "./is-to-as";
2
+ export * from "./returns";
@@ -1,9 +1,12 @@
1
1
  import { describe, it, expect } from "vitest";
2
2
 
3
3
  import {
4
+ isDefined,
4
5
  isTrue,
5
6
  toTrue,
6
7
  asTrue,
8
+ isTruthy,
9
+ isFalsy,
7
10
  isBoolean,
8
11
  toBoolean,
9
12
  isNumber,
@@ -12,18 +15,33 @@ import {
12
15
  isString,
13
16
  toString,
14
17
  asString,
18
+ isNonEmptyString,
19
+ toNonEmptyString,
15
20
  print,
16
- isObject,
17
21
  isArray,
22
+ toArray,
23
+ asArray,
24
+ toNonEmptyArray,
25
+ isObject,
26
+ asObject,
18
27
  isPlainObject,
19
28
  toPlainObject,
20
29
  asPlainObject,
30
+ isNonEmptyPlainObject,
21
31
  toNonEmptyPlainObject,
32
+ isNonEmptyJSONObject,
33
+ toNonEmptyJSONObject,
22
34
  toPlainObjectOf,
23
35
  toPlainObjectOfTrue,
36
+ toTruthy,
24
37
  } from ".";
25
38
 
26
39
  describe("primitive.ts", () => {
40
+ it("isDefined", () => {
41
+ expect(isDefined(1)).toBe(true);
42
+ expect(isDefined(undefined)).toBe(false);
43
+ });
44
+
27
45
  it("isTrue", () => {
28
46
  expect(isTrue(true)).toBe(true);
29
47
  expect(isTrue(false)).toBe(false);
@@ -42,6 +60,34 @@ describe("primitive.ts", () => {
42
60
  expect(asTrue(1)).toBe(false);
43
61
  });
44
62
 
63
+ it("isTruthy", () => {
64
+ expect(isTruthy(true)).toBe(true);
65
+ expect(isTruthy(false)).toBe(false);
66
+ expect(isTruthy(1)).toBe(true);
67
+ });
68
+
69
+ it("toTruthy", () => {
70
+ const expectTruthy = (o: unknown) => expect(toTruthy(o)).toBe(o);
71
+ expectTruthy({});
72
+ expectTruthy([]);
73
+ expectTruthy("string");
74
+ expectTruthy(Symbol());
75
+ expectTruthy(1);
76
+ expectTruthy(true);
77
+
78
+ expect(toTruthy(false)).toBe(undefined);
79
+ expect(toTruthy(0)).toBe(undefined);
80
+ expect(toTruthy(NaN)).toBe(undefined);
81
+ expect(toTruthy(null)).toBe(undefined);
82
+ expect(toTruthy(undefined)).toBe(undefined);
83
+ });
84
+
85
+ it("isFalsy", () => {
86
+ expect(isFalsy(true)).toBe(false);
87
+ expect(isFalsy(false)).toBe(true);
88
+ expect(isFalsy(1)).toBe(false);
89
+ });
90
+
45
91
  it("isBoolean", () => {
46
92
  expect(isBoolean(true)).toBe(true);
47
93
  expect(isBoolean(false)).toBe(true);
@@ -90,20 +136,36 @@ describe("primitive.ts", () => {
90
136
  expect(asString(true)).toBe("");
91
137
  });
92
138
 
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");
139
+ it("isNonEmptyString", () => {
140
+ expect(isNonEmptyString("hello")).toBe(true);
141
+ expect(isNonEmptyString("")).toBe(false);
142
+ expect(isNonEmptyString(1)).toBe(false);
143
+ });
144
+
145
+ it("toNonEmptyString", () => {
146
+ expect(toNonEmptyString("hello")).toBe("hello");
147
+ expect(toNonEmptyString("")).toBe(undefined);
148
+ expect(toNonEmptyString(1)).toBe(undefined);
149
+ });
150
+
151
+ it("isArray", () => {
152
+ expect(isArray([])).toBe(true);
153
+ expect(isArray({})).toBe(false);
154
+ });
155
+
156
+ it("toArray", () => {
157
+ expect(toArray([])).toEqual([]);
158
+ expect(toArray({})).toBe(undefined);
159
+ });
160
+
161
+ it("asArray", () => {
162
+ expect(asArray([])).toEqual([]);
163
+ expect(asArray({})).toEqual([]);
164
+ });
165
+
166
+ it("toNonEmptyArray", () => {
167
+ expect(toNonEmptyArray([1])).toEqual([1]);
168
+ expect(toNonEmptyArray([])).toBe(undefined);
107
169
  });
108
170
 
109
171
  it("isObject", () => {
@@ -112,9 +174,10 @@ describe("primitive.ts", () => {
112
174
  expect(isObject(null)).toBe(false);
113
175
  });
114
176
 
115
- it("isArray", () => {
116
- expect(isArray([])).toBe(true);
117
- expect(isArray({})).toBe(false);
177
+ it("asObject", () => {
178
+ expect(asObject({})).toEqual({});
179
+ expect(asObject([])).toEqual([]);
180
+ expect(asObject(null)).toEqual({});
118
181
  });
119
182
 
120
183
  it("isPlainObject", () => {
@@ -135,11 +198,28 @@ describe("primitive.ts", () => {
135
198
  expect(asPlainObject(null)).toEqual({});
136
199
  });
137
200
 
201
+ it("isNonEmptyPlainObject", () => {
202
+ expect(isNonEmptyPlainObject({ a: 1 })).toBe(true);
203
+ expect(isNonEmptyPlainObject({})).toBe(false);
204
+ expect(isNonEmptyPlainObject([])).toBe(false);
205
+ });
206
+
138
207
  it("toNonEmptyPlainObject", () => {
139
208
  expect(toNonEmptyPlainObject({ a: 1 })).toEqual({ a: 1 });
140
209
  expect(toNonEmptyPlainObject({})).toBe(undefined);
141
210
  });
142
211
 
212
+ it("isNonEmptyJSONObject", () => {
213
+ expect(isNonEmptyJSONObject({ a: 1 })).toBe(true);
214
+ expect(isNonEmptyJSONObject({})).toBe(false);
215
+ expect(isNonEmptyJSONObject([])).toBe(false);
216
+ });
217
+
218
+ it("toNonEmptyJSONObject", () => {
219
+ expect(toNonEmptyJSONObject({ a: 1 })).toEqual({ a: 1 });
220
+ expect(toNonEmptyJSONObject({})).toBe(undefined);
221
+ });
222
+
143
223
  it("toPlainObjectOf", () => {
144
224
  expect(toPlainObjectOf({ a: true, b: false }, isTrue)).toEqual({ a: true });
145
225
  expect(toPlainObjectOf({ a: 1, b: 2 }, isTrue)).toBe(undefined);
@@ -149,4 +229,20 @@ describe("primitive.ts", () => {
149
229
  expect(toPlainObjectOfTrue({ a: true, b: false })).toEqual({ a: true });
150
230
  expect(toPlainObjectOfTrue({ a: 1, b: 2 })).toBe(undefined);
151
231
  });
232
+
233
+ it("show", () => {
234
+ expect(print("hello")).toBe("hello");
235
+ expect(print(null)).toBe("");
236
+ expect(print({ a: 1 })).toBe(JSON.stringify({ a: 1 }, null, 2));
237
+ expect(
238
+ print({
239
+ toJSON() {
240
+ throw new Error("x");
241
+ },
242
+ toString() {
243
+ return "str";
244
+ },
245
+ })
246
+ ).toBe("str");
247
+ });
152
248
  });
@@ -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,22 @@ 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
+
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
+
12
31
  export const isBoolean = (x: unknown): x is boolean => x === true || x === false;
13
32
 
14
33
  /** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */
@@ -32,21 +51,22 @@ export const toString = (x: unknown): string | _ => (isString(x) ? x : _);
32
51
  /** Returns `x` if `x` is a string, otherwise returns `''` (empty string). */
33
52
  export const asString = (x: unknown): string => (isString(x) ? x : "");
34
53
 
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 print = (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
- };
54
+ /** Returns `true` if `x` is a string and not `''`. */
55
+ export const isNonEmptyString = (x: unknown): x is string => isString(x) && x !== "";
56
+
57
+ /** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
58
+ export const toNonEmptyString = (x: unknown): string | _ => (isNonEmptyString(x) ? x : _);
59
+
60
+ export const isArray = Array.isArray;
61
+
62
+ /** Returns `x` if `x` is an array. */
63
+ export const toArray = (x: unknown): unknown[] | undefined => (isArray(x) ? x : _);
64
+
65
+ /** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
66
+ export const toNonEmptyArray = <T>(x: T[]): T[] | _ => (x.length > 0 ? x : _);
67
+
68
+ /** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */
69
+ export const asArray = (x: unknown): unknown[] => (isArray(x) ? x : []);
50
70
 
51
71
  export interface PlainObject {
52
72
  [key: PropertyKey]: unknown;
@@ -58,14 +78,6 @@ export const isObject = (x: unknown): x is object => x !== null && typeof x ===
58
78
  /** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */
59
79
  export const asObject = (x: unknown): object => (isObject(x) ? x : {});
60
80
 
61
- export const isArray = Array.isArray;
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
-
69
81
  /** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
70
82
  export const isPlainObject = (x: unknown): x is PlainObject => isObject(x) && !isArray(x);
71
83
 
@@ -75,9 +87,18 @@ export const toPlainObject = (x: unknown): PlainObject | _ => (isPlainObject(x)
75
87
  /** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */
76
88
  export const asPlainObject = (x: unknown): PlainObject => (isPlainObject(x) ? x : {});
77
89
 
90
+ /** Returns `true` if `x` is a plain object and has at least one key. */
91
+ export const isNonEmptyPlainObject = (x: unknown): x is PlainObject => isPlainObject(x) && Object.keys(x).length > 0;
92
+
78
93
  /** Returns `x` if `x` is a plain object and has at least one key. */
79
- export const toNonEmptyPlainObject = <T extends PlainObject>(x: T): T | _ =>
80
- isPlainObject(x) && Object.keys(x).length > 0 ? x : _;
94
+ export const toNonEmptyPlainObject = <T extends PlainObject>(x: T): T | _ => (isNonEmptyPlainObject(x) ? x : _);
95
+
96
+ /** Returns `true` if `x` is a plain object and has at least one key with non-undefined value. */
97
+ export const isNonEmptyJSONObject = (x: unknown): x is PlainObject =>
98
+ isPlainObject(x) && Object.values(x).some(isDefined);
99
+
100
+ /** Returns `x` if `x` is a plain object and has at least one key with non-undefined value, otherwise returns `undefined`. */
101
+ export const toNonEmptyJSONObject = <T extends PlainObject>(x: T): T | _ => (isNonEmptyJSONObject(x) ? x : _);
81
102
 
82
103
  /**
83
104
  * Creates an object from `x` with keys `k` if `f(x[k])` returns `true`.
@@ -104,3 +125,19 @@ export const toPlainObjectOf = <T>(x: unknown, f: (v: unknown) => v is T): { [ke
104
125
 
105
126
  /** Filter props from object `x` whose values are `true`. */
106
127
  export const toPlainObjectOfTrue = (x: unknown): { [key: PropertyKey]: true } | _ => toPlainObjectOf(x, isTrue);
128
+
129
+ /**
130
+ * Returns `x` if `x` is string, otherwise returns `''` (empty string) if
131
+ * `x` is `null` or `undefined`, otherwise returns `JSON.stringify(x)`.
132
+ * This is very useful to show a value inside a React component.
133
+ */
134
+ export const print = (x: unknown): string => {
135
+ if (isString(x)) return x;
136
+ if (x == null) return "";
137
+ try {
138
+ return JSON.stringify(x, null, 2);
139
+ } catch {
140
+ // Insane case is not handled: x = { toString: () => { throw x } }
141
+ return x + "";
142
+ }
143
+ };
@@ -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 => "";