# safe-string-literal

> Escapes and Unescapes strings into safe literals.

Latest version **1.0.5** (published 2023-06-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install safe-string-literal
pnpm add safe-string-literal
yarn add safe-string-literal
bun add safe-string-literal
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2023-06-03 |
| First published | 2018-12-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | A-yon Lee |
| Maintainers | ayonli |
| Keywords | string, literal, escape, unescape, json |

## Links

- npm: https://www.npmjs.com/package/safe-string-literal
- Repository: https://github.com/ayonli/safe-string-literal
- Homepage: https://github.com/ayonli/safe-string-literal#readme
- Issues: https://github.com/ayonli/safe-string-literal/issues
- npm.io page: https://npm.io/package/safe-string-literal

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.5 (latest) — 2023-06-03
- 1.0.4 — 2020-11-28
- 1.0.3 — 2020-11-28
- 1.0.2 — 2020-11-03
- 1.0.1 — 2018-12-14
- 1.0.0 — 2018-12-11

## README

# Safe-String-Literal

**Escapes and Unescapes strings into safe literals.**

Based on ES2015, these special characters will be escaped by default (unless 
setting `excludes` option):

- `"`
- `'`
- <code>`</code>
- `\`
- `\b`
- `\f`
- `\n`
- `\r`
- `\t`
- `\u2028`
- `\u2029`

The escaped strings are very alike with the strings that `JSON.stringify` 
produces, but with support of extra characters <code>\`</code>, `\u2089` and 
`\u2029`.

## Install

### Node.js

```sh
npm i safe-string-literal
```

### Deno

```ts
import { escape, unescape } from "https://deno.land/x/safe_string_literal/index.js";
// Or
import { escape, unescape } from "https://gtihub.com/ayonli/safe-string-literal/raw/master/index.js";
```

## API

- `escape(str: string, excludes?: string | string[]): string`
- `unescape(str: string): string`

## Example (Node.js)

```javascript
/* global describe, it */
"use strict";

const assert = require("assert");
const { escape, unescape } = require("safe-string-literal");

var inputs = [
    "string' with' single' quotes",
    'string" with" double" quotes',
    "string` with` back` quotes",
    "string\\ with\\ backslashes",
    "string\b with\b backspaces",
    "string\f with\f from\f feed\f char",
    "string\n with\n new\n line\n char",
    "string\r with\r return\r char",
    "string\t with\t tab\t char",
    "string\u2028 with\u2028 unicode\u2028 2028",
    "string\u2029 with\u2029 unicode\u2029 2029",
    "a' string\" contains` all\\ characters\b that\f should\n be\r escaped\t, with\u2028 no\u2029 exceptions",
    "a' string\" contains` all\\ characters\b that\f should\n be\r escaped\t, with\u2028 one\u2029 exception",
    "a' string\" contains` all\\ characters\b that\f should\n be\r escaped\t, with\u2028 several\u2029 exceptions",
    "another' string\" contains` all\\ characters\b that\f should\n be\r escaped\t, with\u2028 several\u2029 exceptions"
];
var outputs = [
    "string\\' with\\' single\\' quotes",
    'string\\" with\\" double\\" quotes',
    "string\\` with\\` back\\` quotes",
    "string\\\\ with\\\\ backslashes",
    "string\\b with\\b backspaces",
    "string\\f with\\f from\\f feed\\f char",
    "string\\n with\\n new\\n line\\n char",
    "string\\r with\\r return\\r char",
    "string\\t with\\t tab\\t char",
    "string\\u2028 with\\u2028 unicode\\u2028 2028",
    "string\\u2029 with\\u2029 unicode\\u2029 2029",
    "a\\' string\\\" contains\\` all\\\\ characters\\b that\\f should\\n be\\r escaped\\t, with\\u2028 no\\u2029 exceptions",
    "a' string\\\" contains\\` all\\\\ characters\\b that\\f should\\n be\\r escaped\\t, with\\u2028 one\\u2029 exception",
    "a' string\" contains\\` all\\\\ characters\\b that\\f should\\n be\\r escaped\\t, with\\u2028 several\\u2029 exceptions",
    "another\\' string\" contains` all\\\\ characters\\b that\\f should\\n be\\r escaped\\t, with\\u2028 several\\u2029 exceptions"
];

describe("Escape", () => {
    it("should escape strings and produce expected results", () => {
        for (let i = 0; i < inputs.length; i++) {
            if (i === 12) {
                assert.strictEqual(escape(inputs[i], "'"), outputs[i]);
            } else if (i === 13) {
                assert.strictEqual(escape(inputs[i], "'\""), outputs[i]);
            } else if (i === 14) {
                assert.strictEqual(escape(inputs[i], "\"`"), outputs[i]);
            } else {
                assert.strictEqual(escape(inputs[i]), outputs[i]);
            }
        }
    });
});

describe("Unescape", () => {
    it("should unescape strings and produce expected results", () => {
        for (let i = 0; i < outputs.length; i++) {
            assert.strictEqual(unescape(outputs[i]), inputs[i]);
        }
    });
});
```

---
_Source: https://npm.io/package/safe-string-literal · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
