@drunkcod/argis 0.0.16 → 0.0.18
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 +18 -11
- package/lib/Json.js +1 -1
- package/lib/TypeUtils.d.ts +23 -7
- package/lib/TypeUtils.js +1 -1
- package/lib/cjs/Json.d.ts +18 -11
- package/lib/cjs/Json.js +1 -1
- package/lib/cjs/TypeUtils.d.ts +23 -7
- package/lib/cjs/TypeUtils.js +1 -1
- package/package.json +3 -2
package/lib/Json.d.ts
CHANGED
|
@@ -1,21 +1,28 @@
|
|
|
1
|
-
import { IfOptional } from './TypeUtils.js';
|
|
1
|
+
import { AnyFn, IfOptional, Pretty, Tagged, TagCycle, TagCycles } from './TypeUtils.js';
|
|
2
2
|
export interface Jsonable<Json> {
|
|
3
3
|
toJSON(): Json;
|
|
4
4
|
}
|
|
5
|
-
type AnyFn = (...args: any[]) => any;
|
|
6
5
|
type NeverJson = undefined | symbol | AnyFn;
|
|
7
6
|
type EmptyJson = Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | RegExp;
|
|
8
|
-
declare const
|
|
9
|
-
export type JsonError<T> =
|
|
10
|
-
|
|
7
|
+
declare const JsonError: unique symbol;
|
|
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;
|
|
11
17
|
};
|
|
12
|
-
type
|
|
13
|
-
type
|
|
14
|
-
[P in
|
|
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]>>;
|
|
15
21
|
} & {
|
|
16
|
-
[P in
|
|
22
|
+
[P in K['optional']]?: Pretty<_Json<T[P]>>;
|
|
17
23
|
};
|
|
18
|
-
|
|
19
|
-
[I in keyof T]: [
|
|
24
|
+
type _Json<T, IsRoot = true> = 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[] ? {
|
|
25
|
+
[I in keyof T]: [_Json<T[I]>] extends [never] ? null : Pretty<_Json<T[I]>>;
|
|
20
26
|
} : T extends object ? JsonObject<T> : T;
|
|
27
|
+
export type Json<T, IsRoot = true> = Pretty<_Json<TagCycles<T>, IsRoot>>;
|
|
21
28
|
export {};
|
package/lib/Json.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const
|
|
1
|
+
const JsonError = Symbol('JsonError');
|
|
2
2
|
export {};
|
package/lib/TypeUtils.d.ts
CHANGED
|
@@ -1,22 +1,33 @@
|
|
|
1
|
-
export type Pretty<T> = {
|
|
1
|
+
export type Pretty<T> = T extends object ? {
|
|
2
2
|
[P in keyof T]: T[P];
|
|
3
|
-
} & {};
|
|
4
|
-
|
|
5
|
-
type
|
|
6
|
-
|
|
7
|
-
}
|
|
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>;
|
|
8
8
|
type TagAny = SpecialTag<'any'>;
|
|
9
9
|
type TagOptional = SpecialTag<'?'>;
|
|
10
10
|
type TagUnknown = SpecialTag<'unknown'>;
|
|
11
|
+
export type TagCycle<T> = SpecialTag<'cycle', {
|
|
12
|
+
data: T;
|
|
13
|
+
}>;
|
|
14
|
+
export type AnyFn = (...args: any[]) => any;
|
|
11
15
|
export type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
16
|
+
export type IsFn<T> = T extends AnyFn ? true : false;
|
|
12
17
|
export type IsUnknown<T> = unknown extends T ? (IsAny<T> extends true ? false : true) : false;
|
|
13
18
|
export type IsOptional<T, K extends keyof T> = IfOptional<T, K, true, false>;
|
|
14
19
|
export type IfOptional<T, K extends keyof T, True, False> = {
|
|
15
20
|
[P in K]?: T[K];
|
|
16
21
|
} extends Pick<T, K> ? True : False;
|
|
17
|
-
type TagSpecial<T> = {
|
|
22
|
+
export type TagSpecial<T> = {
|
|
18
23
|
[P in keyof T]: IfOptional<T, P, TagOptional, never> | (IsAny<T[P]> extends true ? TagAny : IsUnknown<T[P]> extends true ? TagUnknown : T[P]);
|
|
19
24
|
};
|
|
25
|
+
export type IsIdentical<T, V> = IsAny<T> extends true ? IsAny<V> : [T] extends [V] ? ([V] extends [T] ? true : false) : false;
|
|
26
|
+
export type HasIdentical<T, U> = true extends (U extends any ? IsIdentical<T, U> : false) ? true : false;
|
|
27
|
+
export type IsVisited<T, Visited> = [NonNullable<T>] extends [Visited] ? HasIdentical<T, Visited> : false;
|
|
28
|
+
export type TagCycles<T, Visited = never> = T extends SpecialTag<any> ? T : IsFn<T> extends true ? T : T extends undefined ? undefined : IsVisited<T, Visited> extends true ? TagCycle<NonNullable<T>> : T extends object ? {
|
|
29
|
+
[P in keyof T]: TagCycles<T[P], Visited | NonNullable<T>>;
|
|
30
|
+
} : T;
|
|
20
31
|
type IfTagged<T, X extends SpecialTag<any>, True, False> = [Extract<T, X>] extends [never] ? False : True;
|
|
21
32
|
type UnwrapOptional<T> = {
|
|
22
33
|
[P in keyof T as IfTagged<T[P], TagOptional, never, P>]: T[P];
|
|
@@ -35,4 +46,9 @@ export type UnionMerge<A, B> = Pretty<UnwrapTags<UnionMergeCore<TagSpecial<A>, T
|
|
|
35
46
|
export type PickRequired<T> = {
|
|
36
47
|
[P in keyof T as IfOptional<T, P, never, P>]: T[P];
|
|
37
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;
|
|
38
54
|
export {};
|
package/lib/TypeUtils.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const
|
|
1
|
+
const SpecialTag = Symbol('SpecialTag');
|
|
2
2
|
export {};
|
package/lib/cjs/Json.d.ts
CHANGED
|
@@ -1,21 +1,28 @@
|
|
|
1
|
-
import { IfOptional } from './TypeUtils.js';
|
|
1
|
+
import { AnyFn, IfOptional, Pretty, Tagged, TagCycle, TagCycles } from './TypeUtils.js';
|
|
2
2
|
export interface Jsonable<Json> {
|
|
3
3
|
toJSON(): Json;
|
|
4
4
|
}
|
|
5
|
-
type AnyFn = (...args: any[]) => any;
|
|
6
5
|
type NeverJson = undefined | symbol | AnyFn;
|
|
7
6
|
type EmptyJson = Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | RegExp;
|
|
8
|
-
declare const
|
|
9
|
-
export type JsonError<T> =
|
|
10
|
-
|
|
7
|
+
declare const JsonError: unique symbol;
|
|
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;
|
|
11
17
|
};
|
|
12
|
-
type
|
|
13
|
-
type
|
|
14
|
-
[P in
|
|
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]>>;
|
|
15
21
|
} & {
|
|
16
|
-
[P in
|
|
22
|
+
[P in K['optional']]?: Pretty<_Json<T[P]>>;
|
|
17
23
|
};
|
|
18
|
-
|
|
19
|
-
[I in keyof T]: [
|
|
24
|
+
type _Json<T, IsRoot = true> = 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[] ? {
|
|
25
|
+
[I in keyof T]: [_Json<T[I]>] extends [never] ? null : Pretty<_Json<T[I]>>;
|
|
20
26
|
} : T extends object ? JsonObject<T> : T;
|
|
27
|
+
export type Json<T, IsRoot = true> = Pretty<_Json<TagCycles<T>, IsRoot>>;
|
|
21
28
|
export {};
|
package/lib/cjs/Json.js
CHANGED
package/lib/cjs/TypeUtils.d.ts
CHANGED
|
@@ -1,22 +1,33 @@
|
|
|
1
|
-
export type Pretty<T> = {
|
|
1
|
+
export type Pretty<T> = T extends object ? {
|
|
2
2
|
[P in keyof T]: T[P];
|
|
3
|
-
} & {};
|
|
4
|
-
|
|
5
|
-
type
|
|
6
|
-
|
|
7
|
-
}
|
|
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>;
|
|
8
8
|
type TagAny = SpecialTag<'any'>;
|
|
9
9
|
type TagOptional = SpecialTag<'?'>;
|
|
10
10
|
type TagUnknown = SpecialTag<'unknown'>;
|
|
11
|
+
export type TagCycle<T> = SpecialTag<'cycle', {
|
|
12
|
+
data: T;
|
|
13
|
+
}>;
|
|
14
|
+
export type AnyFn = (...args: any[]) => any;
|
|
11
15
|
export type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
16
|
+
export type IsFn<T> = T extends AnyFn ? true : false;
|
|
12
17
|
export type IsUnknown<T> = unknown extends T ? (IsAny<T> extends true ? false : true) : false;
|
|
13
18
|
export type IsOptional<T, K extends keyof T> = IfOptional<T, K, true, false>;
|
|
14
19
|
export type IfOptional<T, K extends keyof T, True, False> = {
|
|
15
20
|
[P in K]?: T[K];
|
|
16
21
|
} extends Pick<T, K> ? True : False;
|
|
17
|
-
type TagSpecial<T> = {
|
|
22
|
+
export type TagSpecial<T> = {
|
|
18
23
|
[P in keyof T]: IfOptional<T, P, TagOptional, never> | (IsAny<T[P]> extends true ? TagAny : IsUnknown<T[P]> extends true ? TagUnknown : T[P]);
|
|
19
24
|
};
|
|
25
|
+
export type IsIdentical<T, V> = IsAny<T> extends true ? IsAny<V> : [T] extends [V] ? ([V] extends [T] ? true : false) : false;
|
|
26
|
+
export type HasIdentical<T, U> = true extends (U extends any ? IsIdentical<T, U> : false) ? true : false;
|
|
27
|
+
export type IsVisited<T, Visited> = [NonNullable<T>] extends [Visited] ? HasIdentical<T, Visited> : false;
|
|
28
|
+
export type TagCycles<T, Visited = never> = T extends SpecialTag<any> ? T : IsFn<T> extends true ? T : T extends undefined ? undefined : IsVisited<T, Visited> extends true ? TagCycle<NonNullable<T>> : T extends object ? {
|
|
29
|
+
[P in keyof T]: TagCycles<T[P], Visited | NonNullable<T>>;
|
|
30
|
+
} : T;
|
|
20
31
|
type IfTagged<T, X extends SpecialTag<any>, True, False> = [Extract<T, X>] extends [never] ? False : True;
|
|
21
32
|
type UnwrapOptional<T> = {
|
|
22
33
|
[P in keyof T as IfTagged<T[P], TagOptional, never, P>]: T[P];
|
|
@@ -35,4 +46,9 @@ export type UnionMerge<A, B> = Pretty<UnwrapTags<UnionMergeCore<TagSpecial<A>, T
|
|
|
35
46
|
export type PickRequired<T> = {
|
|
36
47
|
[P in keyof T as IfOptional<T, P, never, P>]: T[P];
|
|
37
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;
|
|
38
54
|
export {};
|
package/lib/cjs/TypeUtils.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drunkcod/argis",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.18",
|
|
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": "
|
|
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",
|