UNPKG

earljs

Version:

Ergonomic, modern and type-safe assertion library

58 lines (57 loc) 2.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatObjectEntries = void 0; const formatUnknown_1 = require("./formatUnknown"); const getOptionsWith_1 = require("./getOptionsWith"); function formatObjectEntries(value, sibling, options, valueStack, siblingStack) { var _a; const keys = getKeys(value, options); const entries = []; const passedOptions = (0, getOptionsWith_1.getOptionsWith)(options, { requireStrictEquality: false, maxLineLength: options.maxLineLength - 10, }); for (const key of keys) { const keyFormat = formatKey(key, passedOptions); const nestedOptions = (0, getOptionsWith_1.getOptionsWith)(passedOptions, { skipMatcherReplacement: passedOptions.skipMatcherReplacement || (!!sibling && !Object.prototype.hasOwnProperty.call(sibling, key)), }); const valueFormat = (0, formatUnknown_1.formatUnknown)(value[key], (_a = sibling) === null || _a === void 0 ? void 0 : _a[key], nestedOptions, valueStack, siblingStack); valueFormat[0][1] = `${keyFormat}: ${valueFormat[0][1]}`; for (const line of valueFormat) { line[0] += 1; } entries.push(...valueFormat); } return entries; } exports.formatObjectEntries = formatObjectEntries; function formatKey(key, options) { if (options.inline && key.length > options.maxLineLength - 2) { return JSON.stringify(key.slice(0, 7) + '...'); } return /^\w+$/.test(key) ? key : JSON.stringify(key); } function getKeys(value, options) { let keys = Object.keys(value); if (value instanceof Error) { addKey(keys, value, 'name'); addKey(keys, value, 'message'); addKey(keys, value, 'code'); if (options.compareErrorStack) { addKey(keys, value, 'stack'); } else { keys = keys.filter((key) => key !== 'stack'); } } else if (Array.isArray(value) || value instanceof String) { keys = keys.filter((key) => !/^\d+$/.test(key)); } return keys.sort(); } function addKey(keys, value, key) { if (key in value) { keys.push(key); } }