is-kit 1.15.2 → 1.15.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
@@ -674,9 +674,10 @@ export declare const isDataView: Guard$1<DataView<ArrayBufferLike>>;
674
674
  */
675
675
  export declare const isTypedArray: Predicate<ArrayBufferView<ArrayBufferLike>>;
676
676
  /**
677
- * Checks whether a value is an `Error` (instance of Error or plain Error object tag).
677
+ * Checks whether a value is an `Error`.
678
678
  *
679
679
  * @returns Predicate narrowing to `Error`.
680
+ * @see https://github.com/nyaomaru/is-kit/issues/325
680
681
  */
681
682
  export declare const isError: Predicate<Error>;
682
683
  /**
@@ -787,14 +788,18 @@ export declare function oneOfValues<const T extends ReadonlyArray<Primitive>>(va
787
788
  export declare function oneOfValues<const T extends ReadonlyArray<Primitive>>(...valuesOrArray: T | [T]): Predicate<T[number]>;
788
789
  //#endregion
789
790
  //#region src/core/combinators/record.d.ts
791
+ type RecordOfResult<K extends string, V> = string extends K ? Readonly<Record<string, V>> : Readonly<Partial<Record<K, V>>>;
790
792
  /**
791
793
  * Validates a record-like object by guarding both keys and values.
792
794
  *
793
- * @param keyFunction Guard for string keys (e.g., specific literal keys pattern).
795
+ * The key guard restricts which string keys may be present; it does not require
796
+ * every key represented by the guarded key type.
797
+ *
798
+ * @param keyFunction Guard for string keys (for example, a literal-key pattern).
794
799
  * @param valueFunction Guard applied to each value in the record.
795
800
  * @returns Predicate narrowing to a readonly record with guarded key/value types.
796
801
  */
797
- export declare function recordOf<KF extends Predicate<string>, VF extends Predicate<unknown>>(keyFunction: KF, valueFunction: VF): Predicate<Readonly<Record<GuardedOf<KF>, GuardedOf<VF>>>>;
802
+ export declare function recordOf<KF extends Predicate<string>, VF extends Predicate<unknown>>(keyFunction: KF, valueFunction: VF): Predicate<RecordOfResult<GuardedOf<KF>, GuardedOf<VF>>>;
798
803
  //#endregion
799
804
  //#region src/core/combinators/set.d.ts
800
805
  /**
package/dist/index.d.ts CHANGED
@@ -674,9 +674,10 @@ export declare const isDataView: Guard$1<DataView<ArrayBufferLike>>;
674
674
  */
675
675
  export declare const isTypedArray: Predicate<ArrayBufferView<ArrayBufferLike>>;
676
676
  /**
677
- * Checks whether a value is an `Error` (instance of Error or plain Error object tag).
677
+ * Checks whether a value is an `Error`.
678
678
  *
679
679
  * @returns Predicate narrowing to `Error`.
680
+ * @see https://github.com/nyaomaru/is-kit/issues/325
680
681
  */
681
682
  export declare const isError: Predicate<Error>;
682
683
  /**
@@ -787,14 +788,18 @@ export declare function oneOfValues<const T extends ReadonlyArray<Primitive>>(va
787
788
  export declare function oneOfValues<const T extends ReadonlyArray<Primitive>>(...valuesOrArray: T | [T]): Predicate<T[number]>;
788
789
  //#endregion
789
790
  //#region src/core/combinators/record.d.ts
791
+ type RecordOfResult<K extends string, V> = string extends K ? Readonly<Record<string, V>> : Readonly<Partial<Record<K, V>>>;
790
792
  /**
791
793
  * Validates a record-like object by guarding both keys and values.
792
794
  *
793
- * @param keyFunction Guard for string keys (e.g., specific literal keys pattern).
795
+ * The key guard restricts which string keys may be present; it does not require
796
+ * every key represented by the guarded key type.
797
+ *
798
+ * @param keyFunction Guard for string keys (for example, a literal-key pattern).
794
799
  * @param valueFunction Guard applied to each value in the record.
795
800
  * @returns Predicate narrowing to a readonly record with guarded key/value types.
796
801
  */
