https://github.com/pgilad/unquoted-property-validator
Unquoted JavaScript property name validator
https://github.com/pgilad/unquoted-property-validator
identifier javascript literal quotes validation
Last synced: about 1 year ago
JSON representation
Unquoted JavaScript property name validator
- Host: GitHub
- URL: https://github.com/pgilad/unquoted-property-validator
- Owner: pgilad
- License: mit
- Created: 2014-08-27T21:44:15.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2019-02-13T09:24:19.000Z (over 7 years ago)
- Last Synced: 2025-04-15T09:29:33.621Z (about 1 year ago)
- Topics: identifier, javascript, literal, quotes, validation
- Language: JavaScript
- Homepage:
- Size: 23.4 KB
- Stars: 5
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# unquoted-property-validator
> Unquoted JavaScript property name validator
[](https://npmjs.org/package/unquoted-property-validator)
[](https://npmjs.org/package/unquoted-property-validator)
[](https://travis-ci.org/pgilad/unquoted-property-validator)
This module checks if a given property name can be used without quotes and/or with dot notation.
It is based on Mathias Bynens brilliant [javascript-properties article](https://mathiasbynens.be/notes/javascript-properties).
So this is a **node-port** of his [website implementation](https://github.com/mathiasbynens/mothereff.in/tree/master/js-properties).
## Installation
```bash
$ npm install unquoted-property-validator --save
```
## Usage
```js
var unquotedValidator = require('unquoted-property-validator');
var results = unquotedValidator('myCoolLiteral');
console.log(results);
/* {
needsQuotes: false,
needsBrackets: false,
es3Warning: false,
quotedValue: 'myCoolLiteral'
}
*/
```
In ES6/Typescript the first line should look like this instead:
```js
import unquotedValidator = require('unquoted-property-validator');
```
## Results
Your property input will be checked and you will get 3 flags as output:
### needsQuotes
Quotes can only be omitted if the property name is a numeric literal or a valid identifier name:
```js
var obj = {
nonQuoted: true,
'must-be-quoted' : true
};
```
### needsBrackets
Dot notation can only be used when the property name is a valid identifier name:
```js
obj.bracketFree = true; //use the supreme dot notation
obj['requires-brackets'] = true; //string must be inside brackets to be used as property
```
### es3Warning
Should you want to support ES3, you cannot use some identifiers:
```js
obj['var'] //-> invalid in ES3
var obj = {
goto: true //-> invalid in ES3
};
```
### quotedValue
Your quoted string in case there are problematic characters.
## API
`unquotedValidator(input)`
### input
Type: `String`
Property to validate
## License
MIT © [Gilad Peleg](https://www.giladpeleg.com)