quote-js-string
Escape a string and wrap it in quotes to produce a safe JavaScript string literal
Last updated 5 months ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install quote-js-string 
SYNC missed versions from official npm registry.

quote-js-string

Escape a string and wrap it in quotes to produce a safe JavaScript string literal

Useful for code generators, ESLint autofixers, and other tooling that needs to turn an arbitrary runtime string into JavaScript source code.

Escapes backslashes, the quote character, and unsafe characters (C0 control characters, DEL, the U+2028/U+2029 line terminators, which can disrupt line-oriented tooling, and lone surrogates, which have no valid UTF-8 representation).

Install

npm install quote-js-string

Usage

import quoteJsString from 'quote-js-string';

quoteJsString('It\'s a test');
//=> `'It\\'s a test'`

quoteJsString('It\'s a test', '"');
//=> `"It's a test"`

quoteJsString('line1\nline2');
//=> `'line1\\nline2'`

API

quoteJsString(string, quote?)

string

Type: string

The string to quote and escape.

quote

Type: ' | "
Default: '

The quote character to wrap the string in.

Current Tags

  • 0.1.0                                ...           latest (5 months ago)

1 Versions

  • 0.1.0                                ...           5 months ago
Maintainers (1)
Downloads
Total 3
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 0
Last Month 1
Dependencies (0)
None
Dev Dependencies (1)
  • xo ^3.0.2

© 2010 - cnpmjs.org x YWFE | Home | YWFE