Skip to content

Commit 1542983

Browse files
committed
refactor(all): use es6 modules
1 parent c5a22fd commit 1542983

File tree

12 files changed

+264
-92
lines changed

12 files changed

+264
-92
lines changed

dist/compiler.js

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, '__esModule', { value: true });
4+
5+
const filters = new Map();
6+
const limiters = new Map();
7+
8+
function filter (name, handler) {
9+
if (typeof name !== 'string') {
10+
throw new TypeError('First argument must be a string.')
11+
}
12+
if (typeof handler !== 'function') {
13+
throw new TypeError('Second argument must be a function.')
14+
}
15+
if (filters.has(name)) {
16+
throw new Error(`A filter named ${name} is already registered.`)
17+
}
18+
filters.set(name, handler);
19+
return this
20+
}
21+
22+
function limiter (name, handler) {
23+
if (typeof name !== 'string') {
24+
throw new TypeError('First argument must be a string.')
25+
}
26+
if (typeof handler !== 'function') {
27+
throw new TypeError('Second argument must be a function.')
28+
}
29+
if (limiters.has(name)) {
30+
throw new Error(`A limiter named ${name} is already registered.`)
31+
}
32+
limiters.set(name, handler);
33+
return this
34+
}
35+
36+
function compileRawExpression (src) {
37+
return new Function('context', 'tempVars', // eslint-disable-line
38+
`const sandbox = $nxCompileToSandbox(context, tempVars)
39+
try { with (sandbox) { return ${src} } } catch (err) {
40+
if (!(err instanceof TypeError)) throw err
41+
}
42+
$nxClearSandbox()`)
43+
}
44+
45+
function compileRawCode (src) {
46+
return new Function('context', 'tempVars', // eslint-disable-line
47+
`const sandbox = $nxCompileToSandbox(context, tempVars)
48+
with (sandbox) { ${src} }
49+
$nxClearSandbox()`)
50+
}
51+
52+
const filterRegex = /(?:[^\|]|\|\|)+/g;
53+
const limiterRegex = /(?:[^&]|&&)+/g;
54+
const argsRegex = /\S+/g;
55+
56+
function parseExpression (src) {
57+
const tokens = src.match(filterRegex);
58+
if (tokens.length === 1) {
59+
return compileRawExpression(tokens[0])
60+
}
61+
62+
const expression = {
63+
exec: compileRawExpression(tokens[0]),
64+
filters: []
65+
};
66+
for (let i = 1; i < tokens.length; i++) {
67+
let filterTokens = tokens[i].match(argsRegex) || [];
68+
const filterName = filterTokens.shift();
69+
const effect = filters.get(filterName);
70+
if (!effect) {
71+
throw new Error(`There is no filter named: ${filterName}.`)
72+
}
73+
expression.filters.push({effect, argExpressions: filterTokens.map(compileRawExpression)});
74+
}
75+
return expression
76+
}
77+
78+
function parseCode (src) {
79+
const tokens = src.match(limiterRegex);
80+
if (tokens.length === 1) {
81+
return compileRawCode(tokens[0])
82+
}
83+
84+
const code = {
85+
exec: compileRawCode(tokens[0]),
86+
limiters: []
87+
};
88+
for (let i = 1; i < tokens.length; i++) {
89+
const limiterTokens = tokens[i].match(argsRegex) || [];
90+
const limiterName = limiterTokens.shift();
91+
const effect = limiters.get(limiterName);
92+
if (!effect) {
93+
throw new Error(`There is no limiter named: ${limiterName}.`)
94+
}
95+
code.limiters.push({effect, argExpressions: limiterTokens.map(compileRawExpression)});
96+
}
97+
return code
98+
}
99+
100+
const expressionCache = new Map();
101+
const codeCache = new Map();
102+
103+
function compileExpression (src) {
104+
if (typeof src !== 'string') {
105+
throw new TypeError('First argument must be a string.')
106+
}
107+
let expression = expressionCache.get(src);
108+
if (!expression) {
109+
expression = parseExpression(src);
110+
expressionCache.set(src, expression);
111+
}
112+
113+
if (typeof expression === 'function') {
114+
return expression
115+
}
116+
117+
return function evaluateExpression (context, tempVars) {
118+
let value = expression.exec(context, tempVars);
119+
for (let filter of expression.filters) {
120+
const args = filter.argExpressions.map(evaluateArgExpression, context);
121+
value = filter.effect(value, ...args);
122+
}
123+
return value
124+
}
125+
}
126+
127+
function compileCode (src) {
128+
if (typeof src !== 'string') {
129+
throw new TypeError('First argument must be a string.')
130+
}
131+
let code = codeCache.get(src);
132+
if (!code) {
133+
code = parseCode(src);
134+
codeCache.set(src, code);
135+
}
136+
137+
if (typeof code === 'function') {
138+
return code
139+
}
140+
141+
const context = {};
142+
return function evaluateCode (state, tempVars) {
143+
let i = 0;
144+
function next () {
145+
Object.assign(context, tempVars);
146+
if (i < code.limiters.length) {
147+
const limiter = code.limiters[i++];
148+
const args = limiter.argExpressions.map(evaluateArgExpression, state);
149+
limiter.effect(next, context, ...args);
150+
} else {
151+
code.exec(state, tempVars);
152+
}
153+
}
154+
next();
155+
}
156+
}
157+
158+
function evaluateArgExpression (argExpression) {
159+
return argExpression(this)
160+
}
161+
162+
const hasHandler = { has };
163+
const allHandlers = { has, get };
164+
const globals = new Set();
165+
let temp;
166+
167+
let globalObj;
168+
if (typeof window !== 'undefined') globalObj = window; // eslint-disable-line
169+
else if (typeof global !== 'undefined') globalObj = global; // eslint-disable-line
170+
else if (typeof self !== 'undefined') globalObj = self; // eslint-disable-line
171+
globalObj.$nxCompileToSandbox = toSandbox;
172+
globalObj.$nxClearSandbox = clearSandbox;
173+
174+
function expose (...globalNames) {
175+
for (let globalName of globalNames) {
176+
globals.add(globalName);
177+
}
178+
return this
179+
}
180+
181+
function hide (...globalNames) {
182+
for (let globalName of globalNames) {
183+
globals.delete(globalName);
184+
}
185+
return this
186+
}
187+
188+
function hideAll () {
189+
globals.clear();
190+
return this
191+
}
192+
193+
function has (target, key) {
194+
return globals.has(key) ? (key in target) : true
195+
}
196+
197+
function get (target, key) {
198+
return key in temp ? temp[key] : target[key]
199+
}
200+
201+
function toSandbox (obj, tempVars) {
202+
if (tempVars) {
203+
temp = tempVars;
204+
return new Proxy(obj, allHandlers)
205+
}
206+
return new Proxy(obj, hasHandler)
207+
}
208+
209+
function clearSandbox () {
210+
temp = undefined;
211+
}
212+
213+
exports.compileExpression = compileExpression;
214+
exports.compileCode = compileCode;
215+
exports.compileRawExpression = compileRawExpression;
216+
exports.compileRawCode = compileRawCode;
217+
exports.expose = expose;
218+
exports.hide = hide;
219+
exports.hideAll = hideAll;
220+
exports.filters = filters;
221+
exports.limiters = limiters;
222+
exports.filter = filter;
223+
exports.limiter = limiter;

