-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from brahn/fix-sjoin-with-duplicate-fieldnames
Fix sjoin with duplicate fieldnames
- Loading branch information
Showing
18 changed files
with
20,040 additions
and
28,789 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
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,33 +1,49 @@ | ||
function mergeWith (object1, object2, lsuffix, rsuffix) { | ||
const keys1 = Object.keys(object1) | ||
const keys2 = Object.keys(object2) | ||
function findAllDuplicates(array) { | ||
const uniq = new Set(array); | ||
if (uniq.size === array.length) { | ||
return []; | ||
} | ||
const duplicates = new Set() | ||
for (const element of array) { | ||
if (uniq.has(element)) { | ||
uniq.delete(element); | ||
} else { | ||
duplicates.add(element); | ||
} | ||
} | ||
return [...duplicates]; | ||
} | ||
|
||
function mergeWith(leftFeatureProps, rightFeatureProps, lsuffix = 'left', rsuffix = 'right') { | ||
const keys1 = Object.keys(leftFeatureProps) | ||
const keys2 = Object.keys(rightFeatureProps) | ||
const allKeys = [...keys1, ...keys2] | ||
const uniq = [...new Set(allKeys)] | ||
const duplicates = findAllDuplicates(allKeys) | ||
const feature = {} | ||
if (allKeys.length === uniq.length) { | ||
keys1.forEach(key => (feature[key] = object1[key])) | ||
keys2.forEach(key => (feature[key] = object2[key])) | ||
if (duplicates.length === 0) { | ||
keys1.forEach(key => (feature[key] = leftFeatureProps[key])) | ||
keys2.forEach(key => (feature[key] = rightFeatureProps[key])) | ||
return feature | ||
} else { | ||
const duplicates = allKeys.filter(key => uniq.indexOf(key) === -1) | ||
keys1.forEach(key => { | ||
if (duplicates.indexOf(key) > -1) { | ||
feature[key + '_' + lsuffix] = object1[key] | ||
feature[key + '_' + lsuffix] = leftFeatureProps[key] | ||
} else { | ||
feature[key] = object1[key] | ||
feature[key] = leftFeatureProps[key] | ||
} | ||
}) | ||
keys2.forEach(key => { | ||
if (duplicates.indexOf(key) > -1) { | ||
feature[key + '_' + rsuffix] = object2[key] | ||
feature[key + '_' + rsuffix] = rightFeatureProps[key] | ||
} else { | ||
feature[key] = object1[key] | ||
feature[key] = rightFeatureProps[key] | ||
} | ||
}) | ||
return feature | ||
} | ||
} | ||
|
||
export { | ||
findAllDuplicates, // exported just for testing | ||
mergeWith | ||
} |
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
Oops, something went wrong.