README.md
1# constantinople
2
3Determine whether a JavaScript expression evaluates to a constant (using Babylon). Here it is assumed to be safe to underestimate how constant something is.
4
5[](https://travis-ci.org/pugjs/constantinople)
6[](https://david-dm.org/pugjs/constantinople)
7[](https://www.npmjs.org/package/constantinople)
8
9## Installation
10
11 npm install constantinople
12
13## Usage
14
15```js
16var isConstant = require('constantinople')
17
18if (isConstant('"foo" + 5')) {
19 console.dir(isConstant.toConstant('"foo" + 5'))
20}
21if (isConstant('Math.floor(10.5)', {Math: Math})) {
22 console.dir(isConstant.toConstant('Math.floor(10.5)', {Math: Math}))
23}
24```
25
26## API
27
28### isConstant(src, [constants, [options]])
29
30Returns `true` if `src` evaluates to a constant, `false` otherwise. It will also return `false` if there is a syntax error, which makes it safe to use on potentially ES6 code.
31
32Constants is an object mapping strings to values, where those values should be treated as constants. Note that this makes it a pretty bad idea to have `Math` in there if the user might make use of `Math.random` and a pretty bad idea to have `Date` in there.
33
34Options are directly passed-through to [Babylon](https://github.com/babel/babylon#options).
35
36### toConstant(src, [constants, [options]])
37
38Returns the value resulting from evaluating `src`. This method throws an error if the expression is not constant. e.g. `toConstant("Math.random()")` would throw an error.
39
40Constants is an object mapping strings to values, where those values should be treated as constants. Note that this makes it a pretty bad idea to have `Math` in there if the user might make use of `Math.random` and a pretty bad idea to have `Date` in there.
41
42Options are directly passed-through to [Babylon](https://github.com/babel/babylon#options).
43
44## License
45
46 MIT
47