Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shinnn/is-var-name
Check if a string can be used as a JavaScript variable name
https://github.com/shinnn/is-var-name
boolean ecmascript javascript language validation validator
Last synced: 26 days ago
JSON representation
Check if a string can be used as a JavaScript variable name
- Host: GitHub
- URL: https://github.com/shinnn/is-var-name
- Owner: shinnn
- License: isc
- Created: 2014-11-25T03:57:53.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2019-04-05T11:48:41.000Z (over 5 years ago)
- Last Synced: 2024-10-11T22:26:46.761Z (about 1 month ago)
- Topics: boolean, ecmascript, javascript, language, validation, validator
- Language: JavaScript
- Homepage: https://npm.runkit.com/is-var-name
- Size: 46.9 KB
- Stars: 15
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# is-var-name
[![npm version](https://img.shields.io/npm/v/is-var-name.svg)](https://www.npmjs.com/package/is-var-name)
[![Build Status](https://travis-ci.com/shinnn/is-var-name.svg?branch=master)](https://travis-ci.com/shinnn/is-var-name)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/is-var-name.svg)](https://coveralls.io/r/shinnn/is-var-name)Check if a string can be used as a [JavaScript variable name](https://es5.github.io/x7.html#x7.6)
```javascript
isVarName('foo'); //=> true
isVarName('f o o'); //=> false
```## Installation
[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).
```
npm install is-var-name
```## API
```javascript
import isVarName from 'is-var-name';
```### isVarName(*name*)
*name*: `string`
Return: `boolean`It returns `true` if [the string can be used as a valid JavaScript identifier name](https://mathiasbynens.be/notes/javascript-identifiers). If not, or the argument is not a string, it returns `false`.
```javascript
isVarName('______________'); //=> true
isVarName('å'); //=> trueisVarName('123'); //=> false
isVarName('↑→↓←'); //=> false
isVarName('_;'); //=> false
isVarName(''); //=> falseisVarName(['foo']); //=> false
isVarName(); //=> false
```## Another solution
Instead of this module, you can use [the regular expression that matches valid variable names](https://gist.github.com/mathiasbynens/6334847#file-ecmascript-6-js).
### Regular expression pros
*is-var-name* uses [`Function` constructor](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function) but regular expression doesn't.
According to [the ESLint documentation](https://eslint.org/docs/rules/no-new-func), `new Function()` is:
> considered by many to be a bad practice due to the difficult in debugging and reading these types of functions.
### Regular expression cons
Since the regular expression is too long ([about 16,000 characters](https://gist.githubusercontent.com/mathiasbynens/6334847/raw/4b78d62a29da1f340956aa102dffa6c4ac7e620e/ecmascript-6.js)), it increases the file size of your library or application.
## License
[ISC License](./LICENSE) © 2018 - 2019 Shinnosuke Watanabe