index.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. var __spreadArray = (this && this.__spreadArray) || function (to, from) {
  19. for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
  20. to[j] = from[i];
  21. return to;
  22. };
  23. var __importDefault = (this && this.__importDefault) || function (mod) {
  24. return (mod && mod.__esModule) ? mod : { "default": mod };
  25. };
  26. exports.__esModule = true;
  27. exports.unique = exports.mergeWithRules = exports.mergeWithCustomize = exports["default"] = exports.merge = exports.CustomizeRule = exports.customizeObject = exports.customizeArray = void 0;
  28. var wildcard_1 = __importDefault(require("wildcard"));
  29. var merge_with_1 = __importDefault(require("./merge-with"));
  30. var join_arrays_1 = __importDefault(require("./join-arrays"));
  31. var unique_1 = __importDefault(require("./unique"));
  32. exports.unique = unique_1["default"];
  33. var types_1 = require("./types");
  34. exports.CustomizeRule = types_1.CustomizeRule;
  35. var utils_1 = require("./utils");
  36. function merge(firstConfiguration) {
  37. var configurations = [];
  38. for (var _i = 1; _i < arguments.length; _i++) {
  39. configurations[_i - 1] = arguments[_i];
  40. }
  41. return mergeWithCustomize({}).apply(void 0, __spreadArray([firstConfiguration], __read(configurations)));
  42. }
  43. exports.merge = merge;
  44. exports["default"] = merge;
  45. function mergeWithCustomize(options) {
  46. return function mergeWithOptions(firstConfiguration) {
  47. var configurations = [];
  48. for (var _i = 1; _i < arguments.length; _i++) {
  49. configurations[_i - 1] = arguments[_i];
  50. }
  51. if (utils_1.isUndefined(firstConfiguration) || configurations.some(utils_1.isUndefined)) {
  52. throw new TypeError("Merging undefined is not supported");
  53. }
  54. // @ts-ignore
  55. if (firstConfiguration.then) {
  56. throw new TypeError("Promises are not supported");
  57. }
  58. // No configuration at all
  59. if (!firstConfiguration) {
  60. return {};
  61. }
  62. if (configurations.length === 0) {
  63. if (Array.isArray(firstConfiguration)) {
  64. // Empty array
  65. if (firstConfiguration.length === 0) {
  66. return {};
  67. }
  68. if (firstConfiguration.some(utils_1.isUndefined)) {
  69. throw new TypeError("Merging undefined is not supported");
  70. }
  71. // @ts-ignore
  72. if (firstConfiguration[0].then) {
  73. throw new TypeError("Promises are not supported");
  74. }
  75. return merge_with_1["default"](firstConfiguration, join_arrays_1["default"](options));
  76. }
  77. return firstConfiguration;
  78. }
  79. return merge_with_1["default"]([firstConfiguration].concat(configurations), join_arrays_1["default"](options));
  80. };
  81. }
  82. exports.mergeWithCustomize = mergeWithCustomize;
  83. function customizeArray(rules) {
  84. return function (a, b, key) {
  85. var matchedRule = Object.keys(rules).find(function (rule) { return wildcard_1["default"](rule, key); }) || "";
  86. if (matchedRule) {
  87. switch (rules[matchedRule]) {
  88. case types_1.CustomizeRule.Prepend:
  89. return __spreadArray(__spreadArray([], __read(b)), __read(a));
  90. case types_1.CustomizeRule.Replace:
  91. return b;
  92. case types_1.CustomizeRule.Append:
  93. default:
  94. return __spreadArray(__spreadArray([], __read(a)), __read(b));
  95. }
  96. }
  97. };
  98. }
  99. exports.customizeArray = customizeArray;
  100. function mergeWithRules(rules) {
  101. return mergeWithCustomize({
  102. customizeArray: function (a, b, key) {
  103. var currentRule = rules;
  104. key.split(".").forEach(function (k) {
  105. if (!currentRule) {
  106. return;
  107. }
  108. currentRule = currentRule[k];
  109. });
  110. if (utils_1.isPlainObject(currentRule)) {
  111. return mergeWithRule({ currentRule: currentRule, a: a, b: b });
  112. }
  113. if (typeof currentRule === "string") {
  114. return mergeIndividualRule({ currentRule: currentRule, a: a, b: b });
  115. }
  116. return undefined;
  117. }
  118. });
  119. }
  120. exports.mergeWithRules = mergeWithRules;
  121. var isArray = Array.isArray;
  122. function mergeWithRule(_a) {
  123. var currentRule = _a.currentRule, a = _a.a, b = _a.b;
  124. if (!isArray(a)) {
  125. return a;
  126. }
  127. var bAllMatches = [];
  128. var ret = a.map(function (ao) {
  129. if (!utils_1.isPlainObject(currentRule)) {
  130. return ao;
  131. }
  132. var ret = {};
  133. var rulesToMatch = [];
  134. var operations = {};
  135. Object.entries(currentRule).forEach(function (_a) {
  136. var _b = __read(_a, 2), k = _b[0], v = _b[1];
  137. if (v === types_1.CustomizeRule.Match) {
  138. rulesToMatch.push(k);
  139. }
  140. else {
  141. operations[k] = v;
  142. }
  143. });
  144. var bMatches = b.filter(function (o) {
  145. var matches = rulesToMatch.every(function (rule) { var _a, _b; return ((_a = ao[rule]) === null || _a === void 0 ? void 0 : _a.toString()) === ((_b = o[rule]) === null || _b === void 0 ? void 0 : _b.toString()); });
  146. if (matches) {
  147. bAllMatches.push(o);
  148. }
  149. return matches;
  150. });
  151. if (!utils_1.isPlainObject(ao)) {
  152. return ao;
  153. }
  154. Object.entries(ao).forEach(function (_a) {
  155. var _b = __read(_a, 2), k = _b[0], v = _b[1];
  156. var rule = currentRule;
  157. switch (currentRule[k]) {
  158. case types_1.CustomizeRule.Match:
  159. ret[k] = v;
  160. Object.entries(rule).forEach(function (_a) {
  161. var _b = __read(_a, 2), k = _b[0], v = _b[1];
  162. if (v === types_1.CustomizeRule.Replace && bMatches.length > 0) {
  163. var val = last(bMatches)[k];
  164. if (typeof val !== "undefined") {
  165. ret[k] = val;
  166. }
  167. }
  168. });
  169. break;
  170. case types_1.CustomizeRule.Append:
  171. if (!bMatches.length) {
  172. ret[k] = v;
  173. break;
  174. }
  175. var appendValue = last(bMatches)[k];
  176. if (!isArray(v) || !isArray(appendValue)) {
  177. throw new TypeError("Trying to append non-arrays");
  178. }
  179. ret[k] = v.concat(appendValue);
  180. break;
  181. case types_1.CustomizeRule.Merge:
  182. if (!bMatches.length) {
  183. ret[k] = v;
  184. break;
  185. }
  186. var lastValue = last(bMatches)[k];
  187. if (!utils_1.isPlainObject(v) || !utils_1.isPlainObject(lastValue)) {
  188. throw new TypeError("Trying to merge non-objects");
  189. }
  190. // deep merge
  191. ret[k] = merge(v, lastValue);
  192. break;
  193. case types_1.CustomizeRule.Prepend:
  194. if (!bMatches.length) {
  195. ret[k] = v;
  196. break;
  197. }
  198. var prependValue = last(bMatches)[k];
  199. if (!isArray(v) || !isArray(prependValue)) {
  200. throw new TypeError("Trying to prepend non-arrays");
  201. }
  202. ret[k] = prependValue.concat(v);
  203. break;
  204. case types_1.CustomizeRule.Replace:
  205. ret[k] = bMatches.length > 0 ? last(bMatches)[k] : v;
  206. break;
  207. default:
  208. var currentRule_1 = operations[k];
  209. // Use .flat(); starting from Node 12
  210. var b_1 = bMatches
  211. .map(function (o) { return o[k]; })
  212. .reduce(function (acc, val) {
  213. return isArray(acc) && isArray(val) ? __spreadArray(__spreadArray([], __read(acc)), __read(val)) : acc;
  214. }, []);
  215. ret[k] = mergeWithRule({ currentRule: currentRule_1, a: v, b: b_1 });
  216. break;
  217. }
  218. });
  219. return ret;
  220. });
  221. return ret.concat(b.filter(function (o) { return !bAllMatches.includes(o); }));
  222. }
  223. function mergeIndividualRule(_a) {
  224. var currentRule = _a.currentRule, a = _a.a, b = _a.b;
  225. // What if there's no match?
  226. switch (currentRule) {
  227. case types_1.CustomizeRule.Append:
  228. return a.concat(b);
  229. case types_1.CustomizeRule.Prepend:
  230. return b.concat(a);
  231. case types_1.CustomizeRule.Replace:
  232. return b;
  233. }
  234. return a;
  235. }
  236. function last(arr) {
  237. return arr[arr.length - 1];
  238. }
  239. function customizeObject(rules) {
  240. return function (a, b, key) {
  241. switch (rules[key]) {
  242. case types_1.CustomizeRule.Prepend:
  243. return merge_with_1["default"]([b, a], join_arrays_1["default"]());
  244. case types_1.CustomizeRule.Replace:
  245. return b;
  246. case types_1.CustomizeRule.Append:
  247. return merge_with_1["default"]([a, b], join_arrays_1["default"]());
  248. }
  249. };
  250. }
  251. exports.customizeObject = customizeObject;
  252. //# sourceMappingURL=index.js.map