@drunkcod/argis 0.0.17 → 0.0.19

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/lib/Json.d.ts CHANGED
@@ -1,23 +1,27 @@
1
- import { AnyFn, IfOptional, Pretty, TagCycle, TagCycles } from './TypeUtils.js';
1
+ import { AnyFn, IfOptional, Pretty, Tagged, TagCycle, TagCycles, IsAny } from './TypeUtils.js';
2
2
  export interface Jsonable<Json> {
3
3
  toJSON(): Json;
4
4
  }
5
5
  type NeverJson = undefined | symbol | AnyFn;
6
6
  type EmptyJson = Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | RegExp;
7
7
  declare const JsonError: unique symbol;
8
- export type JsonError<T, Data = never> = [Data] extends [never] ? {
9
- [JsonError]: T;
10
- } : {
11
- [JsonError]: T;
12
- data: Data;
8
+ export type JsonError<T, Data = {}> = Tagged<typeof JsonError, T, Data>;
9
+ export type JsonCycleError<T> = JsonError<'cycle-detected', {
10
+ data: T;
11
+ }>;
12
+ type JsonProperty<T, P extends keyof T = keyof T> = P extends string | number ? (_Json<T[P]> extends never ? never : P) : never;
13
+ type OptionalKeys<T, K> = K extends keyof T ? IfOptional<T, K, K, never> : never;
14
+ type Keys<T, K extends keyof T, Optional = OptionalKeys<T, K>> = {
15
+ required: Exclude<K, Optional>;
16
+ optional: Optional;
13
17
  };
14
- type JsonProperty<T, P extends keyof T> = P extends string | number ? (Json<T[P]> extends never ? never : P) : never;
15
- type JsonObject<T> = {
16
- [P in keyof T as IfOptional<T, P, never, JsonProperty<T, P>>]: Pretty<_Json<T[P]>>;
18
+ type JsonObject<T> = _JsonObject<T, Keys<T, JsonProperty<T>>>;
19
+ type _JsonObject<T, K extends Keys<T, any>> = {
20
+ [P in K['required']]: Pretty<_Json<T[P]>>;
17
21
  } & {
18
- [P in keyof T as IfOptional<T, P, JsonProperty<T, P>, never>]?: Pretty<_Json<T[P]>>;
22
+ [P in K['optional']]?: Pretty<_Json<T[P]>>;
19
23
  };
20
- type _Json<T, IsRoot = true> = T extends TagCycle<infer X> ? (X extends Jsonable<infer J> ? _Json<TagCycles<J>, false> : JsonError<'cycle-detected', X>) : T extends Jsonable<infer J> ? (IsRoot extends true ? _Json<TagCycles<J>, false> : _Json<Omit<T, 'toJSON'>>) : T extends NeverJson ? never : T extends EmptyJson ? Record<string, never> : T extends bigint ? JsonError<'bigint-not-serializeable'> : T extends readonly any[] ? {
24
+ type _Json<T, IsRoot = true> = IsAny<T> extends true ? any : T extends TagCycle<infer X> ? (X extends Jsonable<infer J> ? Json<J, false> : JsonCycleError<X>) : T extends Jsonable<infer J> ? (IsRoot extends true ? Json<J, false> : _Json<Omit<T, 'toJSON'>>) : T extends NeverJson ? never : T extends EmptyJson ? Record<string, never> : T extends bigint ? JsonError<'bigint-not-serializeable'> : T extends readonly any[] ? {
21
25
  [I in keyof T]: [_Json<T[I]>] extends [never] ? null : Pretty<_Json<T[I]>>;
22
26
  } : T extends object ? JsonObject<T> : T;
23
27
  export type Json<T, IsRoot = true> = Pretty<_Json<TagCycles<T>, IsRoot>>;
@@ -1,15 +1,16 @@
1
- export type Pretty<T> = {
1
+ export type Pretty<T> = T extends object ? {
2
2
  [P in keyof T]: T[P];
3
- } & {};
4
- declare const SPECIAL_TAG: unique symbol;
5
- type SpecialTag<T, Data = never> = {
6
- readonly [SPECIAL_TAG]: T;
7
- data: Data;
8
- };
3
+ } & {} : T;
4
+ export type Optional<T, K extends keyof T> = Pretty<Omit<T, K> & Partial<Pick<T, K>>>;
5
+ export type Tagged<Sym extends symbol, T, Data = {}> = Pretty<Readonly<Record<Sym, T>> & Data>;
6
+ declare const SpecialTag: unique symbol;
7
+ type SpecialTag<T, Data = {}> = Tagged<typeof SpecialTag, T, Data>;
9
8
  type TagAny = SpecialTag<'any'>;
10
9
  type TagOptional = SpecialTag<'?'>;
11
10
  type TagUnknown = SpecialTag<'unknown'>;
12
- export type TagCycle<T> = SpecialTag<'cycle', T>;
11
+ export type TagCycle<T> = SpecialTag<'cycle', {
12
+ data: T;
13
+ }>;
13
14
  export type AnyFn = (...args: any[]) => any;
14
15
  export type IsAny<T> = 0 extends 1 & T ? true : false;
15
16
  export type IsFn<T> = T extends AnyFn ? true : false;
@@ -45,4 +46,9 @@ export type UnionMerge<A, B> = Pretty<UnwrapTags<UnionMergeCore<TagSpecial<A>, T
45
46
  export type PickRequired<T> = {
46
47
  [P in keyof T as IfOptional<T, P, never, P>]: T[P];
47
48
  };
49
+ export type Substitute<T, Target, Replacement> = T extends any ? IsIdentical<T, Target> extends true ? Replacement : T extends SpecialTag<any> ? T : IsAny<T> extends true ? T : IsUnknown<T> extends true ? T : IsFn<T> extends true ? T : T extends readonly any[] ? {
50
+ [K in keyof T]: Substitute<T[K], Target, Replacement>;
51
+ } : T extends object ? Pretty<UnwrapTags<{
52
+ [P in keyof TagSpecial<T>]: Substitute<TagSpecial<T>[P], Target, Replacement>;
53
+ }>> : T : never;
48
54
  export {};
package/lib/TypeUtils.js CHANGED
@@ -1,2 +1,2 @@
1
- const SPECIAL_TAG = Symbol('');
1
+ const SpecialTag = Symbol('SpecialTag');
2
2
  export {};
package/lib/cjs/Json.d.ts CHANGED
@@ -1,23 +1,27 @@
1
- import { AnyFn, IfOptional, Pretty, TagCycle, TagCycles } from './TypeUtils.js';
1
+ import { AnyFn, IfOptional, Pretty, Tagged, TagCycle, TagCycles, IsAny } from './TypeUtils.js';
2
2
  export interface Jsonable<Json> {
3
3
  toJSON(): Json;
4
4
  }
5
5
  type NeverJson = undefined | symbol | AnyFn;
6
6
  type EmptyJson = Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | RegExp;
7
7
  declare const JsonError: unique symbol;
8
- export type JsonError<T, Data = never> = [Data] extends [never] ? {
9
- [JsonError]: T;
10
- } : {
11
- [JsonError]: T;
12
- data: Data;
8
+ export type JsonError<T, Data = {}> = Tagged<typeof JsonError, T, Data>;
9
+ export type JsonCycleError<T> = JsonError<'cycle-detected', {
10
+ data: T;
11
+ }>;
12
+ type JsonProperty<T, P extends keyof T = keyof T> = P extends string | number ? (_Json<T[P]> extends never ? never : P) : never;
13
+ type OptionalKeys<T, K> = K extends keyof T ? IfOptional<T, K, K, never> : never;
14
+ type Keys<T, K extends keyof T, Optional = OptionalKeys<T, K>> = {
15
+ required: Exclude<K, Optional>;
16
+ optional: Optional;
13
17
  };
14
- type JsonProperty<T, P extends keyof T> = P extends string | number ? (Json<T[P]> extends never ? never : P) : never;
15
- type JsonObject<T> = {
16
- [P in keyof T as IfOptional<T, P, never, JsonProperty<T, P>>]: Pretty<_Json<T[P]>>;
18
+ type JsonObject<T> = _JsonObject<T, Keys<T, JsonProperty<T>>>;
19
+ type _JsonObject<T, K extends Keys<T, any>> = {
20
+ [P in K['required']]: Pretty<_Json<T[P]>>;
17
21
  } & {
18
- [P in keyof T as IfOptional<T, P, JsonProperty<T, P>, never>]?: Pretty<_Json<T[P]>>;
22
+ [P in K['optional']]?: Pretty<_Json<T[P]>>;
19
23
  };
20
- type _Json<T, IsRoot = true> = T extends TagCycle<infer X> ? (X extends Jsonable<infer J> ? _Json<TagCycles<J>, false> : JsonError<'cycle-detected', X>) : T extends Jsonable<infer J> ? (IsRoot extends true ? _Json<TagCycles<J>, false> : _Json<Omit<T, 'toJSON'>>) : T extends NeverJson ? never : T extends EmptyJson ? Record<string, never> : T extends bigint ? JsonError<'bigint-not-serializeable'> : T extends readonly any[] ? {
24
+ type _Json<T, IsRoot = true> = IsAny<T> extends true ? any : T extends TagCycle<infer X> ? (X extends Jsonable<infer J> ? Json<J, false> : JsonCycleError<X>) : T extends Jsonable<infer J> ? (IsRoot extends true ? Json<J, false> : _Json<Omit<T, 'toJSON'>>) : T extends NeverJson ? never : T extends EmptyJson ? Record<string, never> : T extends bigint ? JsonError<'bigint-not-serializeable'> : T extends readonly any[] ? {
21
25
  [I in keyof T]: [_Json<T[I]>] extends [never] ? null : Pretty<_Json<T[I]>>;
22
26
  } : T extends object ? JsonObject<T> : T;
23
27
  export type Json<T, IsRoot = true> = Pretty<_Json<TagCycles<T>, IsRoot>>;
@@ -1,15 +1,16 @@
1
- export type Pretty<T> = {
1
+ export type Pretty<T> = T extends object ? {
2
2
  [P in keyof T]: T[P];
3
- } & {};
4
- declare const SPECIAL_TAG: unique symbol;
5
- type SpecialTag<T, Data = never> = {
6
- readonly [SPECIAL_TAG]: T;
7
- data: Data;
8
- };
3
+ } & {} : T;
4
+ export type Optional<T, K extends keyof T> = Pretty<Omit<T, K> & Partial<Pick<T, K>>>;
5
+ export type Tagged<Sym extends symbol, T, Data = {}> = Pretty<Readonly<Record<Sym, T>> & Data>;
6
+ declare const SpecialTag: unique symbol;
7
+ type SpecialTag<T, Data = {}> = Tagged<typeof SpecialTag, T, Data>;
9
8
  type TagAny = SpecialTag<'any'>;
10
9
  type TagOptional = SpecialTag<'?'>;
11
10
  type TagUnknown = SpecialTag<'unknown'>;
12
- export type TagCycle<T> = SpecialTag<'cycle', T>;
11
+ export type TagCycle<T> = SpecialTag<'cycle', {
12
+ data: T;
13
+ }>;
13
14
  export type AnyFn = (...args: any[]) => any;
14
15
  export type IsAny<T> = 0 extends 1 & T ? true : false;
15
16
  export type IsFn<T> = T extends AnyFn ? true : false;
@@ -45,4 +46,9 @@ export type UnionMerge<A, B> = Pretty<UnwrapTags<UnionMergeCore<TagSpecial<A>, T
45
46
  export type PickRequired<T> = {
46
47
  [P in keyof T as IfOptional<T, P, never, P>]: T[P];
47
48
  };
49
+ export type Substitute<T, Target, Replacement> = T extends any ? IsIdentical<T, Target> extends true ? Replacement : T extends SpecialTag<any> ? T : IsAny<T> extends true ? T : IsUnknown<T> extends true ? T : IsFn<T> extends true ? T : T extends readonly any[] ? {
50
+ [K in keyof T]: Substitute<T[K], Target, Replacement>;
51
+ } : T extends object ? Pretty<UnwrapTags<{
52
+ [P in keyof TagSpecial<T>]: Substitute<TagSpecial<T>[P], Target, Replacement>;
53
+ }>> : T : never;
48
54
  export {};
@@ -1,3 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const SPECIAL_TAG = Symbol('');
3
+ const SpecialTag = Symbol('SpecialTag');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@drunkcod/argis",
3
3
  "type": "module",
4
- "version": "0.0.17",
4
+ "version": "0.0.19",
5
5
  "description": "A tiny, type-first toolkit for runtime argument validation and structural object utilities.",
6
6
  "scripts": {
7
7
  "clean": "rimraf lib",
@@ -9,7 +9,8 @@
9
9
  "cjs:compile": "tsc --module commonjs --outdir lib/cjs",
10
10
  "cjs:fixup": "echo '{\"type\": \"commonjs\"}' > lib/cjs/package.json",
11
11
  "build": "npm-run-all clean -p compile cjs:compile -s cjs:fixup --silent",
12
- "test": "node --experimental-vm-modules --disable-warning=ExperimentalWarning node_modules/jest/bin/jest.js",
12
+ "test": "run-s test:typecheck test:run",
13
+ "test:run": "node --experimental-vm-modules --disable-warning=ExperimentalWarning node_modules/jest/bin/jest.js",
13
14
  "test:typecheck": "tsc --project tsconfig.test.json --noEmit"
14
15
  },
15
16
  "author": "Tobbe Gyllebring",