Unquoted JavaScript property name 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. So this is a node-port of his website implementation.
$ npm install unquoted-property-validator --save
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:
import unquotedValidator = require('unquoted-property-validator');
Your property input will be checked and you will get 3 flags as output:
Quotes can only be omitted if the property name is a numeric literal or a valid identifier name:
var obj = {
nonQuoted: true,
'must-be-quoted' : true
};
Dot notation can only be used when the property name is a valid identifier name:
obj.bracketFree = true; //use the supreme dot notation
obj['requires-brackets'] = true; //string must be inside brackets to be used as property
Should you want to support ES3, you cannot use some identifiers:
obj['var'] //-> invalid in ES3
var obj = {
goto: true //-> invalid in ES3
};
Your quoted string in case there are problematic characters.
unquotedValidator(input)
Type: String
Property to validate
MIT © Gilad Peleg