Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
instantsearch.js implementation (still not working)
Browse files Browse the repository at this point in the history
  • Loading branch information
Haroenv committed May 9, 2019
1 parent a13567a commit 43783b0
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/functions/mergePure.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
'use strict';

module.exports = require('lodash/merge');
const uniq = require('lodash/uniq');

// NOTE: repeated because #656 isn't merged yet
function objectHasKeys(obj) {
return Object.keys(Object(obj)) > -1;
}

// NOTE: this behaves like lodash/merge, but doesn't mutate the target
// module.exports = function defaultsPure() {
// const sources = Array.prototype.slice.call(arguments);
// return sources.reduceRight(function(acc, source) {
// Object.keys(Object(source)).forEach(function(key) {
// if (source[key] !== undefined) {
// acc[key] = source[key];
// }
// });
// return acc;
// }, {});
// };
module.exports = function mergeDeep() {
var values = Array.prototype.slice.call(arguments);
return values.reduce(function(acc, source) {
source = source || {};
Object.keys(Object(source)).forEach(function(key) {
var previousValue = acc[key];
var nextValue = source[key];

if (Array.isArray(previousValue)) {
var nextArray = Array.isArray(nextValue) ? nextValue : [nextValue];
acc[key] = uniq([].concat(previousValue, nextArray));
} else if (objectHasKeys(nextValue)) {
acc[key] = mergeDeep(previousValue, nextValue);
} else {
acc[key] = nextValue;
}
});

return acc;
}, {});
};

// TODO: make this removable
module.exports = require('lodash/merge');

0 comments on commit 43783b0

Please sign in to comment.