-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(explode-fields): Explode name fields such that no field contains…
… more than a single value
- Loading branch information
1 parent
d4cfa21
commit d2cb81d
Showing
4 changed files
with
42 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Explode name fields such that no field contains more than a | ||
* single value. | ||
*/ | ||
|
||
const _ = require('lodash'); | ||
const prefix = 'name'; | ||
|
||
function explode(doc) { | ||
let field = doc[prefix]; | ||
if (!_.isPlainObject(field)) { return; } | ||
|
||
_.each(field, (values, subfield) => { | ||
if (_.isArray(values) && _.size(values) > 1) { | ||
_.each(values, (value, i) => { | ||
if (i === 0) { | ||
doc[prefix][subfield] = value; | ||
} else { | ||
doc[prefix][`${subfield}_${i}`] = value; | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = explode; |
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