foorm 0.0.2-alpha.3 → 0.0.2-alpha.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.cjs CHANGED
@@ -20,6 +20,7 @@ class Foorm {
20
20
  this.entries = (opts === null || opts === void 0 ? void 0 : opts.entries) || [];
21
21
  this.submit = opts === null || opts === void 0 ? void 0 : opts.submit;
22
22
  this.title = (opts === null || opts === void 0 ? void 0 : opts.title) || '';
23
+ this.context = (opts === null || opts === void 0 ? void 0 : opts.context) || {};
23
24
  }
24
25
  addEntry(entry) {
25
26
  this.entries.push(entry);
@@ -30,17 +31,33 @@ class Foorm {
30
31
  setSubmit(submit) {
31
32
  this.submit = submit;
32
33
  }
33
- transportable() {
34
+ setContext(context) {
35
+ this.context = context;
36
+ }
37
+ /**
38
+ * Normalizes form metadata and removes all the functions
39
+ * from validators.
40
+ *
41
+ * @param replaceContext a context to be transported along with metadata
42
+ * @returns form metadata without functions
43
+ */
44
+ transportable(replaceContext) {
34
45
  var _a, _b;
35
46
  return {
36
47
  title: (_a = this.title) !== null && _a !== void 0 ? _a : '',
37
48
  submit: (_b = this.submit) !== null && _b !== void 0 ? _b : { text: 'Submit' },
49
+ context: replaceContext || this.context,
38
50
  entries: this.entries.map((e) => (Object.assign(Object.assign({}, this.normalizeEntry(e)), { validators: (e.validators || []).filter((v) => isFtring(v)) }))),
39
51
  };
40
52
  }
41
53
  normalizeEntry(e) {
42
54
  return Object.assign(Object.assign({}, e), { name: e.name || e.field, label: e.label || e.field, type: e.type || 'text' });
43
55
  }
56
+ /**
57
+ * Evaluates all the ftrings into functions, makes it ready for execution
58
+ *
59
+ * @returns form metadata with functions
60
+ */
44
61
  executable() {
45
62
  var _a, _b;
46
63
  if (!this.fns)
@@ -51,6 +68,7 @@ class Foorm {
51
68
  text: transformFtrings(((_a = this.submit) === null || _a === void 0 ? void 0 : _a.text) || 'Submit', this.fns),
52
69
  disabled: transformFtrings((_b = this.submit) === null || _b === void 0 ? void 0 : _b.disabled, this.fns),
53
70
  },
71
+ context: this.context,
54
72
  entries: this.entries
55
73
  .map((e) => this.normalizeEntry(e))
56
74
  .map((e) => (Object.assign(Object.assign({}, e), {
@@ -86,8 +104,9 @@ class Foorm {
86
104
  const errors = {};
87
105
  for (const [key, value] of Object.entries(fields)) {
88
106
  const evalEntry = Object.assign({}, value.entry);
89
- const ctx = {
107
+ const scope = {
90
108
  v: data[key],
109
+ context: this.context,
91
110
  entry: {
92
111
  field: evalEntry.field,
93
112
  type: evalEntry.type,
@@ -97,25 +116,26 @@ class Foorm {
97
116
  },
98
117
  data,
99
118
  };
100
- if (ctx.entry) {
119
+ if (scope.entry) {
101
120
  if (typeof evalEntry.disabled === 'function') {
102
- ctx.entry.disabled = evalEntry.disabled =
103
- evalEntry.disabled(ctx);
121
+ scope.entry.disabled = evalEntry.disabled =
122
+ evalEntry.disabled(scope);
104
123
  }
105
124
  if (typeof evalEntry.optional === 'function') {
106
- ctx.entry.optional = evalEntry.optional =
107
- evalEntry.optional(ctx);
125
+ scope.entry.optional = evalEntry.optional =
126
+ evalEntry.optional(scope);
108
127
  }
109
128
  if (typeof evalEntry.hidden === 'function') {
110
- ctx.entry.hidden = evalEntry.hidden =
111
- evalEntry.hidden(ctx);
129
+ scope.entry.hidden = evalEntry.hidden =
130
+ evalEntry.hidden(scope);
112
131
  }
113
132
  }
114
133
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
115
134
  const result = validate({
116
135
  v: data[key],
136
+ context: this.context,
117
137
  validators: value.validators,
118
- entry: ctx.entry,
138
+ entry: scope.entry,
119
139
  data,
120
140
  });
121
141
  if (!result.passed) {
@@ -136,6 +156,7 @@ function validate(opts) {
136
156
  for (const validator of opts.validators || []) {
137
157
  const result = validator({
138
158
  v: opts.v,
159
+ context: opts.context,
139
160
  data: opts.data,
140
161
  entry: opts.entry,
141
162
  });
package/dist/index.d.ts CHANGED
@@ -5,9 +5,10 @@ type TFtring = {
5
5
  };
6
6
  type StringOrFtring = string | TFtring;
7
7
  type ObjSOF = Record<string, StringOrFtring>;
8
- type TFoormFnCtx<T = string> = {
8
+ type TFoormFnScope<T = string> = {
9
9
  v?: T;
10
10
  data: Record<string, unknown>;
11
+ context: Record<string, unknown>;
11
12
  entry?: Pick<TFoormEntry<T, unknown, string, boolean>, TRelevantFields> & {
12
13
  optional?: boolean;
13
14
  disabled?: boolean;
@@ -15,8 +16,8 @@ type TFoormFnCtx<T = string> = {
15
16
  };
16
17
  action?: string;
17
18
  };
18
- type TFoormValidatorFn<T = string> = (ctx: TFoormFnCtx<T>) => string | boolean;
19
- type TFoormFn<T = string, R = string | boolean> = (ctx: TFoormFnCtx<T>) => R;
19
+ type TFoormValidatorFn<T = string> = (ctx: TFoormFnScope<T>) => string | boolean;
20
+ type TFoormFn<T = string, R = string | boolean> = (ctx: TFoormFnScope<T>) => R;
20
21
  type TRelevantFields = 'field' | 'type' | 'component' | 'name' | 'attrs' | 'length';
21
22
  interface TFoormEntry<T = string, O = string, SFTR = TFtring, BFTR = TFtring, FNFTR = TFtring> {
22
23
  field: string;
@@ -28,6 +29,7 @@ interface TFoormEntry<T = string, O = string, SFTR = TFtring, BFTR = TFtring, FN
28
29
  styles?: (string | SFTR) | Record<string, string | SFTR>;
29
30
  type?: string;
30
31
  component?: string;
32
+ autocomplete?: string;
31
33
  name?: string;
32
34
  value?: T;
33
35
  options?: O[];
@@ -52,28 +54,46 @@ interface TFoormOptions {
52
54
  title?: StringOrFtring;
53
55
  entries: TFoormEntry[];
54
56
  submit?: TFoormSubmit;
57
+ context?: Record<string, unknown>;
55
58
  }
56
59
  declare class Foorm {
57
60
  protected entries: TFoormEntry[];
58
61
  protected submit?: TFoormSubmit;
59
62
  protected title?: StringOrFtring;
63
+ protected context: Record<string, unknown>;
60
64
  private fns;
61
65
  constructor(opts?: TFoormOptions);
62
66
  addEntry(entry: TFoormEntry): void;
63
67
  setTitle(title: string): void;
64
68
  setSubmit(submit: TFoormSubmit): void;
65
- transportable(): Required<TFoormOptions>;
69
+ setContext<T extends Record<string, unknown>>(context: T): void;
70
+ /**
71
+ * Normalizes form metadata and removes all the functions
72
+ * from validators.
73
+ *
74
+ * @param replaceContext a context to be transported along with metadata
75
+ * @returns form metadata without functions
76
+ */
77
+ transportable<T extends Record<string, unknown>>(replaceContext?: T): Required<TFoormOptions> & {
78
+ context?: Record<string, unknown>;
79
+ };
66
80
  protected normalizeEntry<T, O>(e: TFoormEntry<T, O>): TFoormEntry<T, O> & {
67
81
  name: string;
68
82
  label: string | TFtring;
69
83
  type: string;
70
84
  };
85
+ /**
86
+ * Evaluates all the ftrings into functions, makes it ready for execution
87
+ *
88
+ * @returns form metadata with functions
89
+ */
71
90
  executable(): {
72
91
  title: string | TFoormFn<undefined, string>;
73
92
  submit: {
74
93
  text: string | TFoormFn<undefined, string>;
75
94
  disabled: boolean | TFoormFn<undefined, boolean>;
76
95
  };
96
+ context: Record<string, unknown>;
77
97
  entries: TFoormEntryExecutable[];
78
98
  };
79
99
  prepareValidators(_validators: TFoormEntry['validators']): TFoormValidatorFn<string>[];
@@ -84,7 +104,7 @@ declare class Foorm {
84
104
  }
85
105
  type TFoormExecutableMeta = ReturnType<Foorm['executable']>;
86
106
  type TFoormExecutableEntry = TFoormExecutableMeta['entries'][number];
87
- declare function validate<T = string>(opts: TFoormFnCtx<T> & {
107
+ declare function validate<T = string>(opts: TFoormFnScope<T> & {
88
108
  validators: TFoormValidatorFn<T>[];
89
109
  }): {
90
110
  passed: boolean;
@@ -97,4 +117,4 @@ declare function validate<T = string>(opts: TFoormFnCtx<T> & {
97
117
  declare function isFtring(input: unknown): input is TFtring;
98
118
  declare function ftring(strings: TemplateStringsArray, __type__?: TFtring['__type__']): TFtring;
99
119
 
100
- export { Foorm, type ObjSOF, type StringOrFtring, type TFoormEntry, type TFoormEntryExecutable, type TFoormExecutableEntry, type TFoormExecutableMeta, type TFoormFn, type TFoormFnCtx, type TFoormOptions, type TFoormSubmit, type TFoormValidatorFn, type TFtring, ftring, isFtring, validate };
120
+ export { Foorm, type ObjSOF, type StringOrFtring, type TFoormEntry, type TFoormEntryExecutable, type TFoormExecutableEntry, type TFoormExecutableMeta, type TFoormFn, type TFoormFnScope, type TFoormOptions, type TFoormSubmit, type TFoormValidatorFn, type TFtring, ftring, isFtring, validate };
package/dist/index.mjs CHANGED
@@ -18,6 +18,7 @@ class Foorm {
18
18
  this.entries = (opts === null || opts === void 0 ? void 0 : opts.entries) || [];
19
19
  this.submit = opts === null || opts === void 0 ? void 0 : opts.submit;
20
20
  this.title = (opts === null || opts === void 0 ? void 0 : opts.title) || '';
21
+ this.context = (opts === null || opts === void 0 ? void 0 : opts.context) || {};
21
22
  }
22
23
  addEntry(entry) {
23
24
  this.entries.push(entry);
@@ -28,17 +29,33 @@ class Foorm {
28
29
  setSubmit(submit) {
29
30
  this.submit = submit;
30
31
  }
31
- transportable() {
32
+ setContext(context) {
33
+ this.context = context;
34
+ }
35
+ /**
36
+ * Normalizes form metadata and removes all the functions
37
+ * from validators.
38
+ *
39
+ * @param replaceContext a context to be transported along with metadata
40
+ * @returns form metadata without functions
41
+ */
42
+ transportable(replaceContext) {
32
43
  var _a, _b;
33
44
  return {
34
45
  title: (_a = this.title) !== null && _a !== void 0 ? _a : '',
35
46
  submit: (_b = this.submit) !== null && _b !== void 0 ? _b : { text: 'Submit' },
47
+ context: replaceContext || this.context,
36
48
  entries: this.entries.map((e) => (Object.assign(Object.assign({}, this.normalizeEntry(e)), { validators: (e.validators || []).filter((v) => isFtring(v)) }))),
37
49
  };
38
50
  }
39
51
  normalizeEntry(e) {
40
52
  return Object.assign(Object.assign({}, e), { name: e.name || e.field, label: e.label || e.field, type: e.type || 'text' });
41
53
  }
54
+ /**
55
+ * Evaluates all the ftrings into functions, makes it ready for execution
56
+ *
57
+ * @returns form metadata with functions
58
+ */
42
59
  executable() {
43
60
  var _a, _b;
44
61
  if (!this.fns)
@@ -49,6 +66,7 @@ class Foorm {
49
66
  text: transformFtrings(((_a = this.submit) === null || _a === void 0 ? void 0 : _a.text) || 'Submit', this.fns),
50
67
  disabled: transformFtrings((_b = this.submit) === null || _b === void 0 ? void 0 : _b.disabled, this.fns),
51
68
  },
69
+ context: this.context,
52
70
  entries: this.entries
53
71
  .map((e) => this.normalizeEntry(e))
54
72
  .map((e) => (Object.assign(Object.assign({}, e), {
@@ -84,8 +102,9 @@ class Foorm {
84
102
  const errors = {};
85
103
  for (const [key, value] of Object.entries(fields)) {
86
104
  const evalEntry = Object.assign({}, value.entry);
87
- const ctx = {
105
+ const scope = {
88
106
  v: data[key],
107
+ context: this.context,
89
108
  entry: {
90
109
  field: evalEntry.field,
91
110
  type: evalEntry.type,
@@ -95,25 +114,26 @@ class Foorm {
95
114
  },
96
115
  data,
97
116
  };
98
- if (ctx.entry) {
117
+ if (scope.entry) {
99
118
  if (typeof evalEntry.disabled === 'function') {
100
- ctx.entry.disabled = evalEntry.disabled =
101
- evalEntry.disabled(ctx);
119
+ scope.entry.disabled = evalEntry.disabled =
120
+ evalEntry.disabled(scope);
102
121
  }
103
122
  if (typeof evalEntry.optional === 'function') {
104
- ctx.entry.optional = evalEntry.optional =
105
- evalEntry.optional(ctx);
123
+ scope.entry.optional = evalEntry.optional =
124
+ evalEntry.optional(scope);
106
125
  }
107
126
  if (typeof evalEntry.hidden === 'function') {
108
- ctx.entry.hidden = evalEntry.hidden =
109
- evalEntry.hidden(ctx);
127
+ scope.entry.hidden = evalEntry.hidden =
128
+ evalEntry.hidden(scope);
110
129
  }
111
130
  }
112
131
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
113
132
  const result = validate({
114
133
  v: data[key],
134
+ context: this.context,
115
135
  validators: value.validators,
116
- entry: ctx.entry,
136
+ entry: scope.entry,
117
137
  data,
118
138
  });
119
139
  if (!result.passed) {
@@ -134,6 +154,7 @@ function validate(opts) {
134
154
  for (const validator of opts.validators || []) {
135
155
  const result = validator({
136
156
  v: opts.v,
157
+ context: opts.context,
137
158
  data: opts.data,
138
159
  entry: opts.entry,
139
160
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foorm",
3
- "version": "0.0.2-alpha.3",
3
+ "version": "0.0.2-alpha.5",
4
4
  "description": "foorm",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",