array.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function util() {
  7. const data = _interopRequireWildcard(require('util'));
  8. util = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _prettyFormat() {
  14. const data = _interopRequireDefault(require('pretty-format'));
  15. _prettyFormat = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _interopRequireDefault(obj) {
  21. return obj && obj.__esModule ? obj : {default: obj};
  22. }
  23. function _getRequireWildcardCache() {
  24. if (typeof WeakMap !== 'function') return null;
  25. var cache = new WeakMap();
  26. _getRequireWildcardCache = function () {
  27. return cache;
  28. };
  29. return cache;
  30. }
  31. function _interopRequireWildcard(obj) {
  32. if (obj && obj.__esModule) {
  33. return obj;
  34. }
  35. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  36. return {default: obj};
  37. }
  38. var cache = _getRequireWildcardCache();
  39. if (cache && cache.has(obj)) {
  40. return cache.get(obj);
  41. }
  42. var newObj = {};
  43. var hasPropertyDescriptor =
  44. Object.defineProperty && Object.getOwnPropertyDescriptor;
  45. for (var key in obj) {
  46. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  47. var desc = hasPropertyDescriptor
  48. ? Object.getOwnPropertyDescriptor(obj, key)
  49. : null;
  50. if (desc && (desc.get || desc.set)) {
  51. Object.defineProperty(newObj, key, desc);
  52. } else {
  53. newObj[key] = obj[key];
  54. }
  55. }
  56. }
  57. newObj.default = obj;
  58. if (cache) {
  59. cache.set(obj, newObj);
  60. }
  61. return newObj;
  62. }
  63. /**
  64. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  65. *
  66. * This source code is licensed under the MIT license found in the
  67. * LICENSE file in the root directory of this source tree.
  68. *
  69. */
  70. const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g;
  71. const PRETTY_PLACEHOLDER = '%p';
  72. const INDEX_PLACEHOLDER = '%#';
  73. const PLACEHOLDER_PREFIX = '%';
  74. const JEST_EACH_PLACEHOLDER_ESCAPE = '@@__JEST_EACH_PLACEHOLDER_ESCAPE__@@';
  75. var _default = (title, arrayTable) =>
  76. normaliseTable(arrayTable).map((row, index) => ({
  77. arguments: row,
  78. title: formatTitle(title, row, index)
  79. }));
  80. exports.default = _default;
  81. const normaliseTable = table => (isTable(table) ? table : table.map(colToRow));
  82. const isTable = table => table.every(Array.isArray);
  83. const colToRow = col => [col];
  84. const formatTitle = (title, row, rowIndex) =>
  85. row
  86. .reduce((formattedTitle, value) => {
  87. const [placeholder] = getMatchingPlaceholders(formattedTitle);
  88. const normalisedValue = normalisePlaceholderValue(value);
  89. if (!placeholder) return formattedTitle;
  90. if (placeholder === PRETTY_PLACEHOLDER)
  91. return interpolatePrettyPlaceholder(formattedTitle, normalisedValue);
  92. return util().format(formattedTitle, normalisedValue);
  93. }, interpolateTitleIndex(title, rowIndex))
  94. .replace(new RegExp(JEST_EACH_PLACEHOLDER_ESCAPE, 'g'), PLACEHOLDER_PREFIX);
  95. const normalisePlaceholderValue = value =>
  96. typeof value === 'string' && SUPPORTED_PLACEHOLDERS.test(value)
  97. ? value.replace(PLACEHOLDER_PREFIX, JEST_EACH_PLACEHOLDER_ESCAPE)
  98. : value;
  99. const getMatchingPlaceholders = title =>
  100. title.match(SUPPORTED_PLACEHOLDERS) || [];
  101. const interpolateTitleIndex = (title, index) =>
  102. title.replace(INDEX_PLACEHOLDER, index.toString());
  103. const interpolatePrettyPlaceholder = (title, value) =>
  104. title.replace(
  105. PRETTY_PLACEHOLDER,
  106. (0, _prettyFormat().default)(value, {
  107. maxDepth: 1,
  108. min: true
  109. })
  110. );