-
Notifications
You must be signed in to change notification settings - Fork 533
/
Copy pathschema.js
98 lines (87 loc) · 2.71 KB
/
schema.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
import { get, set, each, isObject, isArray, isFunction, cloneDeep } from "lodash";
// Create a new model by schema default values
module.exports.createDefaultObject = function (schema, obj = {}) {
each(schema.fields, (field) => {
if (get(obj, field.model) === undefined && field.default !== undefined) {
if (isFunction(field.default)) {
set(obj, field.model, field.default(field, schema, obj));
} else if (isObject(field.default) || isArray(field.default)) {
set(obj, field.model, cloneDeep(field.default));
} else
set(obj, field.model, field.default);
}
});
return obj;
};
// Get a new model which contains only properties of multi-edit fields
module.exports.getMultipleFields = function (schema) {
let res = [];
each(schema.fields, (field) => {
if (field.multi === true)
res.push(field);
});
return res;
};
// Merge many models to one 'work model' by schema
module.exports.mergeMultiObjectFields = function (schema, objs) {
let model = {};
let fields = module.exports.getMultipleFields(schema);
each(fields, (field) => {
let mergedValue = undefined;
let notSet = true;
let path = field.model;
each(objs, (obj) => {
let v = get(obj, path);
if (notSet) {
mergedValue = v;
notSet = false;
}
else if (mergedValue != v) {
mergedValue = undefined;
}
});
set(model, path, mergedValue);
});
return model;
};
module.exports.slugifyFormID = function (schema, prefix = "") {
// Try to get a reasonable default id from the schema,
// then slugify it.
if (typeof schema.id !== "undefined") {
// If an ID's been explicitly set, use it unchanged
return prefix + schema.id;
} else {
// Return the slugified version of either:
return prefix + (schema.inputName || schema.label || schema.model || "")
// NB: This is a very simple, conservative, slugify function,
// avoiding extra dependencies.
.toString()
.trim()
.toLowerCase()
// Spaces & underscores to dashes
.replace(/ |_/g, "-")
// Multiple dashes to one
.replace(/-{2,}/g, "-")
// Remove leading & trailing dashes
.replace(/^-+|-+$/g, "")
// Remove anything that isn't a (English/ASCII) letter, number or dash.
.replace(/([^a-zA-Z0-9-]+)/g, "");
}
};
module.exports.slugify = function (name = "") {
// Return the slugified version of either:
return name
// NB: This is a very simple, conservative, slugify function,
// avoiding extra dependencies.
.toString()
.trim()
//.toLowerCase()
// Spaces & underscores to dashes
.replace(/ /g, "-")
// Multiple dashes to one
.replace(/-{2,}/g, "-")
// Remove leading & trailing dashes
.replace(/^-+|-+$/g, "")
// Remove anything that isn't a (English/ASCII) letter, number or dash.
.replace(/([^a-zA-Z0-9-_/./:]+)/g, "");
};