utils.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. function recordFromArray(array, mainKey) {
  4. const record = Object.create(null);
  5. for (const value of array) {
  6. const key = value[mainKey];
  7. // istanbul ignore next
  8. if (record[key]) {
  9. throw new Error(`Duplicate ${mainKey} ${JSON.stringify(key)}`);
  10. }
  11. // @ts-ignore
  12. record[key] = value;
  13. }
  14. return record;
  15. }
  16. exports.recordFromArray = recordFromArray;
  17. function mapFromArray(array, mainKey) {
  18. const map = new Map();
  19. for (const value of array) {
  20. const key = value[mainKey];
  21. // istanbul ignore next
  22. if (map.has(key)) {
  23. throw new Error(`Duplicate ${mainKey} ${JSON.stringify(key)}`);
  24. }
  25. map.set(key, value);
  26. }
  27. return map;
  28. }
  29. exports.mapFromArray = mapFromArray;
  30. function createAutoChecklist() {
  31. const map = Object.create(null);
  32. return (id) => {
  33. const idString = JSON.stringify(id);
  34. if (map[idString]) {
  35. return true;
  36. }
  37. map[idString] = true;
  38. return false;
  39. };
  40. }
  41. exports.createAutoChecklist = createAutoChecklist;
  42. function partition(array, predicate) {
  43. const trueArray = [];
  44. const falseArray = [];
  45. for (const value of array) {
  46. if (predicate(value)) {
  47. trueArray.push(value);
  48. }
  49. else {
  50. falseArray.push(value);
  51. }
  52. }
  53. return [trueArray, falseArray];
  54. }
  55. exports.partition = partition;
  56. function isInt(value) {
  57. return value === Math.floor(value);
  58. }
  59. exports.isInt = isInt;
  60. function comparePrimitive(a, b) {
  61. if (a === b) {
  62. return 0;
  63. }
  64. const typeofA = typeof a;
  65. const typeofB = typeof b;
  66. const orders = [
  67. 'undefined',
  68. 'object',
  69. 'boolean',
  70. 'number',
  71. 'string',
  72. ];
  73. if (typeofA !== typeofB) {
  74. return orders.indexOf(typeofA) - orders.indexOf(typeofB);
  75. }
  76. if (typeofA !== 'string') {
  77. return Number(a) - Number(b);
  78. }
  79. return a.localeCompare(b);
  80. }
  81. exports.comparePrimitive = comparePrimitive;
  82. function normalizeDefaultResult(result) {
  83. return result === undefined ? {} : result;
  84. }
  85. exports.normalizeDefaultResult = normalizeDefaultResult;
  86. function normalizeValidateResult(result, value) {
  87. return result === true ? true : result === false ? { value } : result;
  88. }
  89. exports.normalizeValidateResult = normalizeValidateResult;
  90. function normalizeDeprecatedResult(result, value, doNotNormalizeTrue = false) {
  91. return result === false
  92. ? false
  93. : result === true
  94. ? doNotNormalizeTrue
  95. ? true
  96. : [{ value }]
  97. : 'value' in result
  98. ? [result]
  99. : result.length === 0
  100. ? false
  101. : result;
  102. }
  103. exports.normalizeDeprecatedResult = normalizeDeprecatedResult;
  104. function normalizeTransferResult(result, value) {
  105. return typeof result === 'string' || 'key' in result
  106. ? { from: value, to: result }
  107. : 'from' in result
  108. ? { from: result.from, to: result.to }
  109. : { from: value, to: result.to };
  110. }
  111. exports.normalizeTransferResult = normalizeTransferResult;
  112. function normalizeForwardResult(result, value) {
  113. return result === undefined
  114. ? []
  115. : Array.isArray(result)
  116. ? result.map(transferResult => normalizeTransferResult(transferResult, value))
  117. : [normalizeTransferResult(result, value)];
  118. }
  119. exports.normalizeForwardResult = normalizeForwardResult;
  120. function normalizeRedirectResult(result, value) {
  121. const redirect = normalizeForwardResult(typeof result === 'object' && 'redirect' in result
  122. ? result.redirect
  123. : result, value);
  124. return redirect.length === 0
  125. ? { remain: value, redirect }
  126. : typeof result === 'object' && 'remain' in result
  127. ? { remain: result.remain, redirect }
  128. : { redirect };
  129. }
  130. exports.normalizeRedirectResult = normalizeRedirectResult;