utils.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. var __read = (this && this.__read) || function (o, n) {
  3. var m = typeof Symbol === "function" && o[Symbol.iterator];
  4. if (!m) return o;
  5. var i = m.call(o), r, ar = [], e;
  6. try {
  7. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  8. }
  9. catch (error) { e = { error: error }; }
  10. finally {
  11. try {
  12. if (r && !r.done && (m = i["return"])) m.call(i);
  13. }
  14. finally { if (e) throw e.error; }
  15. }
  16. return ar;
  17. };
  18. exports.__esModule = true;
  19. exports.isSameCondition = exports.isUndefined = exports.isPlainObject = exports.isFunction = exports.isRegex = void 0;
  20. var flat_1 = require("flat");
  21. function isRegex(o) {
  22. return o instanceof RegExp;
  23. }
  24. exports.isRegex = isRegex;
  25. // https://stackoverflow.com/a/7356528/228885
  26. function isFunction(functionToCheck) {
  27. return (functionToCheck && {}.toString.call(functionToCheck) === "[object Function]");
  28. }
  29. exports.isFunction = isFunction;
  30. function isPlainObject(a) {
  31. if (a === null || Array.isArray(a)) {
  32. return false;
  33. }
  34. return typeof a === "object";
  35. }
  36. exports.isPlainObject = isPlainObject;
  37. function isUndefined(a) {
  38. return typeof a === "undefined";
  39. }
  40. exports.isUndefined = isUndefined;
  41. /**
  42. * According to Webpack docs, a "test" should be the following:
  43. *
  44. * - A string
  45. * - A RegExp
  46. * - A function
  47. * - An array of conditions (may be nested)
  48. * - An object of conditions (may be nested)
  49. *
  50. * https://webpack.js.org/configuration/module/#condition
  51. */
  52. function isSameCondition(a, b) {
  53. var _a, _b;
  54. if (!a || !b) {
  55. return a === b;
  56. }
  57. if (typeof a === 'string' || typeof b === 'string' ||
  58. isRegex(a) || isRegex(b) ||
  59. isFunction(a) || isFunction(b)) {
  60. return a.toString() === b.toString();
  61. }
  62. var entriesA = Object.entries(flat_1.flatten(a));
  63. var entriesB = Object.entries(flat_1.flatten(b));
  64. if (entriesA.length !== entriesB.length) {
  65. return false;
  66. }
  67. for (var i = 0; i < entriesA.length; i++) {
  68. entriesA[i][0] = entriesA[i][0].replace(/\b\d+\b/g, "[]");
  69. entriesB[i][0] = entriesB[i][0].replace(/\b\d+\b/g, "[]");
  70. }
  71. function cmp(_a, _b) {
  72. var _c = __read(_a, 2), k1 = _c[0], v1 = _c[1];
  73. var _d = __read(_b, 2), k2 = _d[0], v2 = _d[1];
  74. if (k1 < k2)
  75. return -1;
  76. if (k1 > k2)
  77. return 1;
  78. if (v1 < v2)
  79. return -1;
  80. if (v1 > v2)
  81. return 1;
  82. return 0;
  83. }
  84. ;
  85. entriesA.sort(cmp);
  86. entriesB.sort(cmp);
  87. if (entriesA.length !== entriesB.length) {
  88. return false;
  89. }
  90. for (var i = 0; i < entriesA.length; i++) {
  91. if (entriesA[i][0] !== entriesB[i][0] || ((_a = entriesA[i][1]) === null || _a === void 0 ? void 0 : _a.toString()) !== ((_b = entriesB[i][1]) === null || _b === void 0 ? void 0 : _b.toString())) {
  92. return false;
  93. }
  94. }
  95. return true;
  96. }
  97. exports.isSameCondition = isSameCondition;
  98. //# sourceMappingURL=utils.js.map