Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

should-quote

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

should-quote

A utility function to detect if a string should be wrapped in quotes to work as an object key

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

should-quote

This is a tiny library to detect if a string should be wrapped in quotes to work as an object key.

How to Install

yarn add should-quote

or...

npm install should-quote

How to Use

First you need to import the function as seen below:

import shouldQuote from 'should-quote';

Then you can use it on string literals...

console.log(shouldQuote('foo')); // false

console.log(shouldQuote('foo bar')); // true

...or variables with string value.

const key = 'prop.name';

console.log(shouldQuote(key) ? `obj['${key}']` : `obj.${key}`); // obj['prop.name']

The logged output would have been dot notation if camel case were used.

const key = 'propName';

console.log(shouldQuote(key) ? `obj['${key}']` : `obj.${key}`); // obj.propName

It allows unquoted keys if it complies with the lexical grammar.

const key = '\\u0078'; // yeah, \u0078 does not have to be quoted

console.log(shouldQuote(key) ? `obj['${key}']` : `obj.${key}`); // obj.\u0078

Keywords

FAQs

Package last updated on 11 Mar 2021

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc