Socket
Socket
Sign inDemoInstall

is-var-name

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    is-var-name

Check if a string can be used as a JavaScript variable name


Version published
Maintainers
1
Install size
5.52 kB
Created

Readme

Source

is-var-name

npm version Build Status Coverage Status

Check if a string can be used as a JavaScript variable name

isVarName('foo'); //=> true
isVarName('f o o'); //=> false

Installation

Use npm.

npm install is-var-name

API

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. If not, or the argument is not a string, it returns false.

isVarName('______________'); //=> true
isVarName('å'); //=> true

isVarName('123'); //=> false
isVarName('↑→↓←'); //=> false
isVarName('_;'); //=> false
isVarName(''); //=> false

isVarName(['foo']); //=> false
isVarName(); //=> false

Another solution

Instead of this module, you can use the regular expression that matches valid variable names.

Regular expression pros

is-var-name uses Function constructor but regular expression doesn't.

According to the ESLint documentation, 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), it increases the file size of your library or application.

License

ISC License © 2018 Shinnosuke Watanabe

Keywords

FAQs

Last updated on 19 Feb 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc