{"id":13447391,"url":"https://github.com/mathiasbynens/jsesc","last_synced_at":"2025-05-13T15:09:29.559Z","repository":{"id":8803695,"uuid":"10498729","full_name":"mathiasbynens/jsesc","owner":"mathiasbynens","description":"Given some data, jsesc returns the shortest possible stringified \u0026 ASCII-safe representation of that data.","archived":false,"fork":false,"pushed_at":"2024-12-11T08:25:58.000Z","size":279,"stargazers_count":785,"open_issues_count":22,"forks_count":51,"subscribers_count":16,"default_branch":"main","last_synced_at":"2025-04-18T17:18:36.839Z","etag":null,"topics":["ascii-safe","code-generation","ecmascript","escape","escape-sequences","javascript","json","stringify","unicode"],"latest_commit_sha":null,"homepage":"https://mths.be/jsesc","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mathiasbynens.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-MIT.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-06-05T08:59:30.000Z","updated_at":"2025-04-05T20:00:15.000Z","dependencies_parsed_at":"2025-01-02T11:00:17.186Z","dependency_job_id":"0dd28561-a463-48ee-b815-c0f4cd5a9d96","html_url":"https://github.com/mathiasbynens/jsesc","commit_stats":{"total_commits":133,"total_committers":9,"mean_commits":"14.777777777777779","dds":0.07518796992481203,"last_synced_commit":"203c8694d605b6f29d4c67d372897499ec4468fb"},"previous_names":["mathiasbynens/javascript-string-escape"],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathiasbynens%2Fjsesc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathiasbynens%2Fjsesc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathiasbynens%2Fjsesc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathiasbynens%2Fjsesc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mathiasbynens","download_url":"https://codeload.github.com/mathiasbynens/jsesc/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250495748,"owners_count":21440224,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ascii-safe","code-generation","ecmascript","escape","escape-sequences","javascript","json","stringify","unicode"],"created_at":"2024-07-31T05:01:16.314Z","updated_at":"2025-04-23T18:48:17.590Z","avatar_url":"https://github.com/mathiasbynens.png","language":"JavaScript","readme":"# jsesc\n\nGiven some data, _jsesc_ returns a stringified representation of that data. jsesc is similar to `JSON.stringify()` except:\n\n1. it outputs JavaScript instead of JSON [by default](#json), enabling support for data structures like ES6 maps and sets;\n2. it offers [many options](#api) to customize the output;\n3. its output is ASCII-safe [by default](#minimal), thanks to its use of [escape sequences](https://mathiasbynens.be/notes/javascript-escapes) where needed.\n\nFor any input, jsesc generates the shortest possible valid printable-ASCII-only output. [Here’s an online demo.](https://mothereff.in/js-escapes)\n\njsesc’s output can be used instead of `JSON.stringify`’s to avoid [mojibake](https://en.wikipedia.org/wiki/Mojibake) and other encoding issues, or even to [avoid errors](https://twitter.com/annevk/status/380000829643571200) when passing JSON-formatted data (which may contain U+2028 LINE SEPARATOR, U+2029 PARAGRAPH SEPARATOR, or [lone surrogates](https://esdiscuss.org/topic/code-points-vs-unicode-scalar-values#content-14)) to a JavaScript parser or an UTF-8 encoder.\n\n## Installation\n\nVia [npm](https://www.npmjs.com/):\n\n```bash\nnpm install jsesc\n```\n\nIn [Node.js](https://nodejs.org/):\n\n```js\nconst jsesc = require('jsesc');\n```\n\n## API\n\n### `jsesc(value, options)`\n\nThis function takes a value and returns an escaped version of the value where any characters that are not printable ASCII symbols are escaped using the shortest possible (but valid) [escape sequences for use in JavaScript strings](https://mathiasbynens.be/notes/javascript-escapes). The first supported value type is strings:\n\n```js\njsesc('Ich ♥ Bücher');\n// → 'Ich \\\\u2665 B\\\\xFCcher'\n\njsesc('foo 𝌆 bar');\n// → 'foo \\\\uD834\\\\uDF06 bar'\n```\n\nInstead of a string, the `value` can also be an array, an object, a map, a set, a number, a BigInt, or a buffer. In such cases, `jsesc` returns a stringified version of the value where any characters that are not printable ASCII symbols are escaped in the same way.\n\n```js\n// Escaping an array\njsesc([\n  'Ich ♥ Bücher', 'foo 𝌆 bar'\n]);\n// → '[\\'Ich \\\\u2665 B\\\\xFCcher\\',\\'foo \\\\uD834\\\\uDF06 bar\\']'\n\n// Escaping an object\njsesc({\n  'Ich ♥ Bücher': 'foo 𝌆 bar'\n});\n// → '{\\'Ich \\\\u2665 B\\\\xFCcher\\':\\'foo \\\\uD834\\\\uDF06 bar\\'}'\n```\n\nThe optional `options` argument accepts an object with the following options:\n\n#### `quotes`\n\nThe default value for the `quotes` option is `'single'`. This means that any occurrences of `'` in the input string are escaped as `\\'`, so that the output can be used in a string literal wrapped in single quotes.\n\n```js\njsesc('`Lorem` ipsum \"dolor\" sit \\'amet\\' etc.');\n// → 'Lorem ipsum \"dolor\" sit \\\\\\'amet\\\\\\' etc.'\n\njsesc('`Lorem` ipsum \"dolor\" sit \\'amet\\' etc.', {\n  'quotes': 'single'\n});\n// → '`Lorem` ipsum \"dolor\" sit \\\\\\'amet\\\\\\' etc.'\n// → \"`Lorem` ipsum \\\"dolor\\\" sit \\\\'amet\\\\' etc.\"\n```\n\nIf you want to use the output as part of a string literal wrapped in double quotes, set the `quotes` option to `'double'`.\n\n```js\njsesc('`Lorem` ipsum \"dolor\" sit \\'amet\\' etc.', {\n  'quotes': 'double'\n});\n// → '`Lorem` ipsum \\\\\"dolor\\\\\" sit \\'amet\\' etc.'\n// → \"`Lorem` ipsum \\\\\\\"dolor\\\\\\\" sit 'amet' etc.\"\n```\n\nIf you want to use the output as part of a template literal (i.e. wrapped in backticks), set the `quotes` option to `'backtick'`.\n\n```js\njsesc('`Lorem` ipsum \"dolor\" sit \\'amet\\' etc.', {\n  'quotes': 'backtick'\n});\n// → '\\\\`Lorem\\\\` ipsum \"dolor\" sit \\'amet\\' etc.'\n// → \"\\\\`Lorem\\\\` ipsum \\\"dolor\\\" sit 'amet' etc.\"\n// → `\\\\\\`Lorem\\\\\\` ipsum \"dolor\" sit 'amet' etc.`\n```\n\nThis setting also affects the output for arrays and objects:\n\n```js\njsesc({ 'Ich ♥ Bücher': 'foo 𝌆 bar' }, {\n  'quotes': 'double'\n});\n// → '{\"Ich \\\\u2665 B\\\\xFCcher\":\"foo \\\\uD834\\\\uDF06 bar\"}'\n\njsesc([ 'Ich ♥ Bücher', 'foo 𝌆 bar' ], {\n  'quotes': 'double'\n});\n// → '[\"Ich \\\\u2665 B\\\\xFCcher\",\"foo \\\\uD834\\\\uDF06 bar\"]'\n```\n\n#### `numbers`\n\nThe default value for the `numbers` option is `'decimal'`. This means that any numeric values are represented using decimal integer literals. Other valid options are `binary`, `octal`, and `hexadecimal`, which result in binary integer literals, octal integer literals, and hexadecimal integer literals, respectively.\n\n```js\njsesc(42, {\n  'numbers': 'binary'\n});\n// → '0b101010'\n\njsesc(42, {\n  'numbers': 'octal'\n});\n// → '0o52'\n\njsesc(42, {\n  'numbers': 'decimal'\n});\n// → '42'\n\njsesc(42, {\n  'numbers': 'hexadecimal'\n});\n// → '0x2A'\n```\n\n#### `wrap`\n\nThe `wrap` option takes a boolean value (`true` or `false`), and defaults to `false` (disabled). When enabled, the output is a valid JavaScript string literal wrapped in quotes. The type of quotes can be specified through the `quotes` setting.\n\n```js\njsesc('Lorem ipsum \"dolor\" sit \\'amet\\' etc.', {\n  'quotes': 'single',\n  'wrap': true\n});\n// → '\\'Lorem ipsum \"dolor\" sit \\\\\\'amet\\\\\\' etc.\\''\n// → \"\\'Lorem ipsum \\\"dolor\\\" sit \\\\\\'amet\\\\\\' etc.\\'\"\n\njsesc('Lorem ipsum \"dolor\" sit \\'amet\\' etc.', {\n  'quotes': 'double',\n  'wrap': true\n});\n// → '\"Lorem ipsum \\\\\"dolor\\\\\" sit \\'amet\\' etc.\"'\n// → \"\\\"Lorem ipsum \\\\\\\"dolor\\\\\\\" sit \\'amet\\' etc.\\\"\"\n```\n\n#### `es6`\n\nThe `es6` option takes a boolean value (`true` or `false`), and defaults to `false` (disabled). When enabled, any astral Unicode symbols in the input are escaped using [ECMAScript 6 Unicode code point escape sequences](https://mathiasbynens.be/notes/javascript-escapes#unicode-code-point) instead of using separate escape sequences for each surrogate half. If backwards compatibility with ES5 environments is a concern, don’t enable this setting. If the `json` setting is enabled, the value for the `es6` setting is ignored (as if it was `false`).\n\n```js\n// By default, the `es6` option is disabled:\njsesc('foo 𝌆 bar 💩 baz');\n// → 'foo \\\\uD834\\\\uDF06 bar \\\\uD83D\\\\uDCA9 baz'\n\n// To explicitly disable it:\njsesc('foo 𝌆 bar 💩 baz', {\n  'es6': false\n});\n// → 'foo \\\\uD834\\\\uDF06 bar \\\\uD83D\\\\uDCA9 baz'\n\n// To enable it:\njsesc('foo 𝌆 bar 💩 baz', {\n  'es6': true\n});\n// → 'foo \\\\u{1D306} bar \\\\u{1F4A9} baz'\n```\n\n#### `escapeEverything`\n\nThe `escapeEverything` option takes a boolean value (`true` or `false`), and defaults to `false` (disabled). When enabled, all the symbols in the output are escaped — even printable ASCII symbols.\n\n```js\njsesc('lolwat\"foo\\'bar', {\n  'escapeEverything': true\n});\n// → '\\\\x6C\\\\x6F\\\\x6C\\\\x77\\\\x61\\\\x74\\\\\"\\\\x66\\\\x6F\\\\x6F\\\\\\'\\\\x62\\\\x61\\\\x72'\n// → \"\\\\x6C\\\\x6F\\\\x6C\\\\x77\\\\x61\\\\x74\\\\\\\"\\\\x66\\\\x6F\\\\x6F\\\\'\\\\x62\\\\x61\\\\x72\"\n```\n\nThis setting also affects the output for string literals within arrays and objects.\n\n#### `minimal`\n\nThe `minimal` option takes a boolean value (`true` or `false`), and defaults to `false` (disabled). When enabled, only a limited set of symbols in the output are escaped:\n\n* U+0000 `\\0`\n* U+0008 `\\b`\n* U+0009 `\\t`\n* U+000A `\\n`\n* U+000C `\\f`\n* U+000D `\\r`\n* U+005C `\\\\`\n* U+2028 `\\u2028`\n* U+2029 `\\u2029`\n* whatever symbol is being used for wrapping string literals (based on [the `quotes` option](#quotes))\n* [lone surrogates](https://esdiscuss.org/topic/code-points-vs-unicode-scalar-values#content-14)\n\nNote: with this option enabled, jsesc output is no longer guaranteed to be ASCII-safe.\n\n```js\njsesc('foo\\u2029bar\\nbaz©qux𝌆flops', {\n  'minimal': false\n});\n// → 'foo\\\\u2029bar\\\\nbaz©qux𝌆flops'\n```\n\n#### `isScriptContext`\n\nThe `isScriptContext` option takes a boolean value (`true` or `false`), and defaults to `false` (disabled). When enabled, occurrences of [`\u003c/script` and `\u003c/style`](https://mathiasbynens.be/notes/etago) in the output are escaped as `\u003c\\/script` and `\u003c\\/style`, and [`\u003c!--`](https://mathiasbynens.be/notes/etago#comment-8) is escaped as `\\x3C!--` (or `\\u003C!--` when the `json` option is enabled). This setting is useful when jsesc’s output ends up as part of a `\u003cscript\u003e` or `\u003cstyle\u003e` element in an HTML document.\n\n```js\njsesc('foo\u003c/script\u003ebar', {\n  'isScriptContext': true\n});\n// → 'foo\u003c\\\\/script\u003ebar'\n```\n\n#### `compact`\n\nThe `compact` option takes a boolean value (`true` or `false`), and defaults to `true` (enabled). When enabled, the output for arrays and objects is as compact as possible; it’s not formatted nicely.\n\n```js\njsesc({ 'Ich ♥ Bücher': 'foo 𝌆 bar' }, {\n  'compact': true // this is the default\n});\n// → '{\\'Ich \\u2665 B\\xFCcher\\':\\'foo \\uD834\\uDF06 bar\\'}'\n\njsesc({ 'Ich ♥ Bücher': 'foo 𝌆 bar' }, {\n  'compact': false\n});\n// → '{\\n\\t\\'Ich \\u2665 B\\xFCcher\\': \\'foo \\uD834\\uDF06 bar\\'\\n}'\n\njsesc([ 'Ich ♥ Bücher', 'foo 𝌆 bar' ], {\n  'compact': false\n});\n// → '[\\n\\t\\'Ich \\u2665 B\\xFCcher\\',\\n\\t\\'foo \\uD834\\uDF06 bar\\'\\n]'\n```\n\nThis setting has no effect on the output for strings.\n\n#### `indent`\n\nThe `indent` option takes a string value, and defaults to `'\\t'`. When the `compact` setting is disabled (`false`), the value of the `indent` option is used to format the output for arrays and objects.\n\n```js\njsesc({ 'Ich ♥ Bücher': 'foo 𝌆 bar' }, {\n  'compact': false,\n  'indent': '\\t' // this is the default\n});\n// → '{\\n\\t\\'Ich \\u2665 B\\xFCcher\\': \\'foo \\uD834\\uDF06 bar\\'\\n}'\n\njsesc({ 'Ich ♥ Bücher': 'foo 𝌆 bar' }, {\n  'compact': false,\n  'indent': '  '\n});\n// → '{\\n  \\'Ich \\u2665 B\\xFCcher\\': \\'foo \\uD834\\uDF06 bar\\'\\n}'\n\njsesc([ 'Ich ♥ Bücher', 'foo 𝌆 bar' ], {\n  'compact': false,\n  'indent': '  '\n});\n// → '[\\n  \\'Ich \\u2665 B\\xFCcher\\',\\n\\  t\\'foo \\uD834\\uDF06 bar\\'\\n]'\n```\n\nThis setting has no effect on the output for strings.\n\n#### `indentLevel`\n\nThe `indentLevel` option takes a numeric value, and defaults to `0`. It represents the current indentation level, i.e. the number of times the value of [the `indent` option](#indent) is repeated.\n\n```js\njsesc(['a', 'b', 'c'], {\n  'compact': false,\n  'indentLevel': 1\n});\n// → '[\\n\\t\\t\\'a\\',\\n\\t\\t\\'b\\',\\n\\t\\t\\'c\\'\\n\\t]'\n\njsesc(['a', 'b', 'c'], {\n  'compact': false,\n  'indentLevel': 2\n});\n// → '[\\n\\t\\t\\t\\'a\\',\\n\\t\\t\\t\\'b\\',\\n\\t\\t\\t\\'c\\'\\n\\t\\t]'\n```\n\n#### `json`\n\nThe `json` option takes a boolean value (`true` or `false`), and defaults to `false` (disabled). When enabled, the output is valid JSON. [Hexadecimal character escape sequences](https://mathiasbynens.be/notes/javascript-escapes#hexadecimal) and [the `\\v` or `\\0` escape sequences](https://mathiasbynens.be/notes/javascript-escapes#single) are not used. Setting `json: true` implies `quotes: 'double', wrap: true, es6: false`, although these values can still be overridden if needed — but in such cases, the output won’t be valid JSON anymore.\n\n```js\njsesc('foo\\x00bar\\xFF\\uFFFDbaz', {\n  'json': true\n});\n// → '\"foo\\\\u0000bar\\\\u00FF\\\\uFFFDbaz\"'\n\njsesc({ 'foo\\x00bar\\xFF\\uFFFDbaz': 'foo\\x00bar\\xFF\\uFFFDbaz' }, {\n  'json': true\n});\n// → '{\"foo\\\\u0000bar\\\\u00FF\\\\uFFFDbaz\":\"foo\\\\u0000bar\\\\u00FF\\\\uFFFDbaz\"}'\n\njsesc([ 'foo\\x00bar\\xFF\\uFFFDbaz', 'foo\\x00bar\\xFF\\uFFFDbaz' ], {\n  'json': true\n});\n// → '[\"foo\\\\u0000bar\\\\u00FF\\\\uFFFDbaz\",\"foo\\\\u0000bar\\\\u00FF\\\\uFFFDbaz\"]'\n\n// Values that are acceptable in JSON but aren’t strings, arrays, or object\n// literals can’t be escaped, so they’ll just be preserved:\njsesc([ 'foo\\x00bar', [1, '©', { 'foo': true, 'qux': null }], 42 ], {\n  'json': true\n});\n// → '[\"foo\\\\u0000bar\",[1,\"\\\\u00A9\",{\"foo\":true,\"qux\":null}],42]'\n// Values that aren’t allowed in JSON are run through `JSON.stringify()`:\njsesc([ undefined, -Infinity ], {\n  'json': true\n});\n// → '[null,null]'\n```\n\n**Note:** Using this option on objects or arrays that contain non-string values relies on `JSON.stringify()`. For legacy environments like IE ≤ 7, use [a `JSON` polyfill](http://bestiejs.github.io/json3/).\n\n#### `lowercaseHex`\n\nThe `lowercaseHex` option takes a boolean value (`true` or `false`), and defaults to `false` (disabled). When enabled, any alphabetical hexadecimal digits in escape sequences as well as any hexadecimal integer literals (see [the `numbers` option](#numbers)) in the output are in lowercase.\n\n```js\njsesc('Ich ♥ Bücher', {\n  'lowercaseHex': true\n});\n// → 'Ich \\\\u2665 B\\\\xfccher'\n//                    ^^\n\njsesc(42, {\n  'numbers': 'hexadecimal',\n  'lowercaseHex': true\n});\n// → '0x2a'\n//      ^^\n```\n\n### `jsesc.version`\n\nA string representing the semantic version number.\n\n### Using the `jsesc` binary\n\nTo use the `jsesc` binary in your shell, simply install jsesc globally using npm:\n\n```bash\nnpm install -g jsesc\n```\n\nAfter that you’re able to escape strings from the command line:\n\n```bash\n$ jsesc 'föo ♥ bår 𝌆 baz'\nf\\xF6o \\u2665 b\\xE5r \\uD834\\uDF06 baz\n```\n\nTo escape arrays or objects containing string values, use the `-o`/`--object` option:\n\n```bash\n$ jsesc --object '{ \"föo\": \"♥\", \"bår\": \"𝌆 baz\" }'\n{'f\\xF6o':'\\u2665','b\\xE5r':'\\uD834\\uDF06 baz'}\n```\n\nTo prettify the output in such cases, use the `-p`/`--pretty` option:\n\n```bash\n$ jsesc --pretty '{ \"föo\": \"♥\", \"bår\": \"𝌆 baz\" }'\n{\n  'f\\xF6o': '\\u2665',\n  'b\\xE5r': '\\uD834\\uDF06 baz'\n}\n```\n\nFor valid JSON output, use the `-j`/`--json` option:\n\n```bash\n$ jsesc --json --pretty '{ \"föo\": \"♥\", \"bår\": \"𝌆 baz\" }'\n{\n  \"f\\u00F6o\": \"\\u2665\",\n  \"b\\u00E5r\": \"\\uD834\\uDF06 baz\"\n}\n```\n\nRead a local JSON file, escape any non-ASCII symbols, and save the result to a new file:\n\n```bash\n$ jsesc --json --object \u003c data-raw.json \u003e data-escaped.json\n```\n\nOr do the same with an online JSON file:\n\n```bash\n$ curl -sL \"http://git.io/aorKgQ\" | jsesc --json --object \u003e data-escaped.json\n```\n\nSee `jsesc --help` for the full list of options.\n\n## Support\n\nAs of v3.0.0, jsesc supports Node.js v6+ only.\n\nOlder versions (up to jsesc v1.3.0) support Chrome 27, Firefox 3, Safari 4, Opera 10, IE 6, Node.js v6.0.0, Narwhal 0.3.2, RingoJS 0.8-0.11, PhantomJS 1.9.0, and Rhino 1.7RC4. **Note:** Using the `json` option on objects or arrays that contain non-string values relies on `JSON.parse()`. For legacy environments like IE ≤ 7, use [a `JSON` polyfill](https://bestiejs.github.io/json3/).\n\n## Author\n\n| [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias \"Follow @mathias on Twitter\") |\n|---|\n| [Mathias Bynens](https://mathiasbynens.be/) |\n\n## License\n\nThis library is available under the [MIT](https://mths.be/mit) license.\n","funding_links":[],"categories":["JavaScript","json"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathiasbynens%2Fjsesc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmathiasbynens%2Fjsesc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathiasbynens%2Fjsesc/lists"}