-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzenPolymer1.js
158 lines (158 loc) · 5.26 KB
/
zenPolymer1.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
"use strict";
const xyR = /[xy]/g;
function guid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(xyR, c => {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
function replaceGUIDsWithPolymerSelector(s, objMapping, basePath = '') {
switch (typeof s) {
case 'string':
let returnS = s;
const lu = objMapping.uidToPathLookup;
for (const key in lu) {
const path = lu[key];
//returnS = returnS.replace(key, `[[${path + (path ? '.' : '') + name}]]`);
returnS = returnS.replace(key, `[[${basePath + path}]]`);
}
return returnS;
case 'object':
if (Array.isArray(s)) {
return s.map(part => replaceGUIDsWithPolymerSelector(part, objMapping, 'item.'));
}
throw "Not Implemented";
}
}
function extractPathFromFunction(s) {
const returnSplit = s.split('return ', 2); //ES5
if (returnSplit.length > 1) {
const rhs = returnSplit[1];
const reg = /[;}\s]$/g;
const words = rhs.replace(/[\;\s}]/g, '');
return substringAfter(words, '.');
}
const arrowSplit = s.split('=>');
const lhs = arrowSplit[0].trim();
const rhs = arrowSplit[1].replace(lhs + '.', '').replace(';', '').trim();
return rhs;
}
function substringBefore(s, search) {
const iPos = s.indexOf(search);
if (iPos > -1)
return s.substr(0, iPos);
return s;
}
function substringAfter(s, search) {
const iPos = s.indexOf(search);
if (iPos === -1)
return '';
if (iPos === s.length - 1)
return '';
return s.substr(iPos + 1);
}
function zenToPolymer1(zen, obj, path = '') {
const objectMapping = mapObject(obj);
for (let i = 0, ii = zen.length; i < ii; i++) {
const word = zen[i];
switch (typeof word) {
case 'function':
const evalledFunction = word(objectMapping.uidObject);
const polymerExpr = replaceGUIDsWithPolymerSelector(evalledFunction, objectMapping, path);
zen[i] = polymerExpr;
break;
case 'object':
const loop = word['➰'];
const action = word['🎬'];
if (!loop || !action)
throw "Not Implemented";
const outputArr = [];
const repeatSelector = extractPathFromFunction(loop.toString());
outputArr.push(`<template is="dom-repeat" items="{{${repeatSelector}}}">`);
const loopVal = loop(obj);
const firstItem = loopVal[0];
const actionSeq = [action];
zenToPolymer1(actionSeq, firstItem, "item");
//const zen1 = action(firstItem);
//zenToPolymer1(zen1, firstItem);
for (const child of actionSeq) {
outputArr.push(child);
}
outputArr.push('</template>');
//zen[i] = outputArr.join('');
zen[i] = outputArr;
break;
}
}
}
exports.zenToPolymer1 = zenToPolymer1;
function flattenArray(arr, cumm = []) {
for (const el of arr) {
switch (typeof el) {
case 'string':
cumm.push(el);
break;
case 'object':
if (Array.isArray(el)) {
flattenArray(el, cumm);
}
else {
throw "Not Implemented";
}
}
}
return cumm;
}
exports.flattenArray = flattenArray;
function mapObject(obj) {
const returnObj = {
uidToPathLookup: {},
uidObject: {}
};
const names = Object.getOwnPropertyNames(obj);
for (const name of names) {
const uid = guid();
returnObj.uidToPathLookup[uid] = name;
returnObj.uidObject[name] = uid;
}
return returnObj;
}
function toPolymerElement(obj) {
const properties = [];
const names = Object.getOwnPropertyNames(obj);
const outArr = ['Polymer({'];
//#region Inside of Polymer
let foundProperty = false;
const observingMethods = {};
for (const name of names) {
const prop = obj[name];
if (!foundProperty) {
outArr.push("properties:{");
foundProperty = true;
}
outArr.push(name + ':{');
const typ = prop.type ? prop.type.name : 'String';
outArr.push('type: ' + typ);
if (prop.setter) {
const observerName = '_' + name + '_change';
observingMethods[observerName] = prop.setter;
outArr.push(`observer:'${observerName}'`);
}
outArr.push('},');
}
if (foundProperty)
outArr.push("},");
for (const method in observingMethods) {
const setterString = observingMethods[method].toString();
const splitSetter = setterString.split('=>');
let lhs = splitSetter[0].trim();
lhs = lhs.replace(', _this', '');
outArr.push(method + `: function${lhs}`);
outArr.push(splitSetter[1].replace('_this', 'this'));
}
//#endregion
outArr.push('})');
return outArr;
}
exports.toPolymerElement = toPolymerElement;
//# sourceMappingURL=zenPolymer1.js.map