index.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
"name": "@nx-js/compiler-util",
33
"version": "1.0.0",
44
"description": "An NX util, responsible for executing code in the context of an object.",
5-
"main": "index.js",
5+
"main": "dist/compiler.js",
6+
"module": "src/index.js",
67
"scripts": {
7-
"test": "mocha test",
8-
"lint": "standard"
8+
"build": "rollup src/index.js -f cjs -o dist/compiler.js",
9+
"test": "npm run build && mocha test",
10+
"lint": "standard src/* test/*"
911
},
1012
"author": {
1113
"name": "Miklos Bertalan",
@@ -32,8 +34,9 @@
3234
"devDependencies": {
3335
"chai": "3.5.0",
3436
"mocha": "2.5.3",
35-
"standard": "7.1.2",
36-
"pre-push": "0.1.1"
37+
"pre-push": "0.1.1",
38+
"rollup": "^0.41.6",
39+
"standard": "7.1.2"
3740
},
3841
"engines": {
3942
"node": ">=6.0.0"

src/compiler.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
'use strict'
2-
3-
const parser = require('./parser')
1+
import { parseExpression, parseCode } from './parser'
42

53
const expressionCache = new Map()
64
const codeCache = new Map()
75

8-
module.exports = {
9-
compileExpression,
10-
compileCode
11-
}
12-
13-
function compileExpression (src) {
6+
export function compileExpression (src) {
147
if (typeof src !== 'string') {
158
throw new TypeError('First argument must be a string.')
169
}
1710
let expression = expressionCache.get(src)
1811
if (!expression) {
19-
expression = parser.parseExpression(src)
12+
expression = parseExpression(src)
2013
expressionCache.set(src, expression)
2114
}
2215

@@ -34,13 +27,13 @@ function compileExpression (src) {
3427
}
3528
}
3629

37-
function compileCode (src) {
30+
export function compileCode (src) {
3831
if (typeof src !== 'string') {
3932
throw new TypeError('First argument must be a string.')
4033
}
4134
let code = codeCache.get(src)
4235
if (!code) {
43-
code = parser.parseCode(src)
36+
code = parseCode(src)
4437
codeCache.set(src, code)
4538
}
4639

src/context.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict'
2-
31
const hasHandler = { has }
42
const allHandlers = { has, get }
53
const globals = new Set()
@@ -12,27 +10,21 @@ else if (typeof self !== 'undefined') globalObj = self // eslint-disable-line
1210
globalObj.$nxCompileToSandbox = toSandbox
1311
globalObj.$nxClearSandbox = clearSandbox
1412

15-
module.exports = {
16-
expose,
17-
hide,
18-
hideAll
19-
}
20-
21-
function expose (...globalNames) {
13+
export function expose (...globalNames) {
2214
for (let globalName of globalNames) {
2315
globals.add(globalName)
2416
}
2517
return this
2618
}
2719

28-
function hide (...globalNames) {
20+
export function hide (...globalNames) {
2921
for (let globalName of globalNames) {
3022
globals.delete(globalName)
3123
}
3224
return this
3325
}
3426

35-
function hideAll () {
27+
export function hideAll () {
3628
globals.clear()
3729
return this
3830
}

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './compiler'
2+
export * from './rawCompiler'
3+
export * from './context'
4+
export * from './modifiers'

src/modifiers.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
1-
'use strict'
1+
export const filters = new Map()
2+
export const limiters = new Map()
23

3-
const filters = new Map()
4-
const limiters = new Map()
5-
6-
module.exports = {
7-
filters,
8-
limiters,
9-
filter,
10-
limiter
11-
}
12-
13-
function filter (name, handler) {
4+
export function filter (name, handler) {
145
if (typeof name !== 'string') {
156
throw new TypeError('First argument must be a string.')
167
}
@@ -24,7 +15,7 @@ function filter (name, handler) {
2415
return this
2516
}
2617

27-
function limiter (name, handler) {
18+
export function limiter (name, handler) {
2819
if (typeof name !== 'string') {
2920
throw new TypeError('First argument must be a string.')
3021
}

0 commit comments

Comments
 (0)