Socket
Socket
Sign inDemoInstall

unraw

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unraw - npm Package Compare versions

Comparing version 1.2.5 to 2.0.0

4

changelog.md

@@ -5,2 +5,6 @@ # Change Log

## 2.0.0
- Change to CommonJS module form
## 1.2.5

@@ -7,0 +11,0 @@

124

dist/errors.js

@@ -0,1 +1,2 @@

"use strict";
/**

@@ -7,71 +8,60 @@ * @file **unraw - errors.ts** | Error messages used by `unraw`.

*/
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// NOTE: don't construct errors here or they'll have the wrong stack trace.
// NOTE: don't make custom error class; the JS engines use `SyntaxError`
Object.defineProperty(exports, "__esModule", { value: true });
// NOTE: don't construct errors here or they'll have the wrong stack trace.
// NOTE: don't make custom error class; the JS engines use `SyntaxError`
/**
* Keys for possible error messages used by `unraw`.
* Note: These do _not_ map to actual error object types. All errors thrown
* are `SyntaxError`.
*/
// Don't use const enum or JS users won't be able to access the enum values
var ErrorType;
(function (ErrorType) {
/**
* Keys for possible error messages used by `unraw`.
* Note: These do _not_ map to actual error object types. All errors thrown
* are `SyntaxError`.
* Thrown when a badly formed Unicode escape sequence is found. Possible
* reasons include the code being too short (`"\u25"`) or having invalid
* characters (`"\u2$A5"`).
*/
// Don't use const enum or JS users won't be able to access the enum values
var ErrorType;
(function (ErrorType) {
/**
* Thrown when a badly formed Unicode escape sequence is found. Possible
* reasons include the code being too short (`"\u25"`) or having invalid
* characters (`"\u2$A5"`).
*/
ErrorType["MalformedUnicode"] = "MALFORMED_UNICODE";
/**
* Thrown when a badly formed hexadecimal escape sequence is found. Possible
* reasons include the code being too short (`"\x2"`) or having invalid
* characters (`"\x2$"`).
*/
ErrorType["MalformedHexadecimal"] = "MALFORMED_HEXADECIMAL";
/**
* Thrown when a Unicode code point escape sequence has too high of a code
* point. The maximum code point allowed is `\u{10FFFF}`, so `\u{110000}` and
* higher will throw this error.
*/
ErrorType["CodePointLimit"] = "CODE_POINT_LIMIT";
/**
* Thrown when an octal escape sequences is encountered and `allowOctals` is
* `false`. For example, `unraw("\234", false)`.
*/
ErrorType["OctalDeprecation"] = "OCTAL_DEPRECATION";
/**
* Thrown only when a single backslash is found at the end of a string. For
* example, `"\\"` or `"test\\x24\\"`.
*/
ErrorType["EndOfString"] = "END_OF_STRING";
})(ErrorType = exports.ErrorType || (exports.ErrorType = {}));
/** Map of error message names to the full text of the message. */
exports.errorMessages = new Map([
[ErrorType.MalformedUnicode, "malformed Unicode character escape sequence"],
[
ErrorType.MalformedHexadecimal,
"malformed hexadecimal character escape sequence"
],
[
ErrorType.CodePointLimit,
"Unicode codepoint must not be greater than 0x10FFFF in escape sequence"
],
[
ErrorType.OctalDeprecation,
'"0"-prefixed octal literals and octal escape sequences are deprecated; ' +
'for octal literals use the "0o" prefix instead'
],
[ErrorType.EndOfString, "malformed escape sequence at end of string"]
]);
});
ErrorType["MalformedUnicode"] = "MALFORMED_UNICODE";
/**
* Thrown when a badly formed hexadecimal escape sequence is found. Possible
* reasons include the code being too short (`"\x2"`) or having invalid
* characters (`"\x2$"`).
*/
ErrorType["MalformedHexadecimal"] = "MALFORMED_HEXADECIMAL";
/**
* Thrown when a Unicode code point escape sequence has too high of a code
* point. The maximum code point allowed is `\u{10FFFF}`, so `\u{110000}` and
* higher will throw this error.
*/
ErrorType["CodePointLimit"] = "CODE_POINT_LIMIT";
/**
* Thrown when an octal escape sequences is encountered and `allowOctals` is
* `false`. For example, `unraw("\234", false)`.
*/
ErrorType["OctalDeprecation"] = "OCTAL_DEPRECATION";
/**
* Thrown only when a single backslash is found at the end of a string. For
* example, `"\\"` or `"test\\x24\\"`.
*/
ErrorType["EndOfString"] = "END_OF_STRING";
})(ErrorType = exports.ErrorType || (exports.ErrorType = {}));
/** Map of error message names to the full text of the message. */
exports.errorMessages = new Map([
[ErrorType.MalformedUnicode, "malformed Unicode character escape sequence"],
[
ErrorType.MalformedHexadecimal,
"malformed hexadecimal character escape sequence"
],
[
ErrorType.CodePointLimit,
"Unicode codepoint must not be greater than 0x10FFFF in escape sequence"
],
[
ErrorType.OctalDeprecation,
'"0"-prefixed octal literals and octal escape sequences are deprecated; ' +
'for octal literals use the "0o" prefix instead'
],
[ErrorType.EndOfString, "malformed escape sequence at end of string"]
]);
//# sourceMappingURL=errors.js.map

