@drunkcod/argis 0.0.12 → 0.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.
- package/lib/Json.d.ts +16 -0
- package/lib/Json.js +2 -0
- package/lib/TypeUtils.d.ts +36 -0
- package/lib/TypeUtils.js +2 -0
- package/lib/cjs/Json.d.ts +16 -0
- package/lib/cjs/Json.js +3 -0
- package/lib/cjs/TypeUtils.d.ts +36 -0
- package/lib/cjs/TypeUtils.js +3 -0
- package/lib/cjs/index.d.ts +21 -3
- package/lib/cjs/index.js +11 -5
- package/lib/cjs/parseBool.d.ts +2 -0
- package/lib/cjs/parseBool.js +16 -0
- package/lib/cjs/select.d.ts +19 -0
- package/lib/cjs/select.js +17 -0
- package/lib/index.d.ts +21 -3
- package/lib/index.js +7 -4
- package/lib/parseBool.d.ts +2 -0
- package/lib/parseBool.js +11 -0
- package/lib/select.d.ts +19 -0
- package/lib/select.js +14 -0
- package/package.json +7 -6
package/lib/Json.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface Jsonable<Json> {
|
|
2
|
+
toJSON(): Json;
|
|
3
|
+
}
|
|
4
|
+
type AnyFn = (...args: any[]) => any;
|
|
5
|
+
type NeverJson = undefined | symbol | AnyFn;
|
|
6
|
+
type EmptyJson = Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | RegExp;
|
|
7
|
+
declare const JsonErrorSym: unique symbol;
|
|
8
|
+
export type JsonError<T> = {
|
|
9
|
+
[JsonErrorSym]: T;
|
|
10
|
+
};
|
|
11
|
+
export type Json<T, IsRoot = true> = 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[] ? {
|
|
12
|
+
[I in keyof T]: [Json<T[I]>] extends [never] ? null : Json<T[I]>;
|
|
13
|
+
} : T extends object ? {
|
|
14
|
+
[P in keyof T as P extends string | number ? Json<T[P]> extends never ? never : P : never]: Json<T[P]>;
|
|
15
|
+
} : T;
|
|
16
|
+
export {};
|
package/lib/Json.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
type Pretty<T> = {
|
|
2
|
+
[P in keyof T]: T[P];
|
|
3
|
+
} & {};
|
|
4
|
+
declare const SPECIAL_TAG: unique symbol;
|
|
5
|
+
type SpecialTag<T> = {
|
|
6
|
+
readonly [SPECIAL_TAG]: T;
|
|
7
|
+
};
|
|
8
|
+
type TagAny = SpecialTag<'any'>;
|
|
9
|
+
type TagOptional = SpecialTag<'?'>;
|
|
10
|
+
type TagUnknown = SpecialTag<'unknown'>;
|
|
11
|
+
export type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
12
|
+
export type IsUnknown<T> = unknown extends T ? (IsAny<T> extends true ? false : true) : false;
|
|
13
|
+
export type IsOptional<T, K extends keyof T> = {
|
|
14
|
+
[P in K]?: T[K];
|
|
15
|
+
} extends Pick<T, K> ? true : false;
|
|
16
|
+
type TagSpecial<T> = {
|
|
17
|
+
[P in keyof T]: (IsOptional<T, P> extends true ? TagOptional : never) | (IsAny<T[P]> extends true ? TagAny : IsUnknown<T[P]> extends true ? TagUnknown : T[P]);
|
|
18
|
+
};
|
|
19
|
+
type IsTagged<T, X extends SpecialTag<any>> = [Extract<T, X>] extends [never] ? false : true;
|
|
20
|
+
type UnwrapOptional<T> = {
|
|
21
|
+
[P in keyof T as IsTagged<T[P], TagOptional> extends false ? P : never]: T[P];
|
|
22
|
+
} & {
|
|
23
|
+
[P in keyof T as IsTagged<T[P], TagOptional> extends true ? P : never]?: Exclude<T[P], TagOptional>;
|
|
24
|
+
};
|
|
25
|
+
type UnwrapTagsCore<T> = {
|
|
26
|
+
[P in keyof T]: IsTagged<T[P], TagAny> extends true ? any : IsTagged<T[P], TagUnknown> extends true ? unknown : T[P];
|
|
27
|
+
};
|
|
28
|
+
type UnwrapTags<T> = UnwrapTagsCore<UnwrapOptional<T>>;
|
|
29
|
+
type UnionMergeCore<A, B> = {
|
|
30
|
+
[K in keyof A | keyof B]: (K extends keyof A ? A[K] : TagOptional) | (K extends keyof B ? B[K] : TagOptional);
|
|
31
|
+
};
|
|
32
|
+
export type UnionMerge<A, B> = Pretty<UnwrapTags<UnionMergeCore<TagSpecial<A>, TagSpecial<B>>>>;
|
|
33
|
+
export type PickRequired<T> = {
|
|
34
|
+
[P in keyof T as IsOptional<T, P> extends true ? never : P]: T[P];
|
|
35
|
+
};
|
|
36
|
+
export {};
|
package/lib/TypeUtils.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface Jsonable<Json> {
|
|
2
|
+
toJSON(): Json;
|
|
3
|
+
}
|
|
4
|
+
type AnyFn = (...args: any[]) => any;
|
|
5
|
+
type NeverJson = undefined | symbol | AnyFn;
|
|
6
|
+
type EmptyJson = Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | RegExp;
|
|
7
|
+
declare const JsonErrorSym: unique symbol;
|
|
8
|
+
export type JsonError<T> = {
|
|
9
|
+
[JsonErrorSym]: T;
|
|
10
|
+
};
|
|
11
|
+
export type Json<T, IsRoot = true> = 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[] ? {
|
|
12
|
+
[I in keyof T]: [Json<T[I]>] extends [never] ? null : Json<T[I]>;
|
|
13
|
+
} : T extends object ? {
|
|
14
|
+
[P in keyof T as P extends string | number ? Json<T[P]> extends never ? never : P : never]: Json<T[P]>;
|
|
15
|
+
} : T;
|
|
16
|
+
export {};
|
package/lib/cjs/Json.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
type Pretty<T> = {
|
|
2
|
+
[P in keyof T]: T[P];
|
|
3
|
+
} & {};
|
|
4
|
+
declare const SPECIAL_TAG: unique symbol;
|
|
5
|
+
type SpecialTag<T> = {
|
|
6
|
+
readonly [SPECIAL_TAG]: T;
|
|
7
|
+
};
|
|
8
|
+
type TagAny = SpecialTag<'any'>;
|
|
9
|
+
type TagOptional = SpecialTag<'?'>;
|
|
10
|
+
type TagUnknown = SpecialTag<'unknown'>;
|
|
11
|
+
export type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
12
|
+
export type IsUnknown<T> = unknown extends T ? (IsAny<T> extends true ? false : true) : false;
|
|
13
|
+
export type IsOptional<T, K extends keyof T> = {
|
|
14
|
+
[P in K]?: T[K];
|
|
15
|
+
} extends Pick<T, K> ? true : false;
|
|
16
|
+
type TagSpecial<T> = {
|
|
17
|
+
[P in keyof T]: (IsOptional<T, P> extends true ? TagOptional : never) | (IsAny<T[P]> extends true ? TagAny : IsUnknown<T[P]> extends true ? TagUnknown : T[P]);
|
|
18
|
+
};
|
|
19
|
+
type IsTagged<T, X extends SpecialTag<any>> = [Extract<T, X>] extends [never] ? false : true;
|
|
20
|
+
type UnwrapOptional<T> = {
|
|
21
|
+
[P in keyof T as IsTagged<T[P], TagOptional> extends false ? P : never]: T[P];
|
|
22
|
+
} & {
|
|
23
|
+
[P in keyof T as IsTagged<T[P], TagOptional> extends true ? P : never]?: Exclude<T[P], TagOptional>;
|
|
24
|
+
};
|
|
25
|
+
type UnwrapTagsCore<T> = {
|
|
26
|
+
[P in keyof T]: IsTagged<T[P], TagAny> extends true ? any : IsTagged<T[P], TagUnknown> extends true ? unknown : T[P];
|
|
27
|
+
};
|
|
28
|
+
type UnwrapTags<T> = UnwrapTagsCore<UnwrapOptional<T>>;
|
|
29
|
+
type UnionMergeCore<A, B> = {
|
|
30
|
+
[K in keyof A | keyof B]: (K extends keyof A ? A[K] : TagOptional) | (K extends keyof B ? B[K] : TagOptional);
|
|
31
|
+
};
|
|
32
|
+
export type UnionMerge<A, B> = Pretty<UnwrapTags<UnionMergeCore<TagSpecial<A>, TagSpecial<B>>>>;
|
|
33
|
+
export type PickRequired<T> = {
|
|
34
|
+
[P in keyof T as IsOptional<T, P> extends true ? never : P]: T[P];
|
|
35
|
+
};
|
|
36
|
+
export {};
|
package/lib/cjs/index.d.ts
CHANGED
|
@@ -33,10 +33,25 @@ export declare function assertNotNil<T>(x: T | null): asserts x is T;
|
|
|
33
33
|
export declare function argNotNil<T, K extends keyof T>(x: T, key: K): asserts x is T & {
|
|
34
34
|
[P in K]-?: NonNullable<T[K]>;
|
|
35
35
|
};
|
|
36
|
+
interface TypeGuard<T> {
|
|
37
|
+
(x: unknown): x is T;
|
|
38
|
+
}
|
|
39
|
+
type TypeOf = {
|
|
40
|
+
boolean: boolean;
|
|
41
|
+
number: number;
|
|
42
|
+
bigint: bigint;
|
|
43
|
+
string: string;
|
|
44
|
+
object: object;
|
|
45
|
+
symbol: symbol;
|
|
46
|
+
function: Function;
|
|
47
|
+
undefined: undefined;
|
|
48
|
+
};
|
|
49
|
+
type TypeGuardOrType<T = any> = T extends TypeGuard<infer V> ? TypeGuard<V> : T extends keyof TypeOf ? T : never;
|
|
50
|
+
type TypeFrom<T> = T extends TypeGuard<infer V> ? V : T extends keyof TypeOf ? TypeOf[T] : never;
|
|
36
51
|
export declare function hasOwn<T extends object, K extends PropertyKey>(x: T | unknown, key: K): x is T & WithKey<K>;
|
|
37
|
-
export declare function hasOwn<T extends object, K extends PropertyKey, V>(x: T
|
|
52
|
+
export declare function hasOwn<T extends object, K extends PropertyKey, V>(x: T, key: K, ofType?: TypeGuardOrType<V>): x is T & WithKey<K, TypeFrom<V>>;
|
|
38
53
|
export declare function hasKey<T extends object, K extends PropertyKey>(x: T | unknown, key: K): x is T & WithKey<K>;
|
|
39
|
-
export declare function hasKey<T extends object, K extends PropertyKey, V>(x: T
|
|
54
|
+
export declare function hasKey<T extends object, K extends PropertyKey, V>(x: T, key: K, ofType?: TypeGuardOrType<V>): x is T & WithKey<K, TypeFrom<V>>;
|
|
40
55
|
export declare function assertOwn<T extends object, K extends PropertyKey>(x: T, key: K): asserts x is T & WithKey<K>;
|
|
41
56
|
export declare function assertOwn<T extends object, K extends PropertyKey, V>(x: T, key: K, ofType: (found: unknown) => found is V): asserts x is T & WithKey<K, V>;
|
|
42
57
|
export declare function assertKey<T extends object, K extends PropertyKey>(x: T, key: K): asserts x is T & WithKey<K>;
|
|
@@ -47,4 +62,7 @@ export declare function nullIfEmpty<T extends {
|
|
|
47
62
|
export declare function intOrUndefined(x?: string | null): number | undefined;
|
|
48
63
|
export declare function omit<T extends object, K extends OfType<keyof T, string>>(x: T, ...keys: readonly K[]): Omit<T, K>;
|
|
49
64
|
export declare function pick<T extends object, K extends OfType<keyof T, string>>(x: T, ...keys: readonly K[]): Pick<T, K>;
|
|
50
|
-
export {};
|
|
65
|
+
export { select } from './select.js';
|
|
66
|
+
export { parseBool, isBool } from './parseBool.js';
|
|
67
|
+
export type { Json } from './Json.js';
|
|
68
|
+
export type * from './TypeUtils.js';
|
package/lib/cjs/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ArgumentError = void 0;
|
|
3
|
+
exports.isBool = exports.parseBool = exports.select = exports.ArgumentError = void 0;
|
|
4
4
|
exports.isNil = isNil;
|
|
5
5
|
exports.isNotNil = isNotNil;
|
|
6
6
|
exports.isThenable = isThenable;
|
|
@@ -46,17 +46,18 @@ function assertNotNil(x) {
|
|
|
46
46
|
function argNotNil(x, key) {
|
|
47
47
|
isNotNil(x, key) || ArgumentError.null(`'${String(key)}' was null or undefined`);
|
|
48
48
|
}
|
|
49
|
+
const isObject = (x) => !!x && (typeof x === 'object' || typeof x === 'function');
|
|
49
50
|
function _hasOwn(x, key) {
|
|
50
51
|
return Object.hasOwn(x, key);
|
|
51
52
|
}
|
|
52
53
|
function hasOwn(x, key, ofType) {
|
|
53
|
-
return _hasOwn(x, key) && (ofType
|
|
54
|
+
return isObject(x) && _hasOwn(x, key) && (ofType === undefined || (typeof ofType === 'string' ? typeof x[key] === ofType : ofType(x[key])));
|
|
54
55
|
}
|
|
55
56
|
function _hasKey(x, key) {
|
|
56
|
-
return
|
|
57
|
+
return key in x;
|
|
57
58
|
}
|
|
58
59
|
function hasKey(x, key, ofType) {
|
|
59
|
-
return _hasKey(x, key) && (ofType
|
|
60
|
+
return isObject(x) && _hasKey(x, key) && (ofType === undefined || (typeof ofType === 'string' ? typeof x[key] === ofType : ofType(x[key])));
|
|
60
61
|
}
|
|
61
62
|
function assertOwn(x, key, ofType) {
|
|
62
63
|
(ofType ? hasOwn(x, key, ofType) : hasOwn(x, key)) || ArgumentError.missing({ key });
|
|
@@ -82,5 +83,10 @@ function omit(x, ...keys) {
|
|
|
82
83
|
return Object.fromEntries(excludeEntries(x, Array.prototype.includes.bind(keys)));
|
|
83
84
|
}
|
|
84
85
|
function pick(x, ...keys) {
|
|
85
|
-
return Object.fromEntries(keys.map((k) => [
|
|
86
|
+
return Object.fromEntries(keys.map((k) => [k, x[k]]));
|
|
86
87
|
}
|
|
88
|
+
var select_js_1 = require("./select.js");
|
|
89
|
+
Object.defineProperty(exports, "select", { enumerable: true, get: function () { return select_js_1.select; } });
|
|
90
|
+
var parseBool_js_1 = require("./parseBool.js");
|
|
91
|
+
Object.defineProperty(exports, "parseBool", { enumerable: true, get: function () { return parseBool_js_1.parseBool; } });
|
|
92
|
+
Object.defineProperty(exports, "isBool", { enumerable: true, get: function () { return parseBool_js_1.isBool; } });
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseBool = void 0;
|
|
4
|
+
exports.isBool = isBool;
|
|
5
|
+
const parseBool = (b) => {
|
|
6
|
+
if (b == null)
|
|
7
|
+
return null;
|
|
8
|
+
const p = b.match(/^(?:\s*)(?:(t(?:rue)?|1|y(?:es)?|on)|(?:f(?:alse)?|0|n(?:o)?|off))(?:\s*)$/i);
|
|
9
|
+
if (p)
|
|
10
|
+
return p[1] !== undefined;
|
|
11
|
+
return null;
|
|
12
|
+
};
|
|
13
|
+
exports.parseBool = parseBool;
|
|
14
|
+
function isBool(x) {
|
|
15
|
+
return x !== null;
|
|
16
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
interface Fn1<T, R> {
|
|
2
|
+
(arg0: T): R;
|
|
3
|
+
}
|
|
4
|
+
declare const error: unique symbol;
|
|
5
|
+
type SelectError<T extends string> = T & {
|
|
6
|
+
[error]: void;
|
|
7
|
+
};
|
|
8
|
+
type Select<T, S> = {
|
|
9
|
+
[P in keyof S]: P extends keyof T ? S[P] extends Fn1<infer In, unknown> ? In extends T[P] ? S[P] : SelectError<`wrong-parameter-type:${string & P})`> : S[P] extends 1 ? 1 : SelectError<`not-a-function:${string & P}`> : SelectError<`not-in-source:${string & P}`>;
|
|
10
|
+
};
|
|
11
|
+
type Errors<T> = {
|
|
12
|
+
[P in keyof T as T[P] extends SelectError<string> ? P : never]: T[P];
|
|
13
|
+
};
|
|
14
|
+
type Transform<T, S> = S extends Select<T, S> ? S : Errors<Select<T, S>>;
|
|
15
|
+
type Selected<T, S> = {
|
|
16
|
+
[P in keyof Transform<T, S>]: Transform<T, S>[P] extends Fn1<any, infer R> ? R : P extends keyof T ? T[P] : never;
|
|
17
|
+
};
|
|
18
|
+
export declare function select<T extends object, S>(x: T, transform: Transform<T, S>): Selected<T, S>;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.select = select;
|
|
4
|
+
function select(x, transform) {
|
|
5
|
+
return Object.fromEntries({
|
|
6
|
+
*[Symbol.iterator]() {
|
|
7
|
+
for (const k in transform) {
|
|
8
|
+
const fn = transform[k];
|
|
9
|
+
const v = Reflect.get(x, k);
|
|
10
|
+
if (fn === 1)
|
|
11
|
+
yield [k, v];
|
|
12
|
+
else if (typeof fn === 'function')
|
|
13
|
+
yield [k, fn(v)];
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -33,10 +33,25 @@ export declare function assertNotNil<T>(x: T | null): asserts x is T;
|
|
|
33
33
|
export declare function argNotNil<T, K extends keyof T>(x: T, key: K): asserts x is T & {
|
|
34
34
|
[P in K]-?: NonNullable<T[K]>;
|
|
35
35
|
};
|
|
36
|
+
interface TypeGuard<T> {
|
|
37
|
+
(x: unknown): x is T;
|
|
38
|
+
}
|
|
39
|
+
type TypeOf = {
|
|
40
|
+
boolean: boolean;
|
|
41
|
+
number: number;
|
|
42
|
+
bigint: bigint;
|
|
43
|
+
string: string;
|
|
44
|
+
object: object;
|
|
45
|
+
symbol: symbol;
|
|
46
|
+
function: Function;
|
|
47
|
+
undefined: undefined;
|
|
48
|
+
};
|
|
49
|
+
type TypeGuardOrType<T = any> = T extends TypeGuard<infer V> ? TypeGuard<V> : T extends keyof TypeOf ? T : never;
|
|
50
|
+
type TypeFrom<T> = T extends TypeGuard<infer V> ? V : T extends keyof TypeOf ? TypeOf[T] : never;
|
|
36
51
|
export declare function hasOwn<T extends object, K extends PropertyKey>(x: T | unknown, key: K): x is T & WithKey<K>;
|
|
37
|
-
export declare function hasOwn<T extends object, K extends PropertyKey, V>(x: T
|
|
52
|
+
export declare function hasOwn<T extends object, K extends PropertyKey, V>(x: T, key: K, ofType?: TypeGuardOrType<V>): x is T & WithKey<K, TypeFrom<V>>;
|
|
38
53
|
export declare function hasKey<T extends object, K extends PropertyKey>(x: T | unknown, key: K): x is T & WithKey<K>;
|
|
39
|
-
export declare function hasKey<T extends object, K extends PropertyKey, V>(x: T
|
|
54
|
+
export declare function hasKey<T extends object, K extends PropertyKey, V>(x: T, key: K, ofType?: TypeGuardOrType<V>): x is T & WithKey<K, TypeFrom<V>>;
|
|
40
55
|
export declare function assertOwn<T extends object, K extends PropertyKey>(x: T, key: K): asserts x is T & WithKey<K>;
|
|
41
56
|
export declare function assertOwn<T extends object, K extends PropertyKey, V>(x: T, key: K, ofType: (found: unknown) => found is V): asserts x is T & WithKey<K, V>;
|
|
42
57
|
export declare function assertKey<T extends object, K extends PropertyKey>(x: T, key: K): asserts x is T & WithKey<K>;
|
|
@@ -47,4 +62,7 @@ export declare function nullIfEmpty<T extends {
|
|
|
47
62
|
export declare function intOrUndefined(x?: string | null): number | undefined;
|
|
48
63
|
export declare function omit<T extends object, K extends OfType<keyof T, string>>(x: T, ...keys: readonly K[]): Omit<T, K>;
|
|
49
64
|
export declare function pick<T extends object, K extends OfType<keyof T, string>>(x: T, ...keys: readonly K[]): Pick<T, K>;
|
|
50
|
-
export {};
|
|
65
|
+
export { select } from './select.js';
|
|
66
|
+
export { parseBool, isBool } from './parseBool.js';
|
|
67
|
+
export type { Json } from './Json.js';
|
|
68
|
+
export type * from './TypeUtils.js';
|
package/lib/index.js
CHANGED
|
@@ -29,17 +29,18 @@ export function assertNotNil(x) {
|
|
|
29
29
|
export function argNotNil(x, key) {
|
|
30
30
|
isNotNil(x, key) || ArgumentError.null(`'${String(key)}' was null or undefined`);
|
|
31
31
|
}
|
|
32
|
+
const isObject = (x) => !!x && (typeof x === 'object' || typeof x === 'function');
|
|
32
33
|
function _hasOwn(x, key) {
|
|
33
34
|
return Object.hasOwn(x, key);
|
|
34
35
|
}
|
|
35
36
|
export function hasOwn(x, key, ofType) {
|
|
36
|
-
return _hasOwn(x, key) && (ofType
|
|
37
|
+
return isObject(x) && _hasOwn(x, key) && (ofType === undefined || (typeof ofType === 'string' ? typeof x[key] === ofType : ofType(x[key])));
|
|
37
38
|
}
|
|
38
39
|
function _hasKey(x, key) {
|
|
39
|
-
return
|
|
40
|
+
return key in x;
|
|
40
41
|
}
|
|
41
42
|
export function hasKey(x, key, ofType) {
|
|
42
|
-
return _hasKey(x, key) && (ofType
|
|
43
|
+
return isObject(x) && _hasKey(x, key) && (ofType === undefined || (typeof ofType === 'string' ? typeof x[key] === ofType : ofType(x[key])));
|
|
43
44
|
}
|
|
44
45
|
export function assertOwn(x, key, ofType) {
|
|
45
46
|
(ofType ? hasOwn(x, key, ofType) : hasOwn(x, key)) || ArgumentError.missing({ key });
|
|
@@ -65,5 +66,7 @@ export function omit(x, ...keys) {
|
|
|
65
66
|
return Object.fromEntries(excludeEntries(x, Array.prototype.includes.bind(keys)));
|
|
66
67
|
}
|
|
67
68
|
export function pick(x, ...keys) {
|
|
68
|
-
return Object.fromEntries(keys.map((k) => [
|
|
69
|
+
return Object.fromEntries(keys.map((k) => [k, x[k]]));
|
|
69
70
|
}
|
|
71
|
+
export { select } from './select.js';
|
|
72
|
+
export { parseBool, isBool } from './parseBool.js';
|
package/lib/parseBool.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const parseBool = (b) => {
|
|
2
|
+
if (b == null)
|
|
3
|
+
return null;
|
|
4
|
+
const p = b.match(/^(?:\s*)(?:(t(?:rue)?|1|y(?:es)?|on)|(?:f(?:alse)?|0|n(?:o)?|off))(?:\s*)$/i);
|
|
5
|
+
if (p)
|
|
6
|
+
return p[1] !== undefined;
|
|
7
|
+
return null;
|
|
8
|
+
};
|
|
9
|
+
export function isBool(x) {
|
|
10
|
+
return x !== null;
|
|
11
|
+
}
|
package/lib/select.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
interface Fn1<T, R> {
|
|
2
|
+
(arg0: T): R;
|
|
3
|
+
}
|
|
4
|
+
declare const error: unique symbol;
|
|
5
|
+
type SelectError<T extends string> = T & {
|
|
6
|
+
[error]: void;
|
|
7
|
+
};
|
|
8
|
+
type Select<T, S> = {
|
|
9
|
+
[P in keyof S]: P extends keyof T ? S[P] extends Fn1<infer In, unknown> ? In extends T[P] ? S[P] : SelectError<`wrong-parameter-type:${string & P})`> : S[P] extends 1 ? 1 : SelectError<`not-a-function:${string & P}`> : SelectError<`not-in-source:${string & P}`>;
|
|
10
|
+
};
|
|
11
|
+
type Errors<T> = {
|
|
12
|
+
[P in keyof T as T[P] extends SelectError<string> ? P : never]: T[P];
|
|
13
|
+
};
|
|
14
|
+
type Transform<T, S> = S extends Select<T, S> ? S : Errors<Select<T, S>>;
|
|
15
|
+
type Selected<T, S> = {
|
|
16
|
+
[P in keyof Transform<T, S>]: Transform<T, S>[P] extends Fn1<any, infer R> ? R : P extends keyof T ? T[P] : never;
|
|
17
|
+
};
|
|
18
|
+
export declare function select<T extends object, S>(x: T, transform: Transform<T, S>): Selected<T, S>;
|
|
19
|
+
export {};
|
package/lib/select.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function select(x, transform) {
|
|
2
|
+
return Object.fromEntries({
|
|
3
|
+
*[Symbol.iterator]() {
|
|
4
|
+
for (const k in transform) {
|
|
5
|
+
const fn = transform[k];
|
|
6
|
+
const v = Reflect.get(x, k);
|
|
7
|
+
if (fn === 1)
|
|
8
|
+
yield [k, v];
|
|
9
|
+
else if (typeof fn === 'function')
|
|
10
|
+
yield [k, fn(v)];
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
}
|
package/package.json
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drunkcod/argis",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
5
|
-
"description": "
|
|
4
|
+
"version": "0.0.14",
|
|
5
|
+
"description": "A tiny, type-first toolkit for runtime argument validation and structural object utilities.",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"clean": "rimraf lib",
|
|
8
8
|
"compile": "tsc",
|
|
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": "node --experimental-vm-modules --disable-warning=ExperimentalWarning node_modules/jest/bin/jest.js",
|
|
13
|
+
"test:typecheck": "tsc --project tsconfig.test.json --noEmit"
|
|
13
14
|
},
|
|
14
15
|
"author": "Tobbe Gyllebring",
|
|
15
16
|
"license": "MIT",
|
|
@@ -33,10 +34,10 @@
|
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
36
|
"@jest/globals": "^30.0.4",
|
|
36
|
-
"jest": "^30.
|
|
37
|
+
"jest": "^30.1.1",
|
|
37
38
|
"npm-run-all": "^4.1.5",
|
|
38
39
|
"rimraf": "^6.0.1",
|
|
39
|
-
"ts-jest": "^29.4.
|
|
40
|
-
"typescript": "^5.
|
|
40
|
+
"ts-jest": "^29.4.1",
|
|
41
|
+
"typescript": "^5.9.2"
|
|
41
42
|
}
|
|
42
43
|
}
|