@wopjs/cast 0.1.8 → 0.1.10

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2024 wopjs
3
+ Copyright (c) 2026 wopjs
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -144,46 +144,50 @@ if (isTruthy(val)) {
144
144
  | `toPlainObjectOfTrue` | Filter object to only `true` values |
145
145
  | `print` | Safely stringify any value for display |
146
146
 
147
+ ### Return Helpers
148
+
149
+ Constant functions useful for callbacks and default values:
150
+
151
+ | Function | Returns |
152
+ | -------------------- | ----------- |
153
+ | `noop` | `void` |
154
+ | `returnsUndefined` | `undefined` |
155
+ | `returnsNull` | `null` |
156
+ | `returnsFalse` | `false` |
157
+ | `returnsTrue` | `true` |
158
+ | `returnsEmptyString` | `""` |
159
+
147
160
  ## Usage Example
148
161
 
149
162
  ```ts
150
- import * as c from "@wopjs/cast";
163
+ import {
164
+ asPlainObject,
165
+ asString,
166
+ toNumber,
167
+ asArray,
168
+ toNonEmptyPlainObject,
169
+ toPlainObjectOfTrue,
170
+ isNumber,
171
+ asNumber,
172
+ } from "@wopjs/cast";
151
173
 
152
174
  // Parsing unknown API response
153
175
  function parseUser(data: unknown) {
154
- const obj = c.asPlainObject(data);
176
+ const obj = asPlainObject(data);
155
177
  return {
156
- name: c.asString(obj.name),
157
- age: c.toNumber(obj.age), // undefined if not a number
158
- tags: c.asArray(obj.tags),
159
- settings: c.toNonEmptyPlainObject(obj.settings),
178
+ name: asString(obj.name),
179
+ age: toNumber(obj.age), // undefined if not a number
180
+ tags: asArray(obj.tags),
181
+ settings: toNonEmptyPlainObject(obj.settings),
160
182
  };
161
183
  }
162
184
 
163
185
  // Filtering object values
164
186
  const config = { debug: true, verbose: false, enabled: true };
165
- c.toPlainObjectOfTrue(config); // { debug: true, enabled: true }
187
+ toPlainObjectOfTrue(config); // { debug: true, enabled: true }
166
188
 
167
189
  // Safe number handling
168
- c.isNumber(NaN); // false (NaN is excluded)
169
- c.asNumber(NaN); // 0 (safe fallback)
170
- c.asNumber("42"); // 0 (not coerced, use parseInt for that)
171
- ```
172
-
173
- ## Publish New Version
174
-
175
- You can use [npm version](https://docs.npmjs.com/cli/v10/commands/npm-version) to bump version.
176
-
190
+ isNumber(NaN); // false (NaN is excluded)
191
+ asNumber(NaN); // 0 (safe fallback)
192
+ asNumber("42"); // 0 (not coerced, use parseInt for that)
177
193
  ```
178
- npm version patch
179
- ```
180
-
181
- Push the tag to remote and CI will publish the new version to npm.
182
-
183
- ```
184
- git push --follow-tags
185
- ```
186
-
187
- ### CI Publish
188
-
189
- 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).
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  type _ = undefined;
2
- /** Map `never` to a different type */
3
- type MapNeverTo<T, U> = [T, U][T extends any ? 0 : 1];
2
+ /** Returns `U` if `T` is `never` or `any`, otherwise returns `T`. */
3
+ type SetDefaultType<T, U> = [T, U][T extends any ? (0 extends 1 & T ? 1 : 0) : 1];
4
4
  /** Returns `true` if `x` is not `undefined`. */
5
5
  declare const isDefined: <T>(x: T | undefined) => x is T;
6
6
  declare const isTrue: (x: unknown) => x is true;
@@ -9,7 +9,7 @@ declare const toTrue: (x: unknown) => true | _;
9
9
  /** Returns `true` if `x` is `true`, otherwise returns `false`. */
10
10
  declare const asTrue: (x: unknown) => boolean;
11
11
  type Falsy = false | null | undefined | 0 | "";
12
- type ExtractFalsy<T> = MapNeverTo<Extract<T, Falsy>, Falsy>;
12
+ type ExtractFalsy<T> = SetDefaultType<Extract<T, Falsy>, Falsy>;
13
13
  /** Returns `true` if `Boolean(x)` is `false`. */
14
14
  declare const isFalsy: <T>(x: T) => x is ExtractFalsy<T>;
15
15
  /** Returns `x` if `Boolean(x)` is `false`, otherwise returns `undefined`. */
@@ -37,7 +37,8 @@ declare const asString: (x: unknown) => string;
37
37
  declare const isNonEmptyString: (x: unknown) => x is string;
38
38
  /** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
39
39
  declare const toNonEmptyString: (x: unknown) => string | _;
40
- type ExtractArray<T> = MapNeverTo<Extract<T, readonly unknown[]>, unknown[]>;
40
+ /** Extracts array type from `T`, or `unknown[]` if no array type found. */
41
+ type ExtractArray<T> = SetDefaultType<Extract<T, readonly unknown[]>, unknown[]>;
41
42
  declare const isArray: <T>(x: T) => x is ExtractArray<T>;
42
43
  /** Returns `x` if `x` is an array. */
43
44
  declare const toArray: <T>(x: T) => ExtractArray<T> | _;
@@ -47,7 +48,7 @@ declare const asArray: <T>(x: T) => ExtractArray<T>;
47
48
  declare const isNonEmptyArray: <T>(x: T) => x is ExtractArray<T>;
48
49
  /** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
49
50
  declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | _;
50
- type ExtractObject<T> = MapNeverTo<Extract<T, object>, object>;
51
+ type ExtractObject<T> = SetDefaultType<Extract<T, object>, object>;
51
52
  /** Returns `true` if `x` is an object (including array) and not null. */
52
53
  declare const isObject: <T>(x: T) => x is ExtractObject<T>;
53
54
  /** Returns `x` if `x` is an object (including array). */
@@ -57,7 +58,7 @@ declare const asObject: <T>(x: T) => ExtractObject<T>;
57
58
  interface PlainObject {
58
59
  [key: PropertyKey]: unknown;
59
60
  }
60
- type ExtractPlainObject<T> = MapNeverTo<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>;
61
+ type ExtractPlainObject<T> = SetDefaultType<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>;
61
62
  /** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
62
63
  declare const isPlainObject: <T>(x: T) => x is ExtractPlainObject<T>;
63
64
  /** Returns `x` if `x` is a plain object. */
@@ -94,4 +95,4 @@ declare const returnsFalse: () => false;
94
95
  declare const returnsTrue: () => true;
95
96
  declare const returnsEmptyString: () => string;
96
97
 
97
- export { type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type MapNeverTo, type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy };
98
+ export { type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type PlainObject, type SetDefaultType, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  type _ = undefined;
2
- /** Map `never` to a different type */
3
- type MapNeverTo<T, U> = [T, U][T extends any ? 0 : 1];
2
+ /** Returns `U` if `T` is `never` or `any`, otherwise returns `T`. */
3
+ type SetDefaultType<T, U> = [T, U][T extends any ? (0 extends 1 & T ? 1 : 0) : 1];
4
4
  /** Returns `true` if `x` is not `undefined`. */
5
5
  declare const isDefined: <T>(x: T | undefined) => x is T;
6
6
  declare const isTrue: (x: unknown) => x is true;
@@ -9,7 +9,7 @@ declare const toTrue: (x: unknown) => true | _;
9
9
  /** Returns `true` if `x` is `true`, otherwise returns `false`. */
10
10
  declare const asTrue: (x: unknown) => boolean;
11
11
  type Falsy = false | null | undefined | 0 | "";
12
- type ExtractFalsy<T> = MapNeverTo<Extract<T, Falsy>, Falsy>;
12
+ type ExtractFalsy<T> = SetDefaultType<Extract<T, Falsy>, Falsy>;
13
13
  /** Returns `true` if `Boolean(x)` is `false`. */
14
14
  declare const isFalsy: <T>(x: T) => x is ExtractFalsy<T>;
15
15
  /** Returns `x` if `Boolean(x)` is `false`, otherwise returns `undefined`. */
@@ -37,7 +37,8 @@ declare const asString: (x: unknown) => string;
37
37
  declare const isNonEmptyString: (x: unknown) => x is string;
38
38
  /** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
39
39
  declare const toNonEmptyString: (x: unknown) => string | _;
40
- type ExtractArray<T> = MapNeverTo<Extract<T, readonly unknown[]>, unknown[]>;
40
+ /** Extracts array type from `T`, or `unknown[]` if no array type found. */
41
+ type ExtractArray<T> = SetDefaultType<Extract<T, readonly unknown[]>, unknown[]>;
41
42
  declare const isArray: <T>(x: T) => x is ExtractArray<T>;
42
43
  /** Returns `x` if `x` is an array. */
43
44
  declare const toArray: <T>(x: T) => ExtractArray<T> | _;
@@ -47,7 +48,7 @@ declare const asArray: <T>(x: T) => ExtractArray<T>;
47
48
  declare const isNonEmptyArray: <T>(x: T) => x is ExtractArray<T>;
48
49
  /** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
49
50
  declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | _;
50
- type ExtractObject<T> = MapNeverTo<Extract<T, object>, object>;
51
+ type ExtractObject<T> = SetDefaultType<Extract<T, object>, object>;
51
52
  /** Returns `true` if `x` is an object (including array) and not null. */
52
53
  declare const isObject: <T>(x: T) => x is ExtractObject<T>;
53
54
  /** Returns `x` if `x` is an object (including array). */
@@ -57,7 +58,7 @@ declare const asObject: <T>(x: T) => ExtractObject<T>;
57
58
  interface PlainObject {
58
59
  [key: PropertyKey]: unknown;
59
60
  }
60
- type ExtractPlainObject<T> = MapNeverTo<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>;
61
+ type ExtractPlainObject<T> = SetDefaultType<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>;
61
62
  /** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
62
63
  declare const isPlainObject: <T>(x: T) => x is ExtractPlainObject<T>;
63
64
  /** Returns `x` if `x` is a plain object. */
@@ -94,4 +95,4 @@ declare const returnsFalse: () => false;
94
95
  declare const returnsTrue: () => true;
95
96
  declare const returnsEmptyString: () => string;
96
97
 
97
- export { type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type MapNeverTo, type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy };
98
+ export { type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type PlainObject, type SetDefaultType, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy };
package/dist/index.js CHANGED
@@ -31,9 +31,19 @@ var asObject = (x) => isObject(x) ? x : {};
31
31
  var isPlainObject = (x) => isObject(x) && !isArray(x);
32
32
  var toPlainObject = (x) => isPlainObject(x) ? x : _;
33
33
  var asPlainObject = (x) => isPlainObject(x) ? x : {};
34
- var isNonEmptyPlainObject = (x) => isPlainObject(x) && Object.keys(x).length > 0;
34
+ var walkPlainObjectValues = (x, predicate) => {
35
+ if (isPlainObject(x)) {
36
+ for (let key in x) {
37
+ if (Object.hasOwn(x, key) && (!predicate || predicate(x[key]))) {
38
+ return true;
39
+ }
40
+ }
41
+ }
42
+ return false;
43
+ };
44
+ var isNonEmptyPlainObject = (x) => walkPlainObjectValues(x);
35
45
  var toNonEmptyPlainObject = (x) => isNonEmptyPlainObject(x) ? x : _;
36
- var isNonEmptyJSONObject = (x) => isPlainObject(x) && Object.values(x).some(isDefined);
46
+ var isNonEmptyJSONObject = (x) => walkPlainObjectValues(x, isDefined);
37
47
  var toNonEmptyJSONObject = (x) => isNonEmptyJSONObject(x) ? x : _;
38
48
  var toPlainObjectOf = (x, f) => {
39
49
  if (isPlainObject(x)) {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/is-to-as.ts","../src/returns.ts"],"names":[],"mappings":";;;AAAA,IAAM,CAAA,GAAI,MAAA;AAOH,IAAM,SAAA,GAAY,CAAI,CAAA,KAA6B,CAAA,KAAM;AAEzD,IAAM,MAAA,GAAS,CAAC,CAAA,KAA0B,CAAA,KAAM;AAGhD,IAAM,MAAA,GAAS,CAAC,CAAA,KAA0B,CAAA,KAAM,OAAO,IAAA,GAAO;AAG9D,IAAM,MAAA,GAAS,CAAC,CAAA,KAAyB,CAAA,KAAM,OAAO,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EhC,IAAM,qBAAqB,MAAc","file":"index.js"}
package/dist/index.mjs CHANGED
@@ -29,9 +29,19 @@ var asObject = (x) => isObject(x) ? x : {};
29
29
  var isPlainObject = (x) => isObject(x) && !isArray(x);
30
30
  var toPlainObject = (x) => isPlainObject(x) ? x : _;
31
31
  var asPlainObject = (x) => isPlainObject(x) ? x : {};
32
- var isNonEmptyPlainObject = (x) => isPlainObject(x) && Object.keys(x).length > 0;
32
+ var walkPlainObjectValues = (x, predicate) => {
33
+ if (isPlainObject(x)) {
34
+ for (let key in x) {
35
+ if (Object.hasOwn(x, key) && (!predicate || predicate(x[key]))) {
36
+ return true;
37
+ }
38
+ }
39
+ }
40
+ return false;
41
+ };
42
+ var isNonEmptyPlainObject = (x) => walkPlainObjectValues(x);
33
43
  var toNonEmptyPlainObject = (x) => isNonEmptyPlainObject(x) ? x : _;
34
- var isNonEmptyJSONObject = (x) => isPlainObject(x) && Object.values(x).some(isDefined);
44
+ var isNonEmptyJSONObject = (x) => walkPlainObjectValues(x, isDefined);
35
45
  var toNonEmptyJSONObject = (x) => isNonEmptyJSONObject(x) ? x : _;
36
46
  var toPlainObjectOf = (x, f) => {
37
47
  if (isPlainObject(x)) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/is-to-as.ts","../src/returns.ts"],"names":[],"mappings":";AAAA,IAAM,CAAA,GAAI,MAAA;AAOH,IAAM,SAAA,GAAY,CAAI,CAAA,KAA6B,CAAA,KAAM;AAEzD,IAAM,MAAA,GAAS,CAAC,CAAA,KAA0B,CAAA,KAAM;AAGhD,IAAM,MAAA,GAAS,CAAC,CAAA,KAA0B,CAAA,KAAM,OAAO,IAAA,GAAO;AAG9D,IAAM,MAAA,GAAS,CAAC,CAAA,KAAyB,CAAA,KAAM,OAAO,CAAA,GAAI;AAO1D,IAAM,OAAA,GAAU,CAAI,CAAA,KAA+B,CAAC;AAGpD,IAAM,UAAU,CAAI,CAAA,KAA+B,OAAA,CAAQ,CAAC,IAAI,CAAA,GAAI;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
4
  "description": "Type-safe utilities for filtering and coercing unknown values in TypeScript.",
5
5
  "repository": "wopjs/cast",
6
6
  "main": "./dist/index.js",
@@ -49,7 +49,7 @@
49
49
  "license": "MIT",
50
50
  "devDependencies": {
51
51
  "@eslint/js": "^9.15.0",
52
- "@types/node": "^24.9.1",
52
+ "@types/node": "^25.0.9",
53
53
  "@vitest/coverage-v8": "^4.0.4",
54
54
  "commit-and-tag-version": "^12.5.0",
55
55
  "eslint": "^9.15.0",
@@ -304,6 +304,16 @@ describe("primitive.ts", () => {
304
304
  expect(check).toBe(undefined);
305
305
  }
306
306
  }
307
+
308
+ {
309
+ // Type narrowing - generic
310
+ const _fn = <T>(x: T): void => {
311
+ if (isArray(x)) {
312
+ const y: readonly any[] = x;
313
+ expect(Array.isArray(y)).toBe(true);
314
+ }
315
+ };
316
+ }
307
317
  });
308
318
 
309
319
  it("toArray", () => {
@@ -372,6 +382,15 @@ describe("primitive.ts", () => {
372
382
  const arr = castType<string>("hello");
373
383
  const result: unknown[] | undefined = toArray(arr);
374
384
  expect(result).toBe(undefined);
385
+ toArray(arr)?.map(x => x);
386
+ }
387
+
388
+ {
389
+ // Type narrowing - any returns any[]
390
+ const arr = castType<any>("hello");
391
+ const result: unknown[] | undefined = toArray(arr);
392
+ expect(result).toBe(undefined);
393
+ toArray(arr)?.map(x => x);
375
394
  }
376
395
  });
377
396
 
@@ -507,6 +526,7 @@ describe("primitive.ts", () => {
507
526
  const arr = castType<unknown>(["a", "b"]);
508
527
  const result: unknown[] = asArray(arr);
509
528
  expect(result).toEqual(["a", "b"]);
529
+ asArray(arr).map(x => x);
510
530
  }
511
531
 
512
532
  {
@@ -514,6 +534,7 @@ describe("primitive.ts", () => {
514
534
  const arr = castType<string>("hello");
515
535
  const result: unknown[] = asArray(arr);
516
536
  expect(result).toEqual([]);
537
+ asArray(arr).map(x => x);
517
538
  }
518
539
  });
519
540
 
package/src/is-to-as.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  const _ = undefined;
2
2
  export type _ = undefined;
3
3
 
4
- /** Map `never` to a different type */
5
- export type MapNeverTo<T, U> = [T, U][T extends any ? 0 : 1];
4
+ /** Returns `U` if `T` is `never` or `any`, otherwise returns `T`. */
5
+ export type SetDefaultType<T, U> = [T, U][T extends any ? (0 extends 1 & T ? 1 : 0) : 1];
6
6
 
7
7
  /** Returns `true` if `x` is not `undefined`. */
8
8
  export const isDefined = <T>(x: T | undefined): x is T => x !== _;
@@ -17,7 +17,7 @@ export const asTrue = (x: unknown): boolean => (x === true ? x : false);
17
17
 
18
18
  export type Falsy = false | null | undefined | 0 | "";
19
19
 
20
- export type ExtractFalsy<T> = MapNeverTo<Extract<T, Falsy>, Falsy>;
20
+ export type ExtractFalsy<T> = SetDefaultType<Extract<T, Falsy>, Falsy>;
21
21
 
22
22
  /** Returns `true` if `Boolean(x)` is `false`. */
23
23
  export const isFalsy = <T>(x: T): x is ExtractFalsy<T> => !x;
@@ -60,7 +60,8 @@ export const isNonEmptyString = (x: unknown): x is string => isString(x) && x !=
60
60
  /** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
61
61
  export const toNonEmptyString = (x: unknown): string | _ => (isNonEmptyString(x) ? x : _);
62
62
 
63
- export type ExtractArray<T> = MapNeverTo<Extract<T, readonly unknown[]>, unknown[]>;
63
+ /** Extracts array type from `T`, or `unknown[]` if no array type found. */
64
+ export type ExtractArray<T> = SetDefaultType<Extract<T, readonly unknown[]>, unknown[]>;
64
65
 
65
66
  export const isArray = Array.isArray as <T>(x: T) => x is ExtractArray<T>;
66
67
 
@@ -76,7 +77,7 @@ export const isNonEmptyArray = <T>(x: T): x is ExtractArray<T> => isArray(x) &&
76
77
  /** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */
77
78
  export const toNonEmptyArray = <T>(x: T): ExtractArray<T> | _ => (isNonEmptyArray(x) ? x : _);
78
79
 
79
- export type ExtractObject<T> = MapNeverTo<Extract<T, object>, object>;
80
+ export type ExtractObject<T> = SetDefaultType<Extract<T, object>, object>;
80
81
 
81
82
  /** Returns `true` if `x` is an object (including array) and not null. */
82
83
  export const isObject = <T>(x: T): x is ExtractObject<T> => x !== null && typeof x === "object";
@@ -91,7 +92,7 @@ export interface PlainObject {
91
92
  [key: PropertyKey]: unknown;
92
93
  }
93
94
 
94
- export type ExtractPlainObject<T> = MapNeverTo<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>;
95
+ export type ExtractPlainObject<T> = SetDefaultType<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>;
95
96
 
96
97
  /** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */
97
98
  export const isPlainObject = <T>(x: T): x is ExtractPlainObject<T> => isObject(x) && !isArray(x);
@@ -102,16 +103,25 @@ export const toPlainObject = <T>(x: T): ExtractPlainObject<T> | _ => (isPlainObj
102
103
  /** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */
103
104
  export const asPlainObject = <T>(x: T): ExtractPlainObject<T> => (isPlainObject(x) ? x : ({} as ExtractPlainObject<T>));
104
105
 
106
+ const walkPlainObjectValues = <T>(x: T, predicate?: (x: unknown) => boolean): boolean => {
107
+ if (isPlainObject(x)) {
108
+ for (let key in x) {
109
+ if (Object.hasOwn(x, key) && (!predicate || predicate(x[key]))) {
110
+ return true;
111
+ }
112
+ }
113
+ }
114
+ return false;
115
+ };
116
+
105
117
  /** Returns `true` if `x` is a plain object and has at least one key. */
106
- export const isNonEmptyPlainObject = <T>(x: T): x is ExtractPlainObject<T> =>
107
- isPlainObject(x) && Object.keys(x).length > 0;
118
+ export const isNonEmptyPlainObject = <T>(x: T): x is ExtractPlainObject<T> => walkPlainObjectValues(x);
108
119
 
109
120
  /** Returns `x` if `x` is a plain object and has at least one key. */
110
121
  export const toNonEmptyPlainObject = <T>(x: T): ExtractPlainObject<T> | _ => (isNonEmptyPlainObject(x) ? x : _);
111
122
 
112
123
  /** Returns `true` if `x` is a plain object and has at least one key with non-undefined value. */
113
- export const isNonEmptyJSONObject = <T>(x: T): x is ExtractPlainObject<T> =>
114
- isPlainObject(x) && Object.values(x).some(isDefined);
124
+ export const isNonEmptyJSONObject = <T>(x: T): x is ExtractPlainObject<T> => walkPlainObjectValues(x, isDefined);
115
125
 
116
126
  /** Returns `x` if `x` is a plain object and has at least one key with non-undefined value, otherwise returns `undefined`. */
117
127
  export const toNonEmptyJSONObject = <T>(x: T): ExtractPlainObject<T> | _ => (isNonEmptyJSONObject(x) ? x : _);