@@ -1,1 +0,1 @@

!function(e){if("object"==typeof module&&"object"==typeof module.exports){var o=e(require,exports);void 0!==o&&(module.exports=o)}else"function"==typeof define&&define.amd&&define(["require","exports"],e)}(function(e,o){"use strict";var r;Object.defineProperty(o,"__esModule",{value:!0}),function(e){e.MalformedUnicode="MALFORMED_UNICODE",e.MalformedHexadecimal="MALFORMED_HEXADECIMAL",e.CodePointLimit="CODE_POINT_LIMIT",e.OctalDeprecation="OCTAL_DEPRECATION",e.EndOfString="END_OF_STRING"}(r=o.ErrorType||(o.ErrorType={})),o.errorMessages=new Map([[r.MalformedUnicode,"malformed Unicode character escape sequence"],[r.MalformedHexadecimal,"malformed hexadecimal character escape sequence"],[r.CodePointLimit,"Unicode codepoint must not be greater than 0x10FFFF in escape sequence"],[r.OctalDeprecation,'"0"-prefixed octal literals and octal escape sequences are deprecated; for octal literals use the "0o" prefix instead'],[r.EndOfString,"malformed escape sequence at end of string"]])});
"use strict";var ErrorType;Object.defineProperty(exports,"__esModule",{value:!0}),function(e){e.MalformedUnicode="MALFORMED_UNICODE",e.MalformedHexadecimal="MALFORMED_HEXADECIMAL",e.CodePointLimit="CODE_POINT_LIMIT",e.OctalDeprecation="OCTAL_DEPRECATION",e.EndOfString="END_OF_STRING"}(ErrorType=exports.ErrorType||(exports.ErrorType={})),exports.errorMessages=new Map([[ErrorType.MalformedUnicode,"malformed Unicode character escape sequence"],[ErrorType.MalformedHexadecimal,"malformed hexadecimal character escape sequence"],[ErrorType.CodePointLimit,"Unicode codepoint must not be greater than 0x10FFFF in escape sequence"],[ErrorType.OctalDeprecation,'"0"-prefixed octal literals and octal escape sequences are deprecated; for octal literals use the "0o" prefix instead'],[ErrorType.EndOfString,"malformed escape sequence at end of string"]]);

@@ -0,1 +1,2 @@