797
- export declare function recordOf<KF extends Predicate<string>, VF extends Predicate<unknown>>(keyFunction: KF, valueFunction: VF): Predicate<Readonly<Record<GuardedOf<KF>, GuardedOf<VF>>>>;
802
+ export declare function recordOf<KF extends Predicate<string>, VF extends Predicate<unknown>>(keyFunction: KF, valueFunction: VF): Predicate<RecordOfResult<GuardedOf<KF>, GuardedOf<VF>>>;
798
803
  //#endregion
799
804
  //#region src/core/combinators/set.d.ts
800
805
  /**
package/dist/index.js CHANGED
@@ -41,6 +41,7 @@ const getTag = (value) => objectToString.call(value);
41
41
  //#endregion
42
42
  //#region src/core/object.ts
43
43
  const BUILTIN_BRAND_SENTINEL = Object.freeze({});
44
+ const nativeIsError = Error.isError;
44
45
  const getIntrinsicGetter = (prototype, key) => {
45
46
  return Object.getOwnPropertyDescriptor(prototype, key)?.get ?? (() => {
46
47
  throw new TypeError(`Missing intrinsic getter: ${String(key)}`);
@@ -175,11 +176,18 @@ const isDataView = defineIntrinsicBrandGuard((value) => dataViewBuffer.call(valu
175
176
  */
176
177
  const isTypedArray = define((value) => typedArrayName?.call(value) !== void 0);
177
178
  /**
178
- * Checks whether a value is an `Error` (instance of Error or plain Error object tag).
179
+ * Checks whether a value is an `Error`.
179
180
  *
180
181
  * @returns Predicate narrowing to `Error`.
182
+ * @see https://github.com/nyaomaru/is-kit/issues/325
181
183
  */
182
- const isError = define((value) => value instanceof Error || getTag(value) === "[object Error]");
184
+ const isError = define((value) => {
185
+ try {
186
+ return typeof nativeIsError === "function" ? nativeIsError(value) : value instanceof Error || getTag(value) === "[object Error]";
187
+ } catch {
188
+ return false;
189
+ }
190
+ });
183
191
  /**
184
192
  * Checks whether a value is a `URL` (in environments with URL available).
185
193
  *
@@ -730,7 +738,10 @@ function oneOfValues(...args) {
730
738
  /**
731
739
  * Validates a record-like object by guarding both keys and values.
732
740
  *
733
- * @param keyFunction Guard for string keys (e.g., specific literal keys pattern).
741
+ * The key guard restricts which string keys may be present; it does not require
742
+ * every key represented by the guarded key type.
743
+ *
744
+ * @param keyFunction Guard for string keys (for example, a literal-key pattern).
734
745
  * @param valueFunction Guard applied to each value in the record.
735
746
  * @returns Predicate narrowing to a readonly record with guarded key/value types.
736
747
  */
