@drunkcod/argis 0.0.13 → 0.0.15
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/README.md +203 -0
- package/lib/Json.d.ts +16 -0
- package/lib/Json.js +2 -0
- package/lib/Projection.d.ts +51 -0
- package/lib/Projection.js +3 -0
- package/lib/TypeUtils.d.ts +37 -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/Projection.d.ts +51 -0
- package/lib/cjs/Projection.js +4 -0
- package/lib/cjs/TypeUtils.d.ts +37 -0
- package/lib/cjs/TypeUtils.js +3 -0
- package/lib/cjs/index.d.ts +4 -0
- package/lib/cjs/index.js +4 -1
- package/lib/cjs/parseBool.d.ts +2 -0
- package/lib/cjs/parseBool.js +16 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +1 -0
- package/lib/parseBool.d.ts +2 -0
- package/lib/parseBool.js +11 -0
- package/package.json +7 -6
package/README.md
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# @drunkcod/argis
|
|
2
|
+
|
|
3
|
+
A tiny, type-first toolkit for runtime argument validation and structural object utilities.
|
|
4
|
+
|
|
5
|
+
`argis` (Arguments & Is) is designed to bridge the gap between runtime checks and TypeScript inference. It provides a suite of lightweight, zero-dependency utilities to safely narrow types while performing common runtime validations and data transformations.
|
|
6
|
+
|
|
7
|
+
## Aim & Philosophy
|
|
8
|
+
|
|
9
|
+
In TypeScript, we often find ourselves writing repetitive type guards or utility functions to handle nulls, property existence, and object shaping. `argis` aims to provide a consistent, high-fidelity set of tools that:
|
|
10
|
+
|
|
11
|
+
1. **Prioritize Inference:** Every function is built to provide the strongest possible type narrowing at the call site.
|
|
12
|
+
2. **Handle Objects Safely:** Deeply nested paths and structural transformations are type-checked at compile time.
|
|
13
|
+
3. **Stay Minimal:** Only provide what's necessary for robust argument handling and object manipulation without the overhead of a full-blown validation library.
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @drunkcod/argis
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Utilities
|
|
26
|
+
|
|
27
|
+
### 1. Guarding & Presence
|
|
28
|
+
|
|
29
|
+
#### `isNil` / `isNotNil`
|
|
30
|
+
|
|
31
|
+
Simple checks for `null` or `undefined` that narrow types correctly.
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { isNil, isNotNil } from '@drunkcod/argis';
|
|
35
|
+
|
|
36
|
+
const value: string | null = getNullableValue();
|
|
37
|
+
|
|
38
|
+
if (isNotNil(value)) {
|
|
39
|
+
// value is string
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const obj = { data: null };
|
|
43
|
+
if (isNil(obj, 'data')) {
|
|
44
|
+
// obj.data is null | undefined
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
#### `assertNotNil` / `argNotNil`
|
|
49
|
+
|
|
50
|
+
Throws an `ArgumentError` if a value is nil. Useful for early exits in functions.
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { argNotNil } from '@drunkcod/argis';
|
|
54
|
+
|
|
55
|
+
function process(input: { id: string | null }) {
|
|
56
|
+
argNotNil(input, 'id');
|
|
57
|
+
// input.id is now string
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
### 2. Property Access
|
|
64
|
+
|
|
65
|
+
#### `hasOwn` / `hasKey`
|
|
66
|
+
|
|
67
|
+
Safely check for the presence of a property and narrow the object type. `hasOwn` uses `Object.hasOwn`, while `hasKey` checks the prototype chain (`in` operator).
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { hasOwn } from '@drunkcod/argis';
|
|
71
|
+
|
|
72
|
+
const data: unknown = { id: 123, name: 'Argis' };
|
|
73
|
+
|
|
74
|
+
if (hasOwn(data, 'id', 'number')) {
|
|
75
|
+
// data is { id: number }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (hasOwn(data, 'name', (val): val is string => typeof val === 'string')) {
|
|
79
|
+
// data is { id: number, name: string }
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
#### `assertOwn` / `assertKey`
|
|
84
|
+
|
|
85
|
+
Throws a `MissingPropertyError` (subtype of `ArgumentError`) if the property is missing or fails the type check.
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import { assertOwn } from '@drunkcod/argis';
|
|
89
|
+
|
|
90
|
+
function start(config: object) {
|
|
91
|
+
assertOwn(config, 'apiKey', 'string');
|
|
92
|
+
// config.apiKey is string
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
### 3. Object Shaping
|
|
99
|
+
|
|
100
|
+
#### `pick` / `omit`
|
|
101
|
+
|
|
102
|
+
Type-safe versions of common object subsetting.
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
import { pick, omit } from '@drunkcod/argis';
|
|
106
|
+
|
|
107
|
+
const user = { id: 1, name: 'User', secret: '123' };
|
|
108
|
+
|
|
109
|
+
const publicUser = pick(user, 'id', 'name'); // { id: number, name: string }
|
|
110
|
+
const safeUser = omit(user, 'secret'); // { id: number, name: string }
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
#### `select`
|
|
114
|
+
|
|
115
|
+
A transformative pick. Select fields and optionally transform them in one pass.
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
import { select } from '@drunkcod/argis';
|
|
119
|
+
|
|
120
|
+
const input = { value: 21, message: 'hello' };
|
|
121
|
+
const result = select(input, {
|
|
122
|
+
value: (x) => (x * 2).toString(), // Transform number to string
|
|
123
|
+
message: 1, // Keep as-is (1 acts as 'true')
|
|
124
|
+
});
|
|
125
|
+
// result: { value: '42', message: 'hello' }
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
#### `Projected<T, P>` (Type only)
|
|
129
|
+
|
|
130
|
+
High-fidelity type inference for MongoDB-style projections, including dot-notation for nested paths.
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
import { Projected } from '@drunkcod/argis';
|
|
134
|
+
|
|
135
|
+
type User = {
|
|
136
|
+
id: number;
|
|
137
|
+
profile: {
|
|
138
|
+
firstName: string;
|
|
139
|
+
lastName: string;
|
|
140
|
+
avatar: string;
|
|
141
|
+
};
|
|
142
|
+
posts: { title: string; content: string }[];
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// Inclusion (1)
|
|
146
|
+
type Summary = Projected<
|
|
147
|
+
User,
|
|
148
|
+
{
|
|
149
|
+
'profile.firstName': 1;
|
|
150
|
+
'posts.title': 1;
|
|
151
|
+
}
|
|
152
|
+
>;
|
|
153
|
+
// Result: { profile: { firstName: string }, posts: { title: string }[] }
|
|
154
|
+
|
|
155
|
+
// Exclusion (0)
|
|
156
|
+
type SansAvatar = Projected<User, { 'profile.avatar': 0 }>;
|
|
157
|
+
// Result: { id: number, profile: { firstName: string, lastName: string }, posts: ... }
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
### 4. Serialization & Parsing
|
|
163
|
+
|
|
164
|
+
#### `Json<T>` (Type only)
|
|
165
|
+
|
|
166
|
+
A recursive type that ensures a type is JSON-serializable. It handles `toJSON()` methods and filters out non-serializable members like functions and symbols.
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
import { Json } from '@drunkcod/argis';
|
|
170
|
+
|
|
171
|
+
type Valid = Json<{ a: number; b: string }>; // { a: number, b: string }
|
|
172
|
+
type Invalid = Json<{ a: bigint }>; // JsonError<'bigint-not-serializeable'>
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
#### `parseBool`
|
|
176
|
+
|
|
177
|
+
A robust boolean parser for strings. Supports `true`, `false`, `1`, `0`, `yes`, `no`, `on`, `off` (case-insensitive).
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
import { parseBool, isBool } from '@drunkcod/argis';
|
|
181
|
+
|
|
182
|
+
const enabled = parseBool('on'); // true
|
|
183
|
+
const disabled = parseBool('0'); // false
|
|
184
|
+
const unknown = parseBool('maybe'); // null
|
|
185
|
+
|
|
186
|
+
if (isBool(unknown)) {
|
|
187
|
+
// narrows to boolean
|
|
188
|
+
}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
### 5. Utilities
|
|
194
|
+
|
|
195
|
+
- **`intOrUndefined(str)`**: Safely parses an integer or returns `undefined`.
|
|
196
|
+
- **`nullIfEmpty(arr)`**: Returns `null` if the array (or object with length) is empty.
|
|
197
|
+
- **`isThenable(obj)`**: Checks if an object is a Promise-like (has `.then()`).
|
|
198
|
+
|
|
199
|
+
---
|
|
200
|
+
|
|
201
|
+
## License
|
|
202
|
+
|
|
203
|
+
MIT
|
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,51 @@
|
|
|
1
|
+
import { IsAny, IsUnknown } from 'TypeUtils.js';
|
|
2
|
+
type Split<X extends string> = X extends `${infer H}.${infer R}` ? [H, R] : [X, never];
|
|
3
|
+
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
4
|
+
declare const PROJECTION_ERROR: unique symbol;
|
|
5
|
+
export type ProjectionError<T extends string> = {
|
|
6
|
+
readonly [PROJECTION_ERROR]: T;
|
|
7
|
+
};
|
|
8
|
+
declare const PROJECTION_TYPE: unique symbol;
|
|
9
|
+
type ProjectionType<T extends 0 | 1, P> = {
|
|
10
|
+
readonly [PROJECTION_TYPE]: T;
|
|
11
|
+
P: P;
|
|
12
|
+
};
|
|
13
|
+
type Leafs<T> = T extends object ? {
|
|
14
|
+
[K in keyof T]: Leafs<T[K]>;
|
|
15
|
+
}[keyof T] : T;
|
|
16
|
+
type Errors<T> = T extends ProjectionError<any> ? T : T extends object ? {
|
|
17
|
+
[K in keyof T]: Errors<T[K]>;
|
|
18
|
+
}[keyof T] : never;
|
|
19
|
+
type Splut<T> = {
|
|
20
|
+
[K in keyof T as K extends string ? Split<K>[0] : never]: K extends string ? Split<K> extends [infer _, infer R] ? [R] extends [never] ? T[K] : Splut<{
|
|
21
|
+
[P in R extends string ? R : never]: T[K];
|
|
22
|
+
}> : never : never;
|
|
23
|
+
};
|
|
24
|
+
type Squish<T> = T extends object ? {
|
|
25
|
+
[P in keyof T]: Squish<UnionToIntersection<T[P]>>;
|
|
26
|
+
} : T;
|
|
27
|
+
type _EnsureValidProjection<Leafs, Ok> = [
|
|
28
|
+
Leafs
|
|
29
|
+
] extends [1] ? ProjectionType<1, Ok> : [
|
|
30
|
+
Leafs
|
|
31
|
+
] extends [0] ? ProjectionType<0, Ok> : ProjectionError<'ERROR: Projections can only include *either* inclusions (1) or exclusions(0)'>;
|
|
32
|
+
type EnsureValidProjection<P> = _EnsureValidProjection<Leafs<P>, P>;
|
|
33
|
+
type UnwrapProjection<T> = T extends ProjectionType<any, infer P> ? P : T;
|
|
34
|
+
type EnsureNoError<T> = [Errors<T>] extends [never] ? T : Errors<T>;
|
|
35
|
+
type _ProjectInclusion<T, P> = P extends 1 ? T : T extends (infer U)[] ? _ProjectInclusion<U, P>[] : {
|
|
36
|
+
[K in keyof T as K extends keyof P ? K : never]: K extends keyof P ? _ProjectInclusion<T[K], P[K]> : never;
|
|
37
|
+
};
|
|
38
|
+
type _ProjectExclusion<T, P> = T extends (infer U)[] ? _ProjectExclusion<U, P>[] : {
|
|
39
|
+
[K in keyof T as K extends keyof P ? (P[K] extends 0 ? never : K) : K]: K extends keyof P ? _ProjectExclusion<T[K], P[K]> : T[K];
|
|
40
|
+
};
|
|
41
|
+
type _ProjectParsed<T, P> = P extends ProjectionType<1, infer Inclusion> ? _ProjectInclusion<T, Inclusion> : P extends ProjectionType<0, infer Exclusion> ? _ProjectExclusion<T, Exclusion> : ProjectionError<`ERROR: Unknown Projection type.`>;
|
|
42
|
+
type _ParsedProjection<T> = EnsureValidProjection<Squish<Splut<T>>>;
|
|
43
|
+
export type Projection<T> = EnsureNoError<UnwrapProjection<_ParsedProjection<T>>>;
|
|
44
|
+
/**
|
|
45
|
+
* Ungarbage is a post-processor that detects if a projection has collapsed into
|
|
46
|
+
* an empty object (common when mapping over 'any' or 'unknown') and restores
|
|
47
|
+
* the original input type if the input was 'any' or 'unknown'.
|
|
48
|
+
*/
|
|
49
|
+
type Ungarbage<T, P> = {} extends P ? (IsAny<T> extends true ? T : IsUnknown<T> extends true ? T : P) : P;
|
|
50
|
+
export type Projected<T, P> = Ungarbage<T, _ProjectParsed<T, _ParsedProjection<P>>>;
|
|
51
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export 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 Assign<Target, Source> = Omit<Target, keyof Source> & Source;
|
|
33
|
+
export type UnionMerge<A, B> = Pretty<UnwrapTags<UnionMergeCore<TagSpecial<A>, TagSpecial<B>>>>;
|
|
34
|
+
export type PickRequired<T> = {
|
|
35
|
+
[P in keyof T as IsOptional<T, P> extends true ? never : P]: T[P];
|
|
36
|
+
};
|
|
37
|
+
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,51 @@
|
|
|
1
|
+
import { IsAny, IsUnknown } from 'TypeUtils.js';
|
|
2
|
+
type Split<X extends string> = X extends `${infer H}.${infer R}` ? [H, R] : [X, never];
|
|
3
|
+
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
4
|
+
declare const PROJECTION_ERROR: unique symbol;
|
|
5
|
+
export type ProjectionError<T extends string> = {
|
|
6
|
+
readonly [PROJECTION_ERROR]: T;
|
|
7
|
+
};
|
|
8
|
+
declare const PROJECTION_TYPE: unique symbol;
|
|
9
|
+
type ProjectionType<T extends 0 | 1, P> = {
|
|
10
|
+
readonly [PROJECTION_TYPE]: T;
|
|
11
|
+
P: P;
|
|
12
|
+
};
|
|
13
|
+
type Leafs<T> = T extends object ? {
|
|
14
|
+
[K in keyof T]: Leafs<T[K]>;
|
|
15
|
+
}[keyof T] : T;
|
|
16
|
+
type Errors<T> = T extends ProjectionError<any> ? T : T extends object ? {
|
|
17
|
+
[K in keyof T]: Errors<T[K]>;
|
|
18
|
+
}[keyof T] : never;
|
|
19
|
+
type Splut<T> = {
|
|
20
|
+
[K in keyof T as K extends string ? Split<K>[0] : never]: K extends string ? Split<K> extends [infer _, infer R] ? [R] extends [never] ? T[K] : Splut<{
|
|
21
|
+
[P in R extends string ? R : never]: T[K];
|
|
22
|
+
}> : never : never;
|
|
23
|
+
};
|
|
24
|
+
type Squish<T> = T extends object ? {
|
|
25
|
+
[P in keyof T]: Squish<UnionToIntersection<T[P]>>;
|
|
26
|
+
} : T;
|
|
27
|
+
type _EnsureValidProjection<Leafs, Ok> = [
|
|
28
|
+
Leafs
|
|
29
|
+
] extends [1] ? ProjectionType<1, Ok> : [
|
|
30
|
+
Leafs
|
|
31
|
+
] extends [0] ? ProjectionType<0, Ok> : ProjectionError<'ERROR: Projections can only include *either* inclusions (1) or exclusions(0)'>;
|
|
32
|
+
type EnsureValidProjection<P> = _EnsureValidProjection<Leafs<P>, P>;
|
|
33
|
+
type UnwrapProjection<T> = T extends ProjectionType<any, infer P> ? P : T;
|
|
34
|
+
type EnsureNoError<T> = [Errors<T>] extends [never] ? T : Errors<T>;
|
|
35
|
+
type _ProjectInclusion<T, P> = P extends 1 ? T : T extends (infer U)[] ? _ProjectInclusion<U, P>[] : {
|
|
36
|
+
[K in keyof T as K extends keyof P ? K : never]: K extends keyof P ? _ProjectInclusion<T[K], P[K]> : never;
|
|
37
|
+
};
|
|
38
|
+
type _ProjectExclusion<T, P> = T extends (infer U)[] ? _ProjectExclusion<U, P>[] : {
|
|
39
|
+
[K in keyof T as K extends keyof P ? (P[K] extends 0 ? never : K) : K]: K extends keyof P ? _ProjectExclusion<T[K], P[K]> : T[K];
|
|
40
|
+
};
|
|
41
|
+
type _ProjectParsed<T, P> = P extends ProjectionType<1, infer Inclusion> ? _ProjectInclusion<T, Inclusion> : P extends ProjectionType<0, infer Exclusion> ? _ProjectExclusion<T, Exclusion> : ProjectionError<`ERROR: Unknown Projection type.`>;
|
|
42
|
+
type _ParsedProjection<T> = EnsureValidProjection<Squish<Splut<T>>>;
|
|
43
|
+
export type Projection<T> = EnsureNoError<UnwrapProjection<_ParsedProjection<T>>>;
|
|
44
|
+
/**
|
|
45
|
+
* Ungarbage is a post-processor that detects if a projection has collapsed into
|
|
46
|
+
* an empty object (common when mapping over 'any' or 'unknown') and restores
|
|
47
|
+
* the original input type if the input was 'any' or 'unknown'.
|
|
48
|
+
*/
|
|
49
|
+
type Ungarbage<T, P> = {} extends P ? (IsAny<T> extends true ? T : IsUnknown<T> extends true ? T : P) : P;
|
|
50
|
+
export type Projected<T, P> = Ungarbage<T, _ProjectParsed<T, _ParsedProjection<P>>>;
|
|
51
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export 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 Assign<Target, Source> = Omit<Target, keyof Source> & Source;
|
|
33
|
+
export type UnionMerge<A, B> = Pretty<UnwrapTags<UnionMergeCore<TagSpecial<A>, TagSpecial<B>>>>;
|
|
34
|
+
export type PickRequired<T> = {
|
|
35
|
+
[P in keyof T as IsOptional<T, P> extends true ? never : P]: T[P];
|
|
36
|
+
};
|
|
37
|
+
export {};
|
package/lib/cjs/index.d.ts
CHANGED
|
@@ -63,3 +63,7 @@ export declare function intOrUndefined(x?: string | null): number | undefined;
|
|
|
63
63
|
export declare function omit<T extends object, K extends OfType<keyof T, string>>(x: T, ...keys: readonly K[]): Omit<T, K>;
|
|
64
64
|
export declare function pick<T extends object, K extends OfType<keyof T, string>>(x: T, ...keys: readonly K[]): Pick<T, K>;
|
|
65
65
|
export { select } from './select.js';
|
|
66
|
+
export { parseBool, isBool } from './parseBool.js';
|
|
67
|
+
export type { Projection, Projected, ProjectionError } from './Projection.js';
|
|
68
|
+
export type { Json } from './Json.js';
|
|
69
|
+
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.select = 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;
|
|
@@ -87,3 +87,6 @@ function pick(x, ...keys) {
|
|
|
87
87
|
}
|
|
88
88
|
var select_js_1 = require("./select.js");
|
|
89
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
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -63,3 +63,7 @@ export declare function intOrUndefined(x?: string | null): number | undefined;
|
|
|
63
63
|
export declare function omit<T extends object, K extends OfType<keyof T, string>>(x: T, ...keys: readonly K[]): Omit<T, K>;
|
|
64
64
|
export declare function pick<T extends object, K extends OfType<keyof T, string>>(x: T, ...keys: readonly K[]): Pick<T, K>;
|
|
65
65
|
export { select } from './select.js';
|
|
66
|
+
export { parseBool, isBool } from './parseBool.js';
|
|
67
|
+
export type { Projection, Projected, ProjectionError } from './Projection.js';
|
|
68
|
+
export type { Json } from './Json.js';
|
|
69
|
+
export type * from './TypeUtils.js';
|
package/lib/index.js
CHANGED
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/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.15",
|
|
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
|
}
|