Installations
npm install @nx-js/compiler-util
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=6.0.0
Node Version
8.8.1
NPM Version
5.7.1
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
Download Statistics
Total Downloads
232,875
Last Day
468
Last Week
1,990
Last Month
8,118
Last Year
68,960
GitHub Statistics
MIT License
164 Stars
40 Commits
19 Forks
8 Watchers
2 Branches
2 Contributors
Updated on Nov 08, 2024
Bundle Size
3.63 kB
Minified
1.21 kB
Minified + Gzipped
Package Meta Information
Latest Version
2.0.0
Package Id
@nx-js/compiler-util@2.0.0
Size
10.96 kB
NPM Version
5.7.1
Node Version
8.8.1
Published on
Jun 08, 2018
Total Downloads
Cumulative downloads
Total Downloads
232,875
Last Day
70.2%
468
Compared to previous day
Last Week
38.4%
1,990
Compared to previous week
Last Month
79.2%
8,118
Compared to previous month
Last Year
19.7%
68,960
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
30
The compiler util
This library is part of the NX framework.
The main purpose of this library is to allow the execution of strings as code in the context of an object.
Installation
$ npm install @nx-js/compiler-util
Platform support
- Node: 6 and above
- Chrome: 49 and above (after browserified)
- Firefox: 38 and above (after browserified)
- Safari: 10 and above (after browserified)
- Edge: 12 and above (after browserified)
- Opera: 36 and above (after browserified)
- IE is not supported
Usage
1const compiler = require('@nx-js/compiler-util')
Compiling code
compiler.compileCode(string)
creates a function from a string. The returned function takes
an object as argument and executes the string as code in the context of the passed object.
The string can be any valid JavaScript code.
1const code = compiler.compileCode('return prop1 + prop2') 2const sum = code({prop1: 1, prop2: 2}) // sum is 3
Temporary variables
The returned function also accepts a second object argument, that may contain temporary variables. Temporary variables are added to the context object while the code is executing. They are favored over the permanent context variables.
1const code = compiler.compileCode('return prop1 + prop2') 2const context = {prop1: 1, prop2: 2} 3const temporary = {prop1: 2} 4const sum = code(context, temporary) // sum is 4, context is still {prop1: 1, prop2: 2}
Limiters
Limiters are functions, which can defer or block code execution. Some popular limiters are debounce and throttle for example. Limiters can be registered by name with compiler.limiter(name, function)
and used at the end of the code with the &
symbol.
1// next is the code or the next limiter 2compiler.limiter('delay', next => setTimeout(next, 1000)) 3 4const code = compiler.compileCode('console.log(message) & delay') 5const context = {message: 'Hello World'} 6code(context) // prints 'Hello World' to the console after a second
Limiters accept a context object, which can be used to share a context between executions of the code. It makes the creation of rate limiters - like throttle and debounce - straightforward.
1compiler.limiter('debounce', debounce) 2 3function debounce (next, context) { 4 clearTimeout(context.timer) 5 context.timer = setTimeout(next, 200) 6}
After the context argument limiters accept any number of custom arguments. These can be passed after the limiter name in the code, separated by spaces.
1compiler.limiter('delay', (next, context, amount) => setTimeout(next, amount)) 2 3const code = compiler.compileCode('console.log(message) & delay 2000') 4const code2 = compiler.compileCode('console.log(message) & delay amount') 5 6const context = {message: 'Hello World', amount: 3000} 7code(context) // prints 'Hello World' to the console after 2 seconds 8code2(context) // prints 'Hello World' to the console after 3 seconds
Multiple limiters can be piped with the &
symbol.
1const code = compiler.compileCode('console.log(message) & delay 1000 & throttle 100') 2 3// this logs 'Hello World' a second after you click the button 4// and it logs a message once per 100 milliseconds at most, excess messages are not logged 5button.addEventListener('code', () => code({message: 'Hello World'}))
You can find some commonly used limiters in this repo.
Compiling expressions
compiler.compileExpression(string)
creates a function from a string. The returned function takes
an object as argument and executes the string as an expression in the context of the passed object.
It returns the result of the evaluated expression. The string can be any javascript expression
that may come after a return statement.
1const expression = compiler.compileExpression('prop1 || prop2') 2const result = expression({prop2: 'Hello'}) // result is 'Hello'
Expressions return undefined instead of throwing a TypeError on invalid property access. This allows lazy initialization of your data.
1const expression = compiler.compileExpression('item.name') 2const context = {} 3 4let result = expression(context) // result is undefined, no error is thrown 5 6context.item = {name: 'item name'} 7result = expression(context) // result is 'item name'
Filters
Filters are functions, which can filter and modify expression result. Some popular filters are upperCase and trim for example. Filters can be registered by name with compiler.filter(name, function)
and used at the end of the expression with the |
symbol.
1// txt is the result of the expression 2compiler.filter('upperCase', txt => txt.toUpperCase()) 3 4const expr = compiler.compileExpression('message | upperCase') 5const context = {message: 'Hello World'} 6console.log(expr(context)) // prints 'HELLO WORLD' to the console
Filters accept any number of custom arguments. These can be passed after the filter name in the expression, separated by spaces.
1compiler.filter('splice', (txt, start, end) => txt.splice(start, end)) 2 3const expr = compiler.compileExpression('message | splice 0 6') 4const context = {message: 'Hello World'} 5console.log(expr(context)) // prints 'Hello' to the console
Multiple filters can be piped with the |
symbol.
1const expr = compiler.compileExpression('message | splice 0 6 | upperCase') 2const context = {message: 'Hello World'} 3console.log(expr(context)) // prints 'HELLO' to the console
You can find some commonly used filters in this repo.
Handling globals
compiler.expose('String, String, ...')
exposes globals by name for the compiler. Non of the globals are exposed by default.
1const code = compiler.compileCode('console.log(Math.round(num))') 2compiler.expose('console', 'Math') 3code({num: 1.8}) // logs 2 to the console
Context variables are always favored over global ones, when both are present with the same name.
compiler.hide(String, String, ...)
hides globals by name, while compiler.hideAll()
hides all globals.
1const code = compiler.compileCode('console.log(Math.round(num))') 2compiler.expose('console', 'Math') 3code({num: 1.8}) // logs 2 to the console 4compiler.hide('console', 'Math') 5code({num: 1.8}) // throws an error, console and Math are undefined
Alternative builds
This library detects if you use ES or commonJS modules and serve the right format to you. The exposed bundles are transpiled to ES5 to support common tools - like UglifyJS. If you would like a finer control over the provided build, you can specify them in your imports.
@nx-js/compiler-util/dist/es.es6.js
exposes an ES6 build with ES modules.@nx-js/compiler-util/dist/es.es5.js
exposes an ES5 build with ES modules.@nx-js/compiler-util/dist/cjs.es6.js
exposes an ES6 build with commonJS modules.@nx-js/compiler-util/dist/cjs.es5.js
exposes an ES5 build with commonJS modules.
If you use a bundler, set up an alias for @nx-js/compiler-util
to point to your desired build. You can learn how to do it with webpack here and with rollup here.
Contributions
This library has the very specific purpose of supporting the NX framework. Features should only be added, if they are used by the framework. Otherwise please fork.
Bug fixes, tests and doc updates are always welcome. Tests and linter (standardJS) must pass.
Authors
License
MIT

