typebox 1.0.5 → 1.0.6

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.
@@ -34,6 +34,8 @@ export declare function IsLessThan<Type extends number | bigint>(left: Type, rig
34
34
  export declare function IsLessEqualThan<Type extends number | bigint>(left: Type, right: Type): boolean;
35
35
  export declare function IsGreaterEqualThan<Type extends number | bigint>(left: Type, right: Type): boolean;
36
36
  export declare function IsMultipleOf(dividend: bigint | number, divisor: bigint | number): boolean;
37
+ /** Returns true if the value appears to be an instance of a class. */
38
+ export declare function IsClassInstance(value: unknown): boolean;
37
39
  export declare function IsValueLike(value: unknown): value is bigint | boolean | null | number | string | undefined;
38
40
  /** Returns the number of Unicode Grapheme Clusters */
39
41
  export declare function StringGraphemeCount(value: string): number;
@@ -102,6 +102,20 @@ export function IsMultipleOf(dividend, divisor) {
102
102
  return Math.min(Math.abs(mod), Math.abs(mod - divisor)) < tolerance;
103
103
  }
104
104
  // ------------------------------------------------------------------
105
+ // IsClassInstance
106
+ // ------------------------------------------------------------------
107
+ /** Returns true if the value appears to be an instance of a class. */
108
+ export function IsClassInstance(value) {
109
+ if (!IsObject(value))
110
+ return false;
111
+ const proto = globalThis.Object.getPrototypeOf(value);
112
+ if (IsNull(proto))
113
+ return false;
114
+ return IsEqual(typeof proto.constructor, 'function') &&
115
+ !(IsEqual(proto.constructor, globalThis.Object) ||
116
+ IsEqual(proto.constructor.name, 'Object'));
117
+ }
118
+ // ------------------------------------------------------------------
105
119
  // IsValueLike
106
120
  // ------------------------------------------------------------------
107
121
  export function IsValueLike(value) {
@@ -1,9 +1,22 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { Guard, GlobalsGuard } from '../../guard/index.mjs';
3
3
  // ------------------------------------------------------------------
4
- // Object
4
+ // ClassInstance
5
+ //
6
+ // TypeBox does not support cloning arbitrary class instances. It treats
7
+ // class instances as atomic values, similar to number, boolean, and
8
+ // string. In the future, an implementation could detect the presence of
9
+ // a .clone() method, but no formal specification for this behavior
10
+ // exists, so we don't.
11
+ //
5
12
  // ------------------------------------------------------------------
6
- function FromObject(value) {
13
+ function FromClassInstance(value) {
14
+ return value; // atomic
15
+ }
16
+ // ------------------------------------------------------------------
17
+ // ObjectInstance
18
+ // ------------------------------------------------------------------
19
+ function FromObjectInstance(value) {
7
20
  const result = {};
8
21
  for (const key of Object.getOwnPropertyNames(value)) {
9
22
  result[key] = Clone(value[key]);
@@ -14,6 +27,14 @@ function FromObject(value) {
14
27
  return result;
15
28
  }
16
29
  // ------------------------------------------------------------------
30
+ // Object
31
+ // ------------------------------------------------------------------
32
+ function FromObject(value) {
33
+ return (Guard.IsClassInstance(value)
34
+ ? FromClassInstance(value)
35
+ : FromObjectInstance(value));
36
+ }
37
+ // ------------------------------------------------------------------
17
38
  // Array
18
39
  // ------------------------------------------------------------------
19
40
  function FromArray(value) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typebox",
3
3
  "description": "A Runtime Type System for JavaScript",
4
- "version": "1.0.5",
4
+ "version": "1.0.6",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"
package/readme.md CHANGED
@@ -49,7 +49,7 @@ type T = Static<typeof T> // type T = {
49
49
 
50
50
  ## Overview
51
51
 
52
- [Documentation](https://sinclairzx81.github.io/typebox/)
52
+ [Documentation](https://sinclairzx81.github.io/typebox/) | [1.0 Migration Guide](https://github.com/sinclairzx81/typebox/blob/main/changelog/1.0.0-migration.md)
53
53
 
54
54
  TypeBox is a runtime type system that creates in-memory Json Schema objects that infer as TypeScript types. The schematics produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type system that can be statically checked by TypeScript and validated at runtime using standard Json Schema.
55
55