loupe.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.loupe = {}));
  5. }(this, (function (exports) { 'use strict';
  6. function _typeof(obj) {
  7. "@babel/helpers - typeof";
  8. if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
  9. _typeof = function (obj) {
  10. return typeof obj;
  11. };
  12. } else {
  13. _typeof = function (obj) {
  14. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  15. };
  16. }
  17. return _typeof(obj);
  18. }
  19. function _slicedToArray(arr, i) {
  20. return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
  21. }
  22. function _arrayWithHoles(arr) {
  23. if (Array.isArray(arr)) return arr;
  24. }
  25. function _iterableToArrayLimit(arr, i) {
  26. if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
  27. var _arr = [];
  28. var _n = true;
  29. var _d = false;
  30. var _e = undefined;
  31. try {
  32. for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
  33. _arr.push(_s.value);
  34. if (i && _arr.length === i) break;
  35. }
  36. } catch (err) {
  37. _d = true;
  38. _e = err;
  39. } finally {
  40. try {
  41. if (!_n && _i["return"] != null) _i["return"]();
  42. } finally {
  43. if (_d) throw _e;
  44. }
  45. }
  46. return _arr;
  47. }
  48. function _unsupportedIterableToArray(o, minLen) {
  49. if (!o) return;
  50. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  51. var n = Object.prototype.toString.call(o).slice(8, -1);
  52. if (n === "Object" && o.constructor) n = o.constructor.name;
  53. if (n === "Map" || n === "Set") return Array.from(o);
  54. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  55. }
  56. function _arrayLikeToArray(arr, len) {
  57. if (len == null || len > arr.length) len = arr.length;
  58. for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  59. return arr2;
  60. }
  61. function _nonIterableRest() {
  62. throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  63. }
  64. var ansiColors = {
  65. bold: ['1', '22'],
  66. dim: ['2', '22'],
  67. italic: ['3', '23'],
  68. underline: ['4', '24'],
  69. // 5 & 6 are blinking
  70. inverse: ['7', '27'],
  71. hidden: ['8', '28'],
  72. strike: ['9', '29'],
  73. // 10-20 are fonts
  74. // 21-29 are resets for 1-9
  75. black: ['30', '39'],
  76. red: ['31', '39'],
  77. green: ['32', '39'],
  78. yellow: ['33', '39'],
  79. blue: ['34', '39'],
  80. magenta: ['35', '39'],
  81. cyan: ['36', '39'],
  82. white: ['37', '39'],
  83. brightblack: ['30;1', '39'],
  84. brightred: ['31;1', '39'],
  85. brightgreen: ['32;1', '39'],
  86. brightyellow: ['33;1', '39'],
  87. brightblue: ['34;1', '39'],
  88. brightmagenta: ['35;1', '39'],
  89. brightcyan: ['36;1', '39'],
  90. brightwhite: ['37;1', '39'],
  91. grey: ['90', '39']
  92. };
  93. var styles = {
  94. special: 'cyan',
  95. number: 'yellow',
  96. bigint: 'yellow',
  97. boolean: 'yellow',
  98. undefined: 'grey',
  99. null: 'bold',
  100. string: 'green',
  101. symbol: 'green',
  102. date: 'magenta',
  103. regexp: 'red'
  104. };
  105. var truncator = '…';
  106. function colorise(value, styleType) {
  107. var color = ansiColors[styles[styleType]] || ansiColors[styleType];
  108. if (!color) {
  109. return String(value);
  110. }
  111. return "\x1B[".concat(color[0], "m").concat(String(value), "\x1B[").concat(color[1], "m");
  112. }
  113. function normaliseOptions() {
  114. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  115. _ref$showHidden = _ref.showHidden,
  116. showHidden = _ref$showHidden === void 0 ? false : _ref$showHidden,
  117. _ref$depth = _ref.depth,
  118. depth = _ref$depth === void 0 ? 2 : _ref$depth,
  119. _ref$colors = _ref.colors,
  120. colors = _ref$colors === void 0 ? false : _ref$colors,
  121. _ref$customInspect = _ref.customInspect,
  122. customInspect = _ref$customInspect === void 0 ? true : _ref$customInspect,
  123. _ref$showProxy = _ref.showProxy,
  124. showProxy = _ref$showProxy === void 0 ? false : _ref$showProxy,
  125. _ref$maxArrayLength = _ref.maxArrayLength,
  126. maxArrayLength = _ref$maxArrayLength === void 0 ? Infinity : _ref$maxArrayLength,
  127. _ref$breakLength = _ref.breakLength,
  128. breakLength = _ref$breakLength === void 0 ? Infinity : _ref$breakLength,
  129. _ref$seen = _ref.seen,
  130. seen = _ref$seen === void 0 ? [] : _ref$seen,
  131. _ref$truncate = _ref.truncate,
  132. truncate = _ref$truncate === void 0 ? Infinity : _ref$truncate,
  133. _ref$stylize = _ref.stylize,
  134. stylize = _ref$stylize === void 0 ? String : _ref$stylize;
  135. var options = {
  136. showHidden: Boolean(showHidden),
  137. depth: Number(depth),
  138. colors: Boolean(colors),
  139. customInspect: Boolean(customInspect),
  140. showProxy: Boolean(showProxy),
  141. maxArrayLength: Number(maxArrayLength),
  142. breakLength: Number(breakLength),
  143. truncate: Number(truncate),
  144. seen: seen,
  145. stylize: stylize
  146. };
  147. if (options.colors) {
  148. options.stylize = colorise;
  149. }
  150. return options;
  151. }
  152. function truncate(string, length) {
  153. var tail = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : truncator;
  154. string = String(string);
  155. var tailLength = tail.length;
  156. var stringLength = string.length;
  157. if (tailLength > length && stringLength > tailLength) {
  158. return tail;
  159. }
  160. if (stringLength > length && stringLength > tailLength) {
  161. return "".concat(string.slice(0, length - tailLength)).concat(tail);
  162. }
  163. return string;
  164. } // eslint-disable-next-line complexity
  165. function inspectList(list, options, inspectItem) {
  166. var separator = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ', ';
  167. inspectItem = inspectItem || options.inspect;
  168. var size = list.length;
  169. if (size === 0) return '';
  170. var originalLength = options.truncate;
  171. var output = '';
  172. var peek = '';
  173. var truncated = '';
  174. for (var i = 0; i < size; i += 1) {
  175. var last = i + 1 === list.length;
  176. var secondToLast = i + 2 === list.length;
  177. truncated = "".concat(truncator, "(").concat(list.length - i, ")");
  178. var value = list[i]; // If there is more than one remaining we need to account for a separator of `, `
  179. options.truncate = originalLength - output.length - (last ? 0 : separator.length);
  180. var string = peek || inspectItem(value, options) + (last ? '' : separator);
  181. var nextLength = output.length + string.length;
  182. var truncatedLength = nextLength + truncated.length; // If this is the last element, and adding it would
  183. // take us over length, but adding the truncator wouldn't - then break now
  184. if (last && nextLength > originalLength && output.length + truncated.length <= originalLength) {
  185. break;
  186. } // If this isn't the last or second to last element to scan,
  187. // but the string is already over length then break here
  188. if (!last && !secondToLast && truncatedLength > originalLength) {
  189. break;
  190. } // Peek at the next string to determine if we should
  191. // break early before adding this item to the output
  192. peek = last ? '' : inspectItem(list[i + 1], options) + (secondToLast ? '' : separator); // If we have one element left, but this element and
  193. // the next takes over length, the break early
  194. if (!last && secondToLast && truncatedLength > originalLength && nextLength + peek.length > originalLength) {
  195. break;
  196. }
  197. output += string; // If the next element takes us to length -
  198. // but there are more after that, then we should truncate now
  199. if (!last && !secondToLast && nextLength + peek.length >= originalLength) {
  200. truncated = "".concat(truncator, "(").concat(list.length - i - 1, ")");
  201. break;
  202. }
  203. truncated = '';
  204. }
  205. return "".concat(output).concat(truncated);
  206. }
  207. function quoteComplexKey(key) {
  208. if (key.match(/^[a-zA-Z_][a-zA-Z_0-9]*$/)) {
  209. return key;
  210. }
  211. return JSON.stringify(key).replace(/'/g, "\\'").replace(/\\"/g, '"').replace(/(^"|"$)/g, "'");
  212. }
  213. function inspectProperty(_ref2, options) {
  214. var _ref3 = _slicedToArray(_ref2, 2),
  215. key = _ref3[0],
  216. value = _ref3[1];
  217. options.truncate -= 2;
  218. if (typeof key === 'string') {
  219. key = quoteComplexKey(key);
  220. } else if (typeof key !== 'number') {
  221. key = "[".concat(options.inspect(key, options), "]");
  222. }
  223. options.truncate -= key.length;
  224. value = options.inspect(value, options);
  225. return "".concat(key, ": ").concat(value);
  226. }
  227. function inspectArray(array, options) {
  228. // Object.keys will always output the Array indices first, so we can slice by
  229. // `array.length` to get non-index properties
  230. var nonIndexProperties = Object.keys(array).slice(array.length);
  231. if (!array.length && !nonIndexProperties.length) return '[]';
  232. options.truncate -= 4;
  233. var listContents = inspectList(array, options);
  234. options.truncate -= listContents.length;
  235. var propertyContents = '';
  236. if (nonIndexProperties.length) {
  237. propertyContents = inspectList(nonIndexProperties.map(function (key) {
  238. return [key, array[key]];
  239. }), options, inspectProperty);
  240. }
  241. return "[ ".concat(listContents).concat(propertyContents ? ", ".concat(propertyContents) : '', " ]");
  242. }
  243. /* !
  244. * Chai - getFuncName utility
  245. * Copyright(c) 2012-2016 Jake Luer <jake@alogicalparadox.com>
  246. * MIT Licensed
  247. */
  248. /**
  249. * ### .getFuncName(constructorFn)
  250. *
  251. * Returns the name of a function.
  252. * When a non-function instance is passed, returns `null`.
  253. * This also includes a polyfill function if `aFunc.name` is not defined.
  254. *
  255. * @name getFuncName
  256. * @param {Function} funct
  257. * @namespace Utils
  258. * @api public
  259. */
  260. var toString = Function.prototype.toString;
  261. var functionNameMatch = /\s*function(?:\s|\s*\/\*[^(?:*\/)]+\*\/\s*)*([^\s\(\/]+)/;
  262. var maxFunctionSourceLength = 512;
  263. function getFuncName(aFunc) {
  264. if (typeof aFunc !== 'function') {
  265. return null;
  266. }
  267. var name = '';
  268. if (typeof Function.prototype.name === 'undefined' && typeof aFunc.name === 'undefined') {
  269. // eslint-disable-next-line prefer-reflect
  270. var functionSource = toString.call(aFunc);
  271. // To avoid unconstrained resource consumption due to pathalogically large function names,
  272. // we limit the available return value to be less than 512 characters.
  273. if (functionSource.indexOf('(') > maxFunctionSourceLength) {
  274. return name;
  275. }
  276. // Here we run a polyfill if Function does not support the `name` property and if aFunc.name is not defined
  277. var match = functionSource.match(functionNameMatch);
  278. if (match) {
  279. name = match[1];
  280. }
  281. } else {
  282. // If we've got a `name` property we just use it
  283. name = aFunc.name;
  284. }
  285. return name;
  286. }
  287. var getFuncName_1 = getFuncName;
  288. var getArrayName = function getArrayName(array) {
  289. // We need to special case Node.js' Buffers, which report to be Uint8Array
  290. if (typeof Buffer === 'function' && array instanceof Buffer) {
  291. return 'Buffer';
  292. }
  293. if (array[Symbol.toStringTag]) {
  294. return array[Symbol.toStringTag];
  295. }
  296. return getFuncName_1(array.constructor);
  297. };
  298. function inspectTypedArray(array, options) {
  299. var name = getArrayName(array);
  300. options.truncate -= name.length + 4; // Object.keys will always output the Array indices first, so we can slice by
  301. // `array.length` to get non-index properties
  302. var nonIndexProperties = Object.keys(array).slice(array.length);
  303. if (!array.length && !nonIndexProperties.length) return "".concat(name, "[]"); // As we know TypedArrays only contain Unsigned Integers, we can skip inspecting each one and simply
  304. // stylise the toString() value of them
  305. var output = '';
  306. for (var i = 0; i < array.length; i++) {
  307. var string = "".concat(options.stylize(truncate(array[i], options.truncate), 'number')).concat(i === array.length - 1 ? '' : ', ');
  308. options.truncate -= string.length;
  309. if (array[i] !== array.length && options.truncate <= 3) {
  310. output += "".concat(truncator, "(").concat(array.length - array[i] + 1, ")");
  311. break;
  312. }
  313. output += string;
  314. }
  315. var propertyContents = '';
  316. if (nonIndexProperties.length) {
  317. propertyContents = inspectList(nonIndexProperties.map(function (key) {
  318. return [key, array[key]];
  319. }), options, inspectProperty);
  320. }
  321. return "".concat(name, "[ ").concat(output).concat(propertyContents ? ", ".concat(propertyContents) : '', " ]");
  322. }
  323. function inspectDate(dateObject, options) {
  324. var stringRepresentation = dateObject.toJSON();
  325. if (stringRepresentation === null) {
  326. return 'Invalid Date';
  327. }
  328. var split = stringRepresentation.split('T');
  329. var date = split[0]; // If we need to - truncate the time portion, but never the date
  330. return options.stylize("".concat(date, "T").concat(truncate(split[1], options.truncate - date.length - 1)), 'date');
  331. }
  332. function inspectFunction(func, options) {
  333. var name = getFuncName_1(func);
  334. if (!name) {
  335. return options.stylize('[Function]', 'special');
  336. }
  337. return options.stylize("[Function ".concat(truncate(name, options.truncate - 11), "]"), 'special');
  338. }
  339. function inspectMapEntry(_ref, options) {
  340. var _ref2 = _slicedToArray(_ref, 2),
  341. key = _ref2[0],
  342. value = _ref2[1];
  343. options.truncate -= 4;
  344. key = options.inspect(key, options);
  345. options.truncate -= key.length;
  346. value = options.inspect(value, options);
  347. return "".concat(key, " => ").concat(value);
  348. } // IE11 doesn't support `map.entries()`
  349. function mapToEntries(map) {
  350. var entries = [];
  351. map.forEach(function (value, key) {
  352. entries.push([key, value]);
  353. });
  354. return entries;
  355. }
  356. function inspectMap(map, options) {
  357. var size = map.size - 1;
  358. if (size <= 0) {
  359. return 'Map{}';
  360. }
  361. options.truncate -= 7;
  362. return "Map{ ".concat(inspectList(mapToEntries(map), options, inspectMapEntry), " }");
  363. }
  364. var isNaN = Number.isNaN || function (i) {
  365. return i !== i;
  366. }; // eslint-disable-line no-self-compare
  367. function inspectNumber(number, options) {
  368. if (isNaN(number)) {
  369. return options.stylize('NaN', 'number');
  370. }
  371. if (number === Infinity) {
  372. return options.stylize('Infinity', 'number');
  373. }
  374. if (number === -Infinity) {
  375. return options.stylize('-Infinity', 'number');
  376. }
  377. if (number === 0) {
  378. return options.stylize(1 / number === Infinity ? '+0' : '-0', 'number');
  379. }
  380. return options.stylize(truncate(number, options.truncate), 'number');
  381. }
  382. function inspectBigInt(number, options) {
  383. var nums = truncate(number.toString(), options.truncate - 1);
  384. if (nums !== truncator) nums += 'n';
  385. return options.stylize(nums, 'bigint');
  386. }
  387. function inspectRegExp(value, options) {
  388. var flags = value.toString().split('/')[2];
  389. var sourceLength = options.truncate - (2 + flags.length);
  390. var source = value.source;
  391. return options.stylize("/".concat(truncate(source, sourceLength), "/").concat(flags), 'regexp');
  392. }
  393. function arrayFromSet(set) {
  394. var values = [];
  395. set.forEach(function (value) {
  396. values.push(value);
  397. });
  398. return values;
  399. }
  400. function inspectSet(set, options) {
  401. if (set.size === 0) return 'Set{}';
  402. options.truncate -= 7;
  403. return "Set{ ".concat(inspectList(arrayFromSet(set), options), " }");
  404. }
  405. var stringEscapeChars = new RegExp("['\\u0000-\\u001f\\u007f-\\u009f\\u00ad\\u0600-\\u0604\\u070f\\u17b4\\u17b5" + "\\u200c-\\u200f\\u2028-\\u202f\\u2060-\\u206f\\ufeff\\ufff0-\\uffff]", 'g');
  406. var escapeCharacters = {
  407. '\b': '\\b',
  408. '\t': '\\t',
  409. '\n': '\\n',
  410. '\f': '\\f',
  411. '\r': '\\r',
  412. "'": "\\'",
  413. '\\': '\\\\'
  414. };
  415. var hex = 16;
  416. var unicodeLength = 4;
  417. function escape(char) {
  418. return escapeCharacters[char] || "\\u".concat("0000".concat(char.charCodeAt(0).toString(hex)).slice(-unicodeLength));
  419. }
  420. function inspectString(string, options) {
  421. if (stringEscapeChars.test(string)) {
  422. string = string.replace(stringEscapeChars, escape);
  423. }
  424. return options.stylize("'".concat(truncate(string, options.truncate - 2), "'"), 'string');
  425. }
  426. function inspectSymbol(value) {
  427. if ('description' in Symbol.prototype) {
  428. return value.description ? "Symbol(".concat(value.description, ")") : 'Symbol()';
  429. }
  430. return value.toString();
  431. }
  432. var getPromiseValue = function getPromiseValue() {
  433. return 'Promise{…}';
  434. };
  435. try {
  436. var _process$binding = process.binding('util'),
  437. getPromiseDetails = _process$binding.getPromiseDetails,
  438. kPending = _process$binding.kPending,
  439. kRejected = _process$binding.kRejected;
  440. if (Array.isArray(getPromiseDetails(Promise.resolve()))) {
  441. getPromiseValue = function getPromiseValue(value, options) {
  442. var _getPromiseDetails = getPromiseDetails(value),
  443. _getPromiseDetails2 = _slicedToArray(_getPromiseDetails, 2),
  444. state = _getPromiseDetails2[0],
  445. innerValue = _getPromiseDetails2[1];
  446. if (state === kPending) {
  447. return 'Promise{<pending>}';
  448. }
  449. return "Promise".concat(state === kRejected ? '!' : '', "{").concat(options.inspect(innerValue, options), "}");
  450. };
  451. }
  452. } catch (notNode) {
  453. /* ignore */
  454. }
  455. var inspectPromise = getPromiseValue;
  456. function inspectObject(object, options) {
  457. var properties = Object.getOwnPropertyNames(object);
  458. var symbols = Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(object) : [];
  459. if (properties.length === 0 && symbols.length === 0) {
  460. return '{}';
  461. }
  462. options.truncate -= 4;
  463. options.seen = options.seen || [];
  464. if (options.seen.indexOf(object) >= 0) {
  465. return '[Circular]';
  466. }
  467. options.seen.push(object);
  468. var propertyContents = inspectList(properties.map(function (key) {
  469. return [key, object[key]];
  470. }), options, inspectProperty);
  471. var symbolContents = inspectList(symbols.map(function (key) {
  472. return [key, object[key]];
  473. }), options, inspectProperty);
  474. options.seen.pop();
  475. var sep = '';
  476. if (propertyContents && symbolContents) {
  477. sep = ', ';
  478. }
  479. return "{ ".concat(propertyContents).concat(sep).concat(symbolContents, " }");
  480. }
  481. var toStringTag = typeof Symbol !== 'undefined' && Symbol.toStringTag ? Symbol.toStringTag : false;
  482. function inspectClass(value, options) {
  483. var name = '';
  484. if (toStringTag && toStringTag in value) {
  485. name = value[toStringTag];
  486. }
  487. name = name || getFuncName_1(value.constructor); // Babel transforms anonymous classes to the name `_class`
  488. if (!name || name === '_class') {
  489. name = '<Anonymous Class>';
  490. }
  491. options.truncate -= name.length;
  492. return "".concat(name).concat(inspectObject(value, options));
  493. }
  494. function inspectArguments(args, options) {
  495. if (args.length === 0) return 'Arguments[]';
  496. options.truncate -= 13;
  497. return "Arguments[ ".concat(inspectList(args, options), " ]");
  498. }
  499. var errorKeys = ['stack', 'line', 'column', 'name', 'message', 'fileName', 'lineNumber', 'columnNumber', 'number', 'description'];
  500. function inspectObject$1(error, options) {
  501. var properties = Object.getOwnPropertyNames(error).filter(function (key) {
  502. return errorKeys.indexOf(key) === -1;
  503. });
  504. var name = error.name;
  505. options.truncate -= name.length;
  506. var message = '';
  507. if (typeof error.message === 'string') {
  508. message = truncate(error.message, options.truncate);
  509. } else {
  510. properties.unshift('message');
  511. }
  512. message = message ? ": ".concat(message) : '';
  513. options.truncate -= message.length + 5;
  514. var propertyContents = inspectList(properties.map(function (key) {
  515. return [key, error[key]];
  516. }), options, inspectProperty);
  517. return "".concat(name).concat(message).concat(propertyContents ? " { ".concat(propertyContents, " }") : '');
  518. }
  519. function inspectAttribute(_ref, options) {
  520. var _ref2 = _slicedToArray(_ref, 2),
  521. key = _ref2[0],
  522. value = _ref2[1];
  523. options.truncate -= 3;
  524. if (!value) {
  525. return "".concat(options.stylize(key, 'yellow'));
  526. }
  527. return "".concat(options.stylize(key, 'yellow'), "=").concat(options.stylize("\"".concat(value, "\""), 'string'));
  528. }
  529. function inspectHTMLCollection(collection, options) {
  530. // eslint-disable-next-line no-use-before-define
  531. return inspectList(collection, options, inspectHTML, '\n');
  532. }
  533. function inspectHTML(element, options) {
  534. var properties = element.getAttributeNames();
  535. var name = element.tagName.toLowerCase();
  536. var head = options.stylize("<".concat(name), 'special');
  537. var headClose = options.stylize(">", 'special');
  538. var tail = options.stylize("</".concat(name, ">"), 'special');
  539. options.truncate -= name.length * 2 + 5;
  540. var propertyContents = '';
  541. if (properties.length > 0) {
  542. propertyContents += ' ';
  543. propertyContents += inspectList(properties.map(function (key) {
  544. return [key, element.getAttribute(key)];
  545. }), options, inspectAttribute, ' ');
  546. }
  547. options.truncate -= propertyContents.length;
  548. var truncate = options.truncate;
  549. var children = inspectHTMLCollection(element.children, options);
  550. if (children && children.length > truncate) {
  551. children = "".concat(truncator, "(").concat(element.children.length, ")");
  552. }
  553. return "".concat(head).concat(propertyContents).concat(headClose).concat(children).concat(tail);
  554. }
  555. var symbolsSupported = typeof Symbol === 'function' && typeof Symbol.for === 'function';
  556. var chaiInspect = symbolsSupported ? Symbol.for('chai/inspect') : '@@chai/inspect';
  557. var nodeInspect = false;
  558. try {
  559. // eslint-disable-next-line global-require
  560. var nodeUtil = require('util');
  561. nodeInspect = nodeUtil.inspect ? nodeUtil.inspect.custom : false;
  562. } catch (noNodeInspect) {
  563. nodeInspect = false;
  564. }
  565. function FakeMap() {
  566. // eslint-disable-next-line prefer-template
  567. this.key = 'chai/loupe__' + Math.random() + Date.now();
  568. }
  569. FakeMap.prototype = {
  570. // eslint-disable-next-line object-shorthand
  571. get: function get(key) {
  572. return key[this.key];
  573. },
  574. // eslint-disable-next-line object-shorthand
  575. has: function has(key) {
  576. return this.key in key;
  577. },
  578. // eslint-disable-next-line object-shorthand
  579. set: function set(key, value) {
  580. if (Object.isExtensible(key)) {
  581. Object.defineProperty(key, this.key, {
  582. // eslint-disable-next-line object-shorthand
  583. value: value,
  584. configurable: true
  585. });
  586. }
  587. }
  588. };
  589. var constructorMap = new (typeof WeakMap === 'function' ? WeakMap : FakeMap)();
  590. var stringTagMap = {};
  591. var baseTypesMap = {
  592. undefined: function undefined$1(value, options) {
  593. return options.stylize('undefined', 'undefined');
  594. },
  595. null: function _null(value, options) {
  596. return options.stylize(null, 'null');
  597. },
  598. boolean: function boolean(value, options) {
  599. return options.stylize(value, 'boolean');
  600. },
  601. Boolean: function Boolean(value, options) {
  602. return options.stylize(value, 'boolean');
  603. },
  604. number: inspectNumber,
  605. Number: inspectNumber,
  606. bigint: inspectBigInt,
  607. BigInt: inspectBigInt,
  608. string: inspectString,
  609. String: inspectString,
  610. function: inspectFunction,
  611. Function: inspectFunction,
  612. symbol: inspectSymbol,
  613. // A Symbol polyfill will return `Symbol` not `symbol` from typedetect
  614. Symbol: inspectSymbol,
  615. Array: inspectArray,
  616. Date: inspectDate,
  617. Map: inspectMap,
  618. Set: inspectSet,
  619. RegExp: inspectRegExp,
  620. Promise: inspectPromise,
  621. // WeakSet, WeakMap are totally opaque to us
  622. WeakSet: function WeakSet(value, options) {
  623. return options.stylize('WeakSet{…}', 'special');
  624. },
  625. WeakMap: function WeakMap(value, options) {
  626. return options.stylize('WeakMap{…}', 'special');
  627. },
  628. Arguments: inspectArguments,
  629. Int8Array: inspectTypedArray,
  630. Uint8Array: inspectTypedArray,
  631. Uint8ClampedArray: inspectTypedArray,
  632. Int16Array: inspectTypedArray,
  633. Uint16Array: inspectTypedArray,
  634. Int32Array: inspectTypedArray,
  635. Uint32Array: inspectTypedArray,
  636. Float32Array: inspectTypedArray,
  637. Float64Array: inspectTypedArray,
  638. Generator: function Generator() {
  639. return '';
  640. },
  641. DataView: function DataView() {
  642. return '';
  643. },
  644. ArrayBuffer: function ArrayBuffer() {
  645. return '';
  646. },
  647. Error: inspectObject$1,
  648. HTMLCollection: inspectHTMLCollection,
  649. NodeList: inspectHTMLCollection
  650. }; // eslint-disable-next-line complexity
  651. var inspectCustom = function inspectCustom(value, options, type) {
  652. if (chaiInspect in value && typeof value[chaiInspect] === 'function') {
  653. return value[chaiInspect](options);
  654. }
  655. if (nodeInspect && nodeInspect in value && typeof value[nodeInspect] === 'function') {
  656. return value[nodeInspect](options.depth, options);
  657. }
  658. if ('inspect' in value && typeof value.inspect === 'function') {
  659. return value.inspect(options.depth, options);
  660. }
  661. if ('constructor' in value && constructorMap.has(value.constructor)) {
  662. return constructorMap.get(value.constructor)(value, options);
  663. }
  664. if (stringTagMap[type]) {
  665. return stringTagMap[type](value, options);
  666. }
  667. return '';
  668. };
  669. var toString$1 = Object.prototype.toString; // eslint-disable-next-line complexity
  670. function inspect(value, options) {
  671. options = normaliseOptions(options);
  672. options.inspect = inspect;
  673. var _options = options,
  674. customInspect = _options.customInspect;
  675. var type = value === null ? 'null' : _typeof(value);
  676. if (type === 'object') {
  677. type = toString$1.call(value).slice(8, -1);
  678. } // If it is a base value that we already support, then use Loupe's inspector
  679. if (baseTypesMap[type]) {
  680. return baseTypesMap[type](value, options);
  681. } // If `options.customInspect` is set to true then try to use the custom inspector
  682. if (customInspect && value) {
  683. var output = inspectCustom(value, options, type);
  684. if (output) {
  685. if (typeof output === 'string') return output;
  686. return inspect(output, options);
  687. }
  688. }
  689. var proto = value ? Object.getPrototypeOf(value) : false; // If it's a plain Object then use Loupe's inspector
  690. if (proto === Object.prototype || proto === null) {
  691. return inspectObject(value, options);
  692. } // Specifically account for HTMLElements
  693. // eslint-disable-next-line no-undef
  694. if (value && typeof HTMLElement === 'function' && value instanceof HTMLElement) {
  695. return inspectHTML(value, options);
  696. }
  697. if ('constructor' in value) {
  698. // If it is a class, inspect it like an object but add the constructor name
  699. if (value.constructor !== Object) {
  700. return inspectClass(value, options);
  701. } // If it is an object with an anonymous prototype, display it as an object.
  702. return inspectObject(value, options);
  703. } // last chance to check if it's an object
  704. if (value === Object(value)) {
  705. return inspectObject(value, options);
  706. } // We have run out of options! Just stringify the value
  707. return options.stylize(String(value), type);
  708. }
  709. function registerConstructor(constructor, inspector) {
  710. if (constructorMap.has(constructor)) {
  711. return false;
  712. }
  713. constructorMap.set(constructor, inspector);
  714. return true;
  715. }
  716. function registerStringTag(stringTag, inspector) {
  717. if (stringTag in stringTagMap) {
  718. return false;
  719. }
  720. stringTagMap[stringTag] = inspector;
  721. return true;
  722. }
  723. var custom = chaiInspect;
  724. exports.custom = custom;
  725. exports.default = inspect;
  726. exports.inspect = inspect;
  727. exports.registerConstructor = registerConstructor;
  728. exports.registerStringTag = registerStringTag;
  729. Object.defineProperty(exports, '__esModule', { value: true });
  730. })));