"use strict";
/**

@@ -8,200 +9,189 @@ * @file **unraw** | Convert raw escape sequences to their respective characters

*/
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
Object.defineProperty(exports, "__esModule", { value: true });
const errors_1 = require("./errors");
exports.ErrorType = errors_1.ErrorType;
exports.errorMessages = errors_1.errorMessages;
/**
* Parse a string as a base-16 number. This is more strict than `parseInt` as it
* will not allow any other characters, including (for example) "+", "-", and
* ".".
* @param hex A string containing a hexadecimal number.
* @returns The parsed integer, or `NaN` if the string is not a valid hex
* number.
*/
function parseHexToInt(hex) {
const isOnlyHexChars = !hex.match(/[^a-f0-9]/i);
return isOnlyHexChars ? parseInt(hex, 16) : NaN;
}
/**
* Check the validity and length of a hexadecimal code and optionally enforces
* a specific number of hex digits.
* @param hex The string to validate and parse.
* @param errorName The name of the error message to throw a `SyntaxError` with
* if `hex` is invalid. This is used to index `errorMessages`.
* @param enforcedLength If provided, will throw an error if `hex` is not
* exactly this many characters.
* @returns The parsed hex number as a normal number.
* @throws {SyntaxError} If the code is not valid.
*/
function validateAndParseHex(hex, errorName, enforcedLength) {
const parsedHex = parseHexToInt(hex);
if (Number.isNaN(parsedHex) ||
(enforcedLength !== undefined && enforcedLength !== hex.length)) {
throw new SyntaxError(errors_1.errorMessages.get(errorName));
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "./errors"], factory);
return parsedHex;
}
/**
* Parse a two-digit hexadecimal character escape code.
* @param code The two-digit hexadecimal number that represents the character to
* output.
* @returns The single character represented by the code.
* @throws {SyntaxError} If the code is not valid hex or is not the right
* length.
*/
function parseHexadecimalCode(code) {
const parsedCode = validateAndParseHex(code, errors_1.ErrorType.MalformedHexadecimal, 2);
return String.fromCharCode(parsedCode);
}
/**
* Parse a four-digit Unicode character escape code.
* @param code The four-digit unicode number that represents the character to
* output.
* @param surrogateCode Optional four-digit unicode surrogate that represents
* the other half of the character to output.
* @returns The single character represented by the code.
* @throws {SyntaxError} If the codes are not valid hex or are not the right
* length.
*/
function parseUnicodeCode(code, surrogateCode) {
const parsedCode = validateAndParseHex(code, errors_1.ErrorType.MalformedUnicode, 4);
if (surrogateCode !== undefined) {
const parsedSurrogateCode = validateAndParseHex(surrogateCode, errors_1.ErrorType.MalformedUnicode, 4);
return String.fromCharCode(parsedCode, parsedSurrogateCode);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const errors_1 = require("./errors");
exports.ErrorType = errors_1.ErrorType;
exports.errorMessages = errors_1.errorMessages;
/**
* Parse a string as a base-16 number. This is more strict than `parseInt` as it
* will not allow any other characters, including (for example) "+", "-", and
* ".".
* @param hex A string containing a hexadecimal number.
* @returns The parsed integer, or `NaN` if the string is not a valid hex
* number.
*/
function parseHexToInt(hex) {
const isOnlyHexChars = !hex.match(/[^a-f0-9]/i);
return isOnlyHexChars ? parseInt(hex, 16) : NaN;
return String.fromCharCode(parsedCode);
}
/**
* Test if the text is surrounded by curly braces (`{}`).
* @param text Text to check.
* @returns `true` if the text is in the form `{*}`.
*/
function isCurlyBraced(text) {
return text.charAt(0) === "{" && text.charAt(text.length - 1) === "}";
}
/**
* Parse a Unicode code point character escape code.
* @param codePoint A unicode escape code point, including the surrounding curly
* braces.
* @returns The single character represented by the code.
* @throws {SyntaxError} If the code is not valid hex or does not have the
* surrounding curly braces.
*/
function parseUnicodeCodePointCode(codePoint) {
if (!isCurlyBraced(codePoint)) {
throw new SyntaxError(errors_1.errorMessages.get(errors_1.ErrorType.MalformedUnicode));
}
/**
* Check the validity and length of a hexadecimal code and optionally enforces
* a specific number of hex digits.
* @param hex The string to validate and parse.
* @param errorName The name of the error message to throw a `SyntaxError` with
* if `hex` is invalid. This is used to index `errorMessages`.
* @param enforcedLength If provided, will throw an error if `hex` is not
* exactly this many characters.
* @returns The parsed hex number as a normal number.
* @throws {SyntaxError} If the code is not valid.
*/
function validateAndParseHex(hex, errorName, enforcedLength) {
const parsedHex = parseHexToInt(hex);
if (Number.isNaN(parsedHex) ||
(enforcedLength !== undefined && enforcedLength !== hex.length)) {
throw new SyntaxError(errors_1.errorMessages.get(errorName));
}
return parsedHex;
const withoutBraces = codePoint.slice(1, -1);
const parsedCode = validateAndParseHex(withoutBraces, errors_1.ErrorType.MalformedUnicode);
try {
return String.fromCodePoint(parsedCode);
}
/**
* Parse a two-digit hexadecimal character escape code.
* @param code The two-digit hexadecimal number that represents the character to
* output.
* @returns The single character represented by the code.
* @throws {SyntaxError} If the code is not valid hex or is not the right
* length.
*/
function parseHexadecimalCode(code) {
const parsedCode = validateAndParseHex(code, errors_1.ErrorType.MalformedHexadecimal, 2);
return String.fromCharCode(parsedCode);
catch (err) {
throw err instanceof RangeError
? new SyntaxError(errors_1.errorMessages.get(errors_1.ErrorType.CodePointLimit))
: err;
}
/**
* Parse a four-digit Unicode character escape code.
* @param code The four-digit unicode number that represents the character to
* output.
* @param surrogateCode Optional four-digit unicode surrogate that represents
* the other half of the character to output.
* @returns The single character represented by the code.
* @throws {SyntaxError} If the codes are not valid hex or are not the right
* length.
*/
function parseUnicodeCode(code, surrogateCode) {
const parsedCode = validateAndParseHex(code, errors_1.ErrorType.MalformedUnicode, 4);
if (surrogateCode !== undefined) {
const parsedSurrogateCode = validateAndParseHex(surrogateCode, errors_1.ErrorType.MalformedUnicode, 4);
return String.fromCharCode(parsedCode, parsedSurrogateCode);
}
return String.fromCharCode(parsedCode);
}
// Have to give overload that takes boolean for when compiler doesn't know if
// true or false
function parseOctalCode(code, error = false) {
if (error) {
throw new SyntaxError(errors_1.errorMessages.get(errors_1.ErrorType.OctalDeprecation));
}
/**
* Test if the text is surrounded by curly braces (`{}`).
* @param text Text to check.
* @returns `true` if the text is in the form `{*}`.
*/
function isCurlyBraced(text) {
return text.charAt(0) === "{" && text.charAt(text.length - 1) === "}";
}
/**
* Parse a Unicode code point character escape code.
* @param codePoint A unicode escape code point, including the surrounding curly
* braces.
* @returns The single character represented by the code.
* @throws {SyntaxError} If the code is not valid hex or does not have the
* surrounding curly braces.
*/
function parseUnicodeCodePointCode(codePoint) {
if (!isCurlyBraced(codePoint)) {
throw new SyntaxError(errors_1.errorMessages.get(errors_1.ErrorType.MalformedUnicode));
// The original regex only allows digits so we don't need to have a strict
// octal parser like hexToInt. Length is not enforced for octals.
const parsedCode = parseInt(code, 8);
return String.fromCharCode(parsedCode);
}
/**
* Map of unescaped letters to their corresponding special JS escape characters.
* Intentionally does not include characters that map to themselves like "\'".
*/
const singleCharacterEscapes = new Map([
["b", "\b"],
["f", "\f"],
["n", "\n"],
["r", "\r"],
["t", "\t"],
["v", "\v"],
["0", "\0"]
]);
/**
* Parse a single character escape sequence and return the matching character.
* If none is matched, defaults to `code`.
* @param code A single character code.
*/
function parseSingleCharacterCode(code) {
return singleCharacterEscapes.get(code) || code;
}
/**
* Matches every escape sequence possible, including invalid ones.
*
* All capture groups (described below) are unique (only one will match), except
* for 4, which can only potentially match if 3 does.
*
* **Capture Groups:**
* 0. A single backslash
* 1. Hexadecimal code
* 2. Unicode code point code with surrounding curly braces
* 3. Unicode escape code with surrogate
* 4. Surrogate code
* 5. Unicode escape code without surrogate
* 6. Octal code _NOTE: includes "0"._
* 7. A single character (will never be \, x, u, or 0-3)
*/
const escapeMatch = /\\(?:(\\)|x([\s\S]{0,2})|u(\{[^}]*\}?)|u([\s\S]{4})\\u([^{][\s\S]{0,3})|u([\s\S]{0,4})|([0-3]?[0-7]{1,2})|([\s\S])|$)/g;
/**
* Replace raw escape character strings with their escape characters.
* @param raw A string where escape characters are represented as raw string
* values like `\'` rather than `'`.
* @param allowOctals If `true`, will process the now-deprecated octal escape
* sequences (ie, `\111`).
* @returns The processed string, with escape characters replaced by their
* respective actual Unicode characters.
*/
function unraw(raw, allowOctals = false) {
return raw.replace(escapeMatch, function (_, backslash, hex, codePoint, unicodeWithSurrogate, surrogate, unicode, octal, singleCharacter) {
// Compare groups to undefined because empty strings mean different errors
// Otherwise, `\u` would fail the same as `\` which is wrong.
if (backslash !== undefined) {
return "\\";
}
const withoutBraces = codePoint.slice(1, -1);
const parsedCode = validateAndParseHex(withoutBraces, errors_1.ErrorType.MalformedUnicode);
try {
return String.fromCodePoint(parsedCode);
if (hex !== undefined) {
return parseHexadecimalCode(hex);
}
catch (err) {
throw err instanceof RangeError
? new SyntaxError(errors_1.errorMessages.get(errors_1.ErrorType.CodePointLimit))
: err;
if (codePoint !== undefined) {
return parseUnicodeCodePointCode(codePoint);
}
}
// Have to give overload that takes boolean for when compiler doesn't know if
// true or false
function parseOctalCode(code, error = false) {
if (error) {
throw new SyntaxError(errors_1.errorMessages.get(errors_1.ErrorType.OctalDeprecation));
if (unicodeWithSurrogate !== undefined) {
return parseUnicodeCode(unicodeWithSurrogate, surrogate);
}
// The original regex only allows digits so we don't need to have a strict
// octal parser like hexToInt. Length is not enforced for octals.
const parsedCode = parseInt(code, 8);
return String.fromCharCode(parsedCode);
}
/**
* Map of unescaped letters to their corresponding special JS escape characters.
* Intentionally does not include characters that map to themselves like "\'".
*/
const singleCharacterEscapes = new Map([
["b", "\b"],
["f", "\f"],
["n", "\n"],
["r", "\r"],
["t", "\t"],
["v", "\v"],
["0", "\0"]
]);
/**
* Parse a single character escape sequence and return the matching character.
* If none is matched, defaults to `code`.
* @param code A single character code.
*/
function parseSingleCharacterCode(code) {
return singleCharacterEscapes.get(code) || code;
}
/**
* Matches every escape sequence possible, including invalid ones.
*
* All capture groups (described below) are unique (only one will match), except
* for 4, which can only potentially match if 3 does.
*
* **Capture Groups:**
* 0. A single backslash
* 1. Hexadecimal code
* 2. Unicode code point code with surrounding curly braces
* 3. Unicode escape code with surrogate
* 4. Surrogate code
* 5. Unicode escape code without surrogate
* 6. Octal code _NOTE: includes "0"._
* 7. A single character (will never be \, x, u, or 0-3)
*/
const escapeMatch = /\\(?:(\\)|x([\s\S]{0,2})|u(\{[^}]*\}?)|u([\s\S]{4})\\u([^{][\s\S]{0,3})|u([\s\S]{0,4})|([0-3]?[0-7]{1,2})|([\s\S])|$)/g;
/**
* Replace raw escape character strings with their escape characters.
* @param raw A string where escape characters are represented as raw string
* values like `\'` rather than `'`.
* @param allowOctals If `true`, will process the now-deprecated octal escape
* sequences (ie, `\111`).
* @returns The processed string, with escape characters replaced by their
* respective actual Unicode characters.
*/
function unraw(raw, allowOctals = false) {
return raw.replace(escapeMatch, function (_, backslash, hex, codePoint, unicodeWithSurrogate, surrogate, unicode, octal, singleCharacter) {
// Compare groups to undefined because empty strings mean different errors
// Otherwise, `\u` would fail the same as `\` which is wrong.
if (backslash !== undefined) {
return "\\";
}
if (hex !== undefined) {
return parseHexadecimalCode(hex);
}
if (codePoint !== undefined) {
return parseUnicodeCodePointCode(codePoint);
}
if (unicodeWithSurrogate !== undefined) {
return parseUnicodeCode(unicodeWithSurrogate, surrogate);
}
if (unicode !== undefined) {
return parseUnicodeCode(unicode);
}
if (octal === "0") {
return "\0";
}
if (octal !== undefined) {
return parseOctalCode(octal, !allowOctals);
}
if (singleCharacter !== undefined) {
return parseSingleCharacterCode(singleCharacter);
}
throw new SyntaxError(errors_1.errorMessages.get(errors_1.ErrorType.EndOfString));
});
}
exports.unraw = unraw;
exports.default = unraw;
});
if (unicode !== undefined) {
return parseUnicodeCode(unicode);
}
if (octal === "0") {
return "\0";
}
if (octal !== undefined) {
return parseOctalCode(octal, !allowOctals);
}
if (singleCharacter !== undefined) {
return parseSingleCharacterCode(singleCharacter);
}
throw new SyntaxError(errors_1.errorMessages.get(errors_1.ErrorType.EndOfString));
});
}
exports.unraw = unraw;
exports.default = unraw;
//# sourceMappingURL=index.js.map

@@ -1,1 +0,1 @@

!function(r){if("object"==typeof module&&"object"==typeof module.exports){var e=r(require,exports);void 0!==e&&(module.exports=e)}else"function"==typeof define&&define.amd&&define(["require","exports","./errors"],r)}(function(r,e){"use strict";Object.defineProperty(e,"__esModule",{value:!0});const o=r("./errors");function t(r,e,t){const n=function(r){return r.match(/[^a-f0-9]/i)?NaN:parseInt(r,16)}(r);if(Number.isNaN(n)||void 0!==t&&t!==r.length)throw new SyntaxError(o.errorMessages.get(e));return n}function n(r,e){const n=t(r,o.ErrorType.MalformedUnicode,4);if(void 0!==e){const r=t(e,o.ErrorType.MalformedUnicode,4);return String.fromCharCode(n,r)}return String.fromCharCode(n)}e.ErrorType=o.ErrorType,e.errorMessages=o.errorMessages;const i=new Map([["b","\b"],["f","\f"],["n","\n"],["r","\r"],["t","\t"],["v","\v"],["0","\0"]]);const s=/\\(?:(\\)|x([\s\S]{0,2})|u(\{[^}]*\}?)|u([\s\S]{4})\\u([^{][\s\S]{0,3})|u([\s\S]{0,4})|([0-3]?[0-7]{1,2})|([\s\S])|$)/g;function f(r,e=!1){return r.replace(s,function(r,s,f,a,u,c,d,g,p){if(void 0!==s)return"\\";if(void 0!==f)return function(r){const e=t(r,o.ErrorType.MalformedHexadecimal,2);return String.fromCharCode(e)}(f);if(void 0!==a)return function(r){if("{"!==(e=r).charAt(0)||"}"!==e.charAt(e.length-1))throw new SyntaxError(o.errorMessages.get(o.ErrorType.MalformedUnicode));var e;const n=t(r.slice(1,-1),o.ErrorType.MalformedUnicode);try{return String.fromCodePoint(n)}catch(r){throw r instanceof RangeError?new SyntaxError(o.errorMessages.get(o.ErrorType.CodePointLimit)):r}}(a);if(void 0!==u)return n(u,c);if(void 0!==d)return n(d);if("0"===g)return"\0";if(void 0!==g)return function(r,e=!1){if(e)throw new SyntaxError(o.errorMessages.get(o.ErrorType.OctalDeprecation));const t=parseInt(r,8);return String.fromCharCode(t)}(g,!e);if(void 0!==p)return y=p,i.get(y)||y;var y;throw new SyntaxError(o.errorMessages.get(o.ErrorType.EndOfString))})}e.unraw=f,e.default=f});
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const errors_1=require("./errors");function parseHexToInt(r){return!r.match(/[^a-f0-9]/i)?parseInt(r,16):NaN}function validateAndParseHex(r,e,o){const n=parseHexToInt(r);if(Number.isNaN(n)||void 0!==o&&o!==r.length)throw new SyntaxError(errors_1.errorMessages.get(e));return n}function parseHexadecimalCode(r){const e=validateAndParseHex(r,errors_1.ErrorType.MalformedHexadecimal,2);return String.fromCharCode(e)}function parseUnicodeCode(r,e){const o=validateAndParseHex(r,errors_1.ErrorType.MalformedUnicode,4);if(void 0!==e){const r=validateAndParseHex(e,errors_1.ErrorType.MalformedUnicode,4);return String.fromCharCode(o,r)}return String.fromCharCode(o)}function isCurlyBraced(r){return"{"===r.charAt(0)&&"}"===r.charAt(r.length-1)}function parseUnicodeCodePointCode(r){if(!isCurlyBraced(r))throw new SyntaxError(errors_1.errorMessages.get(errors_1.ErrorType.MalformedUnicode));const e=validateAndParseHex(r.slice(1,-1),errors_1.ErrorType.MalformedUnicode);try{return String.fromCodePoint(e)}catch(r){throw r instanceof RangeError?new SyntaxError(errors_1.errorMessages.get(errors_1.ErrorType.CodePointLimit)):r}}function parseOctalCode(r,e=!1){if(e)throw new SyntaxError(errors_1.errorMessages.get(errors_1.ErrorType.OctalDeprecation));const o=parseInt(r,8);return String.fromCharCode(o)}exports.ErrorType=errors_1.ErrorType,exports.errorMessages=errors_1.errorMessages;const singleCharacterEscapes=new Map([["b","\b"],["f","\f"],["n","\n"],["r","\r"],["t","\t"],["v","\v"],["0","\0"]]);function parseSingleCharacterCode(r){return singleCharacterEscapes.get(r)||r}const escapeMatch=/\\(?:(\\)|x([\s\S]{0,2})|u(\{[^}]*\}?)|u([\s\S]{4})\\u([^{][\s\S]{0,3})|u([\s\S]{0,4})|([0-3]?[0-7]{1,2})|([\s\S])|$)/g;function unraw(r,e=!1){return r.replace(escapeMatch,function(r,o,n,t,a,s,i,d,c){if(void 0!==o)return"\\";if(void 0!==n)return parseHexadecimalCode(n);if(void 0!==t)return parseUnicodeCodePointCode(t);if(void 0!==a)return parseUnicodeCode(a,s);if(void 0!==i)return parseUnicodeCode(i);if("0"===d)return"\0";if(void 0!==d)return parseOctalCode(d,!e);if(void 0!==c)return parseSingleCharacterCode(c);throw new SyntaxError(errors_1.errorMessages.get(errors_1.ErrorType.EndOfString))})}exports.unraw=unraw,exports.default=unraw;
{
"name": "unraw",
"version": "1.2.5",
"version": "2.0.0",
"description": "Convert raw escape sequences to their respective characters (undo String.raw).",

@@ -29,21 +29,21 @@ "main": "dist/index.js",

"devDependencies": {
"@node-minify/core": "^5.2.1",
"@node-minify/cli": "^5.2.1",
"@node-minify/uglify-es": "^5.2.0",
"@types/mocha": "^5.2.7",
"@types/node": "^12.12.9",
"@typescript-eslint/eslint-plugin": "^2.8.0",
"@typescript-eslint/parser": "^2.8.0",
"@node-minify/core": "^5.3.0",
"@node-minify/cli": "^5.3.0",
"@node-minify/uglify-es": "^5.3.0",
"@types/mocha": "^7.0.2",
"@types/node": "^13.9.5",
"@typescript-eslint/eslint-plugin": "^2.25.0",
"@typescript-eslint/parser": "^2.25.0",
"cross-var": "^1.1.0",
"eslint": "^6.6.0",
"eslint": "^6.8.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-jsdoc": "^18.0.1",
"eslint-plugin-mocha": "^6.2.1",
"mocha": "^6.2.2",
"mocha-junit-reporter": "^1.23.1",
"nyc": "^14.1.1",
"prettier": "^1.19.1",
"eslint-plugin-jsdoc": "^22.1.0",
"eslint-plugin-mocha": "^6.3.0",
"mocha": "^7.1.1",
"mocha-junit-reporter": "^1.23.3",
"nyc": "^15.0.0",
"prettier": "^2.0.2",
"source-map-support": "^0.5.16",
"ts-mocha": "^6.0.0",
"typescript": "^3.7.2"
"ts-mocha": "^7.0.0",
"typescript": "^3.8.3"
},

@@ -50,0 +50,0 @@ "dependencies": {},

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc