@strictly/define 0.0.27 → 0.0.28

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.
@@ -7,12 +7,12 @@ $ tsup
7
7
  CLI Target: es6
8
8
  CJS Build start
9
9
  ESM Build start
10
- ESM dist/index.js 32.58 KB
11
- ESM ⚡️ Build success in 83ms
12
- CJS dist/index.cjs 35.17 KB
13
- CJS ⚡️ Build success in 87ms
10
+ CJS dist/index.cjs 35.93 KB
11
+ CJS ⚡️ Build success in 98ms
12
+ ESM dist/index.js 33.34 KB
13
+ ESM ⚡️ Build success in 101ms
14
14
  DTS Build start
15
- DTS ⚡️ Build success in 12982ms
16
- DTS dist/index.d.cts 41.63 KB
17
- DTS dist/index.d.ts 41.63 KB
18
- Done in 14.19s.
15
+ DTS ⚡️ Build success in 12265ms
16
+ DTS dist/index.d.cts 41.80 KB
17
+ DTS dist/index.d.ts 41.80 KB
18
+ Done in 13.64s.
@@ -1,3 +1,3 @@
1
1
  yarn run v1.22.22
2
2
  $ tsc -b
3
- Done in 0.37s.
3
+ Done in 0.33s.
@@ -1,3 +1,3 @@
1
1
  yarn run v1.22.22
2
2
  $ json -f package.json -f package.exports.json --merge > package.release.json
3
- Done in 0.12s.
3
+ Done in 0.11s.
package/dist/index.cjs CHANGED
@@ -258,7 +258,7 @@ function jsonPathUnprefix(prefix, path) {
258
258
  }
259
259
 
260
260
  // transformers/flatteners/flatten_value_to.ts
