struct-fakerator 2.0.1 → 2.1.2

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.mts CHANGED
@@ -76,12 +76,12 @@ declare const createTupleConfig: CreateTupleConfig;
76
76
  declare const createBoundedSeriesConfig: (config: Omit<BoundedSeriesConfig, "type">) => BoundedSeriesConfig;
77
77
 
78
78
  type AllConfig<T> = ValueConfig<T> | SelectionConfig<T> | ArrayConfig<T> | ObjectConfig<T> | TupleConfig<T> | TupleConfig<T, T> | TupleConfig<T, T, T> | TupleConfig<T, T, T, T> | TupleConfig<T, T, T, T, T> | BoundedSeriesConfig;
79
- declare const createValueGenerator: <R = unknown>(config: any) => (() => R);
79
+ declare const createValueGenerator: <R = unknown>(config: ValueConfig<unknown>) => (() => R);
80
80
  declare const createSelectionGenerator: <T extends SelectionConfig<unknown>>(config: T) => (() => Result<T>);
81
- declare const createObjectGenerator: <T extends ObjectConfig<unknown>>(config: T, customTypeMatch?: (config: unknown) => ValueConfig<unknown>) => (() => Result<T>);
82
- declare const createArrayGenerator: <T extends ArrayConfig<unknown>>(config: T, customTypeMatch?: (config: unknown) => ValueConfig<unknown>) => (() => Result<T>);
83
- declare const createTupleGenerator: <T extends TupleConfig<unknown, unknown, unknown, unknown, unknown> | TupleConfig<unknown, unknown, unknown, unknown> | TupleConfig<unknown, unknown, unknown> | TupleConfig<unknown, unknown> | TupleConfig<unknown>>(config: T, customTypeMatch?: (config: unknown) => ValueConfig<unknown>) => (() => Result<T>);
81
+ declare const createObjectGenerator: <T extends ObjectConfig<unknown>>(config: T, customTypeMatch?: (config: unknown, path?: string) => ValueConfig<unknown>) => (() => Result<T>);
82
+ declare const createArrayGenerator: <T extends ArrayConfig<unknown>>(config: T, customTypeMatch?: (config: unknown, path?: string) => ValueConfig<unknown>) => (() => Result<T>);
83
+ declare const createTupleGenerator: <T extends TupleConfig<unknown, unknown, unknown, unknown, unknown> | TupleConfig<unknown, unknown, unknown, unknown> | TupleConfig<unknown, unknown, unknown> | TupleConfig<unknown, unknown> | TupleConfig<unknown>>(config: T, customTypeMatch?: (config: unknown, path?: string) => ValueConfig<unknown>) => (() => Result<T>);
84
84
  declare const createBoundedSeriesGenerator: <T extends BoundedSeriesConfig>(config: T) => (() => Result<T>);
85
- declare const createGeneratorByType: <T extends AllConfig<unknown>>(config: T, customTypeMatch?: (config: unknown) => ValueConfig<unknown>) => (() => Result<T>);
85
+ declare const createGeneratorByType: <T extends AllConfig<unknown>>(config: T, customTypeMatch?: (config: unknown, path?: string) => ValueConfig<unknown>) => (() => Result<T>);
86
86
 
87
87
  export { type ArrayConfig, type BoundedSeriesConfig, type ObjectConfig, type Result, type SelectionConfig, type TupleConfig, type ValueConfig, createArrayConfig, createArrayGenerator, createBoundedSeriesConfig, createBoundedSeriesGenerator, createGeneratorByType, createObjectConfig, createObjectGenerator, createSelectionConfig, createSelectionGenerator, createTupleConfig, createTupleGenerator, createValueConfig, createValueGenerator };
package/dist/index.d.ts CHANGED
@@ -76,12 +76,12 @@ declare const createTupleConfig: CreateTupleConfig;
76
76
  declare const createBoundedSeriesConfig: (config: Omit<BoundedSeriesConfig, "type">) => BoundedSeriesConfig;
77
77
 
78
78
  type AllConfig<T> = ValueConfig<T> | SelectionConfig<T> | ArrayConfig<T> | ObjectConfig<T> | TupleConfig<T> | TupleConfig<T, T> | TupleConfig<T, T, T> | TupleConfig<T, T, T, T> | TupleConfig<T, T, T, T, T> | BoundedSeriesConfig;
79
- declare const createValueGenerator: <R = unknown>(config: any) => (() => R);
79
+ declare const createValueGenerator: <R = unknown>(config: ValueConfig<unknown>) => (() => R);
80
80
  declare const createSelectionGenerator: <T extends SelectionConfig<unknown>>(config: T) => (() => Result<T>);