@@ -754,6 +765,10 @@ const isOptionalSchemaField = define((field) => isObject(field) && field.optiona
754
765
  const hasRequiredKeys = (obj, entries) => entries.every(([key, guard]) => hasOwnPropertyKey(obj, key) && guard(obj[key]));
755
766
  const hasValidOptionalKeys = (obj, entries) => entries.every(([key, guard]) => !hasOwnPropertyKey(obj, key) || guard(obj[key]));
756
767
  const hasOnlyAllowedKeys = (obj, allowed) => Object.keys(obj).every((key) => allowed.has(key));
768
+ const hasOnlyRequiredKeys = (obj, schemaKeys, allowed) => {
769
+ const keys = Object.keys(obj);
770
+ return keys.length <= schemaKeys.length && keys.every((key, index) => key === schemaKeys[index] || allowed.has(key));
771
+ };
757
772
  /**
758
773
  * Marks a struct schema field as optional at the key level.
759
774
  *
@@ -801,10 +816,12 @@ function createStruct(schema, options) {
801
816
  requiredEntries.push([key, field]);
802
817
  }
803
818
  const allowed = options?.exact ? new Set(schemaKeys) : null;
819
+ const requiredOnly = optionalEntries.length === 0;
804
820
  return define((input) => {
805
821
  if (!isPlainObject(input)) return false;
806
822
  const obj = input;
807
823
  if (!hasRequiredKeys(obj, requiredEntries)) return false;
824
+ if (allowed && requiredOnly) return hasOnlyRequiredKeys(obj, schemaKeys, allowed);
808
825
  if (!hasValidOptionalKeys(obj, optionalEntries)) return false;
809
826
  if (!allowed) return true;
810
827
  return hasOnlyAllowedKeys(obj, allowed);
package/dist/index.mjs CHANGED
@@ -40,6 +40,7 @@ const getTag = (value) => objectToString.call(value);
40
40
  //#endregion
41
41
  //#region src/core/object.ts
42
42
  const BUILTIN_BRAND_SENTINEL = Object.freeze({});
43
+ const nativeIsError = Error.isError;
43
44
  const getIntrinsicGetter = (prototype, key) => {
44
45
  return Object.getOwnPropertyDescriptor(prototype, key)?.get ?? (() => {
45
46
  throw new TypeError(`Missing intrinsic getter: ${String(key)}`);
@@ -174,11 +175,18 @@ const isDataView = defineIntrinsicBrandGuard((value) => dataViewBuffer.call(valu
174
175
  */
175
176
  const isTypedArray = define((value) => typedArrayName?.call(value) !== void 0);
176
177
  /**
177
- * Checks whether a value is an `Error` (instance of Error or plain Error object tag).
178
+ * Checks whether a value is an `Error`.
178
179
  *
179
180
  * @returns Predicate narrowing to `Error`.
181
+ * @see https://github.com/nyaomaru/is-kit/issues/325
180
182
  */
181
- const isError = define((value) => value instanceof Error || getTag(value) === "[object Error]");
183
+ const isError = define((value) => {
184
+ try {
185
+ return typeof nativeIsError === "function" ? nativeIsError(value) : value instanceof Error || getTag(value) === "[object Error]";
186
+ } catch {
187
+ return false;
188
+ }
189
+ });
182
190
  /**
183
191
  * Checks whether a value is a `URL` (in environments with URL available).
184
192
  *
@@ -729,7 +737,10 @@ function oneOfValues(...args) {
729
737
  /**
730
738
  * Validates a record-like object by guarding both keys and values.
731
739
  *
732
- * @param keyFunction Guard for string keys (e.g., specific literal keys pattern).
740
+ * The key guard restricts which string keys may be present; it does not require
741
+ * every key represented by the guarded key type.
742
+ *
743
+ * @param keyFunction Guard for string keys (for example, a literal-key pattern).
733
744
  * @param valueFunction Guard applied to each value in the record.
734
745
  * @returns Predicate narrowing to a readonly record with guarded key/value types.
735
746
  */
@@ -753,6 +764,10 @@ const isOptionalSchemaField = define((field) => isObject(field) && field.optiona
753
764
  const hasRequiredKeys = (obj, entries) => entries.every(([key, guard]) => hasOwnPropertyKey(obj, key) && guard(obj[key]));
754
765
  const hasValidOptionalKeys = (obj, entries) => entries.every(([key, guard]) => !hasOwnPropertyKey(obj, key) || guard(obj[key]));
755
766
  const hasOnlyAllowedKeys = (obj, allowed) => Object.keys(obj).every((key) => allowed.has(key));
767
+ const hasOnlyRequiredKeys = (obj, schemaKeys, allowed) => {
768
+ const keys = Object.keys(obj);
769
+ return keys.length <= schemaKeys.length && keys.every((key, index) => key === schemaKeys[index] || allowed.has(key));
770
+ };
756
771
  /**
757
772
  * Marks a struct schema field as optional at the key level.
758
773
  *
@@ -800,10 +815,12 @@ function createStruct(schema, options) {
800
815
  requiredEntries.push([key, field]);
801
816
  }
802
817
  const allowed = options?.exact ? new Set(schemaKeys) : null;
818
+ const requiredOnly = optionalEntries.length === 0;
803
819
  return define((input) => {
804
820
  if (!isPlainObject(input)) return false;
805
821
  const obj = input;
806
822
  if (!hasRequiredKeys(obj, requiredEntries)) return false;
823
+ if (allowed && requiredOnly) return hasOnlyRequiredKeys(obj, schemaKeys, allowed);
807
824
  if (!hasValidOptionalKeys(obj, optionalEntries)) return false;
808
825
  if (!allowed) return true;
809
826
  return hasOnlyAllowedKeys(obj, allowed);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "is-kit",
3
- "version": "1.15.2",
3
+ "version": "1.15.3",
4
4
  "description": "Zero-dependency TypeScript runtime validation toolkit for composable type guards and natural type narrowing.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",