UNPKG

@clynn-fe/akfe-editor-stringify-object

Version:

stringify runtime js object

138 lines 5.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const is_regexp_1 = __importDefault(require("is-regexp")); const akfe_editor_utils_1 = require("@clynn-fe/akfe-editor-utils"); const utils_1 = require("./utils"); const get_own_enumerable_property_symbols_1 = __importDefault(require("get-own-enumerable-property-symbols")); const seen = []; const stringifyObject = (input, options = {}, pad = '') => { options.indent = options.indent || '\t'; let tokens; if (options.inlineCharacterLimit === undefined) { tokens = { newLine: '\n', newLineOrSpace: '\n', pad, indent: pad + options.indent }; } else { tokens = { newLine: '@@__STRINGIFY_OBJECT_NEW_LINE__@@', newLineOrSpace: '@@__STRINGIFY_OBJECT_NEW_LINE_OR_SPACE__@@', pad: '@@__STRINGIFY_OBJECT_PAD__@@', indent: '@@__STRINGIFY_OBJECT_INDENT__@@' }; } const expandWhiteSpace = (string) => { if (options.inlineCharacterLimit === undefined) { return string; } const oneLined = string .replace(new RegExp(tokens.newLine, 'g'), '') .replace(new RegExp(tokens.newLineOrSpace, 'g'), ' ') .replace(new RegExp(tokens.pad + '|' + tokens.indent, 'g'), ''); if (oneLined.length <= options.inlineCharacterLimit) { return oneLined; } return string .replace(new RegExp(tokens.newLine + '|' + tokens.newLineOrSpace, 'g'), '\n') .replace(new RegExp(tokens.pad, 'g'), pad) .replace(new RegExp(tokens.indent, 'g'), pad + options.indent); }; if (seen.indexOf(input) !== -1) { return '"[Circular]"'; } if (input === null || input === undefined || typeof input === 'number' || typeof input === 'boolean' || typeof input === 'function' || typeof input === 'symbol' || (0, is_regexp_1.default)(input)) { return String(input); } if (input instanceof Date) { return `new Date('${input.toISOString()}')`; } if (input instanceof Map) { return `new Map(${stringifyObject([...input.entries()], options, pad)})`; } if (input instanceof Set) { return `new Set(${stringifyObject([...input.values()], options, pad)})`; } if (Array.isArray(input)) { if (input.length === 0) { return '[]'; } seen.push(input); const ret = '[' + tokens.newLine + input .map((el, i) => { const eol = input.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace; let value = stringifyObject(el, options, pad + options.indent); if (options.transform) { value = options.transform(input, i, value); } return tokens.indent + value + eol; }) .join('') + tokens.pad + ']'; seen.pop(); return expandWhiteSpace(ret); } if ((0, utils_1.isObj)(input)) { let objKeys = [...Object.keys(input), ...(0, get_own_enumerable_property_symbols_1.default)(input)]; if (options.filter) { objKeys = objKeys.filter(el => // eslint-disable-next-line @typescript-eslint/no-non-null-assertion options.filter(input, el)); } if (objKeys.length === 0) { return '{}'; } seen.push(input); const ret = '{' + tokens.newLine + objKeys .map((el, i) => { const eol = objKeys.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace; const isClassic = typeof el !== 'symbol' && /^[a-z$_][a-z$_0-9]*$/i.test(el); const key = typeof el === 'symbol' || isClassic ? el : stringifyObject(el, options); let value = stringifyObject(input[el], options, pad + options.indent); const isMethod = typeof input[el] === 'function' && !(0, akfe_editor_utils_1.isArrowFunction)(value) && !/^(function\b|\()/.test(value) && !/^(async function\b|\()/.test(value); if (options.transform) { value = options.transform(input, el, value); } return (tokens.indent + (isMethod ? '' : String(key) + ': ') + value + eol); }) .join('') + tokens.pad + '}'; seen.pop(); return expandWhiteSpace(ret); } let result = String(input).replace(/[\r\n]/g, x => x === '\n' ? '\\n' : '\\r'); if (options.singleQuotes === false) { result = result.replace(/"/g, '\\"'); return `"${result}"`; } result = result.replace(/\\?'/g, "\\'"); return `'${result}'`; }; exports.default = stringifyObject; //# sourceMappingURL=index.js.map