Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pugjs/is-expression
Validates a string as a JavaScript expression
https://github.com/pugjs/is-expression
Last synced: 6 days ago
JSON representation
Validates a string as a JavaScript expression
- Host: GitHub
- URL: https://github.com/pugjs/is-expression
- Owner: pugjs
- License: mit
- Created: 2015-11-12T01:31:44.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-08-16T10:00:34.000Z (6 months ago)
- Last Synced: 2025-01-19T11:12:41.850Z (13 days ago)
- Language: JavaScript
- Homepage:
- Size: 44.9 KB
- Stars: 9
- Watchers: 3
- Forks: 10
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# is-expression
Validates a string as a JavaScript expression
[![Build Status](https://img.shields.io/github/workflow/status/pugjs/is-expression/Test/master)](https://github.com/pugjs/is-expression/actions?query=branch%3Amaster+workflow%3ATest)
[![Dependency Status](https://img.shields.io/david/pugjs/is-expression.svg)](https://david-dm.org/pugjs/is-expression)
[![Rolling Versions](https://img.shields.io/badge/Rolling%20Versions-Enabled-brightgreen)](https://rollingversions.com/pugjs/is-expression)
[![npm version](https://img.shields.io/npm/v/is-expression.svg)](https://www.npmjs.org/package/is-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 in `err.loc` and `err.pos`.
Defaults to `false`.
- `strict`: Use strict mode when trying to parse the string. Defaults to
`false`. Even if this option is `false`, if you have provided
`options.sourceType === 'module'` which imples strict mode under ES2015,
strict mode will be used.
- `lineComment`: When `true`, allows line comments in the expression.
Defaults to `false` for safety.See the examples below for usage.
## Examples
```js
var isExpression = require('is-expression')isExpression('myVar')
//=> true
isExpression('var')
//=> false
isExpression('["an", "array", "\'s"].indexOf("index")')
//=> trueisExpression('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})
//=> falseisExpression('abc // my comment')
//=> false
isExpression('abc // my comment', {lineComment: true})
//=> true
```## License
MIT