array.js 4.2 KB

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