-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
194 lines (152 loc) · 5.29 KB
/
index.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
"use strict";
const GeneratorFunction = (function * gen(){ return gen.constructor; })().next().value;
function template(...data) {
return this.map((part) => {
if (part instanceof GeneratorFunction) {
return [ ...part.call(...data) ].map((bit) => `${bit || ''}`).join('');
}
if (part instanceof Function) {
part = part.call(...data);
}
try { return `${part || ''}`; } catch (e) { return ''; }
}).join('');
}
function walk(path) {
let cursor = this;
for (let s of path) { cursor = (cursor || {})[s]; }
return cursor;
}
function compile(source = '') {
try { source = `${source}` } catch (e) { source = ''; }
let tokens = [], collect = tokens, wants = {};
function want(op, key, count) {
wants[op] = (wants[op] || {});
wants[op][key] = (count === undefined) ? true : ((wants[op][key] || 0) + count);
}
class Immediate extends Function {
constructor(op, body) {
const literal = (l) => JSON.stringify(l);
const field = (reg, f) => (want(reg, f), `(this['${reg}'] || {})[${literal(f)}]`);
function token(t) {
const [ reg ] = t;
return ['$', '_' ].includes(reg) ? field(reg, t.substring(1)) : literal(t);
}
super(`return ${`${op}${body}`.split('|').map(token).join('||')};`);
}
}
class Action extends Function {
constructor(op, body) {
wants[op] = (wants[op] || {}); wants[op][body] = true;
super(`return (this['${op}'] || {})[${JSON.stringify(body)}]`);
}
}
class Tag extends Action {
constructor(op, body) {
super(op, `${op}${body}`);
}
}
const Not = Symbol('Not');
class Tail extends Function {
constructor(op, body) {
if (!body) { collect = tokens; super(`return '';`); return this; }
const [ search, , count ] = body.split(/:(?!(.*:))/);
const [ root, ...path ] = search.split('.');
if (root) {
const max = (count === '0') ? 0 : (parseInt(count) || 1);
want(op, search, max);
const local = []; collect = local;
function each() {
return function * () {
let sel = walk.call(this, [ op, root, ...path]);
if (Array.isArray(sel)) {
let split = Math.min(sel.length, max);
if (split) {
for (let i = 0; i < split; ++i) { yield template.call(local, this, sel.shift()); }
}
else if (local[Not] instanceof Function) { yield local[Not].call(this); }
else { yield ''; }
return;
}
if (sel && (sel.constructor === GeneratorFunction.prototype)) {
let count;
for (count = 0; count < max; ++count) {
let w = sel.next(); if (w.done) { break; }
yield template.call(local, this, w.value);
}
if (!count) {
if (local[Not] instanceof Function) { yield local[Not].call(this); }
else { yield ''; }
}
return;
}
if (!sel) {
if (local[Not] instanceof Function) { yield local[Not].call(this); }
return;
}
yield template.call(local, sel, sel);
};
}
super('return this();');
return this.call(each);
} // !root
if (path.length) {
function get() {
return (item) => walk.call(item, path);
}
super('return this();');
return this.call(get);
}
const local = []; collect = local;
function repeat() {
const times = (count === '0') ? 0 : (parseInt(count) || 1);
return function * () {
for (let i = 0; i < times; ++i) {
yield template.call(local);
}
};
}
super('return this();');
return this.call(repeat);
}
}
class Else extends Function {
constructor() {
const local = [], target = collect; collect = local;
function not() {
return function () {
return template.call(local, this);
};
}
super('return this();');
target[Not] = this.call(not);
return () => '';
}
}
const scan = /\[(\$|_|#|~|:|%|\/|@|!|&)(.*?)\]/g;
let last = 0;
for (let found = scan.exec(source); found; found = scan.exec(source)) {
const [ Tags, op, body ] = found;
let target = collect, token;
switch (op) {
case '$' : token = new Immediate(op, body); break;
case '_' : token = new Immediate(op, body); break;
case '#' : token = new Action(op, body); break;
case '~' : token = new Action(op, body); break;
case ':' : token = new Tag(op, body); break;
case '%' : token = new Tag(op, body); break;
case '/' : token = new Tag(op, body); break;
case '@' : token = new Tail(op, body); break;
case '&' : token = new Tail(op, body); break;
case '!' : token = new Else(op, body); break;
}
target.push(source.substring(last, scan.lastIndex - Tags.length));
target.push(token);
last = scan.lastIndex;
}
collect.push(source.substring(last));
return Object.assign(template.bind(tokens), { wants });
}
compile.consumable = function consumable(value) {
return Array.isArray(value) || (!!value && value.constructor === GeneratorFunction.prototype);
}
module.exports = compile;