Comparing version 2.0.2 to 3.0.0
110
package.json
{ | ||
"name": "slashes", | ||
"description": "Add or strip backslashes.", | ||
"version": "2.0.2", | ||
"license": "ISC", | ||
"description": "Add or remove backslashes (escape or unescape).", | ||
"author": { | ||
@@ -10,84 +8,34 @@ "name": "Chris Ackerman", | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com:ChrisAckerman/slashes" | ||
"license": "ISC", | ||
"version": "3.0.0", | ||
"types": "lib/commonjs/index.d.ts", | ||
"module": "./lib/module/index.js", | ||
"main": "lib/commonjs/index.js", | ||
"exports": { | ||
"types": "./lib/commonjs/index.d.ts", | ||
"import": "./lib/module/index.js", | ||
"default": "./lib/commonjs/index.js" | ||
}, | ||
"main": "lib-commonjs/index.js", | ||
"module": "lib/index.js", | ||
"files": [ | ||
"README.md", | ||
"css/", | ||
"dist/", | ||
"lib-commonjs/", | ||
"lib/", | ||
"scss/" | ||
], | ||
"sideEffects": false, | ||
"scripts": { | ||
"start": "run-script build:commonjs && node ./lib-commonjs/repl.js", | ||
"test": "run-script eslint && run-script tsc --noEmit && run-script jest", | ||
"clean": "del-cli lib lib-commonjs dist css coverage && jest --clearCache", | ||
"build": "run-script clean && run-script build:js && run-script build:commonjs && run-script build:d.ts && run-script build:cleanup", | ||
"build:cleanup": "del-cli \"lib?(-*)/**/@(__demo__|__snapshots__|?(*.)test.@(ts|tsx|js|jsx))\"", | ||
"build:commonjs": "cross-env NODE_ENV=production BABEL_MODULES=cjs babel src --out-dir lib-commonjs --extensions .ts,.tsx,.js,.jsx --copy-files --source-maps", | ||
"build:d.ts": "run-script tsc --emitDeclarationOnly && run-script tsc --emitDeclarationOnly --outDir lib-commonjs", | ||
"build:js": "cross-env NODE_ENV=production BABEL_MODULES=false babel src --out-dir lib --extensions .ts,.tsx,.js,.jsx --copy-files --source-maps", | ||
"eslint": "eslint \"src/**/*.{js,jsx,ts,tsx}\" --ignore-pattern \"/__demo__/\" --no-error-on-unmatched-pattern", | ||
"jest": "cross-env NODE_ENV=test jest --passWithNoTests", | ||
"prepack": "run-script build", | ||
"prepublishOnly": "run-script test", | ||
"reformat": "run-script eslint --fix --quiet", | ||
"tsc": "tsc" | ||
"test": "del-cli out && tsc --noEmit && eslint --max-warnings=0 src && jest --no-cache", | ||
"build": "del-cli lib && tsc -p tsconfig.build-commonjs.json && tsc -p tsconfig.build-module.json && tsc -p tsconfig.build-types.json", | ||
"clean": "del-cli lib out", | ||
"modernize": "npm-check-updates -u -x @types/node", | ||
"coveralls": "cat ./out/coverage/lcov.info | coveralls" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.8.4", | ||
"@babel/core": "^7.8.4", | ||
"@babel/generator": "^7.8.4", | ||
"@babel/plugin-proposal-class-properties": "^7.8.3", | ||
"@babel/plugin-proposal-decorators": "^7.8.3", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.8.3", | ||
"@babel/plugin-transform-typescript": "^7.8.3", | ||
"@babel/preset-env": "^7.8.4", | ||
"@types/jest": "^25.1.3", | ||
"@types/node": "^13.7.4", | ||
"@typescript-eslint/eslint-plugin": "^2.21.0", | ||
"@typescript-eslint/parser": "^2.21.0", | ||
"@xornot/run-script": "^0.1.2", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-jest": "^25.1.0", | ||
"babel-plugin-const-enum": "^0.0.5", | ||
"cross-env": "^7.0.0", | ||
"del-cli": "^3.0.0", | ||
"eslint": "^6.8.0", | ||
"eslint-config-prettier": "^6.10.0", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"eslint-plugin-react": "^7.18.3", | ||
"jest": "^25.1.0", | ||
"prettier": "^1.19.1", | ||
"typescript": "^3.8.2" | ||
}, | ||
"keywords": [ | ||
"escape", | ||
"unescape", | ||
"slashes", | ||
"php", | ||
"strip", | ||
"sequence", | ||
"encode", | ||
"decode", | ||
"backslash", | ||
"slash", | ||
"unicode", | ||
"ascii", | ||
"utf8", | ||
"utf16", | ||
"control", | ||
"char", | ||
"character", | ||
"unprintable", | ||
"printable", | ||
"codepoint", | ||
"codeblock", | ||
"supplemental", | ||
"supplementary" | ||
] | ||
"@babel/core": "^7.17.9", | ||
"@babel/preset-env": "^7.16.11", | ||
"@types/jest": "^27.4.1", | ||
"coveralls": "^3.1.1", | ||
"del-cli": "^4.0.1", | ||
"eslint": "^8.14.0", | ||
"eslint-config-rational": "^0.1.7", | ||
"jest": "^27.5.1", | ||
"npm-check-updates": "^12.5.9", | ||
"prettier": "^2.6.2", | ||
"ts-jest": "^27.1.4", | ||
"typescript": "^4.6.3" | ||
} | ||
} |
152
README.md
# Slashes | ||
Add or strip backslashes. | ||
Add or remove backslashes (escape or unescape). | ||
If you were previously using v1, see the [migration](https://github.com/ChrisAckerman/slashes/blob/master/MIGRATION.md) document for guidance on switching to v2. | ||
[![npm version](https://badgen.net/npm/v/slashes?icon=npm&label=version)](https://www.npmjs.com/package/slashes) | ||
[![npm downloads](https://badgen.net/npm/dw/slashes?icon=npm&color=blue&label=downloads)](https://www.npmjs.com/package/slashes) | ||
[![github stars](https://badgen.net/github/stars/Shakeskeyboarde/slashes?icon=github)](https://github.com/Shakeskeyboarde/slashes) | ||
[![build status](https://badgen.net/travis/Shakeskeyboarde/slashes?icon=travis&label=build)](https://www.travis-ci.com/github/Shakeskeyboarde/slashes) | ||
[![coverage status](https://badgen.net/coveralls/c/github/Shakeskeyboarde/slashes/main)](https://coveralls.io/github/Shakeskeyboarde/slashes) | ||
[![bundle size](https://badgen.net/bundlephobia/minzip/slashes?label=size)](https://bundlephobia.com/result?p=slashes) | ||
@@ -10,133 +15,76 @@ # Getting Started | ||
```ts | ||
import { addSlashes, stripSlashes } from 'slashes'; | ||
// Or using CommonJS require. | ||
const { addSlashes, stripSlashes } = require('slashes'); | ||
import { addSlashes, removeSlashes } from 'slashes'; | ||
addSlashes(`foo\nbar`) === `foo\\nbar`; // true | ||
stripSlashes(`foo\\nbar`) === `foo\nbar`; // true | ||
addSlashes(`foo\nbar`); // "foo\\nbar" | ||
removeSlashes(`foo\\nbar`); // "foo\nbar" | ||
``` | ||
You can also experiment using a pre-configured REPL by running the `npm start` command. | ||
# API | ||
## Adding Slashes | ||
### **Function** `addSlashes(str: string): string` | ||
By default, `addSlashes` will escape the following JSON-unsafe characters. | ||
Returns a string with the following default characters escaped: | ||
- Backspace (`\b`) | ||
- Form Feed (`\f`) | ||
- Newline (`\n`) | ||
- Carriage Return (`\r`) | ||
- Horizontal Tab (`\t`) | ||
- Vertical Tab (`\v`) | ||
- Null (`\0`) | ||
- Double Quote (`"`) | ||
- Backslash (`\`) | ||
- 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. | ||
```ts | ||
addSlashes(`\b\f\n\r\t\v\0'"\\`) === `\\b\\f\\n\\r\\t\\v\\0\\'\\"\\\\`; // true | ||
const encoded = addSlashes(`\n`); // "\\n" | ||
``` | ||
### **Function** `addSlashes(str: string, characters: string): string` | ||
This default character set is the characters which cannot be included in a JSON string literal. | ||
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'\"\\"`. | ||
```ts | ||
addSlashes(`foo\nbar`, `oa`) === `f\\o\\o\nb\\ar`; // true | ||
const jsonString = `{ "value": "${encoded}" }`; | ||
``` | ||
Characters 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"`. | ||
Escape encoding can be customized using the `getEscaped` option. | ||
The following is the default, equivalent to not setting the `getEscaped` option. | ||
```ts | ||
addSlashes(`foo😊bar`, `😊`) === `foo\\ud83d\\ude0abar`; // true | ||
``` | ||
import { getEscapedJsonUnsafe } from 'slashes'; | ||
### **Function** `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. | ||
```ts | ||
addSlashes(`"foo\nbar"`, 2) === `\\\\\\"foo\\\\nbar\\\\\\"`; // true | ||
addSlashes(addSlashes(`"foo\nbar"`)) === `\\\\\\"foo\\\\nbar\\\\\\"`; // true | ||
addSlashes(`"foo\nbar"`, 2) === addSlashes(addSlashes(`"foo\nbar"`)); // true | ||
addSlashes(`\n`, { getEscaped: getEscapedJsonUnsafe }); // "\\n" | ||
``` | ||
### **Function** `addSlashes(str: string, count: number, characters: string): string` | ||
Included `getEscaped` implementations: | ||
An `addSlashes` overload which accepts both a `count` and `characters` parameter. | ||
- `getEscapedJsonUnsafe` - (Default) Encode characters which cannot be used between quotes in a JSON string. | ||
- `getEscapedAny` - Encode _ANY_ character to a single letter (eg. `\n`) or an ES5 Unicode (eg. `\u0100`) escape sequence. | ||
### **Function** `addSlashes(str: string, options: IAddSlashesOptions): string` | ||
## Removing Slashes | ||
An `addSlashes` overload which accepts an options object. | ||
The `removeSlashes` function will _always_ remove one layer of slashes. | ||
- `options` Configurable options for adding slashes to a string. Can have the following fields: | ||
- `count` Number of times to add slashes to the string. | ||
- `characters` A string of characters that should be escaped with slashes. | ||
- `escapeNonAscii` When true, all non-ASCII characters (unicode code points > 127) will be converted to `\x` or `\u` escape sequences. | ||
```ts | ||
addSlashes(`†©`, { count: 2, characters: `†©\\`, escapeNonAscii: true }) === `\\\\u2020\\\\xa9`; // true | ||
// Handles letter escapes | ||
removeSlashes(`\\n`); // "\n" | ||
// Handles ES6 Unicode Code Point escapes | ||
removeSlashes('\\u{a}'); // "\n" | ||
// Handles ES5 Unicode escapes | ||
removeSlashes('\u000a'); // "\n" | ||
// Handles hex escapes | ||
removeSlashes('\x0a'); // "\n" | ||
// Handles octal escapes | ||
removeSlashes('\12'); // "\n" | ||
// The slash is removed if the escape sequence is invalid | ||
removeSlashes(`\\a`); // "a" | ||
``` | ||
## Stripping Slashes | ||
Although it should generally not be necessary because all escapes are automatically handled, escape decoding can be customized using the `getUnescaped` option. | ||
### **Function** `stripSlashes(str: string): string` | ||
The following is the default, equivalent to not setting the `getUnescaped` option. | ||
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. | ||
```ts | ||
stripSlashes(`\\b\\f\\n\\r\\t\\v\\0\\xa9\\u2020\\u{1f60a}\\a\\\\`) === `\b\f\n\r\t\v\0©†😊a\\`; // true | ||
``` | ||
import { getUnescapedAny } from 'slashes'; | ||
If 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. | ||
```ts | ||
stripSlashes(`\\u{110000}`) === `u{110000}`; // true | ||
removeSlashes('\\n', { getUnescaped: getUnescapedAny }); // "\n" | ||
``` | ||
### **Function** `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. | ||
```ts | ||
stripSlashes(`\\\\n\\\\a\\\\\\\\`, 2) === `\na\\`; // true | ||
stripSlashes(stripSlashes(`\\\\n\\\\a\\\\\\\\`)) === `\na\\`; // true | ||
stripSlashes(`\\\\n\\\\a\\\\\\\\`, 2) === stripSlashes(stripSlashes(`\\\\n\\\\a\\\\\\\\`)); // true | ||
``` | ||
### **Function** `stripSlashes(str: string, options: IStripSlashesOptions): string` | ||
A `stripSlashes` overload which accepts an options object. | ||
- `options` Configurable options for stripping slashes from a string. Can have the following fields: | ||
- `count` Number of times to strip slashes from the string. | ||
- `defaultEscapeValue` The 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. | ||
- `b` True to convert `"\\b"` escapes into backspace characters. Defaults to `defaultEscapeValue`. | ||
- `f` True to convert `"\\f"` escapes into form feed characters. Defaults to `defaultEscapeValue`. | ||
- `n` True to convert `"\\n"` escapes into newline (line feed) characters. Defaults to `defaultEscapeValue`. | ||
- `r` True to convert `"\\r"` escapes into carriage return characters. Defaults to `defaultEscapeValue`. | ||
- `t` True to convert `"\\t"` escapes into horizontal tab characters. Defaults to `defaultEscapeValue`. | ||
- `v` True to convert `"\\v"` escapes into vertical tab characters. Defaults to `defaultEscapeValue`. | ||
- `0` True to convert `"\\0"` escapes into null characters. Defaults to `defaultEscapeValue`. | ||
- `x` True to convert `"\\x##"` escapes (where `##` is a hex single byte unicode code point) into unicode characters. Defaults to `defaultEscapeValue`. | ||
- `u` True to convert `"\\u####"` escapes (where `####` is a hex two byte unicode code point) into unicode characters. Defaults to `defaultEscapeValue`. | ||
- `uEs6` True to convert `"\\u{#...}"` escapes (where `#...` is a hex unicode code point) into unicode characters. Defaults to `u`. | ||
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. | ||
```ts | ||
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 | ||
``` | ||
The `getUnescapedAny` implementation is the only one included. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
12
72
775
1
39022
90
1