@gershy/clearing 0.0.2 → 0.0.3

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.
@@ -0,0 +1,488 @@
1
+ declare global {
2
+
3
+ // Util
4
+ type Fn<A=any[], T=any> = (...args: A) => T;
5
+ type Obj<V = any> = { [typeof iden]: 'obj' } & { [k: string]: V };
6
+
7
+ type ObjMode<O extends { [K: string]: any }> = O extends { [K in infer KK]: any } ? (string extends KK ? 'map' : 'rec') : never;
8
+ type ObjKeys<O extends Obj> = Extract<keyof O, string>; // | `${Extract<keyof O, number>}`; // TODO: num -> str conversion... smart??
9
+ type ObjVals<O extends Obj> = O[Extract<keyof O, string>];
10
+ type Arr<V = any> = V[];
11
+ type MaybeFn<T> = T | ((...args: any[]) => T);
12
+ type ResolveFn<T> = T extends ((...args: any[]) => any) ? ReturnType<T> : T;
13
+ type Itr<O extends Obj> = Iterable<[ ObjKeys<O>, ObjVals<O> ]>;
14
+ type Json = null | boolean | number | string | Json[] | { [K: string]: Json };
15
+ type Skip = undefined;
16
+ type SkipNever<V> = V extends Skip ? Skip extends V ? never : V : V; // 0 extends (1 & T) ? any : V extends Skip ? Skip extends V ? never : V : V; // Only thing that doesn't work is `SkipNever<any>`
17
+ type Endable = { end: () => MaybePromise<unknown> };
18
+ type Callable = ((...args: any[]) => any) | { new (...args: any): any };
19
+ type SovereignFn<T extends Callable = Callable> = T; // & { _svrn: 1 };
20
+ type Assign<A, B> = Omit<A, keyof B> & B;
21
+ type UGH = any; // Use when necessary to escape typing because typescript has failed us
22
+
23
+ type Dive<O extends Obj, K extends readonly string[], D = undefined> =
24
+ K extends [ infer K0, ...infer KM ]
25
+ ? K0 extends keyof O
26
+ ? (ObjMode<O> extends 'map' ? D : never) | (
27
+ KM extends []
28
+ ? O[K0]
29
+ : Dive<O[K0], KM, D>
30
+ )
31
+ : D
32
+ : never
33
+
34
+ // Function args have reversed assignability from typical inheritance; they are the key for
35
+ // converting a union to an intersection: https://stackoverflow.com/a/50375286/830905
36
+ // // type Combine<U> =
37
+ // // (U extends any ? (x: U) => void : never) extends (x: infer I) => void
38
+ // // ? I
39
+ // // : never;
40
+ // type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
41
+ // type Combine<Union> = Expand<(Union extends any ? (x: Union) => void : never) extends (x: infer Combined) => void ? Combined : never>;
42
+ // // type Combine0 = { a: number, b?: string } | { a?: number, b: string };
43
+ // type Combine1 = Combine<Combine0>;
44
+
45
+ // Jsfn
46
+ // Implements "jsfn" which is "json" but with functions allowed within the payload too! (Overall,
47
+ // pronounced "jay-sfun"). This is implemented simply using `fn.toString()` + `eval(fnString)`. The
48
+ // encoded format stores a list of all paths within the json which are stringified functions. The
49
+ // decoding process simply calls `JSON.parse`, loops through every stringified path, and expands
50
+ // the stringified values with `eval`. Of course, these functions should only be used with trusted
51
+ // data!
52
+ type Hoist = `<repo>/${string}` | `@aws/${string}`; // TODO: More imports??
53
+ type JsfnInst<Cls extends { new (...args: any[]): any }> = { // "jsfn instance"
54
+ hoist: `${Hoist}::${string}`,
55
+ form: Cls,
56
+ args: ConstructorParameters<Cls> & Jsfn
57
+ };
58
+
59
+ type Jsfn = null | boolean | number | string | SovereignFn | JsfnInst<any> | Jsfn[] | { [K: string]: Jsfn };
60
+ type JsfnDecoded<T extends Jsfn, N extends string = ''> =
61
+ N extends '+++++'
62
+ ? '<TOO DEEP>'
63
+ : T extends null
64
+ ? T
65
+ : T extends boolean
66
+ ? T
67
+ : T extends number
68
+ ? T
69
+ : T extends string
70
+ ? T
71
+ : T extends SovereignFn<infer Fn>
72
+ ? Fn
73
+ : T extends JsfnInst<any>
74
+ ? InstanceType<T['form']>
75
+ : T extends { [K: string]: Jsfn }
76
+ ? { [K in keyof T]: T[K] extends Jsfn ? JsfnDecoded<T[K], `+${N}`> : never }
77
+ : T extends Jsfn[]
78
+ ? { [K in keyof T]: T[K] extends Jsfn ? JsfnDecoded<T[K], `+${N}`> : never }
79
+ : Jsfn;
80
+
81
+ type JsfnEncoded<T extends Jsfn> = string & { _jsfn: T };
82
+
83
+ type ObjLeafs<O> = O extends Obj ? { [K in keyof O]: ObjLeafs<O[K]> }[keyof O] : O;
84
+ // type ObjLeafs0 = ObjLeafs<{ a: string, b: { c: number }, d: null, e: { f: { g: undefined } } }>;
85
+
86
+ type CharSet = { str: string, size: bigint, charVal: (c: string) => bigint, valChar: (n: bigint) => string };
87
+
88
+ type MaybePromise<T> = T | Promise<T>;
89
+
90
+ // Expose native classes
91
+ const AsyncGeneratorFunction: any;
92
+ const GeneratorFunction: any;
93
+
94
+ // Globals
95
+ const skip: Skip;
96
+ const window: any;
97
+ const skip: Skip;
98
+ const AsyncGeneratorFunction: typeof AsyncGeneratorFunction;
99
+ const GeneratorFunction: typeof GeneratorFunction;
100
+ const isForm: typeof isForm;
101
+ const hasForm: typeof hasForm;
102
+
103
+ // Utility
104
+ const sovereign: <Fn extends (...args: any) => any>(val: Fn) => Fn & { _svrn: 1 };
105
+ const then: <V, G, B = never>(val: MaybePromise<V>, resolve: (val: V) => G, reject?: (err: any) => B) => MaybePromise<G | B>;
106
+ const safe: <V, G, B = never>(fn: () => MaybePromise<V>, resolve: (val: V) => G, reject?: (err: any) => B) => MaybePromise<G | B>;
107
+
108
+ // Forms
109
+ const isForm: {
110
+ (val: unknown, num: BooleanConstructor): val is boolean,
111
+ (val: unknown, num: NumberConstructor): val is number,
112
+ (val: unknown, str: StringConstructor): val is string,
113
+ (val: unknown, buff: BufferConstructor): val is Buffer,
114
+ (val: unknown, arr: ArrayConstructor): val is any[],
115
+ (val: unknown, obj: ObjectConstructor): val is Obj<unknown>,
116
+ (val: unknown, fn: FunctionConstructor): val is Fn,
117
+ (val: unknown, fn: SymbolConstructor): val is symbol,
118
+ <T>(val: unknown, prm: PromiseConstructor): val is Promise<T>,
119
+ <C>(val: unknown, cls: C): val is InstanceType<C>
120
+ };
121
+ const getForm: {
122
+ (val: number): NumberConstructor,
123
+ (val: string): StringConstructor,
124
+ (val: Buffer): BufferConstructor,
125
+ (val: any[]): ArrayConstructor,
126
+ (val: { [K: string]: any }): ObjectConstructor,
127
+ (val: (...a: any[]) => any): FunctionConstructor,
128
+ (val: Promise<any>): PromiseConstructor,
129
+ <T>(val: T): { new (...args: any[]): T }
130
+ };
131
+ const hasForm: {
132
+ (val: any, fnc: FunctionConstructor): val is (...args: any) => any,
133
+ (val: any, obj: ObjectConstructor): val is { [K: string]: any },
134
+ (val: any, str: StringConstructor): val is string,
135
+ (val: any, num: NumberConstructor): val is number,
136
+ (val: any, arr: ArrayConstructor): val is any[],
137
+ (val: any, arr: BufferConstructor): val is Buffer,
138
+ <T>(val: any, prm: PromiseConstructor): val is Promise<T>,
139
+ <C>(val: unknown, cls: C): val is InstanceType<C>
140
+ };
141
+ const getFormName: (f: any) => string;
142
+
143
+ const jsfn: {
144
+ encode: SovereignFn<
145
+ <Data extends Jsfn>(dec: Data, opts?: { encodeFn?: (fn: Fn) => string }) => {
146
+ hoists: `<repo>/${string}::${string}`[],
147
+ str: JsfnEncoded<Data>;
148
+ }
149
+ >
150
+ };
151
+
152
+ const add: unique symbol;
153
+ const allow: unique symbol;
154
+ const at: unique symbol;
155
+ const apart: unique symbol;
156
+ const assert: unique symbol;
157
+ const bind: unique symbol;
158
+ const bits: unique symbol;
159
+ const built: unique symbol;
160
+ const char: unique symbol;
161
+ const code: unique symbol;
162
+ const count: unique symbol;
163
+ const cut: unique symbol;
164
+ const dive: unique symbol;
165
+ const empty: unique symbol;
166
+ const fire: unique symbol;
167
+ const group: unique symbol;
168
+ const has: unique symbol;
169
+ const hasHead: unique symbol;
170
+ const hasTail: unique symbol;
171
+ const indent: unique symbol;
172
+ const isInt: unique symbol;
173
+ const limn: unique symbol;
174
+ const lower: unique symbol;
175
+ const mod: unique symbol;
176
+ const map: unique symbol;
177
+ const mapk: unique symbol;
178
+ const merge: unique symbol;
179
+ const padHead: unique symbol;
180
+ const padTail: unique symbol;
181
+ const rem: unique symbol;
182
+ const find: unique symbol;
183
+ const slash: unique symbol;
184
+ const slice: unique symbol;
185
+ const suppress: unique symbol;
186
+ const toArr: unique symbol;
187
+ const toNum: unique symbol;
188
+ const toObj: unique symbol;
189
+ const toStr: unique symbol;
190
+ const upper: unique symbol;
191
+
192
+ // Typing only
193
+ const iden: unique symbol; // associate types with their *direct* constructor
194
+
195
+ interface JSON { parse: (val: Buffer) => any };
196
+
197
+ interface ProtoWithSymbols {
198
+ [add]: undefined
199
+ [at]: undefined
200
+ [apart]: undefined
201
+ [assert]: undefined
202
+ [bind]: undefined
203
+ [built]: undefined
204
+ [count]: undefined
205
+ [cut]: undefined
206
+ [dive]: undefined
207
+ [empty]: undefined
208
+ [fire]: undefined
209
+ [group]: undefined
210
+ [has]: undefined
211
+ [hasHead]: undefined
212
+ [hasTail]: undefined
213
+ [indent]: undefined
214
+ [limn]: undefined
215
+ [lower]: undefined
216
+ [mod]: undefined
217
+ [map]: undefined
218
+ [mapk]: undefined
219
+ [merge]: undefined
220
+ [padHead]: undefined
221
+ [padTail]: undefined
222
+ [rem]: undefined
223
+ [find]: undefined
224
+ [slash]: undefined
225
+ [slice]: undefined
226
+ [suppress]: undefined
227
+ [toArr]: undefined
228
+ [toNum]: undefined
229
+ [toObj]: undefined
230
+ [toStr]: undefined
231
+ [upper]: undefined
232
+ };
233
+
234
+ interface ErrorConstructor {
235
+ assert: {
236
+ <R, V = any>(args: V, fn: (args: V) => boolean): asserts args is R,
237
+ }
238
+ };
239
+ interface Error extends ProtoWithSymbols {
240
+ [mod]: (props: { [K: string]: any }) => Error,
241
+ [fire]: (props?: { [K: string]: any }) => never,
242
+ [suppress]: () => Error,
243
+
244
+ [limn]: (seen?: Map) => (Obj<Json> & {
245
+ form: string,
246
+ msg: string,
247
+ trace: string[],
248
+ cause: null | ReturnType<Error[typeof limn]>
249
+ })
250
+ };
251
+
252
+ interface ArrayConstructor {
253
+ stub: any[]
254
+ };
255
+ interface Array<T> extends ProtoWithSymbols {
256
+ [iden]: 'arr',
257
+ [has]: (val: unknown) => boolean,
258
+ [map]: {
259
+ <
260
+ Fn extends (v: T, i: number) => any
261
+ >(
262
+ fn: Fn
263
+ ): Exclude<ReturnType<Fn>, Skip>[]
264
+ },
265
+ [add]: <TT extends T>(val: TT) => void,
266
+ [rem]: <TT extends T>(val: TT) => void,
267
+ [count]: () => number,
268
+ [empty]: (this: any[]) => this is never[],
269
+ [toObj]: <Fn extends (v: T, n: number) => Skip | readonly [string, any], R = Exclude<ReturnType<Fn>, Skip>>(fn: Fn) => { [K in R[0]]: R[1] },
270
+ [find]: (fn: (val: T, n: number) => any) => ({ found: true, val: T, ind: number } | { found: false, val: null, ind: null }),
271
+ [group]: <G extends string>(fn: (v: T) => G) => { [K in G]?: T[] }
272
+ };
273
+
274
+ interface FunctionConstructor {
275
+ stub: <T>(a: T, ...args: any[]) => T
276
+ };
277
+ interface Function extends ProtoWithSymbols {
278
+ [iden]: 'fnc',
279
+ [bind]: {
280
+ <
281
+ Fn extends (this: any, ...args: any[]) => any,
282
+ To
283
+ >(
284
+ this: Fn,
285
+ to: To
286
+ ): ((this: To, ...args: Parameters<Fn>) => ReturnType<Fn>)
287
+ }
288
+ };
289
+
290
+ interface GeneratorFunctionConstructor {};
291
+ interface Generator<T> extends ProtoWithSymbols {
292
+ [toArr]: {
293
+ <
294
+ Fn extends (v: T) => any
295
+ >(
296
+ fn: Fn
297
+ ):
298
+ ReturnType<Fn>[],
299
+ }
300
+ };
301
+
302
+ interface NumberConstructor {
303
+ int32: number
304
+ };
305
+ interface Number extends ProtoWithSymbols {
306
+ [iden]: 'num',
307
+ [toStr]: (str: string | CharSet, len?: number) => string,
308
+ [toArr]: <T>(fn: (n: number) => T) => T[],
309
+ [map]: undefined // Prevent calling `map` on `Number` - use `toStr` instead!
310
+ };
311
+
312
+ interface BigIntConstructor {};
313
+ interface BigInt extends ProtoWithSymbols {
314
+ [toStr]: (str: string | CharSet, len?: number) => string
315
+ };
316
+
317
+ interface ObjectConstructor {
318
+ plain: (obj?: any) => any,
319
+ };
320
+
321
+ interface Object extends ProtoWithSymbols {
322
+ [iden]: 'obj',
323
+ [empty]: {
324
+ <
325
+ O extends Obj
326
+ >(
327
+ this: O
328
+ ): ObjMode<O> extends 'rec'
329
+ ? never & '<rec>.empty is invalid'
330
+ : this is Obj<never>,
331
+ },
332
+
333
+ [at]: {
334
+ <
335
+ O extends Obj,
336
+ K extends string | string[],
337
+ D extends any = undefined
338
+ >(
339
+ this: O,
340
+ k: string | string[],
341
+ def?: D
342
+ ): Dive<O, K extends string[] ? K : [ K ], D>,
343
+ },
344
+ [has]: {
345
+ // <O extends Obj>(this: O, k: string): boolean,
346
+ // <O extends Obj>(this: O, k: string): k is (ObjMode<O> extends 'rec' ? keyof O : any),
347
+ // <O extends Obj>(this: O, k: string): k is (ObjMode<O> extends 'rec' ? keyof O : any),
348
+
349
+ // <O extends Obj, K extends keyof any>(this: O, k: K): this is { [K]: O[keyof O] }
350
+
351
+ <O extends Obj>(this: O, k: unknown): k is keyof O
352
+
353
+ },
354
+ [map]: {
355
+ <
356
+ O extends Obj,
357
+ Fn extends (v: ObjVals<O>, k: ObjKeys<O>) => any
358
+ >(
359
+ this: O,
360
+ fn: Fn
361
+ ): { [K in keyof O]: Exclude<ReturnType<Fn>, Skip> },
362
+ },
363
+ [mapk]: {
364
+ <
365
+ O extends Obj,
366
+ Fn extends (v: O[keyof O], k: ObjKeys<O>) => undefined | [ string, any ]
367
+ >(
368
+ this: O,
369
+ fn: Fn
370
+ ): { [K in Exclude<ReturnType<Fn>, Skip>[0]]: Exclude<ReturnType<Fn>, Skip>[1] },
371
+ },
372
+ [merge]: {
373
+ <
374
+ O1 extends Obj,
375
+ O2 extends Obj
376
+ >(
377
+ this: readonly O1,
378
+ val: readonly O2
379
+ ): O1 & O2,
380
+ },
381
+ [slice]: {
382
+ <O extends Obj, K extends readonly (keyof O)[]>(this: O, keys: K): { [P in K[number]]: SkipNever<O[P]> }
383
+ },
384
+ [slash]: {
385
+ <
386
+ O extends Obj,
387
+ T extends readonly (keyof O)[]
388
+ >(
389
+ this: O,
390
+ keys: T
391
+ ): { [K in keyof O as Exclude<keyof O, T[number]>]: O[K] }
392
+ },
393
+ [toArr]: {
394
+ <
395
+ O extends Obj,
396
+ Fn extends (v: O[keyof O], k: ObjKeys<O>) => any
397
+ >(
398
+ this: O,
399
+ fn: Fn
400
+ ): Exclude<ReturnType<Fn>, Skip>[],
401
+ },
402
+ [built]: {
403
+ (this: Obj<any>): any
404
+ // <O extends Obj>(this: O): Obj<ObjLeafs<O>>,
405
+ },
406
+ [apart]: {
407
+ // Iterates all terminal values in an object tree, along with the dive key needed to access them
408
+ <O extends Obj>(this: O): Generator<{ dive: string[], val: any }>,
409
+ },
410
+ [dive]: {
411
+ <O extends Obj, Cmps extends (keyof any)[], D extends any = Skip>(this: O, cmps: readonly Cmps, def: D): Dive<O, Cmps> | D,
412
+ },
413
+ [count]: {
414
+ <O extends Obj>(this: O): number,
415
+ },
416
+ [Symbol.iterator]: {
417
+ <O extends Obj>(this: O): Iterator<[ ObjKeys<O>, ObjVals<O> ]>
418
+ },
419
+ $$inspect: <V>(this: V) => { v: V }
420
+ };
421
+
422
+ interface PromiseConstructor {
423
+ // <T>(fn: (resolve: (v: T) => void, reject: (err: any) => void) => any): Promise<T>,
424
+ later: <T=void>() => PromiseLater<T>,
425
+ allArr: PromiseConstructor['all'],
426
+ allObj: <O extends { [K: keyof any]: Promise<any> }>(obj: O) => { [K in keyof O]: Awaited<O[K]> }
427
+ };
428
+ interface Promise<T> {};
429
+ interface PromiseLater<T=void> extends Promise<T>, ProtoWithSymbols {
430
+ resolve: T extends void ? () => void : (v: T) => void,
431
+ reject: (err: any) => void
432
+ };
433
+
434
+ interface SetConstructor {};
435
+ interface Set<T> extends ProtoWithSymbols {
436
+ [toArr]: <V>(fn: (val: T, ind: number) => V) => V[],
437
+ [rem]: (val: T) => void
438
+ };
439
+
440
+
441
+ interface MapConstructor {};
442
+ interface Map<K, V> extends ProtoWithSymbols {
443
+ [add]: (k: K, v: V) => void,
444
+ [toArr]: <T>(fn: (val: V, key: K) => T) => T[],
445
+ [toObj]: <T>(fn: (val: V, key: K) => T) => { [Key in K]: T },
446
+ [rem]: (key: K) => void
447
+ };
448
+
449
+ interface StringConstructor {
450
+ charset: (str: string) => CharSet,
451
+ base32: string,
452
+ base36: string,
453
+ base62: string,
454
+ base64: string,
455
+ base64Std: string,
456
+ baseline: (str: string) => string,
457
+ };
458
+ interface String extends ProtoWithSymbols {
459
+ [count](): number,
460
+ [has](s: string): boolean,
461
+ [padHead](n: number, s?: string): string,
462
+ [padTail](n: number, s?: string): string,
463
+ [toNum]: (chrs: string | CharSet) => bigint,
464
+ [hasHead]<H extends string>(this: string, head: H): this is `${H}${string}`,
465
+ [hasTail]<T extends string>(this: string, tail: T): this is `${string}${T}`,
466
+ [upper]<S extends string>(this: S): Uppercase<S>,
467
+ [lower]<S extends string>(this: S): Lowercase<S>,
468
+ [cut]: {
469
+ (str: string, cuts: 1): [ string, string ],
470
+ (str: string, cuts: 2): [ string, string, string ],
471
+ (str: string, cuts: 3): [ string, string, string, string ],
472
+ (str: string, cuts: 4): [ string, string, string, string, string ],
473
+ (str: string, cuts?: number): string[]
474
+ },
475
+ [indent]: {
476
+ (amount: number, char?: string): string,
477
+ (str: string): string
478
+ }
479
+ };
480
+
481
+ }
482
+
483
+ export {};
484
+
485
+ // Type testing - these aren't exported!
486
+ type Dive0 = Dive<{ a: { b: { c: 'xyz' } } }, [ 'a', 'b', 'c' ]>;
487
+ type Dive1 = Dive<{ a: { b: { c: 'xyz' } } }, [ 'a', 'b' ]>;
488
+ type Dive2 = Dive<{ a: { b: { c: 'xyz' } } }, [ 'a', 'b', 'd' ]>;
package/package.json CHANGED
@@ -1,15 +1,35 @@
1
1
  {
2
2
  "name": "@gershy/clearing",
3
- "version": "0.0.2",
4
- "description": "Simplified js basics",
5
- "repository": {
6
- "type": "git",
7
- "url": "https://github.com/gershy/clearing"
3
+ "version": "0.0.3",
4
+ "description": "Adds the features you always wish javascript had!",
5
+ "keywords": [
6
+ "clearing",
7
+ "toArr",
8
+ "toObj"
9
+ ],
10
+ "author": "Gershom Maes",
11
+ "license": "ISC",
12
+ "exports": {
13
+ ".": {
14
+ "import": "./cmp/mjs/main.js",
15
+ "require": "./cmp/cjs/main.js",
16
+ "types": "./cmp/types/global.d.ts"
17
+ }
8
18
  },
9
- "main": "main.ts",
10
- "scripts": {
11
- "test": "node ./test/main.ts"
19
+ "devDependencies": {
20
+ "@types/node": "^24.10.1",
21
+ "cpx": "^1.5.0",
22
+ "rimraf": "^6.1.2",
23
+ "typescript": "^5.9.3"
12
24
  },
13
- "author": "Gershom Maes",
14
- "license": "ISC"
25
+ "files": [
26
+ "cmp"
27
+ ],
28
+ "scripts": {
29
+ "build.cjs": "tsc -p ts/tsconfig.cjs.json",
30
+ "build.mjs": "tsc -p ts/tsconfig.mjs.json",
31
+ "build.types": "cpx src/global.d.ts cmp/types",
32
+ "build": "rimraf cmp && npm run build.cjs && npm run build.mjs && npm run build.types",
33
+ "publish": "npm run build && npm publish --access public"
34
+ }
15
35
  }