81
- declare const createObjectGenerator: <T extends ObjectConfig<unknown>>(config: T, customTypeMatch?: (config: unknown) => ValueConfig<unknown>) => (() => Result<T>);
82
- declare const createArrayGenerator: <T extends ArrayConfig<unknown>>(config: T, customTypeMatch?: (config: unknown) => ValueConfig<unknown>) => (() => Result<T>);
83
- declare const createTupleGenerator: <T extends TupleConfig<unknown, unknown, unknown, unknown, unknown> | TupleConfig<unknown, unknown, unknown, unknown> | TupleConfig<unknown, unknown, unknown> | TupleConfig<unknown, unknown> | TupleConfig<unknown>>(config: T, customTypeMatch?: (config: unknown) => ValueConfig<unknown>) => (() => Result<T>);
81
+ declare const createObjectGenerator: <T extends ObjectConfig<unknown>>(config: T, customTypeMatch?: (config: unknown, path?: string) => ValueConfig<unknown>) => (() => Result<T>);
82
+ declare const createArrayGenerator: <T extends ArrayConfig<unknown>>(config: T, customTypeMatch?: (config: unknown, path?: string) => ValueConfig<unknown>) => (() => Result<T>);
83
+ declare const createTupleGenerator: <T extends TupleConfig<unknown, unknown, unknown, unknown, unknown> | TupleConfig<unknown, unknown, unknown, unknown> | TupleConfig<unknown, unknown, unknown> | TupleConfig<unknown, unknown> | TupleConfig<unknown>>(config: T, customTypeMatch?: (config: unknown, path?: string) => ValueConfig<unknown>) => (() => Result<T>);
84
84
  declare const createBoundedSeriesGenerator: <T extends BoundedSeriesConfig>(config: T) => (() => Result<T>);
85
- declare const createGeneratorByType: <T extends AllConfig<unknown>>(config: T, customTypeMatch?: (config: unknown) => ValueConfig<unknown>) => (() => Result<T>);
85
+ declare const createGeneratorByType: <T extends AllConfig<unknown>>(config: T, customTypeMatch?: (config: unknown, path?: string) => ValueConfig<unknown>) => (() => Result<T>);
86
86
 
87
87
  export { type ArrayConfig, type BoundedSeriesConfig, type ObjectConfig, type Result, type SelectionConfig, type TupleConfig, type ValueConfig, createArrayConfig, createArrayGenerator, createBoundedSeriesConfig, createBoundedSeriesGenerator, createGeneratorByType, createObjectConfig, createObjectGenerator, createSelectionConfig, createSelectionGenerator, createTupleConfig, createTupleGenerator, createValueConfig, createValueGenerator };
