forked from teslamotors/informed
/
utils.js
454 lines (393 loc) · 11.4 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import ObjectMap from './ObjectMap';
export const getChildDisplayName = WrappedComponent => {
// fix for "memo" components
if (WrappedComponent.type && WrappedComponent.type.name) {
return WrappedComponent.type.name;
}
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
};
export const yupToFormErrors = yupError => {
const errors = {};
if (yupError.inner) {
if (yupError.inner.length === 0) {
// console.log(yupError.path);
ObjectMap.set(errors, yupError.path, yupError.message);
return;
}
for (let err of yupError.inner) {
if (!ObjectMap.get(errors, err.path)) {
// console.log(errors, err.path, err.message);
ObjectMap.set(errors, err.path, err.message);
}
}
}
return errors;
};
export const validateYupSchema = (schema, values) => {
try {
schema.validateSync(values, { abortEarly: false });
} catch (e) {
const formErrors = yupToFormErrors(e);
return formErrors;
}
};
export const yupToFormError = yupError => {
if (yupError.inner) {
if (yupError.inner.length === 0) {
return;
}
const err = yupError.inner[0];
return err.message;
}
};
export const validateYupField = (schema, value) => {
try {
schema.validateSync(value, { abortEarly: false });
} catch (e) {
return yupToFormError(e);
}
};
export const validateAjvSchema = (validate, data) => {
validate(data);
const errors = {};
if (validate.errors) {
validate.errors.forEach(({ message, dataPath, keyword, params }) => {
let path = dataPath;
// Special case for required
if (keyword === 'required') {
path = `${path}.${params.missingProperty}`;
}
// Get rid of leading dot
path = path.replace('.', '');
// console.log('PATH', path, message);
// TODO get message from informed if present
ObjectMap.set(errors, path, message);
});
}
return errors;
};
// https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
export const uuidv4 = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (Math.random() * 16) | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
};
export const debounce = (func, wait) => {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
};
export const computeFieldFromProperty = (propertyName, property, prefix) => {
// Special case for relevant fields
// Example 'conditional:over21'
if (/conditional:.*/.test(propertyName)) {
return {
componentType: 'relevantFields',
// Example 'conditional:over21' ---> over21
relevant: propertyName.replace(/conditional:(.*)/, '$1')
};
}
// Special case for component fields
// Example 'component:marriedLabel'
if (/component:.*/.test(propertyName)) {
const { 'ui:control': uiControl, ...props } = property;
return {
componentType: uiControl,
// Not needed as this is not an informed field
field: undefined,
props
};
}
const {
'ui:control': uiControl,
'informed:props': informedProps,
'input:props': inputProps,
'ui:before': uiBefore,
'ui:after': uiAfter,
oneOf,
items,
enum: schemaEnum,
title: label,
minimum: min,
maximum: max,
minLength,
maxLength,
pattern,
type,
properties: subProperties
} = property;
// Set Id if not passed
let id = uuidv4();
if (inputProps && inputProps.id) {
id = inputProps.id;
}
const field = {
componentType: uiControl,
field: prefix ? `${prefix}.${propertyName}` : propertyName,
type,
uiBefore,
uiAfter,
properties: type === 'object' ? subProperties : undefined,
items: type === 'array' ? items : undefined,
props: {
label: label,
id,
min,
max,
minLength,
maxLength,
pattern,
...informedProps,
...inputProps
}
};
if (oneOf) {
const options = property.oneOf.map(option => {
const { 'input:props': inputProps = {} } = option;
return {
value: option.const,
label: option.title,
...inputProps
};
});
field.props.options = options;
}
if (schemaEnum) {
const options = property.enum.map(val => {
return {
value: val,
label: val
};
});
field.props.options = options;
}
if (items && items.oneOf) {
const options = items.oneOf.map(option => {
const { 'input:props': inputProps = {} } = option;
return {
value: option.const,
label: option.title,
...inputProps
};
});
field.props.options = options;
}
return field;
};
export const computeFieldsFromSchema = (schema, onlyValidateSchema, prefix) => {
if (!schema || onlyValidateSchema) {
return [];
}
// Grab properties and items off of schema
const { properties = {}, allOf, propertyOrder = [] } = schema;
if (Object.keys(properties).length > 0) {
// Attempt to generate fields from properties
const fields = Object.keys(properties)
.sort((a, b) => {
const aIndex = propertyOrder.indexOf(a);
const bIndex = propertyOrder.indexOf(b);
return (
(aIndex > -1 ? aIndex : propertyOrder.length + 1) -
(bIndex > -1 ? bIndex : propertyOrder.length + 1)
);
})
.map(propertyName => {
const property = properties[propertyName];
const field = computeFieldFromProperty(propertyName, property, prefix);
return field;
});
// Check for all of ( we have conditionals )
if (allOf) {
// Only do this if the user did not chose to control location
if (!allOf[0].relevant) {
fields.push({
componentType: 'conditionals',
// Each element of the "allOf" array is a conditional
allOf: allOf
});
}
}
return fields;
}
return [];
};
// Examples
// field = "name" ---> properties.name
// field = "brother.name" ---> properties.brother.properties.name
// field = "brother.siblings[1].friend.name" ---> properties.brother.properties.siblings.items[1].properties.friend.properties.name
export const getSchemaPathFromJsonPath = jsonPath => {
// Convert
let schemaPath = jsonPath
.replace(/\./g, '.properties.')
.replace(/\[/g, '.itmes[');
// Add first properties
schemaPath = `properties.${schemaPath}`;
return schemaPath;
};
/* -------------------------- Formatter ----------------------------- */
const getFormatter = (formatter, value) => {
// If mask is a string turn it into an array;
if (typeof formatter === 'string') {
return formatter.split('').map(char => {
if (char === '#') {
return /\d/;
}
if (char === '*') {
return /[\w]/;
}
return char;
});
}
// If mask is a function use it to genreate current mask
if (typeof formatter === 'function') {
return formatter(value);
}
if (Array.isArray(formatter)) {
return formatter;
}
// Should never make it here throw
throw new Error('Formatter must be string, array, or function');
};
const matchingIndex = (a, b) => {
let i = 0;
let mi = -1;
let matching = true;
// a = "+1 "
// b = "+12"
while (matching && i < a.length) {
if (a[i] == b[i]) {
mi = i;
} else {
matching = false;
}
i = i + 1;
}
return mi;
};
export const informedFormat = (value, frmtr) => {
// console.log('Formatting', value);
// Null check
if (!value) {
return {
value,
offset: 0
};
}
// Generate formatter array
const formatter = getFormatter(frmtr, value);
// Start to fill in the array
// Example: phone formatter
// formatter =['+', '1', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]
// value examples:
// "1231231234 ----> +1 123-123-1234
// "+" ----> +
// "+1" ----> +1
// "+2" ----> +1 2
// "1" ----> +1 1
// "1234" ----> +1 123-4
// "123a" ----> +1 123
// Determine prefix length and suffix start
const prefixLength = formatter.findIndex(v => typeof v != 'string');
const suffixStart =
formatter.length -
[...formatter].reverse().findIndex(v => typeof v != 'string');
// Formatted value
let formatted = [];
// The characters from the current value
const chars = value.split('');
// To track the value index during itteration
let vIndex = 0;
let start = 0;
// If the value matches part of the prefix take it out
// Example prefix = "+1 " value = ["+1 123-123-1234", "+12", "+2"]
const matchIndex = matchingIndex(
formatter.slice(0, prefixLength),
chars.slice(0, prefixLength)
);
// console.log('Matching index', matchIndex);
if (matchIndex > -1) {
//vIndex = prefixLength;
vIndex = matchIndex + 1;
formatted = formatted.concat(formatter.slice(0, matchIndex + 1));
start = matchIndex + 1;
}
// Example prefix = "+1 " value=["1", "1234"]
if (matchIndex < 0) {
// Start past the prefix
formatted = formatted.concat(formatter.slice(0, prefixLength));
start = prefixLength;
}
// console.log('start', start, formatted);
// console.log('PREFIX_LENGTHT', prefixLength);
// console.log('SUFIX_START', suffixStart);
// console.log('FORMATTER_LENGTH', formatter.length);
// To track if we have made it past the prefix
let pastPrefix = false;
// Fill in the stuff
for (let i = start; i < formatter.length; i++) {
// Get current formatter location matcher
const matcher = formatter[i];
// We get past the prefix if matcher is not a string
if (!pastPrefix && typeof matcher != 'string') {
pastPrefix = true;
}
// Chec to see if there is more value to look at
if (vIndex != chars.length) {
// Get the current value character
const curChar = chars[vIndex];
// If type is string normal compare otherwise regex compare
const match =
typeof matcher === 'string'
? matcher === curChar
: matcher.test(curChar);
// If the current character of the value matches and matcher is a string
// "1" === "1"
if (match && typeof matcher === 'string') {
formatted.push(matcher);
//if( pastPrefix ){
vIndex = vIndex + 1;
//}
}
// If the current character does not match and matcher is a stirng
// "1" != "+"
else if (!match && typeof matcher === 'string') {
// Special check for 123a ---> dont want "+1 123-"
// Special check for 1234 ---> DO want "+1 123-4"
if (vIndex != chars.length) formatted.push(matcher);
}
// If the current character matches and the matcher is not a string
// /\d/.test("2")
else if (match && typeof matcher != 'string') {
formatted.push(curChar);
vIndex = vIndex + 1;
}
// If the current character does NOT match and the matecer is regex
// /\d/.test("a")
else if (!match && typeof matcher != 'string') {
// Throw out this value
vIndex = vIndex + 1;
i = i - 1;
}
} else {
// If mattcher is a string and we are at suffix keep going
if (typeof matcher === 'string' && i >= suffixStart) {
formatted.push(matcher);
} else {
// Otherwise we want to break out
break;
}
}
}
return {
value: formatted.join(''),
offset: value ? formatted.length - value.length : 0
};
};