You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
monaxys
1ba7100de7
|
2 years ago | |
---|---|---|
.. | ||
CHANGELOG.md | 2 years ago | |
LICENSE.md | 2 years ago | |
README.md | 2 years ago | |
index.js | 2 years ago | |
package.json | 2 years ago |
README.md
is-expression
Validates a string as a JavaScript expression
Installation
npm install is-expression
Usage
isExpression(src[, options])
Validates a string as a JavaScript expression.
src
contains the source.
options
can contain any Acorn options (since we use Acorn under-the-hood),
or any of the following:
throw
: Throw an error if the string is not an expression. The error can be an Acorn error, with location information inerr.loc
anderr.pos
. Defaults tofalse
.strict
: Use strict mode when trying to parse the string. Defaults tofalse
. Even if this option isfalse
, if you have providedoptions.sourceType === 'module'
which imples strict mode under ES2015, strict mode will be used.lineComment
: Whentrue
, allows line comments in the expression. Defaults tofalse
for safety.
See the examples below for usage.
Examples
var isExpression = require('is-expression')
isExpression('myVar')
//=> true
isExpression('var')
//=> false
isExpression('["an", "array", "\'s"].indexOf("index")')
//=> true
isExpression('var', {throw: true})
// SyntaxError: Unexpected token (1:0)
// at Parser.pp.raise (acorn/dist/acorn.js:940:13)
// at ...
isExpression('public')
//=> true
isExpression('public', {strict: true})
//=> false
isExpression('abc // my comment')
//=> false
isExpression('abc // my comment', {lineComment: true})
//=> true
License
MIT