No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.md:0
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 2 are checked with a SAST tool
Reason
119 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-42xw-2xvc-qx8m
- Warn: Project is vulnerable to: GHSA-4w2v-q235-vp99
- Warn: Project is vulnerable to: GHSA-cph5-m8f7-6c5x
- Warn: Project is vulnerable to: GHSA-wf5p-g6vw-rhxx
- Warn: Project is vulnerable to: GHSA-pp7h-53gx-mx7r
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-c6rq-rjc2-86v2
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-9j49-mfvp-vmhm
- Warn: Project is vulnerable to: GHSA-j4f2-536g-r55m
- Warn: Project is vulnerable to: GHSA-r7qp-cfhv-p84w
- Warn: Project is vulnerable to: GHSA-qrmc-fj45-qfc2
- Warn: Project is vulnerable to: GHSA-74fj-2j2h-c42q
- Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-q42p-pg8m-cqh6
- Warn: Project is vulnerable to: GHSA-w457-6q6x-cgp9
- Warn: Project is vulnerable to: GHSA-62gr-4qp9-h98f
- Warn: Project is vulnerable to: GHSA-f52g-6jhx-586p
- Warn: Project is vulnerable to: GHSA-2cf5-4w76-r9qv
- Warn: Project is vulnerable to: GHSA-3cqr-58rm-57f8
- Warn: Project is vulnerable to: GHSA-g9r4-xpmj-mj65
- Warn: Project is vulnerable to: GHSA-q2c6-c6pm-g3gh
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-44pw-h2cw-w3vq
- Warn: Project is vulnerable to: GHSA-jp4x-w63m-7wgm
- Warn: Project is vulnerable to: GHSA-c429-5p7v-vgjp
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-6x33-pw7p-hmpq
- Warn: Project is vulnerable to: GHSA-pc5p-h8pf-mvwp
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-2pr6-76vf-7546
- Warn: Project is vulnerable to: GHSA-8j8c-7jfh-h6hx
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-282f-qqgm-c34q
- Warn: Project is vulnerable to: GHSA-7x7c-qm48-pq9c
- Warn: Project is vulnerable to: GHSA-rc3x-jf5g-xvc5
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574
- Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-82v2-mx6x-wq7q
- Warn: Project is vulnerable to: GHSA-4xcv-9jjx-gfj3
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-hxm2-r34f-qmc5
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-fhjf-83wg-r2j9
- Warn: Project is vulnerable to: GHSA-4c7m-wxvm-r7gc
- Warn: Project is vulnerable to: GHSA-pch5-whg9-qr2r
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-48ww-j4fc-435p
- Warn: Project is vulnerable to: GHSA-hwqf-gcqm-7353
- Warn: Project is vulnerable to: GHSA-9h6g-pr28-7cqp
- Warn: Project is vulnerable to: GHSA-6fx8-h7jm-663j
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-g6ww-v8xp-vmwg
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-35q2-47q7-3pc3
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-hjp8-2cm3-cc45
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-4g88-fppr-53pp
- Warn: Project is vulnerable to: GHSA-4jqc-8m5r-9rpr
- Warn: Project is vulnerable to: GHSA-4rq4-32rv-6wp6
- Warn: Project is vulnerable to: GHSA-64g7-mvw6-v9qj
- Warn: Project is vulnerable to: GHSA-fxwf-4rqh-v8g3
- Warn: Project is vulnerable to: GHSA-25hc-qcg6-38wj
- Warn: Project is vulnerable to: GHSA-xfhh-g9f5-x4m4
- Warn: Project is vulnerable to: GHSA-qm95-pgcg-qqfq
- Warn: Project is vulnerable to: GHSA-cqmj-92xf-r6r9
- Warn: Project is vulnerable to: GHSA-j44m-qm6p-hp7m
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-f523-2f5j-gfcg
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-xc7v-wxcw-j472
- Warn: Project is vulnerable to: GHSA-662x-fhqg-9p8v
- Warn: Project is vulnerable to: GHSA-394c-5j6w-4xmx
- Warn: Project is vulnerable to: GHSA-78cj-fxph-m83p
- Warn: Project is vulnerable to: GHSA-fhg7-m89q-25r3
- Warn: Project is vulnerable to: GHSA-cf4h-3jhx-xvhq
- Warn: Project is vulnerable to: GHSA-mgfv-m47x-4wqp
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-72mh-269x-7mh5
- Warn: Project is vulnerable to: GHSA-h4j5-c7cj-74xg
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
2
/10
Last Scanned on 2025-02-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More