Skip to content

Commit 9a24739

Browse files
committed
feat(expose, hide): allow any number of strings as argument
1 parent 25965d3 commit 9a24739

File tree

3 files changed

+18
-32
lines changed

3 files changed

+18
-32
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,28 @@ context.item = {name: 'item name'}
7676
result = expression(context) // result is 'item name'
7777
```
7878

79-
### compiler.expose(String)
79+
### compiler.expose(String, String, String, ...)
8080

8181
Use this method to expose globals to the compiler. Non of the globals are exposed by default.
8282

8383
```js
84-
const code = compiler.compileCode('console.log(message)')
85-
compiler.expose('console')
86-
code({message: 'Hello World'}) // logs 'Hello World' to the console
84+
const code = compiler.compileCode('console.log(Math.round(num))')
85+
compiler.expose('console', 'Math')
86+
code({num: 1.8}) // logs 2 to the console
8787
```
8888

8989
Context variables are always favored over global ones, when both are present (with the same name).
9090

91-
### compiler.hide(String)
91+
### compiler.hide(String, String, String, ...)
9292

9393
Use this method to hide exposed globals from the compiler.
9494

9595
```js
96-
const code = compiler.compileCode('console.log(message)')
97-
compiler.expose('console')
98-
code({message: 'Hello World'}) // logs 'Hello World' to the console
99-
compiler.hide('console')
100-
code({message: 'Hello World'}) // throws an error, console is undefined
96+
const code = compiler.compileCode('console.log(Math.round(num))')
97+
compiler.expose('console', 'Math')
98+
code({num: 1.8}) // logs 2 to the console
99+
compiler.hide('console', 'Math')
100+
code({num: 1.8}) // throws an error, console and Math are undefined
101101
```
102102

103103
Context variables are always favored over global ones, when both are present (with the same name).

compiler.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,16 @@ function compileCode (src) {
5656
return code
5757
}
5858

59-
function expose (globalName) {
60-
if (typeof globalName !== 'string') {
61-
throw new TypeError('first argument must be a string')
59+
function expose (...globalNames) {
60+
for (let globalName of globalNames) {
61+
globals.add(globalName)
6262
}
63-
globals.add(globalName)
6463
}
6564

66-
function hide (globalName) {
67-
if (typeof globalName !== 'string') {
68-
throw new TypeError('first argument must be a string')
65+
function hide (...globalNames) {
66+
for (let globalName of globalNames) {
67+
globals.delete(globalName)
6968
}
70-
globals.delete(globalName)
7169
}
7270

7371
function toSandbox (obj) {

compiler.test.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,10 @@ describe('nx-compile', () => {
8181
})
8282

8383
describe('expose', () => {
84-
it('should throw a TypeError on non string source argument', () => {
85-
expect(() => compiler.expose({})).to.throw(TypeError)
86-
expect(() => compiler.expose(undefined)).to.throw(TypeError)
87-
expect(() => compiler.expose(12)).to.throw(TypeError)
88-
})
89-
9084
it('should expose the passed global', () => {
9185
const expression = compiler.compileExpression('console')
9286
expect(expression({})).to.be.undefined
93-
compiler.expose('console')
87+
compiler.expose('Math', 'console')
9488
expect(expression({})).to.equal(console)
9589
})
9690

@@ -101,16 +95,10 @@ describe('nx-compile', () => {
10195
})
10296

10397
describe('hide', () => {
104-
it('should throw a TypeError on non string source argument', () => {
105-
expect(() => compiler.hide({})).to.throw(TypeError)
106-
expect(() => compiler.hide(undefined)).to.throw(TypeError)
107-
expect(() => compiler.hide(12)).to.throw(TypeError)
108-
})
109-
11098
it('should hide exposed globals', () => {
11199
const expression = compiler.compileExpression('console')
112100
expect(expression({})).to.equal(console)
113-
compiler.hide('console')
101+
compiler.hide('Math', 'console')
114102
expect(expression({})).to.be.undefined
115103
})
116104
})

0 commit comments

Comments
 (0)