typebox 1.0.13 → 1.0.14

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.
@@ -1,7 +1,8 @@
1
1
  import { type TEnumValue } from '../../types/enum.mjs';
2
2
  import { type TUnionToTuple } from '../helpers/union.mjs';
3
- export type TTSEnumLike = Record<PropertyKey, TEnumValue>;
4
- type TReduceEnumValues<Keys extends string[], Type extends TTSEnumLike, Result extends TEnumValue[] = []> = (Keys extends [infer Left extends string, ...infer Right extends string[]] ? TReduceEnumValues<Right, Type, [...Result, Type[Left]]> : Result);
5
- export type TTSEnumToEnumValues<Type extends TTSEnumLike, EnumKeys extends string[] = TUnionToTuple<Extract<keyof Type, string>>, Elements extends TEnumValue[] = TReduceEnumValues<EnumKeys, Type>> = Elements;
6
- export declare function TSEnumToEnumValues<Type extends TTSEnumLike>(type: Type): TTSEnumToEnumValues<Type>;
3
+ export type TTypeScriptEnumLike = Record<PropertyKey, TEnumValue>;
4
+ export declare function IsTypeScriptEnumLike(value: unknown): value is TTypeScriptEnumLike;
5
+ type TReduceEnumValues<Keys extends string[], Type extends TTypeScriptEnumLike, Result extends TEnumValue[] = []> = (Keys extends [infer Left extends string, ...infer Right extends string[]] ? TReduceEnumValues<Right, Type, [...Result, Type[Left]]> : Result);
6
+ export type TTypeScriptEnumToEnumValues<Type extends TTypeScriptEnumLike, EnumKeys extends string[] = TUnionToTuple<Extract<keyof Type, string>>, Elements extends TEnumValue[] = TReduceEnumValues<EnumKeys, Type>> = Elements;
7
+ export declare function TypeScriptEnumToEnumValues<Type extends TTypeScriptEnumLike>(type: Type): TTypeScriptEnumToEnumValues<Type>;
7
8
  export {};
@@ -1,6 +1,9 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { Guard } from '../../../guard/index.mjs';
3
- export function TSEnumToEnumValues(type) {
3
+ export function IsTypeScriptEnumLike(value) {
4
+ return Guard.IsObjectNotArray(value);
5
+ }
6
+ export function TypeScriptEnumToEnumValues(type) {
4
7
  const keys = Guard.Keys(type).filter((key) => isNaN(key));
5
8
  return keys.reduce((result, key) => [...result, type[key]], []);
6
9
  }
@@ -1,5 +1,6 @@
1
1
  import { type TSchema, type TSchemaOptions } from './schema.mjs';
2
- import { type TTSEnumToEnumValues, type TTSEnumLike } from '../engine/enum/typescript-enum-to-enum-values.mjs';
2
+ import { type TTypeScriptEnumLike } from '../engine/enum/typescript-enum-to-enum-values.mjs';
3
+ import { type TTypeScriptEnumToEnumValues } from '../engine/enum/typescript-enum-to-enum-values.mjs';
3
4
  export type StaticEnum<Values extends TEnumValue[]> = (Values[number]);
4
5
  export type TEnumValue = string | number | null;
5
6
  /** Represents an Enum type. */
@@ -8,8 +9,8 @@ export interface TEnum<Values extends TEnumValue[] = TEnumValue[]> extends TSche
8
9
  enum: Values;
9
10
  }
10
11
  /** Creates an Enum type. */
11
- export declare function Enum<Enum extends TTSEnumLike>(value: Enum, options?: TSchemaOptions): TEnum<TTSEnumToEnumValues<Enum>>;
12
- /** Creates an Enum type. */
13
- export declare function Enum<Values extends TEnumValue[]>(values: [...Values], options?: TSchemaOptions): TEnum<Values>;
12
+ export declare function Enum<Values extends TEnumValue[]>(values: readonly [...Values], options?: TSchemaOptions): TEnum<Values>;
13
+ /** Creates an Enum type from a TypeScript enum declaration. */
14
+ export declare function Enum<Enum extends TTypeScriptEnumLike>(value: Enum, options?: TSchemaOptions): TEnum<TTypeScriptEnumToEnumValues<Enum>>;
14
15
  /** Returns true if the given value is a TEnum. */
15
16
  export declare function IsEnum(value: unknown): value is TEnum;
@@ -1,11 +1,11 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { Memory } from '../../system/memory/index.mjs';
3
- import { Guard } from '../../guard/index.mjs';
4
3
  import { IsKind } from './schema.mjs';
5
- import { TSEnumToEnumValues } from '../engine/enum/typescript-enum-to-enum-values.mjs';
4
+ import { IsTypeScriptEnumLike } from '../engine/enum/typescript-enum-to-enum-values.mjs';
5
+ import { TypeScriptEnumToEnumValues } from '../engine/enum/typescript-enum-to-enum-values.mjs';
6
6
  /** Creates an Enum type. */
7
7
  export function Enum(value, options) {
8
- const values = Guard.IsArray(value) ? value : TSEnumToEnumValues(value);
8
+ const values = IsTypeScriptEnumLike(value) ? TypeScriptEnumToEnumValues(value) : value;
9
9
  return Memory.Create({ '~kind': 'Enum' }, { enum: values }, options);
10
10
  }
11
11
  // ------------------------------------------------------------------
@@ -6,7 +6,7 @@ import { type TNumber } from './number.mjs';
6
6
  import { type TString } from './string.mjs';
7
7
  import { type TDeferred } from './deferred.mjs';
8
8
  import { type TInstantiate } from '../engine/instantiate.mjs';
9
- export type StaticRecord<Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Key extends string, Value extends TSchema, StaticValue extends unknown = StaticType<Direction, Context, This, Value>, Result extends Record<PropertyKey, unknown> = (Key extends TStringKey ? Record<string, StaticValue> : Key extends TIntegerKey ? Record<number, StaticValue> : Key extends TNumberKey ? Record<number, StaticValue> : Record<PropertyKey, StaticValue>)> = Result;
9
+ export type StaticRecord<Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Key extends string, Value extends TSchema, StaticValue extends unknown = StaticType<Direction, Context, This, Value>, Result extends Record<PropertyKey, unknown> = (Key extends TStringKey ? Record<string, StaticValue> : Key extends TIntegerKey ? Record<number, StaticValue> : Key extends TNumberKey ? Record<number, StaticValue> : Record<string, StaticValue>)> = Result;
10
10
  export type TStringKey = typeof StringKey;
11
11
  export type TIntegerKey = typeof IntegerKey;
12
12
  export type TNumberKey = typeof NumberKey;
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.13",
4
+ "version": "1.0.14",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"