index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. 'use strict';
  2. var _values2 = require('lodash/values');
  3. var _values3 = _interopRequireDefault(_values2);
  4. var _unionWith2 = require('lodash/unionWith');
  5. var _unionWith3 = _interopRequireDefault(_unionWith2);
  6. var _mergeWith2 = require('lodash/mergeWith');
  7. var _mergeWith3 = _interopRequireDefault(_mergeWith2);
  8. var _differenceWith2 = require('lodash/differenceWith');
  9. var _differenceWith3 = _interopRequireDefault(_differenceWith2);
  10. var _joinArrays = require('./join-arrays');
  11. var _joinArrays2 = _interopRequireDefault(_joinArrays);
  12. var _joinArraysSmart = require('./join-arrays-smart');
  13. var _unique = require('./unique');
  14. var _unique2 = _interopRequireDefault(_unique);
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  17. function merge() {
  18. for (var _len = arguments.length, sources = Array(_len), _key = 0; _key < _len; _key++) {
  19. sources[_key] = arguments[_key];
  20. }
  21. // This supports
  22. // merge([<object>] | ...<object>)
  23. // merge({ customizeArray: <fn>, customizeObject: <fn>})([<object>] | ...<object>)
  24. // where fn = (a, b, key)
  25. if (sources.length === 1) {
  26. if (Array.isArray(sources[0])) {
  27. return _mergeWith3.default.apply(undefined, [{}].concat(_toConsumableArray(sources[0]), [(0, _joinArrays2.default)(sources[0])]));
  28. }
  29. if (sources[0].customizeArray || sources[0].customizeObject) {
  30. return function () {
  31. for (var _len2 = arguments.length, structures = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  32. structures[_key2] = arguments[_key2];
  33. }
  34. if (Array.isArray(structures[0])) {
  35. return _mergeWith3.default.apply(undefined, [{}].concat(_toConsumableArray(structures[0]), [(0, _joinArrays2.default)(sources[0])]));
  36. }
  37. return _mergeWith3.default.apply(undefined, [{}].concat(structures, [(0, _joinArrays2.default)(sources[0])]));
  38. };
  39. }
  40. return sources[0];
  41. }
  42. return _mergeWith3.default.apply(undefined, [{}].concat(sources, [(0, _joinArrays2.default)()]));
  43. }
  44. var mergeSmart = merge({
  45. customizeArray: function customizeArray(a, b, key) {
  46. if (isRule(key.split('.').slice(-1)[0])) {
  47. return (0, _unionWith3.default)(a, b, _joinArraysSmart.uniteRules.bind(null, {}, key));
  48. }
  49. return null;
  50. }
  51. });
  52. var mergeMultiple = function mergeMultiple() {
  53. for (var _len3 = arguments.length, sources = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  54. sources[_key3] = arguments[_key3];
  55. }
  56. return (0, _values3.default)(merge(sources));
  57. };
  58. // rules: { <field>: <'append'|'prepend'|'replace'> }
  59. // All default to append but you can override here
  60. var mergeStrategy = function mergeStrategy() {
  61. var rules = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  62. return merge({
  63. customizeArray: _customizeArray(rules),
  64. customizeObject: customizeObject(rules)
  65. });
  66. };
  67. var mergeSmartStrategy = function mergeSmartStrategy() {
  68. var rules = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  69. return merge({
  70. customizeArray: function customizeArray(a, b, key) {
  71. var topKey = key.split('.').slice(-1)[0];
  72. if (isRule(topKey)) {
  73. switch (rules[key]) {
  74. case 'prepend':
  75. return [].concat(_toConsumableArray((0, _differenceWith3.default)(b, a, function (newRule, seenRule) {
  76. return (0, _joinArraysSmart.uniteRules)(rules, key, newRule, seenRule, 'prepend');
  77. })), _toConsumableArray(a));
  78. case 'replace':
  79. return b;
  80. default:
  81. // append
  82. return (0, _unionWith3.default)(a, b, _joinArraysSmart.uniteRules.bind(null, rules, key));
  83. }
  84. }
  85. return _customizeArray(rules)(a, b, key);
  86. },
  87. customizeObject: customizeObject(rules)
  88. });
  89. };
  90. function _customizeArray(rules) {
  91. return function (a, b, key) {
  92. switch (rules[key]) {
  93. case 'prepend':
  94. return [].concat(_toConsumableArray(b), _toConsumableArray(a));
  95. case 'replace':
  96. return b;
  97. default:
  98. // append
  99. return false;
  100. }
  101. };
  102. }
  103. function customizeObject(rules) {
  104. return function (a, b, key) {
  105. switch (rules[key]) {
  106. case 'prepend':
  107. return (0, _mergeWith3.default)({}, b, a, (0, _joinArrays2.default)());
  108. case 'replace':
  109. return b;
  110. default:
  111. // append
  112. return false;
  113. }
  114. };
  115. }
  116. function isRule(key) {
  117. return ['preLoaders', 'loaders', 'postLoaders', 'rules'].indexOf(key) >= 0;
  118. }
  119. module.exports = merge;
  120. module.exports.multiple = mergeMultiple;
  121. module.exports.smart = mergeSmart;
  122. module.exports.strategy = mergeStrategy;
  123. module.exports.smartStrategy = mergeSmartStrategy;
  124. module.exports.unique = _unique2.default;