inferred-types 0.28.3 → 0.28.5

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,13 @@ 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; if you specify
836
+ * a _string_ for a value that will be sent to stderr along
837
+ * with other debugging info.
838
+ */
839
+ debug?: boolean | string;
833
840
  }
834
841
  /**
835
842
  * A finalized configuration of a **mapTo** mapper functions cardinality
@@ -838,16 +845,25 @@ interface MapConfig<IR extends OptRequired | undefined = undefined, D extends Ma
838
845
  * Note: _this configuration does _not_ yet include the actual mapping
839
846
  * configuration between the input and output._
840
847
  */
841
- declare type FinalizedMapConfig<IR extends OptRequired = MapIR<DefaultOneToManyMapping>, D extends MapCardinalityIllustrated = MapCard<DefaultOneToManyMapping>, OR extends OptRequired = MapOR<DefaultOneToManyMapping>> = Required<MapConfig<IR, D, OR>> & {
848
+ declare type FinalizedMapConfig<IR extends OptRequired = MapIR<DefaultOneToManyMapping>, D extends MapCardinalityIllustrated = MapCard<DefaultOneToManyMapping>, OR extends OptRequired = MapOR<DefaultOneToManyMapping>> = Required<Omit<MapConfig<IR, D, OR>, "debug">> & {
842
849
  finalized: true;
850
+ debug: boolean | string;
843
851
  };
844
852
  /**
845
853
  * User configuration exposed by a config function which specifies the
846
854
  * cardinality already (e.g., `oneToMany()`, `manyToOne()`)
847
855
  */
848
856
  declare type MapCardinalityConfig<IR extends OptRequired | undefined, OR extends OptRequired | undefined> = {
857
+ /** whether we the input can _optionally_ be an `undefined` value or not */
849
858
  input?: IR;
859
+ /** whether we the output can _optionally_ be an `undefined` value or not */
850
860
  output?: OR;
861
+ /**
862
+ * Whether calls to the final `MapFn` will be logged to stderr
863
+ * for debugging purposes. Defaults to false; if you set to a string
864
+ * value than this will be echoed out with stderr.
865
+ */
866
+ debug?: boolean | string;
851
867
  };
852
868
  /**
853
869
  * **ConfiguredMap**
@@ -861,6 +877,7 @@ declare type ConfiguredMap<C extends FinalizedMapConfig<OptRequired, MapCardinal
861
877
  input: MapIR<C>;
862
878
  cardinality: MapCard<C>;
863
879
  output: MapOR<C>;
880
+ debug: boolean | string;
864
881
  };
865
882
  /**
866
883
  * Extracts the IR, Cardinality, and OR generics from a FinalizedMapConfig
package/dist/index.js CHANGED
@@ -611,13 +611,32 @@ var DEFAULT_MANY_TO_ONE_MAPPING = toFinalizedConfig({
611
611
  output: "req",
612
612
  cardinality: "I[] -> O"
613
613
  });
614
+ var debugMsg = (config, source, output) => {
615
+ if (config.debug) {
616
+ console.error(
617
+ `MapFn[${typeof config.output === "string" ? `${config.output}, ` : ""}${config.input}, ${config.cardinality}, ${config.output}] received:
618
+
619
+ ${JSON.stringify(source)}
620
+
621
+ And produced: ${JSON.stringify(
622
+ output
623
+ )}
624
+
625
+ `
626
+ );
627
+ }
628
+ };
614
629
  var mapper = (config) => (map) => {
615
630
  return (source) => {
616
631
  const isArray2 = config.cardinality === "I -> O[]" && Array.isArray(source) ? true : config.cardinality === "I[] -> O" && Array.isArray(source) && Array.isArray(source[0]) ? true : false;
617
632
  if (isArray2) {
618
- return source.flatMap(map);
633
+ const output = source.flatMap(map);
634
+ debugMsg(config, source, output);
635
+ return output;
619
636
  } else {
620
- return map(source);
637
+ const output = map(source);
638
+ debugMsg(config, source, output);
639
+ return output;
621
640
  }
622
641
  };
623
642
  };
@@ -631,7 +650,8 @@ var setMapper = (config = { input: void 0, output: void 0, cardinality: void 0 }
631
650
  },
632
651
  input: (config == null ? void 0 : config.input) || defaultValue.input,
633
652
  output: (config == null ? void 0 : config.output) || defaultValue.output,
634
- cardinality: (config == null ? void 0 : config.cardinality) || defaultValue.cardinality
653
+ cardinality: (config == null ? void 0 : config.cardinality) || defaultValue.cardinality,
654
+ debug: (config == null ? void 0 : config.debug) || false
635
655
  });
636
656
  var mapToFn = (map) => {
637
657
  return mapper(DEFAULT_ONE_TO_MANY_MAPPING)(map);
package/dist/index.mjs CHANGED
@@ -520,13 +520,32 @@ var DEFAULT_MANY_TO_ONE_MAPPING = toFinalizedConfig({
520
520
  output: "req",
521
521
  cardinality: "I[] -> O"
522
522
  });
523
+ var debugMsg = (config, source, output) => {
524
+ if (config.debug) {
525
+ console.error(
526
+ `MapFn[${typeof config.output === "string" ? `${config.output}, ` : ""}${config.input}, ${config.cardinality}, ${config.output}] received:
527
+
528
+ ${JSON.stringify(source)}
529
+
530
+ And produced: ${JSON.stringify(
531
+ output
532
+ )}
533
+
534
+ `
535
+ );
536
+ }
537
+ };
523
538
  var mapper = (config) => (map) => {
524
539
  return (source) => {
525
540
  const isArray2 = config.cardinality === "I -> O[]" && Array.isArray(source) ? true : config.cardinality === "I[] -> O" && Array.isArray(source) && Array.isArray(source[0]) ? true : false;
526
541
  if (isArray2) {
527
- return source.flatMap(map);
542
+ const output = source.flatMap(map);
543
+ debugMsg(config, source, output);
544
+ return output;
528
545
  } else {
529
- return map(source);
546
+ const output = map(source);
547
+ debugMsg(config, source, output);
548
+ return output;
530
549
  }
531
550
  };
532
551
  };
@@ -540,7 +559,8 @@ var setMapper = (config = { input: void 0, output: void 0, cardinality: void 0 }
540
559
  },
541
560
  input: (config == null ? void 0 : config.input) || defaultValue.input,
542
561
  output: (config == null ? void 0 : config.output) || defaultValue.output,
543
- cardinality: (config == null ? void 0 : config.cardinality) || defaultValue.cardinality
562
+ cardinality: (config == null ? void 0 : config.cardinality) || defaultValue.cardinality,
563
+ debug: (config == null ? void 0 : config.debug) || false
544
564
  });
545
565
  var mapToFn = (map) => {
546
566
  return mapper(DEFAULT_ONE_TO_MANY_MAPPING)(map);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inferred-types",
3
- "version": "0.28.3",
3
+ "version": "0.28.5",
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,13 @@ 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; if you specify
41
+ * a _string_ for a value that will be sent to stderr along
42
+ * with other debugging info.
43
+ */
44
+ debug?: boolean | string;
38
45
  }
