This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+73
View File
@@ -0,0 +1,73 @@
export interface IAddSlashesOptions {
/**
* Number of times to add slashes. Equivalent to invoking the function
* `count` times. Defaults to 1.
*/
count?: number;
/**
* Characters to escape. Defaults to all single character escape sequence
* characters (i.e. `\b`, `\f`, `\n`, `\r`, `\t`, `\v`, `\0`, `\'`, `\"`,
* and `\\`).
*/
characters?: string;
/**
* Use escape sequences (e.g. `"\xa9"` or `"\u2020"`) for non-ascii
* characters (i.e. unicode code point 0x0080 and greater). Defaults to
* false.
*
* _NOTE: Escaped code points over 2 bytes (Unicode supplementary planes),
* are **always** encoded using two escape sequences as a surrogate pair
* (e.g. `"\\ud83d\\ude0a"`), so that the output doesn't contain invalid
* UTF-16 characters._
*/
escapeNonAscii?: boolean;
}
/**
* Escape all single character escapable sequences (i.e. `\b`, `\f`, `\n`,
* `\r`, `\t`, `\v`, `\0`, `\'`, `\"`, and `\\`).
*
* @param str String in which to escape characters.
* @param options Options for which characters to add slashes to, how many
* slashes to add, and whether to use escape sequences for non-ascii
* characters.
*/
export declare function addSlashes(str: string, options?: IAddSlashesOptions): string;
/**
* Escape all single character escapable sequences (i.e. `\b`, `\f`, `\n`,
* `\r`, `\t`, `\v`, `\0`, `\'`, `\"`, and `\\`).
*
* _NOTE: Escaped code points over 2 bytes (Unicode supplementary planes),
* are **always** encoded using two escape sequences as a surrogate pair
* (e.g. `"\\ud83d\\ude0a"`), so that the output doesn't contain invalid
* UTF-16 characters._
*
* @param str String in which to escape characters.
* @param characters Characters to escape. Defaults to all single character
* escape sequence characters (i.e. `\b`, `\f`, `\n`, `\r`, `\t`, `\v`, `\0`,
* `\'`, `\"`, and `\\`).
*/
export declare function addSlashes(str: string, characters?: string): string;
/**
* Escape specific characters.
*
* _NOTE: Escaped code points over 2 bytes (Unicode supplementary planes),
* are **always** encoded using two escape sequences as a surrogate pair
* (e.g. `"\\ud83d\\ude0a"`), so that the output doesn't contain invalid
* UTF-16 characters._
*
* @param str String to add slashes to.
* @param count Number of times to add slashes. Equivalent to invoking the
* function `count` times. Defaults to 1.
* @param characters Characters to escape. Defaults to all single character
* escape sequence characters (i.e. `\b`, `\f`, `\n`, `\r`, `\t`, `\v`, `\0`,
* `\'`, `\"`, and `\\`).
*/
export declare function addSlashes(str: string, count?: number, characters?: string): string;
/**
* @deprecated Use `addSlashes()` instead.
*
* Maintains the legacy behavior of only adding slashes to newlines (`"\n"`),
* carriage returns (`"\r"`), nulls (`"\0"`), single quotes (`"'"`), double
* quotes (`"\""`), and backslashes (`"\\"`).
*/
export declare const add: (str: string, count?: number | undefined) => string;
+66
View File
@@ -0,0 +1,66 @@
import { deprecated } from './deprecated';
import { CHAR_TO_ESCAPE } from './escapes';
export function addSlashes(str, countOrCharactersOrOptions, characters = `\b\f\n\r\t\v\0'"\\`) {
let count = 1;
let escapeNonAscii = false;
if (typeof countOrCharactersOrOptions === 'number') {
count = countOrCharactersOrOptions;
} else if (countOrCharactersOrOptions) {
if (typeof countOrCharactersOrOptions === 'object') {
({
count = count,
characters = characters,
escapeNonAscii = escapeNonAscii
} = countOrCharactersOrOptions);
} else {
characters = countOrCharactersOrOptions;
}
}
const rx = new RegExp(`[${characters.replace(/[\]\\^]/g, '\\$&')}]`, 'g');
for (let i = Math.max(1, count >> 0); i > 0; --i) {
str = str.replace(rx, char => {
const escape = CHAR_TO_ESCAPE.get(char);
if (escape) {
return escape;
}
const charCode = char.charCodeAt(0);
if (charCode >= 0xd800 && charCode <= 0xf8ff) {
const hex = charCode.toString(16);
return `\\u${hex}`;
} else if (escapeNonAscii && charCode > 0x7f) {
let hex = charCode.toString(16);
if (charCode <= 0xff) {
return `\\x${hex}`;
} else {
while (hex.length < 4) hex = `0${hex}`;
return `\\u${hex}`;
}
}
return `\\${char}`;
});
}
return str;
}
/**
* @deprecated Use `addSlashes()` instead.
*
* Maintains the legacy behavior of only adding slashes to newlines (`"\n"`),
* carriage returns (`"\r"`), nulls (`"\0"`), single quotes (`"'"`), double
* quotes (`"\""`), and backslashes (`"\\"`).
*/
export const add = (str, count) => {
deprecated('The add() function is deprecated and should be replaced with addSlashes().');
return addSlashes(str, count, `\n\0'"\\`);
};
//# sourceMappingURL=addSlashes.js.map
+1
View File
File diff suppressed because one or more lines are too long
+1
View File
@@ -0,0 +1 @@
export declare function deprecated(message: string): void;
+8
View File
@@ -0,0 +1,8 @@
const guard = new Set();
export function deprecated(message) {
if (!guard.has(message)) {
guard.add(message);
console.warn(message);
}
}
//# sourceMappingURL=deprecated.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../src/deprecated.ts"],"names":["guard","Set","deprecated","message","has","add","console","warn"],"mappings":"AAAA,MAAMA,KAAK,GAAG,IAAIC,GAAJ,EAAd;AAEA,OAAO,SAASC,UAAT,CAAoBC,OAApB,EAAqC;AAC1C,MAAI,CAACH,KAAK,CAACI,GAAN,CAAUD,OAAV,CAAL,EAAyB;AACvBH,IAAAA,KAAK,CAACK,GAAN,CAAUF,OAAV;AACAG,IAAAA,OAAO,CAACC,IAAR,CAAaJ,OAAb;AACD;AACF","sourcesContent":["const guard = new Set<string>();\n\nexport function deprecated(message: string) {\n if (!guard.has(message)) {\n guard.add(message);\n console.warn(message);\n }\n}\n"],"file":"deprecated.js"}
+3
View File
@@ -0,0 +1,3 @@
export declare function getSingleCharEscapes(): Map<string, string>;
export declare const SINGLE_CHAR_ESCAPES: ReadonlyMap<string, string>;
export declare const CHAR_TO_ESCAPE: ReadonlyMap<string, string>;
+13
View File
@@ -0,0 +1,13 @@
export function getSingleCharEscapes() {
return [['b', '\b'], ['f', '\f'], ['n', '\n'], ['r', '\r'], ['t', '\t'], ['v', '\v'], ['0', '\0']].reduce((map, [key, value]) => {
map.set(key, value);
return map;
}, new Map());
}
export const SINGLE_CHAR_ESCAPES = getSingleCharEscapes();
export const CHAR_TO_ESCAPE = [...SINGLE_CHAR_ESCAPES.keys()].reduce((map, code) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
map.set(SINGLE_CHAR_ESCAPES.get(code), `\\${code}`);
return map;
}, new Map());
//# sourceMappingURL=escapes.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../src/escapes.ts"],"names":["getSingleCharEscapes","reduce","map","key","value","set","Map","SINGLE_CHAR_ESCAPES","CHAR_TO_ESCAPE","keys","code","get"],"mappings":"AAAA,OAAO,SAASA,oBAAT,GAAqD;AAC1D,SAAO,CACL,CAAC,GAAD,EAAM,IAAN,CADK,EAEL,CAAC,GAAD,EAAM,IAAN,CAFK,EAGL,CAAC,GAAD,EAAM,IAAN,CAHK,EAIL,CAAC,GAAD,EAAM,IAAN,CAJK,EAKL,CAAC,GAAD,EAAM,IAAN,CALK,EAML,CAAC,GAAD,EAAM,IAAN,CANK,EAOL,CAAC,GAAD,EAAM,IAAN,CAPK,EAQLC,MARK,CAQE,CAACC,GAAD,EAAM,CAACC,GAAD,EAAMC,KAAN,CAAN,KAAuB;AAC9BF,IAAAA,GAAG,CAACG,GAAJ,CAAQF,GAAR,EAAaC,KAAb;AACA,WAAOF,GAAP;AACD,GAXM,EAWJ,IAAII,GAAJ,EAXI,CAAP;AAYD;AAED,OAAO,MAAMC,mBAAgD,GAAGP,oBAAoB,EAA7E;AAEP,OAAO,MAAMQ,cAA2C,GAAG,CAAC,GAAGD,mBAAmB,CAACE,IAApB,EAAJ,EAAgCR,MAAhC,CAAuC,CAACC,GAAD,EAAMQ,IAAN,KAAe;AAC/G;AACAR,EAAAA,GAAG,CAACG,GAAJ,CAAQE,mBAAmB,CAACI,GAApB,CAAwBD,IAAxB,CAAR,EAAyC,KAAIA,IAAK,EAAlD;AACA,SAAOR,GAAP;AACD,CAJ0D,EAIxD,IAAII,GAAJ,EAJwD,CAApD","sourcesContent":["export function getSingleCharEscapes(): Map<string, string> {\n return [\n ['b', '\\b'],\n ['f', '\\f'],\n ['n', '\\n'],\n ['r', '\\r'],\n ['t', '\\t'],\n ['v', '\\v'],\n ['0', '\\0']\n ].reduce((map, [key, value]) => {\n map.set(key, value);\n return map;\n }, new Map());\n}\n\nexport const SINGLE_CHAR_ESCAPES: ReadonlyMap<string, string> = getSingleCharEscapes();\n\nexport const CHAR_TO_ESCAPE: ReadonlyMap<string, string> = [...SINGLE_CHAR_ESCAPES.keys()].reduce((map, code) => {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n map.set(SINGLE_CHAR_ESCAPES.get(code)!, `\\\\${code}`);\n return map;\n}, new Map<string, string>());\n"],"file":"escapes.js"}
+2
View File
@@ -0,0 +1,2 @@
export * from './addSlashes';
export * from './stripSlashes';
+3
View File
@@ -0,0 +1,3 @@
export * from './addSlashes';
export * from './stripSlashes';
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAd;AACA,cAAc,gBAAd","sourcesContent":["export * from './addSlashes';\nexport * from './stripSlashes';\n"],"file":"index.js"}
+1
View File
@@ -0,0 +1 @@
export {};
+21
View File
@@ -0,0 +1,21 @@
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/* istanbul ignore file */
import repl from 'repl';
import * as slashes from './';
const context = _objectSpread({
slashes
}, slashes);
console.log();
console.log('The library API has been added to the REPL context:');
Object.keys(context).forEach(key => console.log(` ${key}`));
console.log();
const r = repl.start();
Object.assign(r.context, context);
//# sourceMappingURL=repl.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../src/repl.ts"],"names":["repl","slashes","context","console","log","Object","keys","forEach","key","r","start","assign"],"mappings":";;;;;;AAAA;AACA,OAAOA,IAAP,MAAiB,MAAjB;AAEA,OAAO,KAAKC,OAAZ,MAAyB,IAAzB;;AAEA,MAAMC,OAAO;AAAKD,EAAAA;AAAL,GAAiBA,OAAjB,CAAb;;AAEAE,OAAO,CAACC,GAAR;AACAD,OAAO,CAACC,GAAR,CAAY,qDAAZ;AACAC,MAAM,CAACC,IAAP,CAAYJ,OAAZ,EAAqBK,OAArB,CAA6BC,GAAG,IAAIL,OAAO,CAACC,GAAR,CAAa,KAAII,GAAI,EAArB,CAApC;AACAL,OAAO,CAACC,GAAR;AAEA,MAAMK,CAAC,GAAGT,IAAI,CAACU,KAAL,EAAV;AAEAL,MAAM,CAACM,MAAP,CAAcF,CAAC,CAACP,OAAhB,EAAyBA,OAAzB","sourcesContent":["/* istanbul ignore file */\nimport repl from 'repl';\n\nimport * as slashes from './';\n\nconst context = { slashes, ...slashes };\n\nconsole.log();\nconsole.log('The library API has been added to the REPL context:');\nObject.keys(context).forEach(key => console.log(` ${key}`));\nconsole.log();\n\nconst r = repl.start();\n\nObject.assign(r.context, context);\n"],"file":"repl.js"}
+81
View File
@@ -0,0 +1,81 @@
export interface IStripSlashesOptions {
/**
* Number of times to strip slashes. Equivalent to invoking the function
* `count` times. Defaults to 1.
*/
readonly count?: number;
/**
* 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.
*/
readonly defaultEscapeValue?: boolean;
/**
* Unescape `"\\b"` to a backspace character.
*/
readonly b?: boolean;
/**
* Unescape `"\\f"` to a form feed character.
*/
readonly f?: boolean;
/**
* Unescape `"\\n"` to a newline (line feed) character.
*/
readonly n?: boolean;
/**
* Unescape `"\\r"` to a carriage return character.
*/
readonly r?: boolean;
/**
* Unescape `"\\t"` to a tag character.
*/
readonly t?: boolean;
/**
* Unescape `"\\v"` to a vertical tab character.
*/
readonly v?: boolean;
/**
* Unescape `"\\0"` to a null character.
*/
readonly '0'?: boolean;
/**
* Unescape hex escape sequences (e.g. `"\\xa9"`).
*/
readonly x?: boolean;
/**
* Unescape basic unicode escape sequences (e.g. `"\\u2020"`).
*/
readonly u?: boolean;
/**
* Unescape ES6 unicode code point escape sequences (e.g. `"\\u{1F60a}"`).
*/
readonly uEs6?: boolean;
}
/**
* Remove one layer of slashes. All escape sequences will be transformed into
* the character indicated by the sequence (e.g. `"\\n"` will become a
* newline).
*
* @param str String to remove slashes from.
* @param options Options for which escape sequences to recognize. Defaults to
* recognizing all single character, hex, and unicode escape sequences
*/
export declare function stripSlashes(str: string, options?: IStripSlashesOptions): string;
/**
* Remove one layer of slashes. All escape sequences will be transformed into
* the character indicated by the sequence (e.g. `"\\n"` will become a
* newline).
*
* @param str String to remove slashes from.
* @param count Number of times to strip slashes. Equivalent to invoking the
* function `count` times. Defaults to 1.
*/
export declare function stripSlashes(str: string, count?: number): string;
/**
* @deprecated Use `stripSlashes()` instead.
*
* Maintains legacy behavior of _only_ recognizing newline (`"\n"`) and null
* escape sequences (`"\0"`). Other slashes will not consume any trailing
* characters (e.g. `"\\r"` becomes `"r"`, _not a carriage return_).
*/
export declare const strip: (str: string, count?: number | undefined) => string;
+97
View File
@@ -0,0 +1,97 @@
import { deprecated } from './deprecated';
import { getSingleCharEscapes } from './escapes';
export function stripSlashes(str, countOrOptions) {
let options;
if (typeof countOrOptions === 'number') {
options = {
count: countOrOptions
};
} else if (countOrOptions && typeof countOrOptions === 'object') {
options = countOrOptions;
} else {
options = {};
}
const {
count = 1,
defaultEscapeValue = true,
b = defaultEscapeValue,
f = defaultEscapeValue,
n = defaultEscapeValue,
r = defaultEscapeValue,
t = defaultEscapeValue,
v = defaultEscapeValue,
'0': nul = defaultEscapeValue,
x = defaultEscapeValue,
u = defaultEscapeValue,
uEs6 = u
} = options;
const singleCharEscapes = getSingleCharEscapes();
if (!b) singleCharEscapes.delete('b');
if (!f) singleCharEscapes.delete('f');
if (!n) singleCharEscapes.delete('n');
if (!r) singleCharEscapes.delete('r');
if (!t) singleCharEscapes.delete('t');
if (!v) singleCharEscapes.delete('v');
if (!nul) singleCharEscapes.delete('0');
const patterns = [];
if (x) patterns.push('x[a-fA-F0-9]{2}');
if (u) patterns.push('u[a-fA-F0-9]{4}');
if (uEs6) patterns.push('u{[a-fA-F0-9]+}');
const rx = new RegExp(`\\\\(${[...patterns, '.', '$'].join('|')})`, 'g');
for (let i = Math.max(1, count >> 0); i > 0; --i) {
str = str.replace(rx, (...[, g1]) => {
const suffix = g1;
if (suffix.length > 1) {
if (suffix[0] === 'x') {
return String.fromCharCode(Number.parseInt(suffix.slice(1), 16));
} else if (suffix[0] === 'u') {
if (suffix[1] === '{') {
const codePointStr = suffix.slice(2, -1);
const codePoint = Number.parseInt(codePointStr, 16);
if (codePoint > 0x10ffff) {
// Not valid unicode, so just remove the slash, leaving the
// trailing characters in place.
return suffix;
} else if (codePoint >= 0x10000) {
const u = codePoint - 0x10000;
const s1 = 0xd800 + (u >> 10);
const s2 = 0xdc00 + (u & 0x3ff);
return `${String.fromCharCode(s1, s2)}`;
}
return String.fromCharCode(codePoint);
}
return String.fromCharCode(Number.parseInt(suffix.slice(1), 16));
}
}
return singleCharEscapes.get(suffix) || suffix;
});
}
return str;
}
/**
* @deprecated Use `stripSlashes()` instead.
*
* Maintains legacy behavior of _only_ recognizing newline (`"\n"`) and null
* escape sequences (`"\0"`). Other slashes will not consume any trailing
* characters (e.g. `"\\r"` becomes `"r"`, _not a carriage return_).
*/
export const strip = (str, count) => {
deprecated('The strip() function is deprecated and should be replaced with stripSlashes().');
return stripSlashes(str, {
count,
defaultEscapeValue: false,
n: true,
'0': true
});
};
//# sourceMappingURL=stripSlashes.js.map
+1
View File
File diff suppressed because one or more lines are too long