inferred-types 0.28.2 → 0.28.4

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/dist/index.d.ts CHANGED
@@ -830,6 +830,11 @@ interface MapConfig<IR extends OptRequired | undefined = undefined, D extends Ma
830
830
  input?: IR;
831
831
  output?: OR;
832
832
  cardinality?: D;
833
+ /**
834
+ * Whether calls to the final `MapFn` will be logged to stderr
835
+ * for debugging purposes. Defaults to false.
836
+ */
837
+ debug?: boolean;
833
838
  }
834
839
  /**
835
840
  * A finalized configuration of a **mapTo** mapper functions cardinality
@@ -838,7 +843,7 @@ interface MapConfig<IR extends OptRequired | undefined = undefined, D extends Ma
838
843
  * Note: _this configuration does _not_ yet include the actual mapping
839
844
  * configuration between the input and output._
840
845
  */
841
- declare type FinalizedMapConfig<IR extends OptRequired, D extends MapCardinalityIllustrated, OR extends OptRequired> = Required<MapConfig<IR, D, OR>> & {
846
+ declare type FinalizedMapConfig<IR extends OptRequired = MapIR<DefaultOneToManyMapping>, D extends MapCardinalityIllustrated = MapCard<DefaultOneToManyMapping>, OR extends OptRequired = MapOR<DefaultOneToManyMapping>> = Required<MapConfig<IR, D, OR>> & {
842
847
  finalized: true;
843
848
  };
844
849
  /**
@@ -846,8 +851,15 @@ declare type FinalizedMapConfig<IR extends OptRequired, D extends MapCardinality
846
851
  * cardinality already (e.g., `oneToMany()`, `manyToOne()`)
847
852
  */
848
853
  declare type MapCardinalityConfig<IR extends OptRequired | undefined, OR extends OptRequired | undefined> = {
854
+ /** whether we the input can _optionally_ be an `undefined` value or not */
849
855
  input?: IR;
856
+ /** whether we the output can _optionally_ be an `undefined` value or not */
850
857
  output?: OR;
858
+ /**
859
+ * Whether calls to the final `MapFn` will be logged to stderr
860
+ * for debugging purposes. Defaults to false.
861
+ */
862
+ debug?: boolean;
851
863
  };
852
864
  /**
853
865
  * **ConfiguredMap**
package/dist/index.js CHANGED
@@ -615,9 +615,33 @@ var mapper = (config) => (map) => {
615
615
  return (source) => {
616
616
  const isArray2 = config.cardinality === "I -> O[]" && Array.isArray(source) ? true : config.cardinality === "I[] -> O" && Array.isArray(source) && Array.isArray(source[0]) ? true : false;
617
617
  if (isArray2) {
618
- return source.flatMap(map);
618
+ const output = source.flatMap(map);
619
+ if (config.debug) {
620
+ console.error(
621
+ `MapFn[${config.input}, ${config.cardinality}, ${config.output}] received:
622
+
623
+ ${JSON.stringify(source)}
624
+
625
+ And produced: ${JSON.stringify(
626
+ output
627
+ )}
628
+
629
+ `
630
+ );
631
+ }
632
+ return output;
619
633
  } else {
620
- return map(source);
634
+ const output = map(source);
635
+ if (config.debug) {
636
+ console.error(
637
+ `MapFn[${config.input}, ${config.cardinality}, ${config.output}] received:
638
+
639
+ ${JSON.stringify(source)}
640
+ And produced: ${JSON.stringify(output)}
641
+ `
642
+ );
643
+ }
644
+ return output;
621
645
  }
622
646
  };
623
647
  };
package/dist/index.mjs CHANGED
@@ -524,9 +524,33 @@ var mapper = (config) => (map) => {
524
524
  return (source) => {
525
525
  const isArray2 = config.cardinality === "I -> O[]" && Array.isArray(source) ? true : config.cardinality === "I[] -> O" && Array.isArray(source) && Array.isArray(source[0]) ? true : false;
526
526
  if (isArray2) {
527
- return source.flatMap(map);
527
+ const output = source.flatMap(map);
528
+ if (config.debug) {
529
+ console.error(
530
+ `MapFn[${config.input}, ${config.cardinality}, ${config.output}] received:
531
+
532
+ ${JSON.stringify(source)}
533
+
534
+ And produced: ${JSON.stringify(
535
+ output
536
+ )}
537
+
538
+ `
539
+ );
540
+ }
541
+ return output;
528
542
  } else {
529
- return map(source);
543
+ const output = map(source);
544
+ if (config.debug) {
545
+ console.error(
546
+ `MapFn[${config.input}, ${config.cardinality}, ${config.output}] received:
547
+
548
+ ${JSON.stringify(source)}
549
+ And produced: ${JSON.stringify(output)}
550
+ `
551
+ );
552
+ }
553
+ return output;
530
554
  }
531
555
  };
532
556
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inferred-types",
3
- "version": "0.28.2",
3
+ "version": "0.28.4",
4
4
  "description": "Functions which provide useful type inference on TS projects",
5
5
  "license": "MIT",
6
6
  "author": "Ken Snyder<ken@ken.net>",
@@ -35,6 +35,11 @@ export interface MapConfig<
35
35
  input?: IR;
36
36
  output?: OR;
37
37
  cardinality?: D;
38
+ /**
39
+ * Whether calls to the final `MapFn` will be logged to stderr
40
+ * for debugging purposes. Defaults to false.
41
+ */
42
+ debug?: boolean;
38
43
  }
