Package Exports
- slashes
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (slashes) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Slashes
Add or strip backslashes.
If you were previously using v1, see the migration document for guidance on switching to v2.
Getting Started
import { addSlashes, stripSlashes } from 'slashes';
// Or using CommonJS require.
const { addSlashes, stripSlashes } = require('slashes');
addSlashes(`foo\nbar`) === `foo\\nbar`; // true
stripSlashes(`foo\\nbar`) === `foo\nbar`; // trueYou can also experiment using a pre-configured REPL by running the npm start command.
API
Adding Slashes
Function addSlashes(str: string): string
Returns a string with the following default characters escaped:
- backspace (
"\b"->"\\b") - form feed (
"\f"->"\\f") - newline (
"\n"->"\\n") - carriage return (
"\r"->"\\r") - horizontal tab (
"\t"->"\\t") - vertical tab (
"\v"->"\\v") - null (
"\0"->"\\0") - single quote (
"'"->"\\'") - double quote (
"\""->"\\\"") - backslash (
"\\"->"\\\\")
All addSlashes overloads use the above as the default character set to escape if no explicit character set is given.
addSlashes(`\b\f\n\r\t\v\0'"\\`) === `\\b\\f\\n\\r\\t\\v\\0\\'\\"\\\\`; // trueFunction addSlashes(str: string, characters: string): string
An addSlashes overload which returns a string with only the characters in the characters string escaped. The explicit characters completely override the default character set. The default characters string is: "\b\f\n\r\t\v\0'\"\\".
addSlashes(`foo\nbar`, `oa`) === `f\\o\\o\nb\\ar`; // trueCharacters in the unicode supplementary range (code points > 65535) are always converted to a unicode escape surrogate pairs. This is because Javascript strings are UTF-16, which actually sees these as two characters ("😊".length === 2). So, using 😊 in the characters string is actually setting two escapable characters which are not valid individually. If the "😊" character were not escaped to two unicode escape sequences, you would end up with a string containing invalid characters which would print like this: "\\�\\�", instead of valid characters: "\\ud83d\\ude0a".
addSlashes(`foo😊bar`, `😊`) === `foo\\ud83d\\ude0abar`; // trueFunction addSlashes(str: string, count: number): string
An addSlashes overload which returns a string with count layers of slashes added to the default escape character set. This is the same as recursively invoking this function count times.
addSlashes(`"foo\nbar"`, 2) === `\\\\\\"foo\\\\nbar\\\\\\"`; // true
addSlashes(addSlashes(`"foo\nbar"`)) === `\\\\\\"foo\\\\nbar\\\\\\"`; // true
addSlashes(`"foo\nbar"`, 2) === addSlashes(addSlashes(`"foo\nbar"`)); // trueFunction addSlashes(str: string, count: number, characters: string): string
An addSlashes overload which accepts both a count and characters parameter.
Function addSlashes(str: string, options: IAddSlashesOptions): string
An addSlashes overload which accepts an options object.
optionsConfigurable options for adding slashes to a string. Can have the following fields:countNumber of times to add slashes to the string.charactersA string of characters that should be escaped with slashes.escapeNonAsciiWhen true, all non-ASCII characters (unicode code points > 127) will be converted to\xor\uescape sequences.
addSlashes(`†©`, { count: 2, characters: `†©\\`, escapeNonAscii: true }) === `\\\\u2020\\\\xa9`; // trueStripping Slashes
Function stripSlashes(str: string): string
Returns a string with one layer of slashes removed. It will convert all ES6 escape sequences into their corresponding characters. Slashes which are not part of a recognized escape sequence are removed, and the following character is left in place.
stripSlashes(`\\b\\f\\n\\r\\t\\v\\0\\xa9\\u2020\\u{1f60a}\\a\\\\`) === `\b\f\n\r\t\v\0©†😊a\\`; // trueIf a \u{...} escape sequence has a code point greater than 0x10ffff, the slash is removed and the u{...} suffix is left as a literal string.
stripSlashes(`\\u{110000}`) === `u{110000}`; // trueFunction stripSlashes(str: string, count: number): string
A stripSlashes overload which returns a string with count layers of slashes removed. This is the same as recursively invoking this function count times.
stripSlashes(`\\\\n\\\\a\\\\\\\\`, 2) === `\na\\`; // true
stripSlashes(stripSlashes(`\\\\n\\\\a\\\\\\\\`)) === `\na\\`; // true
stripSlashes(`\\\\n\\\\a\\\\\\\\`, 2) === stripSlashes(stripSlashes(`\\\\n\\\\a\\\\\\\\`)); // trueFunction stripSlashes(str: string, options: IStripSlashesOptions): string
A stripSlashes overload which accepts an options object.
optionsConfigurable options for stripping slashes from a string. Can have the following fields:countNumber of times to strip slashes from the string.defaultEscapeValueThe default value for all escape options (b, f, n, r, t, v, 0, x, u, and uEs6). When true, escape options must be explicitly disabled. When false, escape options must be explicitly enabled. Defaults to true.bTrue to convert"\\b"escapes into backspace characters. Defaults todefaultEscapeValue.fTrue to convert"\\f"escapes into form feed characters. Defaults todefaultEscapeValue.nTrue to convert"\\n"escapes into newline (line feed) characters. Defaults todefaultEscapeValue.rTrue to convert"\\r"escapes into carriage return characters. Defaults todefaultEscapeValue.tTrue to convert"\\t"escapes into horizontal tab characters. Defaults todefaultEscapeValue.vTrue to convert"\\v"escapes into vertical tab characters. Defaults todefaultEscapeValue.0True to convert"\\0"escapes into null characters. Defaults todefaultEscapeValue.xTrue to convert"\\x##"escapes (where##is a hex single byte unicode code point) into unicode characters. Defaults todefaultEscapeValue.uTrue to convert"\\u####"escapes (where####is a hex two byte unicode code point) into unicode characters. Defaults todefaultEscapeValue.uEs6True to convert"\\u{#...}"escapes (where#...is a hex unicode code point) into unicode characters. Defaults tou.
If an escape option is false, then the corresponding escape sequence will be treated like any other backslash before a non-escape character. The backslash will be removed, and all trailing characters left untouched.
stripSlashes(`\\\\tfoo\\\\nbar`, {
// Strip slashes twice.
count: 2,
// All escape sequences are disabled by default.
defaultEscapeValue: false,
// Enable newlines escapes explicitly.
n: true
}) === `tfoo\nbar`; // true