package/dist/index.js CHANGED
@@ -73,40 +73,40 @@ var boundedSeriesScheme = import_zod.z.object({
73
73
 
74
74
  // src/create_config.ts
75
75
  var createValueConfig = (generateFn) => {
76
- const config2 = {
76
+ const config = {
77
77
  type: "value",
78
78
  generateFn
79
79
  };
80
- valueConfigScheme.parse(config2);
81
- return config2;
80
+ valueConfigScheme.parse(config);
81
+ return config;
82
82
  };
83
83
  var createSelectionConfig = (items) => {
84
- const config2 = { type: "select", items };
85
- selectionConfigScheme.parse(config2);
86
- return config2;
84
+ const config = { type: "select", items };
85
+ selectionConfigScheme.parse(config);
86
+ return config;
87
87
  };
88
88
  var createObjectConfig = (content) => {
89
- const config2 = { type: "obj", content };
90
- objConfigScheme.parse(config2);
91
- return config2;
89
+ const config = { type: "obj", content };
90
+ objConfigScheme.parse(config);
91
+ return config;
92
92
  };
93
93
  var createArrayConfig = (item, len) => {
94
- const config2 = { type: "arr", item, len };
95
- arrayConfigScheme.parse(config2);
96
- return config2;
94
+ const config = { type: "arr", item, len };
95
+ arrayConfigScheme.parse(config);
96
+ return config;
97
97
  };
98
98
  var createTupleConfig = (configItems) => {
99
- const config2 = {
99
+ const config = {
100
100
  type: "tuple",
101
101
  configItems
102
102
  };
103
- tupleConfigScheme.parse(config2);
104
- return config2;
103
+ tupleConfigScheme.parse(config);
104
+ return config;
105
105
  };
106
- var createBoundedSeriesConfig = (config2) => {
106
+ var createBoundedSeriesConfig = (config) => {
107
107
  const newConfig = {
108
108
  type: "bounded_series",
109
- ...config2
109
+ ...config
110
110
  };
111
111
  boundedSeriesScheme.parse(newConfig);
112
112
  return newConfig;
@@ -5723,20 +5723,37 @@ var r38 = {};
5723
5723
  Xr(r38, { af_ZA: () => B58, ar: () => Y35, az: () => U39, base: () => Mi, cs_CZ: () => x61, da: () => le10, de: () => pr, de_AT: () => I40, de_CH: () => J42, dv: () => Q32, el: () => le9, en: () => ul, en_AU: () => be7, en_AU_ocker: () => P54, en_BORK: () => u60, en_CA: () => S41, en_GB: () => B46, en_GH: () => J39, en_HK: () => w42, en_IE: () => D49, en_IN: () => w43, en_NG: () => O41, en_US: () => A49, en_ZA: () => L39, eo: () => ro, es: () => ao3, es_MX: () => ia7, fa: () => wo, fi: () => A43, fr: () => Wi, fr_BE: () => I36, fr_CA: () => b32, fr_CH: () => _32, fr_LU: () => _33, fr_SN: () => _34, he: () => Z27, hr: () => C33, hu: () => U29, hy: () => j34, id_ID: () => F18, it: () => w21, ja: () => M24, ka_GE: () => H23, ko: () => G23, lv: () => $13, mk: () => S26, nb_NO: () => Z24, ne: () => j13, nl: () => an, nl_BE: () => j15, pl: () => ea3, pt_BR: () => W15, pt_PT: () => k17, ro: () => Mi2, ro_MD: () => j20, ru: () => ae4, sk: () => O8, sr_RS_latin: () => T7, sv: () => $5, th: () => E8, tr: () => V10, uk: () => S11, ur: () => yt, vi: () => U3, yo_NG: () => r3, zh_CN: () => Ke3, zh_TW: () => B5, zu_ZA: () => x5 });
5724
5724
 
5725
5725
  // src/create_generator_fn.ts
5726
- var createValueGenerator = (config2) => {
5727
- valueConfigScheme.parse(config2);
5728
- return config2.generateFn;
5726
+ var _createValueGenerator = (config, path) => {
5727
+ try {
5728
+ valueConfigScheme.parse(config);
5729
+ } catch (err) {
5730
+ throw new Error(`config path: ${path}.value
5731
+ ${err}`);
5732
+ }
5733
+ return config.generateFn;
5729
5734
  };
5730
- var createSelectionGenerator = (config2) => {
5731
- selectionConfigScheme.parse(config2);
5732
- const { items } = config2;
5735
+ var createValueGenerator = (config) => _createValueGenerator(config, "*");
5736
+ var _createSelectionGenerator = (config, path) => {
5737
+ try {
5738
+ selectionConfigScheme.parse(config);
5739
+ } catch (err) {
5740
+ throw new Error(`config path: ${path}.select
5741
+ ${err}`);
5742
+ }
5743
+ const { items } = config;
5733
5744
  return () => items[f55.number.int(items.length - 1)];
5734
5745
  };
5735
- var createObjectGenerator = (config2, customTypeMatch) => {
5736
- objConfigScheme.parse(config2);
5737
- const keyWithFns = Object.entries(config2.content).map(([key, subConfig]) => [
5746
+ var createSelectionGenerator = (config) => _createSelectionGenerator(config, "*");
5747
+ var _createObjectGenerator = (config, path, customTypeMatch) => {
5748
+ try {
5749
+ objConfigScheme.parse(config);
5750
+ } catch (err) {
5751
+ throw new Error(`config path: ${path}.obj
5752
+ ${err}`);
5753
+ }
5754
+ const keyWithFns = Object.entries(config.content).map(([key, subConfig]) => [
5738
5755
  key,
5739
- createGeneratorByType(subConfig, customTypeMatch)
5756
+ _createGeneratorByType(subConfig, `${path}.obj[${key}]`, customTypeMatch)
5740
5757
  ]);
5741
5758
  return () => {
5742
5759
  const result = {};
@@ -5746,24 +5763,47 @@ var createObjectGenerator = (config2, customTypeMatch) => {
5746
5763
  return result;
5747
5764
  };
5748
5765
  };
5749
- var createArrayGenerator = (config2, customTypeMatch) => {
5750
- arrayConfigScheme.parse(config2);
5751
- const itemGeneratorFn = createGeneratorByType(
5752
- config2.item,
5766
+ var createObjectGenerator = (config, customTypeMatch) => _createObjectGenerator(config, "*", customTypeMatch);
5767
+ var _createArrayGenerator = (config, path, customTypeMatch) => {
5768
+ try {
5769
+ arrayConfigScheme.parse(config);
5770
+ } catch (err) {
5771
+ throw new Error(`config path: ${path}.arr
5772
+ ${err}`);
5773
+ }
5774
+ const itemGeneratorFn = _createGeneratorByType(
5775
+ config.item,
5776
+ `${path}.arr`,
5753
5777
  customTypeMatch
5754
5778
  );
5755
- return () => Array.from({ length: config2.len ?? 0 }, itemGeneratorFn);
5779
+ return () => Array.from({ length: config.len ?? 0 }, itemGeneratorFn);
5756
5780
  };
5757
- var createTupleGenerator = (config2, customTypeMatch) => {
5758
- tupleConfigScheme.parse(config2);
5759
- const itemsFns = config2.configItems.map(
5760
- (configItem) => createGeneratorByType(configItem, customTypeMatch)
5781
+ var createArrayGenerator = (config, customTypeMatch) => _createArrayGenerator(config, "*", customTypeMatch);
5782
+ var _createTupleGenerator = (config, path, customTypeMatch) => {
5783
+ try {
5784
+ tupleConfigScheme.parse(config);
5785
+ } catch (err) {
5786
+ throw new Error(`config path: ${path}.tuple
5787
+ ${err}`);
5788
+ }
5789
+ const itemsFns = config.configItems.map(
5790
+ (configItem, index) => _createGeneratorByType(
5791
+ configItem,
5792
+ `${path}.tuple[${index}]`,
5793
+ customTypeMatch
5794
+ )
5761
5795
  );
5762
5796
  return () => itemsFns.map((generateFn) => generateFn());
5763
5797
  };
5764
- var createBoundedSeriesGenerator = (config2) => {
5765
- boundedSeriesScheme.parse(config2);
5766
- const { upperLimit, lowerLimit, createInitValue, count } = config2;
5798
+ var createTupleGenerator = (config, customTypeMatch) => _createTupleGenerator(config, "*", customTypeMatch);
5799
+ var _createBoundedSeriesGenerator = (config, path) => {
5800
+ try {
5801
+ boundedSeriesScheme.parse(config);
5802
+ } catch (err) {
5803
+ throw new Error(`config path: ${path}.boundedSeries
5804
+ ${err}`);
5805
+ }
5806
+ const { upperLimit, lowerLimit, createInitValue, count } = config;
5767
5807
  return () => {
5768
5808
  let value = createInitValue();
5769
5809
  const boundedSeries = [];
@@ -5774,34 +5814,43 @@ var createBoundedSeriesGenerator = (config2) => {
5774
5814
  return boundedSeries;
5775
5815
  };
5776
5816
  };
5777
- var createGeneratorByType = (config2, customTypeMatch) => {
5778
- switch (config2.type) {
5817
+ var createBoundedSeriesGenerator = (config) => _createBoundedSeriesGenerator(config, "*");
5818
+ var _createGeneratorByType = (config, path, customTypeMatch) => {
5819
+ switch (config.type) {
5779
5820
  case "obj":
5780
- return createObjectGenerator(config2, customTypeMatch);
5821
+ return _createObjectGenerator(
5822
+ config,
5823
+ path,
5824
+ customTypeMatch
5825
+ );
5781
5826
  case "arr":
5782
- return createArrayGenerator(config2, customTypeMatch);
5827
+ return _createArrayGenerator(
5828
+ config,
5829
+ path,
5830
+ customTypeMatch
5831
+ );
5783
5832
  case "tuple":
5784
- return createTupleGenerator(config2, customTypeMatch);
5833
+ return _createTupleGenerator(
5834
+ config,
5835
+ path,
5836
+ customTypeMatch
5837
+ );
5785
5838
  case "select":
5786
- return createSelectionGenerator(config2);
5839
+ return _createSelectionGenerator(config, path);
5787
5840
  case "value":
5788
- return createValueGenerator(config2);
5841
+ return _createValueGenerator(config, path);
5789
5842
  case "bounded_series":
5790
- return createBoundedSeriesGenerator(config2);
5843
+ return _createBoundedSeriesGenerator(config, path);
5791
5844
  default: {
5792
5845
  if (customTypeMatch) {
5793
- return createValueGenerator(customTypeMatch(config2));
5846
+ return createValueGenerator(customTypeMatch(config, path));
5794
5847
  }
5795
- throw Error(`config type "${config2}" is not supported`);
5848
+ throw new Error(`path: ${path}
5849
+ config type is not supported`);
5796
5850
  }
5797
5851
  }
5798
5852
  };
5799
- var config = createObjectConfig({
5800
- a: createObjectConfig({
5801
- b: createValueConfig(() => 1)
5802
- })
5803
- });
5804
- var fn = createGeneratorByType(config);
5853
+ var createGeneratorByType = (config, customTypeMatch) => _createGeneratorByType(config, "*", customTypeMatch);
5805
5854
  // Annotate the CommonJS export names for ESM import in node:
5806
5855
  0 && (module.exports = {
5807
5856
  createArrayConfig,
package/dist/index.mjs CHANGED
@@ -36,40 +36,40 @@ var boundedSeriesScheme = z.object({
36
36
 
37
37
  // src/create_config.ts
38
38
  var createValueConfig = (generateFn) => {
39
- const config2 = {
39
+ const config = {
40
40
  type: "value",
41
41
  generateFn
42
42
  };
43
- valueConfigScheme.parse(config2);
44
- return config2;
43
+ valueConfigScheme.parse(config);
44
+ return config;
45
45
  };
46
46
  var createSelectionConfig = (items) => {
47
- const config2 = { type: "select", items };
48
- selectionConfigScheme.parse(config2);
49
- return config2;
47
+ const config = { type: "select", items };
48
+ selectionConfigScheme.parse(config);
49
+ return config;
50
50
  };
51
51
  var createObjectConfig = (content) => {
52
- const config2 = { type: "obj", content };
53
- objConfigScheme.parse(config2);
54
- return config2;
52
+ const config = { type: "obj", content };
53
+ objConfigScheme.parse(config);
54
+ return config;
55
55
  };
56
56
  var createArrayConfig = (item, len) => {
57
- const config2 = { type: "arr", item, len };
58
- arrayConfigScheme.parse(config2);
59
- return config2;
57
+ const config = { type: "arr", item, len };
58
+ arrayConfigScheme.parse(config);
59
+ return config;
60
60
  };
61
61
  var createTupleConfig = (configItems) => {
62
- const config2 = {
62
+ const config = {
63
63
  type: "tuple",
64
64
  configItems
65
65
  };
66
- tupleConfigScheme.parse(config2);
67
- return config2;
66
+ tupleConfigScheme.parse(config);
67
+ return config;
68
68
  };
69
- var createBoundedSeriesConfig = (config2) => {
69
+ var createBoundedSeriesConfig = (config) => {
70
70
  const newConfig = {
71
71
  type: "bounded_series",
72
- ...config2
72
+ ...config
73
73
  };
74
74
  boundedSeriesScheme.parse(newConfig);
75
75
  return newConfig;
@@ -5686,20 +5686,37 @@ var r38 = {};
5686
5686
  Xr(r38, { af_ZA: () => B58, ar: () => Y35, az: () => U39, base: () => Mi, cs_CZ: () => x61, da: () => le10, de: () => pr, de_AT: () => I40, de_CH: () => J42, dv: () => Q32, el: () => le9, en: () => ul, en_AU: () => be7, en_AU_ocker: () => P54, en_BORK: () => u60, en_CA: () => S41, en_GB: () => B46, en_GH: () => J39, en_HK: () => w42, en_IE: () => D49, en_IN: () => w43, en_NG: () => O41, en_US: () => A49, en_ZA: () => L39, eo: () => ro, es: () => ao3, es_MX: () => ia7, fa: () => wo, fi: () => A43, fr: () => Wi, fr_BE: () => I36, fr_CA: () => b32, fr_CH: () => _32, fr_LU: () => _33, fr_SN: () => _34, he: () => Z27, hr: () => C33, hu: () => U29, hy: () => j34, id_ID: () => F18, it: () => w21, ja: () => M24, ka_GE: () => H23, ko: () => G23, lv: () => $13, mk: () => S26, nb_NO: () => Z24, ne: () => j13, nl: () => an, nl_BE: () => j15, pl: () => ea3, pt_BR: () => W15, pt_PT: () => k17, ro: () => Mi2, ro_MD: () => j20, ru: () => ae4, sk: () => O8, sr_RS_latin: () => T7, sv: () => $5, th: () => E8, tr: () => V10, uk: () => S11, ur: () => yt, vi: () => U3, yo_NG: () => r3, zh_CN: () => Ke3, zh_TW: () => B5, zu_ZA: () => x5 });
5687
5687
 
5688
5688
  // src/create_generator_fn.ts
5689
- var createValueGenerator = (config2) => {
5690
- valueConfigScheme.parse(config2);
5691
- return config2.generateFn;
5689
+ var _createValueGenerator = (config, path) => {
5690
+ try {
5691
+ valueConfigScheme.parse(config);
5692
+ } catch (err) {
5693
+ throw new Error(`config path: ${path}.value
5694
+ ${err}`);
5695
+ }
5696
+ return config.generateFn;
5692
5697
  };
5693
- var createSelectionGenerator = (config2) => {
5694
- selectionConfigScheme.parse(config2);
5695
- const { items } = config2;
5698
+ var createValueGenerator = (config) => _createValueGenerator(config, "*");
5699
+ var _createSelectionGenerator = (config, path) => {
5700
+ try {
5701
+ selectionConfigScheme.parse(config);
5702
+ } catch (err) {
5703
+ throw new Error(`config path: ${path}.select
5704
+ ${err}`);
5705
+ }
5706
+ const { items } = config;
5696
5707
  return () => items[f55.number.int(items.length - 1)];
5697
5708
  };
5698
- var createObjectGenerator = (config2, customTypeMatch) => {
5699
- objConfigScheme.parse(config2);
5700
- const keyWithFns = Object.entries(config2.content).map(([key, subConfig]) => [
5709
+ var createSelectionGenerator = (config) => _createSelectionGenerator(config, "*");
5710
+ var _createObjectGenerator = (config, path, customTypeMatch) => {
5711
+ try {
5712
+ objConfigScheme.parse(config);
5713
+ } catch (err) {
5714
+ throw new Error(`config path: ${path}.obj
5715
+ ${err}`);
5716
+ }
5717
+ const keyWithFns = Object.entries(config.content).map(([key, subConfig]) => [
5701
5718
  key,
5702
- createGeneratorByType(subConfig, customTypeMatch)
5719
+ _createGeneratorByType(subConfig, `${path}.obj[${key}]`, customTypeMatch)
5703
5720
  ]);
5704
5721
  return () => {
5705
5722
  const result = {};
@@ -5709,24 +5726,47 @@ var createObjectGenerator = (config2, customTypeMatch) => {
5709
5726
  return result;
5710
5727
  };
5711
5728
  };
5712
- var createArrayGenerator = (config2, customTypeMatch) => {
5713
- arrayConfigScheme.parse(config2);
5714
- const itemGeneratorFn = createGeneratorByType(
5715
- config2.item,
5729
+ var createObjectGenerator = (config, customTypeMatch) => _createObjectGenerator(config, "*", customTypeMatch);
5730
+ var _createArrayGenerator = (config, path, customTypeMatch) => {
5731
+ try {
5732
+ arrayConfigScheme.parse(config);
5733
+ } catch (err) {
5734
+ throw new Error(`config path: ${path}.arr
5735
+ ${err}`);
5736
+ }
5737
+ const itemGeneratorFn = _createGeneratorByType(
5738
+ config.item,
5739
+ `${path}.arr`,
5716
5740
  customTypeMatch
5717
5741
  );
5718
- return () => Array.from({ length: config2.len ?? 0 }, itemGeneratorFn);
5742
+ return () => Array.from({ length: config.len ?? 0 }, itemGeneratorFn);
5719
5743
  };
5720
- var createTupleGenerator = (config2, customTypeMatch) => {
5721
- tupleConfigScheme.parse(config2);
5722
- const itemsFns = config2.configItems.map(
5723
- (configItem) => createGeneratorByType(configItem, customTypeMatch)
5744
+ var createArrayGenerator = (config, customTypeMatch) => _createArrayGenerator(config, "*", customTypeMatch);
5745
+ var _createTupleGenerator = (config, path, customTypeMatch) => {
5746
+ try {
5747
+ tupleConfigScheme.parse(config);
5748
+ } catch (err) {
5749
+ throw new Error(`config path: ${path}.tuple
5750
+ ${err}`);
5751
+ }
5752
+ const itemsFns = config.configItems.map(
5753
+ (configItem, index) => _createGeneratorByType(
5754
+ configItem,
5755
+ `${path}.tuple[${index}]`,
5756
+ customTypeMatch
5757
+ )
5724
5758
  );
5725
5759
  return () => itemsFns.map((generateFn) => generateFn());
5726
5760
  };
5727
- var createBoundedSeriesGenerator = (config2) => {
5728
- boundedSeriesScheme.parse(config2);
5729
- const { upperLimit, lowerLimit, createInitValue, count } = config2;
5761
+ var createTupleGenerator = (config, customTypeMatch) => _createTupleGenerator(config, "*", customTypeMatch);
5762
+ var _createBoundedSeriesGenerator = (config, path) => {
5763
+ try {
5764
+ boundedSeriesScheme.parse(config);
5765
+ } catch (err) {
5766
+ throw new Error(`config path: ${path}.boundedSeries
5767
+ ${err}`);
5768
+ }
5769
+ const { upperLimit, lowerLimit, createInitValue, count } = config;
5730
5770
  return () => {
5731
5771
  let value = createInitValue();
5732
5772
  const boundedSeries = [];
@@ -5737,34 +5777,43 @@ var createBoundedSeriesGenerator = (config2) => {
5737
5777
  return boundedSeries;
5738
5778
  };
5739
5779
  };
5740
- var createGeneratorByType = (config2, customTypeMatch) => {
5741
- switch (config2.type) {
5780
+ var createBoundedSeriesGenerator = (config) => _createBoundedSeriesGenerator(config, "*");
5781
+ var _createGeneratorByType = (config, path, customTypeMatch) => {
5782
+ switch (config.type) {
5742
5783
  case "obj":
5743
- return createObjectGenerator(config2, customTypeMatch);
5784
+ return _createObjectGenerator(
5785
+ config,
5786
+ path,
5787
+ customTypeMatch
5788
+ );
5744
5789
  case "arr":
5745
- return createArrayGenerator(config2, customTypeMatch);
5790
+ return _createArrayGenerator(
5791
+ config,
5792
+ path,
5793
+ customTypeMatch
5794
+ );
5746
5795
  case "tuple":
5747
- return createTupleGenerator(config2, customTypeMatch);
5796
+ return _createTupleGenerator(
5797
+ config,
5798
+ path,
5799
+ customTypeMatch
5800
+ );
5748
5801
  case "select":
5749
- return createSelectionGenerator(config2);
5802
+ return _createSelectionGenerator(config, path);
5750
5803
  case "value":
5751
- return createValueGenerator(config2);
5804
+ return _createValueGenerator(config, path);
5752
5805
  case "bounded_series":
5753
- return createBoundedSeriesGenerator(config2);
5806
+ return _createBoundedSeriesGenerator(config, path);
5754
5807
  default: {
5755
5808
  if (customTypeMatch) {
5756
- return createValueGenerator(customTypeMatch(config2));
5809
+ return createValueGenerator(customTypeMatch(config, path));
5757
5810
  }
5758
- throw Error(`config type "${config2}" is not supported`);
5811
+ throw new Error(`path: ${path}
5812
+ config type is not supported`);
5759
5813
  }
5760
5814
  }
5761
5815
  };
5762
- var config = createObjectConfig({
5763
- a: createObjectConfig({
5764
- b: createValueConfig(() => 1)
5765
- })
5766
- });
5767
- var fn = createGeneratorByType(config);
5816
+ var createGeneratorByType = (config, customTypeMatch) => _createGeneratorByType(config, "*", customTypeMatch);
5768
5817
  export {
5769
5818
  createArrayConfig,
5770
5819
  createArrayGenerator,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "struct-fakerator",
3
- "version": "2.0.1",
3
+ "version": "2.1.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -24,7 +24,7 @@
24
24
  "@faker-js/faker": "^8.4.1",
25
25
  "tsup": "^8.1.0",
26
26
  "typescript": "^5.5.3",
27
- "vitest": "^1.6.0"
27
+ "vitest": "^2.0.3"
28
28
  },
29
29
  "dependencies": {
30
30
  "zod": "^3.23.8"
@@ -14,6 +14,7 @@ import {
14
14
  createObjectConfig,
15
15
  createArrayConfig,
16
16
  } from "./create_config";
17
+ import type { ObjectConfig } from "./type";
17
18
 
18
19
  describe("createValueGenerator", () => {
19
20
  test("normal", () => {
@@ -155,6 +156,20 @@ describe("createGeneratorByType", () => {
155
156
  });
156
157
  });
157
158
 
159
+ test("test error config", () => {
160
+ const config = {
161
+ type: "obj",
162
+ content: {
163
+ name: {
164
+ type: "123",
165
+ generateFn: "John",
166
+ },
167
+ },
168
+ } as ObjectConfig<unknown>;
169
+
170
+ expect(() => createGeneratorByType(config)).toThrowError();
171
+ });
172
+
158
173
  test("with custom type match", () => {
159
174
  const createIntValueConfig = (option) => createValueConfig(() => 50);
160
175
  const createEmailValueConfig = (option) =>
@@ -17,7 +17,6 @@ import type {
17
17
  Result,
18
18
  BoundedSeriesConfig,
19
19
  } from "./type";
20
- import { createObjectConfig, createValueConfig } from "./create_config";
21
20
 
22
21
  type AllConfig<T> =
23
22
  | ValueConfig<T>
@@ -31,33 +30,60 @@ type AllConfig<T> =
31
30
  | TupleConfig<T, T, T, T, T>
32
31
  | BoundedSeriesConfig;
33
32
 
34
- // biome-ignore lint/suspicious/noExplicitAny: <explanation>
35
- export const createValueGenerator = <R = unknown>(config: any): (() => R) => {
36
- valueConfigScheme.parse(config);
33
+ const _createValueGenerator = <R = unknown>(
34
+ config: ValueConfig<unknown>,
35
+ path: string,
36
+ ): (() => R) => {
37
+ try {
38
+ valueConfigScheme.parse(config);
39
+ } catch (err) {
40
+ throw new Error(`config path: ${path}.value\n${err}`);
41
+ }
37
42
 
38
43
  return config.generateFn as () => R;
39
44
  };
45
+ export const createValueGenerator = <R = unknown>(
46
+ config: ValueConfig<unknown>,
47
+ ): (() => R) => _createValueGenerator(config, "*");
40
48
 
41
- export const createSelectionGenerator = <T extends SelectionConfig<unknown>>(
49
+ // =================== generator fn ====================
50
+
51
+ const _createSelectionGenerator = <T extends SelectionConfig<unknown>>(
42
52
  config: T,
53
+ path: string,
43
54
  ): (() => Result<T>) => {
44
- selectionConfigScheme.parse(config);
55
+ try {
56
+ selectionConfigScheme.parse(config);
57
+ } catch (err) {
58
+ throw new Error(`config path: ${path}.select\n${err}`);
59
+ }
45
60
 
46
61
  const { items } = config;
47
62
 
48
63
  return (() => items[faker.number.int(items.length - 1)]) as () => Result<T>;
49
64
  };
50
65
 
51
- export const createObjectGenerator = <T extends ObjectConfig<unknown>>(
66
+ export const createSelectionGenerator = <T extends SelectionConfig<unknown>>(
52
67
  config: T,
53
- customTypeMatch?: (config: unknown) => ValueConfig<unknown>,
68
+ ): (() => Result<T>) => _createSelectionGenerator(config, "*");
69
+
70
+ // =================== generator fn ====================
71
+
72
+ const _createObjectGenerator = <T extends ObjectConfig<unknown>>(
73
+ config: T,
74
+ path: string,
75
+ customTypeMatch?: (config: unknown, path?: string) => ValueConfig<unknown>,
54
76
  ): (() => Result<T>) => {
55
- objConfigScheme.parse(config);
77
+ try {
78
+ objConfigScheme.parse(config);
79
+ } catch (err) {
80
+ throw new Error(`config path: ${path}.obj\n ${err}`);
81
+ }
56
82
 
57
83
  const keyWithFns: [string, () => Result<AllConfig<unknown>>][] =
58
84
  Object.entries(config.content as object).map(([key, subConfig]) => [
59
85
  key,
60
- createGeneratorByType(subConfig, customTypeMatch),
86
+ _createGeneratorByType(subConfig, `${path}.obj[${key}]`, customTypeMatch),
61
87
  ]);
62
88
 
63
89
  return () => {
@@ -68,23 +94,41 @@ export const createObjectGenerator = <T extends ObjectConfig<unknown>>(
68
94
  return result as Result<T>;
69
95
  };
70
96
  };
97
+ export const createObjectGenerator = <T extends ObjectConfig<unknown>>(
98
+ config: T,
99
+ customTypeMatch?: (config: unknown, path?: string) => ValueConfig<unknown>,
100
+ ): (() => Result<T>) => _createObjectGenerator(config, "*", customTypeMatch);
71
101
 
72
- export const createArrayGenerator = <T extends ArrayConfig<unknown>>(
102
+ // =================== generator fn ====================
103
+
104
+ const _createArrayGenerator = <T extends ArrayConfig<unknown>>(
73
105
  config: T,
74
- customTypeMatch?: (config: unknown) => ValueConfig<unknown>,
106
+ path: string,
107
+ customTypeMatch?: (config: unknown, path?: string) => ValueConfig<unknown>,
75
108
  ): (() => Result<T>) => {
76
- arrayConfigScheme.parse(config);
109
+ try {
110
+ arrayConfigScheme.parse(config);
111
+ } catch (err) {
112
+ throw new Error(`config path: ${path}.arr\n ${err}`);
113
+ }
77
114
 
78
- const itemGeneratorFn = createGeneratorByType(
115
+ const itemGeneratorFn = _createGeneratorByType(
79
116
  config.item as AllConfig<unknown>,
117
+ `${path}.arr`,
80
118
  customTypeMatch,
81
119
  );
82
120
 
83
121
  return () =>
84
122
  Array.from({ length: config.len ?? 0 }, itemGeneratorFn) as Result<T>;
85
123
  };
124
+ export const createArrayGenerator = <T extends ArrayConfig<unknown>>(
125
+ config: T,
126
+ customTypeMatch?: (config: unknown, path?: string) => ValueConfig<unknown>,
127
+ ): (() => Result<T>) => _createArrayGenerator(config, "*", customTypeMatch);
86
128
 
87
- export const createTupleGenerator = <
129
+ // =================== generator fn ====================
130
+
131
+ const _createTupleGenerator = <
88
132
  T extends
89
133
  | TupleConfig<unknown, unknown, unknown, unknown, unknown>
90
134
  | TupleConfig<unknown, unknown, unknown, unknown>
@@ -93,21 +137,48 @@ export const createTupleGenerator = <
93
137
  | TupleConfig<unknown>,
94
138
  >(
95
139
  config: T,
96
- customTypeMatch?: (config: unknown) => ValueConfig<unknown>,
140
+ path: string,
141
+ customTypeMatch?: (config: unknown, path?: string) => ValueConfig<unknown>,
97
142
  ): (() => Result<T>) => {
98
- tupleConfigScheme.parse(config);
143
+ try {
144
+ tupleConfigScheme.parse(config);
145
+ } catch (err) {
146
+ throw new Error(`config path: ${path}.tuple\n ${err}`);
147
+ }
99
148
 
100
- const itemsFns = config.configItems.map((configItem) =>
101
- createGeneratorByType(configItem as AllConfig<unknown>, customTypeMatch),
149
+ const itemsFns = config.configItems.map((configItem, index) =>
150
+ _createGeneratorByType(
151
+ configItem as AllConfig<unknown>,
152
+ `${path}.tuple[${index}]`,
153
+ customTypeMatch,
154
+ ),
102
155
  );
103
156
 
104
157
  return () => itemsFns.map((generateFn) => generateFn()) as Result<T>;
105
158
  };
159
+ export const createTupleGenerator = <
160
+ T extends
161
+ | TupleConfig<unknown, unknown, unknown, unknown, unknown>
162
+ | TupleConfig<unknown, unknown, unknown, unknown>
163
+ | TupleConfig<unknown, unknown, unknown>
164
+ | TupleConfig<unknown, unknown>
165
+ | TupleConfig<unknown>,
166
+ >(
167
+ config: T,
168
+ customTypeMatch?: (config: unknown, path?: string) => ValueConfig<unknown>,
169
+ ): (() => Result<T>) => _createTupleGenerator(config, "*", customTypeMatch);
106
170
 
107
- export const createBoundedSeriesGenerator = <T extends BoundedSeriesConfig>(
171
+ // =================== generator fn ====================
172
+
173
+ const _createBoundedSeriesGenerator = <T extends BoundedSeriesConfig>(
108
174
  config: T,
175
+ path: string,
109
176
  ): (() => Result<T>) => {
110
- boundedSeriesScheme.parse(config);
177
+ try {
178
+ boundedSeriesScheme.parse(config);
179
+ } catch (err) {
180
+ throw new Error(`config path: ${path}.boundedSeries\n ${err}`);
181
+ }
111
182
 
112
183
  const { upperLimit, lowerLimit, createInitValue, count } = config;
113
184
 
@@ -124,37 +195,52 @@ export const createBoundedSeriesGenerator = <T extends BoundedSeriesConfig>(
124
195
  return boundedSeries as Result<T>;
125
196
  };
126
197
  };
198
+ export const createBoundedSeriesGenerator = <T extends BoundedSeriesConfig>(
199
+ config: T,
200
+ ): (() => Result<T>) => _createBoundedSeriesGenerator(config, "*");
127
201
 
128
- export const createGeneratorByType = <T extends AllConfig<unknown>>(
202
+ // =================== generator fn ====================
203
+
204
+ const _createGeneratorByType = <T extends AllConfig<unknown>>(
129
205
  config: T,
130
- customTypeMatch?: (config: unknown) => ValueConfig<unknown>,
206
+ path: string,
207
+ customTypeMatch?: (config: unknown, path?: string) => ValueConfig<unknown>,
131
208
  ): (() => Result<T>) => {
132
209
  switch (config.type) {
133
210
  case "obj":
134
- return createObjectGenerator(config, customTypeMatch) as () => Result<T>;
211
+ return _createObjectGenerator(
212
+ config,
213
+ path,
214
+ customTypeMatch,
215
+ ) as () => Result<T>;
135
216
  case "arr":
136
- return createArrayGenerator(config, customTypeMatch) as () => Result<T>;
217
+ return _createArrayGenerator(
218
+ config,
219
+ path,
220
+ customTypeMatch,
221
+ ) as () => Result<T>;
137
222
  case "tuple":
138
- return createTupleGenerator(config, customTypeMatch) as () => Result<T>;
223
+ return _createTupleGenerator(
224
+ config,
225
+ path,
226
+ customTypeMatch,
227
+ ) as () => Result<T>;
139
228
  case "select":
140
- return createSelectionGenerator(config) as () => Result<T>;
229
+ return _createSelectionGenerator(config, path) as () => Result<T>;
141
230
  case "value":
142
- return createValueGenerator(config);
231
+ return _createValueGenerator(config, path);
143
232
  case "bounded_series":
144
- return createBoundedSeriesGenerator(config) as () => Result<T>;
233
+ return _createBoundedSeriesGenerator(config, path) as () => Result<T>;
145
234
  default: {
146
235
  if (customTypeMatch) {
147
- return createValueGenerator(customTypeMatch(config));
236
+ return createValueGenerator(customTypeMatch(config, path));
148
237
  }
149
- throw Error(`config type "${config}" is not supported`);
238
+ throw new Error(`path: ${path}\nconfig type is not supported`);
150
239
  }
151
240
  }
152
241
  };
153
242
 
154
- const config = createObjectConfig({
155
- a: createObjectConfig({
156
- b: createValueConfig(() => 1),
157
- }),
158
- });
159
-
160
- const fn = createGeneratorByType(config);
243
+ export const createGeneratorByType = <T extends AllConfig<unknown>>(
244
+ config: T,
245
+ customTypeMatch?: (config: unknown, path?: string) => ValueConfig<unknown>,
246
+ ): (() => Result<T>) => _createGeneratorByType(config, "*", customTypeMatch);
package/.babelrc DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "ignore": [
3
- "src/*.test.js"
4
- ]
5
- }