UNPKG

2.11 kBJavaScriptView Raw
1var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria
2 ? {
3 ...errors[name],
4 types: {
5 ...(errors[name] && errors[name].types ? errors[name].types : {}),
6 [type]: message || true,
7 },
8 }
9 : {};
10
11var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
12
13var isNullOrUndefined = (value) => value == null;
14
15var isDateObject = (value) => value instanceof Date;
16
17const isObjectType = (value) => typeof value === 'object';
18var isObject = (value) => !isNullOrUndefined(value) &&
19 !Array.isArray(value) &&
20 isObjectType(value) &&
21 !isDateObject(value);
22
23var isUndefined = (val) => val === undefined;
24
25var get = (object, path, defaultValue) => {
26 if (!path || !isObject(object)) {
27 return defaultValue;
28 }
29 const result = compact(path.split(/[,[\].]+?/)).reduce((result, key) => isNullOrUndefined(result) ? result : result[key], object);
30 return isUndefined(result) || result === object
31 ? isUndefined(object[path])
32 ? defaultValue
33 : object[path]
34 : result;
35};
36
37var isKey = (value) => /^\w*$/.test(value);
38
39var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
40
41var set = (object, path, value) => {
42 let index = -1;
43 const tempPath = isKey(path) ? [path] : stringToPath(path);
44 const length = tempPath.length;
45 const lastIndex = length - 1;
46 while (++index < length) {
47 const key = tempPath[index];
48 let newValue = value;
49 if (index !== lastIndex) {
50 const objValue = object[key];
51 newValue =
52 isObject(objValue) || Array.isArray(objValue)
53 ? objValue
54 : !isNaN(+tempPath[index + 1])
55 ? []
56 : {};
57 }
58 if (key === '__proto__') {
59 return;
60 }
61 object[key] = newValue;
62 object = object[key];
63 }
64 return object;
65};
66
67export { appendErrors, get, set };
68//# sourceMappingURL=react-server.esm.mjs.map