@betterinternship/core 1.3.1 → 1.3.3

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.
@@ -1,224 +1,224 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FormMetadata = exports.FIELD_TYPES = exports.SOURCES = exports.PARTIES = exports.SCHEMA_VERSION = void 0;
4
- const zod_1 = require("zod");
5
- const fields_client_1 = require("./fields.client");
6
- exports.SCHEMA_VERSION = 0;
7
- exports.PARTIES = ['student', 'student-guardian', 'university', 'entity'];
8
- exports.SOURCES = ['auto', 'prefill', 'derived', 'manual'];
9
- exports.FIELD_TYPES = ['text', 'signature'];
10
- class FormMetadata {
11
- formMetadata;
12
- fields;
13
- constructor(formMetadata) {
14
- this.formMetadata = formMetadata;
15
- this.fields = formMetadata.schema;
16
- }
17
- encodeAsJSON() {
18
- return JSON.stringify(this.formMetadata);
19
- }
20
- static decodeFromJSON(json) {
21
- return JSON.parse(json);
22
- }
23
- getLabel() {
24
- return this.formMetadata.label;
25
- }
26
- getFieldForClient(index, sourceDomains, derivationBase) {
27
- const field = this.fields[index];
28
- if (!field)
29
- throw new Error('Field does not exist');
30
- const finalParams = {
31
- ...derivationBase,
32
- ...sourceDomains,
33
- ...this.formMetadata.params,
34
- };
35
- const validator = this.parseValidator(field.field, finalParams);
36
- const prefiller = this.parsePrefiller(field.field, finalParams);
37
- const section = field.field.split('.')[0];
38
- let options = undefined;
39
- if (validator?.type === 'enum')
40
- options = validator.options;
41
- if (validator?.type === 'array') {
42
- const element = validator.element;
43
- if (element?.options)
44
- options = element.options;
45
- }
46
- const type = field.type === 'signature'
47
- ? 'signature'
48
- : validator
49
- ? (0, fields_client_1.getSchemaClientType)(validator)
50
- : 'text';
51
- const coerce = (value) => {
52
- switch (type) {
53
- case 'number': {
54
- const n = parseInt(value);
55
- return isNaN(n) ? 0 : n;
56
- }
57
- case 'date': {
58
- const n = parseInt(value);
59
- return isNaN(n) ? new Date() : new Date(n);
60
- }
61
- case 'checkbox': {
62
- return !!value?.trim();
63
- }
64
- case 'multiselect': {
65
- return value?.split('\n');
66
- }
67
- default:
68
- return value;
69
- }
70
- };
71
- return {
72
- field: field.field,
73
- shared: field.shared,
74
- label: field.label,
75
- source: field.source,
76
- party: field.party,
77
- tooltip_label: field.tooltip_label,
78
- descriptor: field.descriptor,
79
- coerce,
80
- type,
81
- section,
82
- validator,
83
- prefiller,
84
- options,
85
- };
86
- }
87
- getFieldsForClient(sourceDomains, derivationBase) {
88
- return this.fields
89
- .map((field, i) => this.getFieldForClient(i, sourceDomains, derivationBase))
90
- .filter((field) => field !== null);
91
- }
92
- getFieldsForServer() {
93
- return this.fields
94
- .map((field) => ({
95
- field: field.field,
96
- type: field.type,
97
- x: field.x,
98
- y: field.y,
99
- w: field.w,
100
- h: field.h,
101
- align_h: field.align_h ?? 'center',
102
- align_v: field.align_v ?? 'bottom',
103
- page: field.page,
104
- }))
105
- .filter((field) => field !== null);
106
- }
107
- getPhantomFields() {
108
- return this.formMetadata.schema_phantoms ?? [];
109
- }
110
- inferParams() {
111
- const inferenceRegex = /params\[\s*(['"])(.*?)\1\s*\]/g;
112
- const params = [];
113
- let match;
114
- for (const field of this.formMetadata.schema) {
115
- while ((match = inferenceRegex.exec(field.validator)) !== null)
116
- if (!params.includes(match[2]))
117
- params.push(match[2]);
118
- while ((match = inferenceRegex.exec(field.prefiller)) !== null)
119
- if (!params.includes(match[2]))
120
- params.push(match[2]);
121
- }
122
- for (const field of this.formMetadata.schema_phantoms ?? []) {
123
- while ((match = inferenceRegex.exec(field.validator)) !== null)
124
- if (!params.includes(match[2]))
125
- params.push(match[2]);
126
- while ((match = inferenceRegex.exec(field.prefiller)) !== null)
127
- if (!params.includes(match[2]))
128
- params.push(match[2]);
129
- }
130
- return params;
131
- }
132
- getParams() {
133
- return this.formMetadata.params ?? {};
134
- }
135
- getDescriptors() {
136
- return this.fields.filter((f) => f.descriptor);
137
- }
138
- getSignatoryAsValues() {
139
- const signatories = this.formMetadata.signatories;
140
- const values = {};
141
- for (const signatory of signatories)
142
- values[signatory.field] = signatory.name;
143
- return values;
144
- }
145
- parseValidator(fieldname, params = {}) {
146
- const field = this.fields.find((f) => f.field === fieldname);
147
- if (!field)
148
- return null;
149
- const d = new Date();
150
- const defaults = {
151
- currentDate: d,
152
- currentDateTimestamp: d.getTime(),
153
- };
154
- const internalParams = this.formMetadata.params ?? {};
155
- const validator = this.populateParams(field.validator, {
156
- ...params,
157
- ...internalParams,
158
- });
159
- const ret = `return ${validator}`;
160
- const evaluator = new Function('z', 'params', ret);
161
- return evaluator(zod_1.default, { ...params, ...defaults });
162
- }
163
- parsePrefiller(fieldname, params = {}) {
164
- const field = this.fields.find((f) => f.field === fieldname);
165
- if (!field)
166
- return null;
167
- const internalParams = this.formMetadata.params ?? {};
168
- const prefiller = this.populateParams(field.prefiller, {
169
- ...params,
170
- ...internalParams,
171
- });
172
- const ret = `return ${prefiller}`;
173
- const evaluator = new Function('params', ret);
174
- return evaluator(params ?? {});
175
- }
176
- enumerateParams(s) {
177
- const detectedParams = s.match(/#\{[^}]*\}/g) ?? [];
178
- const params = detectedParams.map((dp) => {
179
- const [fieldName, propertyTree] = dp
180
- .replaceAll('#{', '')
181
- .replaceAll('}', '')
182
- .replaceAll(')', '')
183
- .split('(');
184
- const properties = propertyTree?.split('.') ?? [];
185
- return [fieldName, ...properties];
186
- });
187
- return params?.reduce((acc, cur, i) => ((acc[detectedParams[i]] = cur), acc), {});
188
- }
189
- populateParams(s, params) {
190
- const paramsReflection = this.enumerateParams(s);
191
- const paramsValues = {};
192
- for (const match in paramsReflection) {
193
- const path = paramsReflection[match];
194
- paramsValues[match] = path.reduce((acc, cur) => acc?.[cur], params) ?? '';
195
- }
196
- let out = s;
197
- for (const match in paramsValues) {
198
- const replacement = JSON.stringify(paramsValues[match]);
199
- out = out.replaceAll(match, replacement);
200
- }
201
- return out;
202
- }
203
- getSignatories() {
204
- return this.formMetadata.signatories.map((signatory) => ({
205
- name: signatory.name,
206
- title: signatory.title,
207
- email: signatory.email,
208
- honorific: signatory.honorific,
209
- }));
210
- }
211
- getSubscribers() {
212
- return this.formMetadata.subscribers.map((subscriber) => ({
213
- name: subscriber.name,
214
- title: subscriber.title,
215
- email: subscriber.email,
216
- honorific: subscriber.honorific,
217
- }));
218
- }
219
- getRequiredParties() {
220
- return this.formMetadata.required_parties;
221
- }
222
- }
223
- exports.FormMetadata = FormMetadata;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FormMetadata = exports.FIELD_TYPES = exports.SOURCES = exports.PARTIES = exports.SCHEMA_VERSION = void 0;
4
+ const zod_1 = require("zod");
5
+ const fields_client_1 = require("./fields.client");
6
+ exports.SCHEMA_VERSION = 0;
7
+ exports.PARTIES = ['student', 'student-guardian', 'university', 'entity'];
8
+ exports.SOURCES = ['auto', 'prefill', 'derived', 'manual'];
9
+ exports.FIELD_TYPES = ['text', 'signature'];
10
+ class FormMetadata {
11
+ formMetadata;
12
+ fields;
13
+ constructor(formMetadata) {
14
+ this.formMetadata = formMetadata;
15
+ this.fields = formMetadata.schema;
16
+ }
17
+ encodeAsJSON() {
18
+ return JSON.stringify(this.formMetadata);
19
+ }
20
+ static decodeFromJSON(json) {
21
+ return JSON.parse(json);
22
+ }
23
+ getLabel() {
24
+ return this.formMetadata.label;
25
+ }
26
+ getFieldForClient(index, sourceDomains, derivationBase) {
27
+ const field = this.fields[index];
28
+ if (!field)
29
+ throw new Error('Field does not exist');
30
+ const finalParams = {
31
+ ...derivationBase,
32
+ ...sourceDomains,
33
+ ...this.formMetadata.params,
34
+ };
35
+ const validator = this.parseValidator(field.field, finalParams);
36
+ const prefiller = this.parsePrefiller(field.field, finalParams);
37
+ const section = field.field.split('.')[0];
38
+ let options = undefined;
39
+ if (validator?.type === 'enum')
40
+ options = validator.options;
41
+ if (validator?.type === 'array') {
42
+ const element = validator.element;
43
+ if (element?.options)
44
+ options = element.options;
45
+ }
46
+ const type = field.type === 'signature'
47
+ ? 'signature'
48
+ : validator
49
+ ? (0, fields_client_1.getSchemaClientType)(validator)
50
+ : 'text';
51
+ const coerce = (value) => {
52
+ switch (type) {
53
+ case 'number': {
54
+ const n = parseInt(value);
55
+ return isNaN(n) ? 0 : n;
56
+ }
57
+ case 'date': {
58
+ const n = parseInt(value);
59
+ return isNaN(n) ? new Date() : new Date(n);
60
+ }
61
+ case 'checkbox': {
62
+ return !!value?.trim();
63
+ }
64
+ case 'multiselect': {
65
+ return value?.split('\n');
66
+ }
67
+ default:
68
+ return value;
69
+ }
70
+ };
71
+ return {
72
+ field: field.field,
73
+ shared: field.shared,
74
+ label: field.label,
75
+ source: field.source,
76
+ party: field.party,
77
+ tooltip_label: field.tooltip_label,
78
+ descriptor: field.descriptor,
79
+ coerce,
80
+ type,
81
+ section,
82
+ validator,
83
+ prefiller,
84
+ options,
85
+ };
86
+ }
87
+ getFieldsForClient(sourceDomains, derivationBase) {
88
+ return this.fields
89
+ .map((field, i) => this.getFieldForClient(i, sourceDomains, derivationBase))
90
+ .filter((field) => field !== null);
91
+ }
92
+ getFieldsForServer() {
93
+ return this.fields
94
+ .map((field) => ({
95
+ field: field.field,
96
+ type: field.type,
97
+ x: field.x,
98
+ y: field.y,
99
+ w: field.w,
100
+ h: field.h,
101
+ align_h: field.align_h ?? 'center',
102
+ align_v: field.align_v ?? 'bottom',
103
+ page: field.page,
104
+ }))
105
+ .filter((field) => field !== null);
106
+ }
107
+ getPhantomFields() {
108
+ return this.formMetadata.schema_phantoms ?? [];
109
+ }
110
+ inferParams() {
111
+ const inferenceRegex = /params\[\s*(['"])(.*?)\1\s*\]/g;
112
+ const params = [];
113
+ let match;
114
+ for (const field of this.formMetadata.schema) {
115
+ while ((match = inferenceRegex.exec(field.validator)) !== null)
116
+ if (!params.includes(match[2]))
117
+ params.push(match[2]);
118
+ while ((match = inferenceRegex.exec(field.prefiller)) !== null)
119
+ if (!params.includes(match[2]))
120
+ params.push(match[2]);
121
+ }
122
+ for (const field of this.formMetadata.schema_phantoms ?? []) {
123
+ while ((match = inferenceRegex.exec(field.validator)) !== null)
124
+ if (!params.includes(match[2]))
125
+ params.push(match[2]);
126
+ while ((match = inferenceRegex.exec(field.prefiller)) !== null)
127
+ if (!params.includes(match[2]))
128
+ params.push(match[2]);
129
+ }
130
+ return params;
131
+ }
132
+ getParams() {
133
+ return this.formMetadata.params ?? {};
134
+ }
135
+ getDescriptors() {
136
+ return this.fields.filter((f) => f.descriptor);
137
+ }
138
+ getSignatoryAsValues() {
139
+ const signatories = this.formMetadata.signatories;
140
+ const values = {};
141
+ for (const signatory of signatories)
142
+ values[signatory.field] = signatory.name;
143
+ return values;
144
+ }
145
+ parseValidator(fieldname, params = {}) {
146
+ const field = this.fields.find((f) => f.field === fieldname);
147
+ if (!field)
148
+ return null;
149
+ const d = new Date();
150
+ const defaults = {
151
+ currentDate: d,
152
+ currentDateTimestamp: d.getTime(),
153
+ };
154
+ const internalParams = this.formMetadata.params ?? {};
155
+ const validator = this.populateParams(field.validator, {
156
+ ...params,
157
+ ...internalParams,
158
+ });
159
+ const ret = `return ${validator}`;
160
+ const evaluator = new Function('z', 'params', ret);
161
+ return evaluator(zod_1.default, { ...params, ...defaults });
162
+ }
163
+ parsePrefiller(fieldname, params = {}) {
164
+ const field = this.fields.find((f) => f.field === fieldname);
165
+ if (!field)
166
+ return null;
167
+ const internalParams = this.formMetadata.params ?? {};
168
+ const prefiller = this.populateParams(field.prefiller, {
169
+ ...params,
170
+ ...internalParams,
171
+ });
172
+ const ret = `return ${prefiller}`;
173
+ const evaluator = new Function('params', ret);
174
+ return evaluator(params ?? {});
175
+ }
176
+ enumerateParams(s) {
177
+ const detectedParams = s.match(/#\{[^}]*\}/g) ?? [];
178
+ const params = detectedParams.map((dp) => {
179
+ const [fieldName, propertyTree] = dp
180
+ .replaceAll('#{', '')
181
+ .replaceAll('}', '')
182
+ .replaceAll(')', '')
183
+ .split('(');
184
+ const properties = propertyTree?.split('.') ?? [];
185
+ return [fieldName, ...properties];
186
+ });
187
+ return params?.reduce((acc, cur, i) => ((acc[detectedParams[i]] = cur), acc), {});
188
+ }
189
+ populateParams(s, params) {
190
+ const paramsReflection = this.enumerateParams(s);
191
+ const paramsValues = {};
192
+ for (const match in paramsReflection) {
193
+ const path = paramsReflection[match];
194
+ paramsValues[match] = path.reduce((acc, cur) => acc?.[cur], params) ?? '';
195
+ }
196
+ let out = s;
197
+ for (const match in paramsValues) {
198
+ const replacement = JSON.stringify(paramsValues[match]);
199
+ out = out.replaceAll(match, replacement);
200
+ }
201
+ return out;
202
+ }
203
+ getSignatories() {
204
+ return this.formMetadata.signatories.map((signatory) => ({
205
+ name: signatory.name,
206
+ title: signatory.title,
207
+ email: signatory.email,
208
+ honorific: signatory.honorific,
209
+ }));
210
+ }
211
+ getSubscribers() {
212
+ return this.formMetadata.subscribers.map((subscriber) => ({
213
+ name: subscriber.name,
214
+ title: subscriber.title,
215
+ email: subscriber.email,
216
+ honorific: subscriber.honorific,
217
+ }));
218
+ }
219
+ getRequiredParties() {
220
+ return this.formMetadata.required_parties;
221
+ }
222
+ }
223
+ exports.FormMetadata = FormMetadata;
224
224
  //# sourceMappingURL=form-metadata.js.map
@@ -1,2 +1,2 @@
1
- export * from './form-metadata';
2
- export * from './fields.client';
1
+ export * from './form-metadata';
2
+ export * from './fields.client';
@@ -1,19 +1,19 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./form-metadata"), exports);
18
- __exportStar(require("./fields.client"), exports);
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./form-metadata"), exports);
18
+ __exportStar(require("./fields.client"), exports);
19
19
  //# sourceMappingURL=index.js.map