261
- function flattenValueTo({ definition }, v, setter, mapper2) {
261
+ function flattenValueTo({ definition }, v, setter, mapper2, listIndicesToKeys = {}) {
262
262
  const r = {};
263
263
  internalFlattenValue(
264
264
  "$",
@@ -267,50 +267,61 @@ function flattenValueTo({ definition }, v, setter, mapper2) {
267
267
  v,
268
268
  setter,
269
269
  mapper2,
270
- r
270
+ r,
271
+ listIndicesToKeys
271
272
  );
272
273
  return r;
273
274
  }
274
- function internalFlattenValue(valuePath, typePath, typeDef, v, setter, mapper2, r) {
275
+ function internalFlattenValue(valuePath, typePath, typeDef, v, setter, mapper2, r, listIndicesToKeys) {
275
276
  r[valuePath] = mapper2(typeDef, v, setter, typePath, valuePath);
276
277
  if (v !== void 0) {
277
- return internalFlattenValueChildren(valuePath, typePath, typeDef, v, mapper2, r);
278
+ return internalFlattenValueChildren(valuePath, typePath, typeDef, v, mapper2, r, listIndicesToKeys);
278
279
  }
279
280
  return r;
280
281
  }
281
- function internalFlattenValueChildren(valuePath, typePath, typeDef, v, mapper2, r) {
282
+ function internalFlattenValueChildren(valuePath, typePath, typeDef, v, mapper2, r, listIndicesToKeys) {
282
283
  switch (typeDef.type) {
283
284
  case 1 /* Literal */:
284
285
  return r;
285
286
  case 2 /* List */:
286
- return internalFlattenListChildren(valuePath, typePath, typeDef, v, mapper2, r);
287
+ return internalFlattenListChildren(valuePath, typePath, typeDef, v, mapper2, r, listIndicesToKeys);
287
288
  case 3 /* Record */:
288
- return internalFlattenRecordChildren(valuePath, typePath, typeDef, v, mapper2, r);
289
+ return internalFlattenRecordChildren(valuePath, typePath, typeDef, v, mapper2, r, listIndicesToKeys);
289
290
  case 4 /* Object */:
290
- return internalFlattenObjectChildren(valuePath, typePath, typeDef, v, mapper2, r);
291
+ return internalFlattenObjectChildren(valuePath, typePath, typeDef, v, mapper2, r, listIndicesToKeys);
291
292
  case 5 /* Union */:
292
- return internalFlattenUnionChildren(valuePath, typePath, typeDef, v, mapper2, r);
293
+ return internalFlattenUnionChildren(valuePath, typePath, typeDef, v, mapper2, r, listIndicesToKeys);
293
294
  default:
294
295
  throw new import_base3.UnreachableError(typeDef);
295
296
  }
296
297
  }
297
- function internalFlattenListChildren(valuePath, typePath, { elements }, v, mapper2, r) {
298
+ function internalFlattenListChildren(valuePath, typePath, { elements }, v, mapper2, r, listIndicesToKeys) {
299
+ let indicesToKeys = listIndicesToKeys[valuePath];
300
+ if (indicesToKeys == null) {
301
+ indicesToKeys = [0];
302
+ listIndicesToKeys[valuePath] = indicesToKeys;
303
+ }
298
304
  const newTypePath = jsonPath(typePath, "*");
299
- return v.reduce(function(r2, e, i) {
305
+ return v.reduce(function(r2, e, index) {
306
+ const key = indicesToKeys[index];
307
+ if (index === indicesToKeys.length - 1) {
308
+ indicesToKeys.push(key + 1);
309
+ }
300
310
  return internalFlattenValue(
301
- jsonPath(valuePath, i),
311
+ jsonPath(valuePath, key),
302
312
  newTypePath,
303
313
  elements,
304
314
  e,
305
315
  (e2) => {
306
- v[i] = e2;
316
+ v[index] = e2;
307
317
  },
308
318
  mapper2,
309
- r2
319
+ r2,
320
+ listIndicesToKeys
310
321
  );
311
322
  }, r);
312
323
  }
313
- function internalFlattenRecordChildren(valuePath, typePath, { valueTypeDef }, v, mapper2, r) {
324
+ function internalFlattenRecordChildren(valuePath, typePath, { valueTypeDef }, v, mapper2, r, listIndicesToKeys) {
314
325
  const newTypePath = jsonPath(typePath, "*");
315
326
  return (0, import_base3.reduce)(
316
327
  v,
@@ -324,13 +335,14 @@ function internalFlattenRecordChildren(valuePath, typePath, { valueTypeDef }, v,
324
335
  v[k] = value2;
325
336
  },
326
337
  mapper2,
327
- r2
338
+ r2,
339
+ listIndicesToKeys
328
340
  );
329
341
  },
330
342
  r
331
343
  );
332
344
  }
333
- function internalFlattenObjectChildren(valuePath, typePath, { fields }, v, mapper2, r) {
345
+ function internalFlattenObjectChildren(valuePath, typePath, { fields }, v, mapper2, r, listIndicesToKeys) {
334
346
  return (0, import_base3.reduce)(
335
347
  fields,
336
348
  function(r2, k, fieldTypeDef) {
@@ -344,13 +356,14 @@ function internalFlattenObjectChildren(valuePath, typePath, { fields }, v, mappe
344
356
  v[k] = value;
345
357
  },
346
358
  mapper2,
347
- r2
359
+ r2,
360
+ listIndicesToKeys
348
361
  );
349
362
  },
350
363
  r
351
364
  );
352
365
  }
353
- function internalFlattenUnionChildren(valuePath, typePath, typeDef, v, mapper2, r) {
366
+ function internalFlattenUnionChildren(valuePath, typePath, typeDef, v, mapper2, r, listIndicesToKeys) {
354
367
  const childTypeDef = getUnionTypeDef(typeDef, v);
355
368
  const qualifier = typeDef.discriminator != null ? `:${v[typeDef.discriminator]}` : "";
356
369
  return internalFlattenValueChildren(
@@ -359,7 +372,8 @@ function internalFlattenUnionChildren(valuePath, typePath, typeDef, v, mapper2,
359
372
  childTypeDef,
360
373
  v,
361
374
  mapper2,
362
- r
375
+ r,
376
+ listIndicesToKeys
363
377
  );
364
378
  }
365
379
  function getUnionTypeDef(typeDef, v) {
@@ -499,12 +513,13 @@ function mapAccessor(_t, value, set) {
499
513
  set
500
514
  };
501
515
  }
502
- function flattenAccessorsOfType(t, value, setValue) {
516
+ function flattenAccessorsOfType(t, value, setValue, listIndicesToKeys) {
503
517
  return flattenValueTo(
504
518
  t,
505
519
  value,
506
520
  setValue,
507
- mapAccessor
521
+ mapAccessor,
522
+ listIndicesToKeys
508
523
  );
509
524
  }
510
525
 
@@ -512,13 +527,14 @@ function flattenAccessorsOfType(t, value, setValue) {
512
527
  function mapTypePaths(_t, _value, _set, typePath) {
513
528
  return typePath;
514
529
  }
515
- function flattenJsonValueToTypePathsOf(t, value) {
530
+ function flattenJsonValueToTypePathsOf(t, value, listIndicesToKeys) {
516
531
  return flattenValueTo(
517
532
  t,
518
533
  value,
519
534
  function() {
520
535
  },
521
- mapTypePaths
536
+ mapTypePaths,
537
+ listIndicesToKeys
522
538
  );
523
539
  }
524
540
 
@@ -628,13 +644,14 @@ function flattenValidatorsOfValidatingType(type) {
628
644
  function mapper(_t, v) {
629
645
  return v;
630
646
  }
631
- function flattenValuesOfType(typeDef, value) {
647
+ function flattenValuesOfType(typeDef, value, listIndicesToKeys) {
632
648
  return flattenValueTo(
633
649
  typeDef,
634
650
  value,
635
651
  () => {
636
652
  },
637
- mapper
653
+ mapper,
654
+ listIndicesToKeys
638
655
  );
639
656
  }
640
657
 
package/dist/index.d.cts CHANGED
@@ -189,14 +189,14 @@ type FlattenedAccessorsOfType<T extends Type, Flattened extends Readonly<Record<
189
189
  type AnyValueType = any;
190
190
  type Setter<V> = (v: V) => void;
191
191
  type Mapper<R> = (t: StrictTypeDef, v: AnyValueType, setter: Setter<AnyValueType>, typePath: string, valuePath: string) => R;
192
- declare function flattenValueTo<T extends StrictType, M, R extends Readonly<Record<string, M>>>({ definition }: T, v: ValueOfType<T>, setter: Setter<ValueOfType<T>>, mapper: Mapper<M>): R;
192
+ declare function flattenValueTo<T extends StrictType, M, R extends Readonly<Record<string, M>>>({ definition }: T, v: ValueOfType<T>, setter: Setter<ValueOfType<T>>, mapper: Mapper<M>, listIndicesToKeys?: Record<string, number[]>): R;
193
193
  declare function getUnionTypeDef<T extends UnionTypeDef>(typeDef: T, v: ValueOfType<ReadonlyTypeOfType<{
194
194
  definition: T;
195
195
  }>>): any;
196
196
 
197
- declare function flattenAccessorsOfType<T extends Type, R extends Readonly<Record<string, Accessor<any>>> = FlattenedAccessorsOfType<T>>(t: T, value: ValueOfType<T>, setValue: Setter<ValueOfType<T>>): R;
197
+ declare function flattenAccessorsOfType<T extends Type, R extends Readonly<Record<string, Accessor<any>>> = FlattenedAccessorsOfType<T>>(t: T, value: ValueOfType<T>, setValue: Setter<ValueOfType<T>>, listIndicesToKeys?: Record<string, number[]>): R;
198
198
 
199
- declare function flattenJsonValueToTypePathsOf<T extends Type, R extends Record<string, string | number | symbol>>(t: T, value: ValueOfType<T>): R;
199
+ declare function flattenJsonValueToTypePathsOf<T extends Type, R extends Record<string, string | number | symbol>>(t: T, value: ValueOfType<T>, listIndicesToKeys?: Record<string, number[]>): R;
200
200
 
201
201
  declare function flattenTypesOfType<T extends StrictType>(t: T): Record<string, Type>;
202
202
 
@@ -300,7 +300,7 @@ type FlattenedValidatorsOfValidatingType<T extends ValidatingType, TypePathsToVa
300
300
 
301
301
  declare function flattenValidatorsOfValidatingType<T extends ValidatingType, TypePathsToValuePaths extends Readonly<Record<keyof FlattenedTypes, string>>, FlattenedTypes extends Readonly<Record<string, ValidatingType>> = FlattenedTypesOfValidatingType<T, '*'>>(type: T): FlattenedValidatorsOfValidatingType<T, TypePathsToValuePaths, FlattenedTypes>;
302
302
 
303
- declare function flattenValuesOfType<T extends Type>(typeDef: Type, value: ValueOfType<T>): Record<string, any>;
303
+ declare function flattenValuesOfType<T extends Type>(typeDef: Type, value: ValueOfType<T>, listIndicesToKeys?: Record<string, number[]>): Record<string, any>;
304
304
 
305
305
  declare function jsonPath<Prefix extends string, Segment extends number | string>(prefix: Prefix, segment: Segment): `${Prefix}.${Segment}`;
306
306
  declare function jsonPath<Prefix extends string, Segment extends number | string, Qualifier extends string>(prefix: Prefix, segment: Segment, qualifier: Qualifier): `${Prefix}${Qualifier}.${Segment}`;
package/dist/index.d.ts CHANGED
@@ -189,14 +189,14 @@ type FlattenedAccessorsOfType<T extends Type, Flattened extends Readonly<Record<
189
189
  type AnyValueType = any;
190
190
  type Setter<V> = (v: V) => void;
191
191
  type Mapper<R> = (t: StrictTypeDef, v: AnyValueType, setter: Setter<AnyValueType>, typePath: string, valuePath: string) => R;
192
- declare function flattenValueTo<T extends StrictType, M, R extends Readonly<Record<string, M>>>({ definition }: T, v: ValueOfType<T>, setter: Setter<ValueOfType<T>>, mapper: Mapper<M>): R;
192
+ declare function flattenValueTo<T extends StrictType, M, R extends Readonly<Record<string, M>>>({ definition }: T, v: ValueOfType<T>, setter: Setter<ValueOfType<T>>, mapper: Mapper<M>, listIndicesToKeys?: Record<string, number[]>): R;
193
193
  declare function getUnionTypeDef<T extends UnionTypeDef>(typeDef: T, v: ValueOfType<ReadonlyTypeOfType<{
194
194
  definition: T;
195
195
  }>>): any;
196
196
 
197
- declare function flattenAccessorsOfType<T extends Type, R extends Readonly<Record<string, Accessor<any>>> = FlattenedAccessorsOfType<T>>(t: T, value: ValueOfType<T>, setValue: Setter<ValueOfType<T>>): R;
197
+ declare function flattenAccessorsOfType<T extends Type, R extends Readonly<Record<string, Accessor<any>>> = FlattenedAccessorsOfType<T>>(t: T, value: ValueOfType<T>, setValue: Setter<ValueOfType<T>>, listIndicesToKeys?: Record<string, number[]>): R;
198
198
 
199
- declare function flattenJsonValueToTypePathsOf<T extends Type, R extends Record<string, string | number | symbol>>(t: T, value: ValueOfType<T>): R;
199
+ declare function flattenJsonValueToTypePathsOf<T extends Type, R extends Record<string, string | number | symbol>>(t: T, value: ValueOfType<T>, listIndicesToKeys?: Record<string, number[]>): R;
200
200
 
201
201
  declare function flattenTypesOfType<T extends StrictType>(t: T): Record<string, Type>;
202
202
 
@@ -300,7 +300,7 @@ type FlattenedValidatorsOfValidatingType<T extends ValidatingType, TypePathsToVa
300
300
 
301
301
  declare function flattenValidatorsOfValidatingType<T extends ValidatingType, TypePathsToValuePaths extends Readonly<Record<keyof FlattenedTypes, string>>, FlattenedTypes extends Readonly<Record<string, ValidatingType>> = FlattenedTypesOfValidatingType<T, '*'>>(type: T): FlattenedValidatorsOfValidatingType<T, TypePathsToValuePaths, FlattenedTypes>;
302
302
 
303
- declare function flattenValuesOfType<T extends Type>(typeDef: Type, value: ValueOfType<T>): Record<string, any>;
303
+ declare function flattenValuesOfType<T extends Type>(typeDef: Type, value: ValueOfType<T>, listIndicesToKeys?: Record<string, number[]>): Record<string, any>;
304
304
 
305
305
  declare function jsonPath<Prefix extends string, Segment extends number | string>(prefix: Prefix, segment: Segment): `${Prefix}.${Segment}`;
306
306
  declare function jsonPath<Prefix extends string, Segment extends number | string, Qualifier extends string>(prefix: Prefix, segment: Segment, qualifier: Qualifier): `${Prefix}${Qualifier}.${Segment}`;
package/dist/index.js CHANGED
@@ -213,7 +213,7 @@ function jsonPathUnprefix(prefix, path) {
213
213
  }
214
214
 
215
215
  // transformers/flatteners/flatten_value_to.ts
216
- function flattenValueTo({ definition }, v, setter, mapper2) {
216
+ function flattenValueTo({ definition }, v, setter, mapper2, listIndicesToKeys = {}) {
217
217
  const r = {};
218
218
  internalFlattenValue(
219
219
  "$",
@@ -222,50 +222,61 @@ function flattenValueTo({ definition }, v, setter, mapper2) {
222
222
  v,
223
223
  setter,
224
224
  mapper2,
225
- r
225
+ r,
226
+ listIndicesToKeys
226
227
  );
227
228
  return r;
228
229
  }
229
- function internalFlattenValue(valuePath, typePath, typeDef, v, setter, mapper2, r) {
230
+ function internalFlattenValue(valuePath, typePath, typeDef, v, setter, mapper2, r, listIndicesToKeys) {
230
231
  r[valuePath] = mapper2(typeDef, v, setter, typePath, valuePath);
231
232
  if (v !== void 0) {
232
- return internalFlattenValueChildren(valuePath, typePath, typeDef, v, mapper2, r);
233
+ return internalFlattenValueChildren(valuePath, typePath, typeDef, v, mapper2, r, listIndicesToKeys);
233
234
  }
234
235
  return r;
235
236
  }
236
- function internalFlattenValueChildren(valuePath, typePath, typeDef, v, mapper2, r) {
237
+ function internalFlattenValueChildren(valuePath, typePath, typeDef, v, mapper2, r, listIndicesToKeys) {
237
238
  switch (typeDef.type) {
238
239
  case 1 /* Literal */:
239
240
  return r;
240
241
  case 2 /* List */:
241
- return internalFlattenListChildren(valuePath, typePath, typeDef, v, mapper2, r);
242
+ return internalFlattenListChildren(valuePath, typePath, typeDef, v, mapper2, r, listIndicesToKeys);
242
243
  case 3 /* Record */:
243
- return internalFlattenRecordChildren(valuePath, typePath, typeDef, v, mapper2, r);
244
+ return internalFlattenRecordChildren(valuePath, typePath, typeDef, v, mapper2, r, listIndicesToKeys);
244
245
  case 4 /* Object */:
245
- return internalFlattenObjectChildren(valuePath, typePath, typeDef, v, mapper2, r);
246
+ return internalFlattenObjectChildren(valuePath, typePath, typeDef, v, mapper2, r, listIndicesToKeys);
246
247
  case 5 /* Union */:
247
- return internalFlattenUnionChildren(valuePath, typePath, typeDef, v, mapper2, r);
248
+ return internalFlattenUnionChildren(valuePath, typePath, typeDef, v, mapper2, r, listIndicesToKeys);
248
249
  default:
249
250
  throw new UnreachableError2(typeDef);
250
251
  }
251
252
  }
252
- function internalFlattenListChildren(valuePath, typePath, { elements }, v, mapper2, r) {
253
+ function internalFlattenListChildren(valuePath, typePath, { elements }, v, mapper2, r, listIndicesToKeys) {
254
+ let indicesToKeys = listIndicesToKeys[valuePath];
255
+ if (indicesToKeys == null) {
256
+ indicesToKeys = [0];
257
+ listIndicesToKeys[valuePath] = indicesToKeys;
258
+ }
253
259
  const newTypePath = jsonPath(typePath, "*");
254
- return v.reduce(function(r2, e, i) {
260
+ return v.reduce(function(r2, e, index) {
261
+ const key = indicesToKeys[index];
262
+ if (index === indicesToKeys.length - 1) {
263
+ indicesToKeys.push(key + 1);
264
+ }
255
265
  return internalFlattenValue(
256
- jsonPath(valuePath, i),
266
+ jsonPath(valuePath, key),
257
267
  newTypePath,
258
268
  elements,
259
269
  e,
260
270
  (e2) => {
261
- v[i] = e2;
271
+ v[index] = e2;
262
272
  },
263
273
  mapper2,
264
- r2
274
+ r2,
275
+ listIndicesToKeys
265
276
  );
266
277
  }, r);
267
278
  }
268
- function internalFlattenRecordChildren(valuePath, typePath, { valueTypeDef }, v, mapper2, r) {
279
+ function internalFlattenRecordChildren(valuePath, typePath, { valueTypeDef }, v, mapper2, r, listIndicesToKeys) {
269
280
  const newTypePath = jsonPath(typePath, "*");
270
281
  return reduce2(
271
282
  v,
@@ -279,13 +290,14 @@ function internalFlattenRecordChildren(valuePath, typePath, { valueTypeDef }, v,
279
290
  v[k] = value2;
280
291
  },
281
292
  mapper2,
282
- r2
293
+ r2,
294
+ listIndicesToKeys
283
295
  );
284
296
  },
285
297
  r
286
298
  );
287
299
  }
288
- function internalFlattenObjectChildren(valuePath, typePath, { fields }, v, mapper2, r) {
300
+ function internalFlattenObjectChildren(valuePath, typePath, { fields }, v, mapper2, r, listIndicesToKeys) {
289
301
  return reduce2(
290
302
  fields,
291
303
  function(r2, k, fieldTypeDef) {
@@ -299,13 +311,14 @@ function internalFlattenObjectChildren(valuePath, typePath, { fields }, v, mappe
299
311
  v[k] = value;
300
312
  },
301
313
  mapper2,
302
- r2
314
+ r2,
315
+ listIndicesToKeys
303
316
  );
304
317
  },
305
318
  r
306
319
  );
307
320
  }
308
- function internalFlattenUnionChildren(valuePath, typePath, typeDef, v, mapper2, r) {
321
+ function internalFlattenUnionChildren(valuePath, typePath, typeDef, v, mapper2, r, listIndicesToKeys) {
309
322
  const childTypeDef = getUnionTypeDef(typeDef, v);
310
323
  const qualifier = typeDef.discriminator != null ? `:${v[typeDef.discriminator]}` : "";
311
324
  return internalFlattenValueChildren(
@@ -314,7 +327,8 @@ function internalFlattenUnionChildren(valuePath, typePath, typeDef, v, mapper2,
314
327
  childTypeDef,
315
328
  v,
316
329
  mapper2,
317
- r
330
+ r,
331
+ listIndicesToKeys
318
332
  );
319
333
  }
320
334
  function getUnionTypeDef(typeDef, v) {
@@ -456,12 +470,13 @@ function mapAccessor(_t, value, set) {
456
470
  set
457
471
  };
458
472
  }
459
- function flattenAccessorsOfType(t, value, setValue) {
473
+ function flattenAccessorsOfType(t, value, setValue, listIndicesToKeys) {
460
474
  return flattenValueTo(
461
475
  t,
462
476
  value,
463
477
  setValue,
464
- mapAccessor
478
+ mapAccessor,
479
+ listIndicesToKeys
465
480
  );
466
481
  }
467
482
 
@@ -469,13 +484,14 @@ function flattenAccessorsOfType(t, value, setValue) {
469
484
  function mapTypePaths(_t, _value, _set, typePath) {
470
485
  return typePath;
471
486
  }
472
- function flattenJsonValueToTypePathsOf(t, value) {
487
+ function flattenJsonValueToTypePathsOf(t, value, listIndicesToKeys) {
473
488
  return flattenValueTo(
474
489
  t,
475
490
  value,
476
491
  function() {
477
492
  },
478
- mapTypePaths
493
+ mapTypePaths,
494
+ listIndicesToKeys
479
495
  );
480
496
  }
481
497
 
@@ -588,13 +604,14 @@ function flattenValidatorsOfValidatingType(type) {
588
604
  function mapper(_t, v) {
589
605
  return v;
590
606
  }
591
- function flattenValuesOfType(typeDef, value) {
607
+ function flattenValuesOfType(typeDef, value, listIndicesToKeys) {
592
608
  return flattenValueTo(
593
609
  typeDef,
594
610
  value,
595
611
  () => {
596
612
  },
597
- mapper
613
+ mapper,
614
+ listIndicesToKeys
598
615
  );
599
616
  }
600
617
 
package/package.json CHANGED
@@ -38,7 +38,7 @@
38
38
  "test:watch": "vitest"
39
39
  },
40
40
  "type": "module",
41
- "version": "0.0.27",
41
+ "version": "0.0.28",
42
42
  "exports": {
43
43
  ".": {
44
44
  "import": {
@@ -32,6 +32,7 @@ export function flattenAccessorsOfType<
32
32
  t: T,
33
33
  value: ValueOfType<T>,
34
34
  setValue: Setter<ValueOfType<T>>,
35
+ listIndicesToKeys?: Record<string, number[]>,
35
36
  ): R {
36
37
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
38
  return flattenValueTo<T, Accessor<any>, R>(
@@ -39,5 +40,6 @@ export function flattenAccessorsOfType<
39
40
  value,
40
41
  setValue,
41
42
  mapAccessor,
43
+ listIndicesToKeys,
42
44
  )
43
45
  }
@@ -26,6 +26,7 @@ export function flattenJsonValueToTypePathsOf<
26
26
  value: ValueOfType<T>,
27
27
  // TODO
28
28
  // : FlattenedJsonValueToTypePathsOf<T>
29
+ listIndicesToKeys?: Record<string, number[]>,
29
30
  ): R {
30
31
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
31
32
  return flattenValueTo(
@@ -35,5 +36,6 @@ export function flattenJsonValueToTypePathsOf<
35
36
  // do nothing
36
37
  },
37
38
  mapTypePaths,
39
+ listIndicesToKeys,
38
40
  ) as R
39
41
  }
@@ -72,6 +72,7 @@ export function flattenValidationErrorsOfType<
72
72
  type: T,
73
73
  value: ValueOfType<T>,
74
74
  validators: TypePathsToValidators,
75
+ listIndicesToKeys?: Record<string, number[]>,
75
76
  ): ErrorsOfFlattenedValidators<ValuePathsToValidators> {
76
77
  return flattenValueTo(
77
78
  type,
@@ -89,5 +90,6 @@ export function flattenValidationErrorsOfType<
89
90
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
90
91
  return validator != null ? validate(validator as Validator, v, valuePath, value) : null
91
92
  },
93
+ listIndicesToKeys,
92
94
  )
93
95
  }
@@ -40,6 +40,10 @@ export function flattenValueTo<
40
40
  v: ValueOfType<T>,
41
41
  setter: Setter<ValueOfType<T>>,
42
42
  mapper: Mapper<M>,
43
+ // used to maintain keys when changing lists, note that the format for a list of three elements is
44
+ // [key1, key2, key3, nextKey]
45
+ // the final value always contains the next key
46
+ listIndicesToKeys: Record<string, number[]> = {},
43
47
  ): R {
44
48
  const r: Record<string, AnyValueType> = {}
45
49
  internalFlattenValue(
@@ -50,6 +54,7 @@ export function flattenValueTo<
50
54
  setter,
51
55
  mapper,
52
56
  r,
57
+ listIndicesToKeys,
53
58
  )
54
59
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
55
60
  return r as R
@@ -63,12 +68,13 @@ function internalFlattenValue<M>(
63
68
  setter: Setter<AnyValueType>,
64
69
  mapper: Mapper<M>,
65
70
  r: Record<string, M>,
71
+ listIndicesToKeys: Record<string, number[]>,
66
72
  ) {
67
73
  r[valuePath] = mapper(typeDef, v, setter, typePath, valuePath)
68
74
  // assume undefined means the field is optional and not populated
69
75
  // TODO: actually capture if field is optional in typedef (or in builder for creating validator)
70
76
  if (v !== undefined) {
71
- return internalFlattenValueChildren(valuePath, typePath, typeDef, v, mapper, r)
77
+ return internalFlattenValueChildren(valuePath, typePath, typeDef, v, mapper, r, listIndicesToKeys)
72
78
  }
73
79
  return r
74
80
  }
@@ -80,19 +86,20 @@ function internalFlattenValueChildren<M>(
80
86
  v: AnyValueType,
81
87
  mapper: Mapper<M>,
82
88
  r: Record<string, M>,
89
+ listIndicesToKeys: Record<string, number[]>,
83
90
  ) {
84
91
  switch (typeDef.type) {
85
92
  case TypeDefType.Literal:
86
93
  // no children
87
94
  return r
88
95
  case TypeDefType.List:
89
- return internalFlattenListChildren(valuePath, typePath, typeDef, v, mapper, r)
96
+ return internalFlattenListChildren(valuePath, typePath, typeDef, v, mapper, r, listIndicesToKeys)
90
97
  case TypeDefType.Record:
91
- return internalFlattenRecordChildren(valuePath, typePath, typeDef, v, mapper, r)
98
+ return internalFlattenRecordChildren(valuePath, typePath, typeDef, v, mapper, r, listIndicesToKeys)
92
99
  case TypeDefType.Object:
93
- return internalFlattenObjectChildren(valuePath, typePath, typeDef, v, mapper, r)
100
+ return internalFlattenObjectChildren(valuePath, typePath, typeDef, v, mapper, r, listIndicesToKeys)
94
101
  case TypeDefType.Union:
95
- return internalFlattenUnionChildren(valuePath, typePath, typeDef, v, mapper, r)
102
+ return internalFlattenUnionChildren(valuePath, typePath, typeDef, v, mapper, r, listIndicesToKeys)
96
103
  default:
97
104
  throw new UnreachableError(typeDef)
98
105
  }
@@ -105,19 +112,33 @@ function internalFlattenListChildren<M>(
105
112
  v: AnyValueType[],
106
113
  mapper: Mapper<M>,
107
114
  r: Record<string, M>,
115
+ listIndicesToKeys: Record<string, number[]>,
108
116
  ) {
117
+ let indicesToKeys = listIndicesToKeys[valuePath]
118
+ if (indicesToKeys == null) {
119
+ indicesToKeys = [0]
120
+ listIndicesToKeys[valuePath] = indicesToKeys
121
+ }
122
+
109
123
  const newTypePath = jsonPath(typePath, '*')
110
- return v.reduce(function (r, e, i) {
124
+ return v.reduce(function (r, e, index) {
125
+ const key = indicesToKeys[index]
126
+ // we have consumed the next id passively
127
+ if (index === indicesToKeys.length - 1) {
128
+ // we have consumed the next key, so we need to add a new one
129
+ indicesToKeys.push(key + 1)
130
+ }
111
131
  return internalFlattenValue(
112
- jsonPath(valuePath, i),
132
+ jsonPath(valuePath, key),
113
133
  newTypePath,
114
134
  elements,
115
135
  e,
116
136
  (e: AnyValueType) => {
117
- v[i] = e
137
+ v[index] = e
118
138
  },
119
139
  mapper,
120
140
  r,
141
+ listIndicesToKeys,
121
142
  )
122
143
  }, r)
123
144
  }
@@ -129,6 +150,7 @@ function internalFlattenRecordChildren<M>(
129
150
  v: Record<string, AnyValueType>,
130
151
  mapper: Mapper<M>,
131
152
  r: Record<string, M>,
153
+ listIndicesToKeys: Record<string, number[]>,
132
154
  ): Record<string, M> {
133
155
  const newTypePath = jsonPath(typePath, '*')
134
156
  return reduce(
@@ -144,6 +166,7 @@ function internalFlattenRecordChildren<M>(
144
166
  },
145
167
  mapper,
146
168
  r,
169
+ listIndicesToKeys,
147
170
  )
148
171
  },
149
172
  r,
@@ -157,6 +180,7 @@ function internalFlattenObjectChildren<M>(
157
180
  v: Record<string, AnyValueType>,
158
181
  mapper: Mapper<M>,
159
182
  r: Record<string, M>,
183
+ listIndicesToKeys: Record<string, number[]>,
160
184
  ): Record<string, M> {
161
185
  return reduce(
162
186
  fields,
@@ -172,6 +196,7 @@ function internalFlattenObjectChildren<M>(
172
196
  },
173
197
  mapper,
174
198
  r,
199
+ listIndicesToKeys,
175
200
  )
176
201
  },
177
202
  r,
@@ -185,6 +210,7 @@ function internalFlattenUnionChildren<M>(
185
210
  v: AnyValueType,
186
211
  mapper: Mapper<M>,
187
212
  r: Record<string, M>,
213
+ listIndicesToKeys: Record<string, number[]>,
188
214
  ): AnyValueType {
189
215
  const childTypeDef = getUnionTypeDef(typeDef, v)
190
216
  const qualifier = typeDef.discriminator != null ? `:${v[typeDef.discriminator]}` : ''
@@ -195,6 +221,7 @@ function internalFlattenUnionChildren<M>(
195
221
  v,
196
222
  mapper,
197
223
  r,
224
+ listIndicesToKeys,
198
225
  )
199
226
  }
200
227
 
@@ -13,6 +13,7 @@ function mapper(_t: StrictTypeDef, v: AnyValueType) {
13
13
  export function flattenValuesOfType<T extends Type>(
14
14
  typeDef: Type,
15
15
  value: ValueOfType<T>,
16
+ listIndicesToKeys?: Record<string, number[]>,
16
17
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
18
  ): Record<string, any> {
18
19
  return flattenValueTo(
@@ -20,5 +21,6 @@ export function flattenValuesOfType<T extends Type>(
20
21
  value,
21
22
  () => {},
22
23
  mapper,
24
+ listIndicesToKeys,
23
25
  )
24
26
  }