-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
80 lines (80 loc) · 2.86 KB
/
init.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
export async function init(self, props, newTarget, controller, passedIn, ifWantsToBe) {
const { actions, proxyPropDefaults, primaryProp, parseAndCamelize } = props;
controller.propChangeQueue = new Set();
const objToAssign = proxyPropDefaults !== undefined ? { ...proxyPropDefaults } : {};
const { getAttrInfo } = await import('./upgrade.js');
const attr = getAttrInfo(newTarget, ifWantsToBe, true);
let parsedObj;
let json;
if (attr !== null && attr.length !== 0 && attr[0].length !== 0) {
json = attr[0].trim();
if (typeof Sanitizer !== 'undefined') {
const sanitizer = new Sanitizer();
if (sanitizer.sanitizeFor !== undefined) {
json = sanitizer.sanitizeFor('template', json).innerHTML;
}
}
const firstChar = json[0];
if (firstChar === '{' || firstChar === '[') {
try {
if (parseAndCamelize) {
const { parseAndCamelize } = await import('./parseAndCamelize.js');
parsedObj = parseAndCamelize(json);
}
else {
parsedObj = JSON.parse(json);
}
}
catch (e) {
console.error(e);
}
}
else {
if (parseAndCamelize) {
const { camelize } = await import('./camelize.js');
parsedObj = camelize(json);
}
}
}
const proxy = controller.proxy;
if (primaryProp !== undefined) {
if (parsedObj === undefined) {
if (json)
objToAssign[primaryProp] = json;
}
else {
const { primaryPropReq } = props;
if (Array.isArray(parsedObj) || (primaryPropReq && parsedObj[primaryProp] === undefined)) {
objToAssign[primaryProp] = parsedObj;
}
else {
Object.assign(objToAssign, parsedObj);
}
}
if (attr !== null && primaryProp && parseAndCamelize) {
const { camelizeOptions } = props;
if (camelizeOptions !== undefined) {
const { camelPlus } = await import('./camelPlus.js');
await camelPlus(objToAssign, camelizeOptions, primaryProp, props);
}
}
}
else {
if (parsedObj !== undefined) {
Object.assign(objToAssign, parsedObj);
}
}
if (passedIn !== undefined) {
Object.assign(objToAssign, passedIn);
}
Object.assign(controller.proxy, objToAssign);
}
export function getPropsFromActions(action) {
return typeof (action) === 'string' ? new Set([action]) : new Set([
...(action.ifAllOf || []),
...(action.ifKeyIn || []),
...(action.ifNoneOf || []),
...(action.ifEquals || []),
...(action.ifAtLeastOneOf || [])
]);
}