• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..--

lib/25-Sep-2023-429424

src/25-Sep-2023-435425

test/25-Sep-2023-8783

.prettierrcD24-Sep-202378 55

.travis.ymlD24-Sep-202351 65

LICENSED24-Sep-20231 KiB1916

README.mdD24-Sep-20232 KiB4728

package.jsonD24-Sep-2023810 3535

tsconfig.jsonD24-Sep-2023119 98

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[![Build Status](https://img.shields.io/travis/pugjs/constantinople/master.svg)](https://travis-ci.org/pugjs/constantinople)
6[![Dependency Status](https://img.shields.io/david/pugjs/constantinople.svg)](https://david-dm.org/pugjs/constantinople)
7[![NPM version](https://img.shields.io/npm/v/constantinople.svg)](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