This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
instantsearch.js implementation (still not working)
- Loading branch information
Showing
1 changed file
with
30 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |