@bolttech/form-engine-core 0.0.1-beta.10 → 0.0.1-beta.11

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/index.esm.js CHANGED
@@ -2846,7 +2846,8 @@ class FormCore {
2846
2846
  * @param {((payload: {field: string;data: TFormValues;}) => void) | undefined} [entry.onData] - A callback function to handle data emission.
2847
2847
  */
2848
2848
  constructor(entry) {
2849
- var _a, _b, _c, _d, _e, _f, _g, _h;
2849
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
2850
+ this.mappers = new Map();
2850
2851
  this.schema = entry.schema;
2851
2852
  this.fields = new Map();
2852
2853
  this.initialValues = entry.initialValues || ((_a = entry.schema) === null || _a === void 0 ? void 0 : _a.initialValues);
@@ -2858,7 +2859,9 @@ class FormCore {
2858
2859
  defaultAPIdebounceTimeMS: Number((_e = entry.config) === null || _e === void 0 ? void 0 : _e.defaultAPIdebounceTimeMS) ? Number((_f = entry.config) === null || _f === void 0 ? void 0 : _f.defaultAPIdebounceTimeMS) : DEFAULT_API_DEBOUNCE_TIME,
2859
2860
  defaultStateRefreshTimeMS: Number((_g = entry.config) === null || _g === void 0 ? void 0 : _g.defaultStateRefreshTimeMS) ? Number((_h = entry.config) === null || _h === void 0 ? void 0 : _h.defaultStateRefreshTimeMS) : DEFAULT_STATE_REFRESH_TIME
2860
2861
  };
2861
- this.mappers = entry.mappers;
2862
+ (_j = entry.mappers) === null || _j === void 0 ? void 0 : _j.map(mapper => {
2863
+ this.mappers.set(mapper.componentName, mapper);
2864
+ });
2862
2865
  this.schema && FormCore.checkIndexes(this.schema.components);
2863
2866
  this.templateSubject$ = new Subject();
2864
2867
  this.submitSubject$ = new Subject();
@@ -3091,6 +3094,11 @@ class FormCore {
3091
3094
  case 'object':
3092
3095
  if (currValue === null) {
3093
3096
  value = null;
3097
+ break;
3098
+ }
3099
+ if (currValue instanceof Date) {
3100
+ value = `new Date(\`${currValue}\`)`;
3101
+ break;
3094
3102
  }
3095
3103
  value = JSON.stringify(currValue);
3096
3104
  break;
@@ -3118,7 +3126,7 @@ class FormCore {
3118
3126
  */
3119
3127
  replaceExpression(expression, values) {
3120
3128
  const regex = /\${(.*?)}/g;
3121
- return expression.replace(regex, () => values.shift() || '');
3129
+ return expression.replace(regex, () => String(values.shift()) || '');
3122
3130
  }
3123
3131
  /**
3124
3132
  * Checks if an expression string contains string concatenation within a marked expression.
@@ -3252,7 +3260,7 @@ class FormCore {
3252
3260
  if (this.fields.has(fieldSchema.name)) {
3253
3261
  throw new Error(`field name ${fieldSchema.name} already defined`);
3254
3262
  }
3255
- const mapper = mapperElement || ((_a = this.mappers) === null || _a === void 0 ? void 0 : _a.find(mapEl => mapEl.componentName === fieldSchema.component));
3263
+ const mapper = mapperElement || ((_a = this.mappers) === null || _a === void 0 ? void 0 : _a.get(fieldSchema.component));
3256
3264
  if (!mapper) throw new Error(`mapper not found for ${fieldSchema.component}, add it to the mappers configuration`);
3257
3265
  this.fields.set(fieldSchema.name, new FormField({
3258
3266
  schemaComponent: fieldSchema,
@@ -3299,7 +3307,12 @@ class FormCore {
3299
3307
  var _a, _b, _c;
3300
3308
  const currField = this.fields.get(structElement.name);
3301
3309
  if (!currField) {
3302
- const mapper = (_a = this.mappers) === null || _a === void 0 ? void 0 : _a.find(mapEl => mapEl.componentName === structElement.component);
3310
+ let mapper;
3311
+ if (structElement === null || structElement === void 0 ? void 0 : structElement.mapper) {
3312
+ mapper = structElement === null || structElement === void 0 ? void 0 : structElement.mapper;
3313
+ } else {
3314
+ mapper = (_a = this.mappers) === null || _a === void 0 ? void 0 : _a.get(structElement.component);
3315
+ }
3303
3316
  if (!mapper) throw new Error(`mapper not found for ${structElement.component}, add it to the mappers configuration`);
3304
3317
  this.fields.set(structElement.name, new FormField({
3305
3318
  schemaComponent: structElement,
@@ -3317,10 +3330,11 @@ class FormCore {
3317
3330
  } else {
3318
3331
  currField.children = ((_c = structElement === null || structElement === void 0 ? void 0 : structElement.children) === null || _c === void 0 ? void 0 : _c.map(el => el.name)) || (currField === null || currField === void 0 ? void 0 : currField.children) || [];
3319
3332
  currField.path = path;
3333
+ currField.originalSchema = structElement;
3320
3334
  currField.templateSubject$ = this.templateSubject$;
3321
3335
  }
3322
3336
  if (structElement.children) {
3323
- return this.serializeStructure(structElement.children, `${path ? `${path}.` : ``}${structElement.name}`);
3337
+ this.serializeStructure(structElement.children, `${path ? `${path}.` : ``}${structElement.name}`);
3324
3338
  }
3325
3339
  });
3326
3340
  }
@@ -3444,15 +3458,22 @@ class FormCore {
3444
3458
  */
3445
3459
  FormCore.checkIndexes = (struct, indexes = []) => {
3446
3460
  if (!struct) return indexes;
3447
- for (let i = 0; i < struct.length; i++) {
3448
- const structElement = struct[i];
3449
- if (structElement.name === IVARPROPNAME) {
3450
- throw new Error(`reserved ${IVARPROPNAME} name for field names`);
3451
- }
3452
- indexes.push(structElement.name);
3453
- if (structElement.children) {
3454
- return FormCore.checkIndexes(structElement.children, indexes);
3461
+ const helper = (struct, indexes) => {
3462
+ for (let i = 0; i < struct.length; i++) {
3463
+ const structElement = struct[i];
3464
+ if (structElement.name === IVARPROPNAME) {
3465
+ throw new Error(`reserved ${IVARPROPNAME} name for field names`);
3466
+ }
3467
+ indexes.push(structElement.name);
3468
+ if (structElement.children) {
3469
+ helper(structElement.children, indexes);
3470
+ }
3455
3471
  }
3472
+ };
3473
+ helper(struct, indexes);
3474
+ const duppedIndexes = indexes.filter((item, index) => indexes.indexOf(item) !== index);
3475
+ if (duppedIndexes.length > 0) {
3476
+ throw new Error(`duplicated indexes found on schema: ${JSON.stringify(duppedIndexes)}`);
3456
3477
  }
3457
3478
  return indexes;
3458
3479
  };
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@bolttech/form-engine-core",
3
- "version": "0.0.1-beta.10",
3
+ "version": "0.0.1-beta.11",
4
4
  "module": "./index.esm.js",
5
5
  "type": "module",
6
6
  "main": "./index.esm.js",
7
7
  "dependencies": {
8
8
  "@gaignoux/currency": "1.1.0",
9
- "credit-card-type": "10.0.0",
9
+ "credit-card-type": "10.0.1",
10
10
  "lodash": "4.17.21",
11
11
  "rxjs": "7.8.1"
12
12
  },
@@ -1,3 +1,4 @@
1
+ import { TMapper } from '../types/mapper';
1
2
  import { TApiEvent, TErrorMessages, TEvent, TFormatters, TMasks, TProps, TResetValueMethods, TSchemaFormConfig, TValidationMethods, TVisibility } from '../types/schema';
2
3
  /**
3
4
  * @interface IComponentSchema
@@ -48,6 +49,11 @@ interface IComponentSchema {
48
49
  masks?: TMasks;
49
50
  children?: IComponentSchema[];
50
51
  }
52
+ interface IComponentSchemaAsFormField<T> extends IComponentSchema {
53
+ mapper?: TMapper<T>;
54
+ order?: number;
55
+ children?: IComponentSchemaAsFormField<T>[];
56
+ }
51
57
  /**
52
58
  * @interface IFormSchema
53
59
  * Represents the schema for a form.
@@ -83,4 +89,4 @@ interface IFormSchema {
83
89
  iVars?: Record<string, unknown>;
84
90
  components?: IComponentSchema[];
85
91
  }
86
- export { IFormSchema, IComponentSchema };
92
+ export { IFormSchema, IComponentSchema, IComponentSchemaAsFormField };
@@ -1,6 +1,6 @@
1
1
  import { Observable, Subject, Subscription } from 'rxjs';
2
2
  import { TApiConfig, TApiEvent, TApiResponse, TErrorMessages, TEvent, TFormatters, TMasks, TResetValueMethods, TSchemaFormConfig, TValidationMethods, TVisibility } from '../types/schema';
3
- import { IComponentSchema } from '../interfaces/schema';
3
+ import { IComponentSchema, IComponentSchemaAsFormField } from '../interfaces/schema';
4
4
  import { IState } from '../interfaces/state';
5
5
  import { TEvents, TFieldEvent, TMutationEvents, TValueChangeEvent } from '../types/event';
6
6
  import { TMapper } from '../types/mapper';
@@ -13,7 +13,7 @@ declare class FormField {
13
13
  component: string;
14
14
  path?: string;
15
15
  children?: string[];
16
- originalSchema: IComponentSchema;
16
+ originalSchema: IComponentSchemaAsFormField<unknown>;
17
17
  validations?: TEvent<TValidationMethods>;
18
18
  visibilityConditions?: TVisibility[];
19
19
  resetValues?: TResetValueMethods[];
@@ -1,6 +1,6 @@
1
1
  import { IFormField } from './field';
2
2
  import { Subject, Subscription } from 'rxjs';
3
- import { IComponentSchema, IFormSchema } from '../interfaces/schema';
3
+ import { IComponentSchema, IComponentSchemaAsFormField, IFormSchema } from '../interfaces/schema';
4
4
  import { TSchemaFormConfig } from '../types/schema';
5
5
  import { TSubscribedTemplates } from '../types/template';
6
6
  import { TEvents, TFieldEvent, TMutationEvents } from '../types/event';
@@ -29,7 +29,7 @@ declare class FormCore {
29
29
  action?: string;
30
30
  method?: string;
31
31
  config: Required<TSchemaFormConfig>;
32
- mappers?: TMapper<unknown>[];
32
+ mappers: Map<string, TMapper<unknown>>;
33
33
  onSubmit?: (data: TFormValues<Record<string, unknown>>) => void;
34
34
  /**
35
35
  * Creates an instance of FormCore.
@@ -113,7 +113,7 @@ declare class FormCore {
113
113
  * @param {string} expression - The expression containing parameters.
114
114
  * @returns {string[]} An array of extracted parameters.
115
115
  */
116
- extractParams(expression: string): string[];
116
+ extractParams(expression: string): unknown[];
117
117
  /**
118
118
  * Replaces expressions marked by ${...} in the expression string with the provided values.
119
119
  *
@@ -121,7 +121,7 @@ declare class FormCore {
121
121
  * @param {string[]} values - The values to be inserted into the marked expressions.
122
122
  * @returns {string} The expression string with the replacements made.
123
123
  */
124
- replaceExpression(expression: string, values: string[]): string;
124
+ replaceExpression(expression: string, values: unknown[]): string;
125
125
  /**
126
126
  * Checks if an expression string contains string concatenation within a marked expression.
127
127
  *
@@ -190,13 +190,13 @@ declare class FormCore {
190
190
  * @param {IComponentSchema[]} [struct] - The schema structure to serialize.
191
191
  * @param {string} [path] - The path of the parent component.
192
192
  */
193
- serializeStructure(struct?: IComponentSchema[], path?: string): void;
193
+ serializeStructure(struct?: IComponentSchemaAsFormField<unknown>[], path?: string): void;
194
194
  /**
195
195
  * Refreshes form fields based on changes in the schema structure.
196
196
  *
197
197
  * @param {IComponentSchema[]} struct - The updated schema structure.
198
198
  */
199
- refreshFields(struct: IComponentSchema[]): void;
199
+ refreshFields(struct: IComponentSchemaAsFormField<unknown>[]): void;
200
200
  /**
201
201
  * Gets a form field by its key.
202
202
  *