39
44
 
40
45
  /**
@@ -45,9 +50,9 @@ export interface MapConfig<
45
50
  * configuration between the input and output._
46
51
  */
47
52
  export type FinalizedMapConfig<
48
- IR extends OptRequired,
49
- D extends MapCardinalityIllustrated,
50
- OR extends OptRequired
53
+ IR extends OptRequired = MapIR<DefaultOneToManyMapping>,
54
+ D extends MapCardinalityIllustrated = MapCard<DefaultOneToManyMapping>,
55
+ OR extends OptRequired = MapOR<DefaultOneToManyMapping>
51
56
  > = Required<MapConfig<IR, D, OR>> & { finalized: true };
52
57
 
53
58
  /**
@@ -58,8 +63,15 @@ type MapCardinalityConfig<
58
63
  IR extends OptRequired | undefined,
59
64
  OR extends OptRequired | undefined
60
65
  > = {
66
+ /** whether we the input can _optionally_ be an `undefined` value or not */
61
67
  input?: IR;
68
+ /** whether we the output can _optionally_ be an `undefined` value or not */
62
69
  output?: OR;
70
+ /**
71
+ * Whether calls to the final `MapFn` will be logged to stderr
72
+ * for debugging purposes. Defaults to false.
73
+ */
74
+ debug?: boolean;
63
75
  };
64
76
 
65
77
  /**
@@ -77,11 +77,30 @@ const mapper =
77
77
  // item and this approach achieves this.
78
78
  // TODO: we should check that the approach below doesn't work for M:1 here
79
79
  // as well
80
- return (source as any).flatMap(map) as MapFnOutput<I, O, S, MapOR<C>, MapCard<C>>;
80
+ const output = (source as any).flatMap(map) as MapFnOutput<I, O, S, MapOR<C>, MapCard<C>>;
81
+ if (config.debug) {
82
+ console.error(
83
+ `MapFn[${config.input}, ${config.cardinality}, ${
84
+ config.output
85
+ }] received:\n\n${JSON.stringify(source)}\n\nAnd produced: ${JSON.stringify(
86
+ output
87
+ )}\n\n`
88
+ );
89
+ }
90
+
91
+ return output;
81
92
  } else {
82
93
  // receive _all_ inputs provided as pass into ; this is just a single input unless the
83
94
  // cardinality is
84
- return map(source as any) as MapFnOutput<I, O, S, MapOR<C>, MapCard<C>>;
95
+ const output = map(source as any) as MapFnOutput<I, O, S, MapOR<C>, MapCard<C>>;
96
+ if (config.debug) {
97
+ console.error(
98
+ `MapFn[${config.input}, ${config.cardinality}, ${
99
+ config.output
100
+ }] received:\n\n${JSON.stringify(source)}\nAnd produced: ${JSON.stringify(output)}\n`
101
+ );
102
+ }
103
+ return output;
85
104
  }
86
105
  };
87
106
  };
@@ -319,6 +319,26 @@ describe("mapTo() utility function", () => {
319
319
  o.map((item) => expect("title" in item).toBeTruthy());
320
320
  });
321
321
 
322
+ it("1:M conversion with filtering", () => {
323
+ const m = mapTo<I, O>((i) =>
324
+ i.title === "i2" ? [{ title: i.title, count: i.products.length }] : []
325
+ );
326
+ const o = m([i, i2]);
327
+
328
+ expect(o.length).toBe(1);
329
+ o.map((item) => expect(item.title).toBe("i2"));
330
+ });
331
+
332
+ it("1:M conversion with filtering and debugging", () => {
333
+ const m = mapTo
334
+ .config({ debug: true })
335
+ .map<I, O>((i) => (i.title === "i2" ? [{ title: i.title, count: i.products.length }] : []));
336
+ const o = m([i, i2]);
337
+
338
+ expect(o.length).toBe(1);
339
+ o.map((item) => expect(item.title).toBe("i2"));
340
+ });
341
+
322
342
  it("M:1 conversion", () => {
323
343
  const m = mapTo.config({ output: "req", cardinality: "I[] -> O" }).map<I, O>((i) => {
324
344
  return {