39
46
 
40
47
  /**
@@ -48,7 +55,7 @@ export type FinalizedMapConfig<
48
55
  IR extends OptRequired = MapIR<DefaultOneToManyMapping>,
49
56
  D extends MapCardinalityIllustrated = MapCard<DefaultOneToManyMapping>,
50
57
  OR extends OptRequired = MapOR<DefaultOneToManyMapping>
51
- > = Required<MapConfig<IR, D, OR>> & { finalized: true };
58
+ > = Required<Omit<MapConfig<IR, D, OR>, "debug">> & { finalized: true; debug: boolean | string };
52
59
 
53
60
  /**
54
61
  * User configuration exposed by a config function which specifies the
@@ -58,8 +65,16 @@ type MapCardinalityConfig<
58
65
  IR extends OptRequired | undefined,
59
66
  OR extends OptRequired | undefined
60
67
  > = {
68
+ /** whether we the input can _optionally_ be an `undefined` value or not */
61
69
  input?: IR;
70
+ /** whether we the output can _optionally_ be an `undefined` value or not */
62
71
  output?: OR;
72
+ /**
73
+ * Whether calls to the final `MapFn` will be logged to stderr
74
+ * for debugging purposes. Defaults to false; if you set to a string
75
+ * value than this will be echoed out with stderr.
76
+ */
77
+ debug?: boolean | string;
63
78
  };
64
79
 
65
80
  /**
@@ -76,6 +91,7 @@ export type ConfiguredMap<
76
91
  input: MapIR<C>;
77
92
  cardinality: MapCard<C>;
78
93
  output: MapOR<C>;
94
+ debug: boolean | string;
79
95
  };
80
96
 
81
97
  /**
@@ -49,6 +49,22 @@ export type DefaultOneToManyMapping = typeof DEFAULT_ONE_TO_MANY_MAPPING;
49
49
  export type DefaultOneToOneMapping = typeof DEFAULT_ONE_TO_ONE_MAPPING;
50
50
  export type DefaultManyToOneMapping = typeof DEFAULT_MANY_TO_ONE_MAPPING;
51
51
 
52
+ const debugMsg = <C extends FinalizedMapConfig<any, any, any>>(
53
+ config: C,
54
+ source: any,
55
+ output: any
56
+ ) => {
57
+ if (config.debug) {
58
+ console.error(
59
+ `MapFn[${typeof config.output === "string" ? `${config.output}, ` : ""}${config.input}, ${
60
+ config.cardinality
61
+ }, ${config.output}] received:\n\n${JSON.stringify(source)}\n\nAnd produced: ${JSON.stringify(
62
+ output
63
+ )}\n\n`
64
+ );
65
+ }
66
+ };
67
+
52
68
  /**
53
69
  * The single implementation for all mapping
54
70
  */
@@ -77,11 +93,16 @@ const mapper =
77
93
  // item and this approach achieves this.
78
94
  // TODO: we should check that the approach below doesn't work for M:1 here
79
95
  // as well
80
- return (source as any).flatMap(map) as MapFnOutput<I, O, S, MapOR<C>, MapCard<C>>;
96
+ const output = (source as any).flatMap(map) as MapFnOutput<I, O, S, MapOR<C>, MapCard<C>>;
97
+ debugMsg(config, source, output);
98
+
99
+ return output;
81
100
  } else {
82
101
  // receive _all_ inputs provided as pass into ; this is just a single input unless the
83
102
  // cardinality is
84
- return map(source as any) as MapFnOutput<I, O, S, MapOR<C>, MapCard<C>>;
103
+ const output = map(source as any) as MapFnOutput<I, O, S, MapOR<C>, MapCard<C>>;
104
+ debugMsg(config, source, output);
105
+ return output;
85
106
  }
86
107
  };
87
108
  };
@@ -112,6 +133,7 @@ const setMapper = <
112
133
  input: config?.input || defaultValue.input,
113
134
  output: config?.output || defaultValue.output,
114
135
  cardinality: config?.cardinality || defaultValue.cardinality,
136
+ debug: config?.debug || false,
115
137
  });
116
138
 
117
139
  /**
@@ -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 {