CoolFace
Apppublic

Ejdjdososs/fable-ai

sourceHugging Facemitupdated 3mo agoView on Hugging Face
0likes
jsesc.js338 linesDownload Raw Back to jsesc
1'use strict';2 3const object = {};4const hasOwnProperty = object.hasOwnProperty;5const forOwn = (object, callback) => {6	for (const key in object) {7		if (hasOwnProperty.call(object, key)) {8			callback(key, object[key]);9		}10	}11};12 13const extend = (destination, source) => {14	if (!source) {15		return destination;16	}17	forOwn(source, (key, value) => {18		destination[key] = value;19	});20	return destination;21};22 23const forEach = (array, callback) => {24	const length = array.length;25	let index = -1;26	while (++index < length) {27		callback(array[index]);28	}29};30 31const fourHexEscape = (hex) => {32	return '\\u' + ('0000' + hex).slice(-4);33}34 35const hexadecimal = (code, lowercase) => {36	let hexadecimal = code.toString(16);37	if (lowercase) return hexadecimal;38	return hexadecimal.toUpperCase();39};40 41const toString = object.toString;42const isArray = Array.isArray;43const isBuffer = (value) => {44	return typeof Buffer === 'function' && Buffer.isBuffer(value);45};46const isObject = (value) => {47	// This is a very simple check, but it’s good enough for what we need.48	return toString.call(value) == '[object Object]';49};50const isString = (value) => {51	return typeof value == 'string' ||52		toString.call(value) == '[object String]';53};54const isNumber = (value) => {55	return typeof value == 'number' ||56		toString.call(value) == '[object Number]';57};58const isBigInt = (value) => {59  return typeof value == 'bigint';60};61const isFunction = (value) => {62	return typeof value == 'function';63};64const isMap = (value) => {65	return toString.call(value) == '[object Map]';66};67const isSet = (value) => {68	return toString.call(value) == '[object Set]';69};70 71/*--------------------------------------------------------------------------*/72 73// https://mathiasbynens.be/notes/javascript-escapes#single74const singleEscapes = {75	'\\': '\\\\',76	'\b': '\\b',77	'\f': '\\f',78	'\n': '\\n',79	'\r': '\\r',80	'\t': '\\t'81	// `\v` is omitted intentionally, because in IE < 9, '\v' == 'v'.82	// '\v': '\\x0B'83};84const regexSingleEscape = /[\\\b\f\n\r\t]/;85 86const regexDigit = /[0-9]/;87const regexWhitespace = /[\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000]/;88 89const escapeEverythingRegex = /([\uD800-\uDBFF][\uDC00-\uDFFF])|([\uD800-\uDFFF])|(['"`])|[^]/g;90const escapeNonAsciiRegex = /([\uD800-\uDBFF][\uDC00-\uDFFF])|([\uD800-\uDFFF])|(['"`])|[^ !#-&\(-\[\]-_a-~]/g;91 92const jsesc = (argument, options) => {93	const increaseIndentation = () => {94		oldIndent = indent;95		++options.indentLevel;96		indent = options.indent.repeat(options.indentLevel)97	};98	// Handle options99	const defaults = {100		'escapeEverything': false,101		'minimal': false,102		'isScriptContext': false,103		'quotes': 'single',104		'wrap': false,105		'es6': false,106		'json': false,107		'compact': true,108		'lowercaseHex': false,109		'numbers': 'decimal',110		'indent': '\t',111		'indentLevel': 0,112		'__inline1__': false,113		'__inline2__': false114	};115	const json = options && options.json;116	if (json) {117		defaults.quotes = 'double';118		defaults.wrap = true;119	}120	options = extend(defaults, options);121	if (122		options.quotes != 'single' &&123		options.quotes != 'double' &&124		options.quotes != 'backtick'125	) {126		options.quotes = 'single';127	}128	const quote = options.quotes == 'double' ?129		'"' :130		(options.quotes == 'backtick' ?131			'`' :132			'\''133		);134	const compact = options.compact;135	const lowercaseHex = options.lowercaseHex;136	let indent = options.indent.repeat(options.indentLevel);137	let oldIndent = '';138	const inline1 = options.__inline1__;139	const inline2 = options.__inline2__;140	const newLine = compact ? '' : '\n';141	let result;142	let isEmpty = true;143	const useBinNumbers = options.numbers == 'binary';144	const useOctNumbers = options.numbers == 'octal';145	const useDecNumbers = options.numbers == 'decimal';146	const useHexNumbers = options.numbers == 'hexadecimal';147 148	if (json && argument && isFunction(argument.toJSON)) {149		argument = argument.toJSON();150	}151 152	if (!isString(argument)) {153		if (isMap(argument)) {154			if (argument.size == 0) {155				return 'new Map()';156			}157			if (!compact) {158				options.__inline1__ = true;159				options.__inline2__ = false;160			}161			return 'new Map(' + jsesc(Array.from(argument), options) + ')';162		}163		if (isSet(argument)) {164			if (argument.size == 0) {165				return 'new Set()';166			}167			return 'new Set(' + jsesc(Array.from(argument), options) + ')';168		}169		if (isBuffer(argument)) {170			if (argument.length == 0) {171				return 'Buffer.from([])';172			}173			return 'Buffer.from(' + jsesc(Array.from(argument), options) + ')';174		}175		if (isArray(argument)) {176			result = [];177			options.wrap = true;178			if (inline1) {179				options.__inline1__ = false;180				options.__inline2__ = true;181			}182			if (!inline2) {183				increaseIndentation();184			}185			forEach(argument, (value) => {186				isEmpty = false;187				if (inline2) {188					options.__inline2__ = false;189				}190				result.push(191					(compact || inline2 ? '' : indent) +192					jsesc(value, options)193				);194			});195			if (isEmpty) {196				return '[]';197			}198			if (inline2) {199				return '[' + result.join(', ') + ']';200			}201			return '[' + newLine + result.join(',' + newLine) + newLine +202				(compact ? '' : oldIndent) + ']';203		} else if (isNumber(argument) || isBigInt(argument)) {204			if (json) {205				// Some number values (e.g. `Infinity`) cannot be represented in JSON.206				// `BigInt` values less than `-Number.MAX_VALUE` or greater than207        // `Number.MAX_VALUE` cannot be represented in JSON so they will become208        // `-Infinity` or `Infinity`, respectively, and then become `null` when209        // stringified.210				return JSON.stringify(Number(argument));211			}212 213      let result;214			if (useDecNumbers) {215				result = String(argument);216			} else if (useHexNumbers) {217				let hexadecimal = argument.toString(16);218				if (!lowercaseHex) {219					hexadecimal = hexadecimal.toUpperCase();220				}221				result = '0x' + hexadecimal;222			} else if (useBinNumbers) {223				result = '0b' + argument.toString(2);224			} else if (useOctNumbers) {225				result = '0o' + argument.toString(8);226			}227 228      if (isBigInt(argument)) {229        return result + 'n';230      }231      return result;232		} else if (isBigInt(argument)) {233			if (json) {234				// `BigInt` values less than `-Number.MAX_VALUE` or greater than235        // `Number.MAX_VALUE` will become `-Infinity` or `Infinity`,236        // respectively, and cannot be represented in JSON.237				return JSON.stringify(Number(argument));238			}239      return argument + 'n';240    } else if (!isObject(argument)) {241			if (json) {242				// For some values (e.g. `undefined`, `function` objects),243				// `JSON.stringify(value)` returns `undefined` (which isn’t valid244				// JSON) instead of `'null'`.245				return JSON.stringify(argument) || 'null';246			}247			return String(argument);248		} else { // it’s an object249			result = [];250			options.wrap = true;251			increaseIndentation();252			forOwn(argument, (key, value) => {253				isEmpty = false;254				result.push(255					(compact ? '' : indent) +256					jsesc(key, options) + ':' +257					(compact ? '' : ' ') +258					jsesc(value, options)259				);260			});261			if (isEmpty) {262				return '{}';263			}264			return '{' + newLine + result.join(',' + newLine) + newLine +265				(compact ? '' : oldIndent) + '}';266		}267	}268 269	const regex = options.escapeEverything ? escapeEverythingRegex : escapeNonAsciiRegex;270	result = argument.replace(regex, (char, pair, lone, quoteChar, index, string) => {271		if (pair) {272			if (options.minimal) return pair;273			const first = pair.charCodeAt(0);274			const second = pair.charCodeAt(1);275			if (options.es6) {276				// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae277				const codePoint = (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;278				const hex = hexadecimal(codePoint, lowercaseHex);279				return '\\u{' + hex + '}';280			}281			return fourHexEscape(hexadecimal(first, lowercaseHex)) + fourHexEscape(hexadecimal(second, lowercaseHex));282		}283 284		if (lone) {285			return fourHexEscape(hexadecimal(lone.charCodeAt(0), lowercaseHex));286		}287 288		if (289			char == '\0' &&290			!json &&291			!regexDigit.test(string.charAt(index + 1))292		) {293			return '\\0';294		}295 296		if (quoteChar) {297			if (quoteChar == quote || options.escapeEverything) {298				return '\\' + quoteChar;299			}300			return quoteChar;301		}302 303		if (regexSingleEscape.test(char)) {304			// no need for a `hasOwnProperty` check here305			return singleEscapes[char];306		}307 308		if (options.minimal && !regexWhitespace.test(char)) {309			return char;310		}311 312		const hex = hexadecimal(char.charCodeAt(0), lowercaseHex);313		if (json || hex.length > 2) {314			return fourHexEscape(hex);315		}316 317		return '\\x' + ('00' + hex).slice(-2);318	});319 320	if (quote == '`') {321		result = result.replace(/\$\{/g, '\\${');322	}323	if (options.isScriptContext) {324		// https://mathiasbynens.be/notes/etago325		result = result326			.replace(/<\/(script|style)/gi, '<\\/$1')327			.replace(/<!--/g, json ? '\\u003C!--' : '\\x3C!--');328	}329	if (options.wrap) {330		result = quote + result + quote;331	}332	return result;333};334 335jsesc.version = '3.0.2';336 337module.exports = jsesc;338