1.0.1 • Published 8 years ago

js-string-escape v1.0.1

Weekly downloads
2,160,306
License
MIT
Repository
github
Last release
8 years ago

js-string-escape

Build Status

Escape any string to be a valid JavaScript string literal between double quotes or single quotes.

Installation

npm install js-string-escape

Example

If you need to generate JavaScript output, this library will help you safely put arbitrary data in JavaScript strings:

jsStringEscape = require('js-string-escape')

console.log('"' + jsStringEscape('Quotes (\", \'), newlines (\n), etc.') + '"')
// => "Quotes (\", \'), newlines (\n), etc."

In other words, given any string s, the following invariants hold:

eval('"' + jsStringEscape(s) + '"') === s
eval("'" + jsStringEscape(s) + "'") === s

These eval expressions are safe with untrusted strings s.

Non-strings will be cast to strings.

Compliance

This library has been checked against ECMAScript 5.1 and tested against all Unicode code points.

Note that the returned string is not necessarily valid JSON, since JSON disallows control characters, and \' is illegal in JSON.

@ianlucas/service-analyticsdokztestmkdir-nativedas-builddokz-docs-3drupenucly-templatevue-webpack-index@drupe/cherry@drupe/env@drupe/html-plugin@you54f/serverless-offlinezy-ember-cli-stylelint@scrumble/appsync-emulator-serverlessyak-shave@xavo/cypress-cucumber-preprocessor@saaspe/componentsvisionr-dev@everything-registry/sub-chunk-1978blix_svelvetfly-ng-templatecachefn-lib-example@conio/amplify-appsync-simulator@conio/serverless-offline@connorads/serverless-offlinebotstrap-gc@conduitvc/appsync-emulator-serverless@component-controls/blocks@component-controls/loader@component-controls/instrumentsteno-xbricks-clibroccoli-ember-hbs-template-compilerbroccoli-es6-concatbroccoli-es6-concatenatorbroccoli-sass-lintbroccoli-scss-linterbroccoli-jscsbroccoli-jstbroccoli-jst-ecobroccoli-templatebroccoli-template-builderbroccoli-template-compilerbroccoli-standardbroccoli-style-lintbroccoli-stylelintbrowser-based-exportdemo-source-loaderuselessdenali-eslintcss2reactjs-inline-styledecksmanagementlibrarydesign-system-fitbank-450urify-emitter@cox-automotive/clorthobroccoli-concat-cabbagebroccoli-billy-builder@cybermega/flutter-resource-manager-rssubcodebrowserify-haml-rubystylusifyswagger-test-templates@denali-js/eslintcat-clientcater-runtime@diconium/commerce-cif-hybris-clients@dttvinh/appsync-emulator-serverlessdomervitaminjsvitaminjs-runtime@erikwithuhk/html-to-jsx@fed135/replayverdaccio-local@eki-group/svelvet@embroider/core@embroider/shared-internals@engrafia/plugincypress-cucumber-predecessorcypress-cucumber-preprocessorcypress-cucumber-preprocessor-featurefilescypress-cucumber-tags-testrailcypress-cucumber-with-tagscypress10-cucumber-preprocessorwebantwebant-handler-csswebant-handler-dustjswebant-handler-hbswebant-handler-jswebant-handler-lesswebant-handler-scsswebant-handler-styluswebant-handler-text@finviet-jsrpt/jsrpt-assetswd-serverless-offlinewebpack-image-srcset-loadertint-serverless-offlinetranspiletplacedkastner-replaydocm-utils
1.0.1

8 years ago

1.0.0

11 years ago

0.0.1

11 years ago