Giter Club home page Giter Club logo

unquoted-property-validator's Introduction

unquoted-property-validator

Unquoted JavaScript property name validator

NPM Version NPM Downloads Build Status

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.

Installation

$ npm install unquoted-property-validator --save

Usage

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');

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:

var obj = {
        nonQuoted: true,
        'must-be-quoted' : true
    };

needsBrackets

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

es3Warning

Should you want to support ES3, you cannot use some identifiers:

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

unquoted-property-validator's People

Contributors

pgilad avatar xkr47 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

kesko-dev

unquoted-property-validator's Issues

Terminology

Hey, glad to see you packaged this into a module!

Just a small suggestion: the terminology “valid literal” and “valid string literal” is a bit off. E.g.

Check if input is a valid javascript string literal or needs quoting

A string literal is by quoted already, by definition.

Instead, this code checks for valid identifier names or numeric literals as only those can be used as property names in object literals.

[…] quotes can only be omitted if the property name is a numeric literal or a valid identifier name […]

You may want to correct things like this in the README:

Check if input is a valid javascript string literal or needs quoting

And arguably the repository name could be changed as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.