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