UNPKG

@slightning/anything-to-string

Version:
213 lines (212 loc) 8.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MaximumObjectRule = exports.MajorObjectRule = exports.LesserObjectRule = exports.MinimumObjectRule = exports.ObjectRule = void 0; const utils_1 = require("../utils"); const anything_1 = require("./anything"); function exclude(exclude, key) { if (Array.isArray(exclude)) { return exclude.includes(key); } else if (typeof Set != undefined && exclude instanceof Set) { return exclude.has(key); } else if (typeof exclude == "function") { return exclude(key); } else { return false; } } class ObjectRule { constructor({ defaultConfig, specialValue = {} }) { this.defaultConfig = defaultConfig; this.specialValue = specialValue; } test(data) { return typeof data == "object"; } prepare(data, config, context) { const objectConfig = Object.assign({}, this.defaultConfig, config.object); let keys = []; keys.push(...Object.getOwnPropertyNames(data)); if (objectConfig.symbol) { keys.push(...Object.getOwnPropertySymbols(data)); } if (!objectConfig.unenumerable) { keys = keys.filter((key) => { var _a, _b; return (_b = (_a = Object.getOwnPropertyDescriptor(data, key)) === null || _a === void 0 ? void 0 : _a.enumerable) !== null && _b !== void 0 ? _b : false; }); } keys = keys.filter((key) => !exclude(objectConfig.exclude, key)); for (const key of keys) { const property = Object.getOwnPropertyDescriptor(data, key); if (objectConfig.get || (property != undefined && "value" in property)) { let value; try { value = data[key]; } catch (error) { value = error; } new anything_1.AnythingRule().prepare(value, config, context); } } for (const key of keys) { const property = Object.getOwnPropertyDescriptor(data, key); if (objectConfig.getter && (property === null || property === void 0 ? void 0 : property.get) != undefined) { new anything_1.AnythingRule().prepare(property.get, config, context); } if (objectConfig.setter && (property === null || property === void 0 ? void 0 : property.set) != undefined) { new anything_1.AnythingRule().prepare(property.set, config, context); } } if (objectConfig.prototype) { new anything_1.AnythingRule().prepare(Object.getPrototypeOf(data), config, context); } } toString(data, config, context) { var _a, _b, _c; const objectConfig = Object.assign({}, this.defaultConfig, config.object); let result = (_c = (_b = (_a = Object.getPrototypeOf(data)) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : "Object"; result += " {\n"; let keys = []; keys.push(...Object.getOwnPropertyNames(data)); if (objectConfig.symbol) { keys.push(...Object.getOwnPropertySymbols(data)); } if (!objectConfig.unenumerable) { keys = keys.filter((key) => { var _a, _b; return (_b = (_a = Object.getOwnPropertyDescriptor(data, key)) === null || _a === void 0 ? void 0 : _a.enumerable) !== null && _b !== void 0 ? _b : false; }); } keys = keys.filter((key) => !exclude(objectConfig.exclude, key)); for (const key of keys) { const keyDescription = typeof key == "string" ? key : `[${String(key)}]`; const property = Object.getOwnPropertyDescriptor(data, key); if (objectConfig.get || (property != undefined && "value" in property)) { result += `${keyDescription}: ${this.propertyToString(data, key, config, context)}` .split("\n") .map((line) => `${(0, utils_1.getIndentString)(config)}${line}`) .join("\n") + "\n"; } } for (const key of keys) { const keyDescription = typeof key == "string" ? key : `[${String(key)}]`; const property = Object.getOwnPropertyDescriptor(data, key); if (objectConfig.getter && (property === null || property === void 0 ? void 0 : property.get) != undefined) { result += `get ${keyDescription}: ${new anything_1.AnythingRule().toString(property.get, config, context)}` .split("\n") .map((line) => `${(0, utils_1.getIndentString)(config)}${line}`) .join("\n") + "\n"; } if (objectConfig.setter && (property === null || property === void 0 ? void 0 : property.set) != undefined) { result += `set ${keyDescription}: ${new anything_1.AnythingRule().toString(property.set, config, context)}` .split("\n") .map((line) => `${(0, utils_1.getIndentString)(config)}${line}`) .join("\n") + "\n"; } } if (objectConfig.prototype) { this.specialValue["[[Prototype]]"] = new anything_1.AnythingRule().toString(Object.getPrototypeOf(data), config, context); } for (const [key, value] of Object.entries(this.specialValue)) { result += `${key}: ${value}` .split("\n") .map((line) => `${(0, utils_1.getIndentString)(config)}${line}`) .join("\n") + "\n"; } result += "}"; if (keys.length == 0 && Object.keys(this.specialValue).length == 0) { return "{}"; } return result; } propertyToString(object, key, config, context) { let hasError = false; let value; try { value = object[key]; } catch (error) { hasError = true; value = error; } const result = new anything_1.AnythingRule().toString(value, config, context); if (hasError) { return `[Exception: ${result}]`; } else { return result; } } } exports.ObjectRule = ObjectRule; class MinimumObjectRule { constructor() { this.test = ObjectRule.prototype.test; } toString(data, __config, __context) { return Object.prototype.toString.call(data); } } exports.MinimumObjectRule = MinimumObjectRule; class LesserObjectRule extends ObjectRule { constructor({ defaultConfig = {}, specialValue = {} } = {}) { super({ defaultConfig: Object.assign({ unenumerable: false, symbol: false, get: false, getter: false, setter: false, prototype: false, exclude: [] }, defaultConfig), specialValue }); } } exports.LesserObjectRule = LesserObjectRule; class MajorObjectRule extends ObjectRule { constructor({ defaultConfig = {}, specialValue = {} } = {}) { super({ defaultConfig: Object.assign({ unenumerable: true, symbol: true, get: true, getter: false, setter: false, prototype: false, exclude: [] }, defaultConfig), specialValue }); } } exports.MajorObjectRule = MajorObjectRule; class MaximumObjectRule extends ObjectRule { constructor({ defaultConfig = {}, specialValue = {} } = {}) { super({ defaultConfig: Object.assign({ unenumerable: true, symbol: true, get: true, getter: true, setter: true, prototype: true, exclude: [] }, defaultConfig), specialValue }); } } exports.MaximumObjectRule = MaximumObjectRule;