-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for transforms (#431)
BREAKING CHANGE: module no longer takes `unwind`, `unwindBlank`, `flatten` or the `flattenSeparator` options, instead see the new `transforms` option. CLI options are unchanged from the callers side, but use the built in transforms under the hood. * Add support for transforms * Add documentation about transforms
- Loading branch information
1 parent
f7dd7eb
commit f1d04d0
Showing
13 changed files
with
739 additions
and
606 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
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
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,31 @@ | ||
/** | ||
* Performs the flattening of a data row recursively | ||
* | ||
* @param {String} separator Separator to be used as the flattened field name | ||
* @returns {Object => Object} Flattened object | ||
*/ | ||
function flatten(separator = '.') { | ||
function step (obj, flatDataRow, currentPath) { | ||
Object.keys(obj).forEach((key) => { | ||
const newPath = currentPath ? `${currentPath}${separator}${key}` : key; | ||
const value = obj[key]; | ||
|
||
if (typeof value !== 'object' | ||
|| value === null | ||
|| Array.isArray(value) | ||
|| Object.prototype.toString.call(value.toJSON) === '[object Function]' | ||
|| !Object.keys(value).length) { | ||
flatDataRow[newPath] = value; | ||
return; | ||
} | ||
|
||
step(value, flatDataRow, newPath); | ||
}); | ||
|
||
return flatDataRow; | ||
} | ||
|
||
return dataRow => step(dataRow, {}); | ||
} | ||
|
||
module.exports = flatten; |
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,40 @@ | ||
|
||
const lodashGet = require('lodash.get'); | ||
const { setProp, flattenReducer } = require('../utils'); | ||
|
||
/** | ||
* Performs the unwind recursively in specified sequence | ||
* | ||
* @param {String[]} unwindPaths The paths as strings to be used to deconstruct the array | ||
* @returns {Object => Array} Array of objects containing all rows after unwind of chosen paths | ||
*/ | ||
function unwind(paths, blankOut = false) { | ||
function unwindReducer(rows, unwindPath) { | ||
return rows | ||
.map(row => { | ||
const unwindArray = lodashGet(row, unwindPath); | ||
|
||
if (!Array.isArray(unwindArray)) { | ||
return row; | ||
} | ||
|
||
if (!unwindArray.length) { | ||
return setProp(row, unwindPath, undefined); | ||
} | ||
|
||
return unwindArray.map((unwindRow, index) => { | ||
const clonedRow = (blankOut && index > 0) | ||
? {} | ||
: row; | ||
|
||
return setProp(clonedRow, unwindPath, unwindRow); | ||
}); | ||
}) | ||
.reduce(flattenReducer, []); | ||
} | ||
|
||
paths = Array.isArray(paths) ? paths : (paths ? [paths] : []); | ||
return dataRow => paths.reduce(unwindReducer, [dataRow]); | ||
} | ||
|
||
module.exports = unwind; |
Oops, something went wrong.