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