Skip to content

Commit

Permalink
feat: Use annotation in headerMap for required fields
Browse files Browse the repository at this point in the history
Instead of whole separate array.  Neater.
  • Loading branch information
jezhiggins committed Mar 13, 2019
1 parent 11370a4 commit e655b15
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
11 changes: 9 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ function selectTypes (headerMap) {
return headerMap.map(fields => fields[2])
} // selectTypes

function selectRequired (headerMap) {
return headerMap
.filter(fields => fields[3] === 'required')
.map(fields => fields[0])
} // selectRequired

async function openInputStream (xmlPath) {
if (!xmlPath) throw error('xmlPath must be set')

Expand Down Expand Up @@ -98,6 +104,7 @@ async function xmlFlatten2Csv (options) {
const selectors = selectPaths(options.headerMap)
const headers = selectHeaders(options.headerMap)
const types = selectTypes(options.headerMap)
const required = selectRequired(options.headerMap)

const inputStream = await openInputStream(options.xmlPath)
const outputStream = await createOutputStream(options.csvPath)
Expand All @@ -110,11 +117,11 @@ async function xmlFlatten2Csv (options) {
options.rootXMLElement,
options.pivotPath,
selectors,
required,
{
namespace: options.namespace,
xmllang: options.xmllang,
transform: options.transform,
required: options.required
transform: options.transform
}
)
.each(fields => writeLine(outputStream, fields, types))
Expand Down
5 changes: 1 addition & 4 deletions lib/xml-transform-to-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,11 @@ function xmlTransformToCsv (
elementName,
pivotPath,
selectPaths,
required,
options
) {
const treeTransform = (options && options.transform) || identityFn

const required = (options && Array.isArray(options.required) && options.required.length !== 0)
? options.required
: null

return new EachPromise((each, resolve, reject) => {
xmlSubtreeProcessor(inputStream, elementName, options)
.each(subTree => {
Expand Down
14 changes: 5 additions & 9 deletions test/xml-flatten2csv-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ describe('xmlFlatten2csv', () => {
['$.state', 'state', 'number'],
['$.stateDate', 'state_date', 'date'],
['@.streetDescription.en', 'description', 'string', 'required'],
['@.locality.en', 'locality', 'string', 'required'],
['@.townName.en', 'town_name', 'string', 'required'],
['@.locality.en', 'locality', 'string'],
['@.townName.en', 'town_name', 'string'],
['@.administrativeArea.en', 'administrative_area', 'string']
]

Expand All @@ -85,9 +85,7 @@ describe('xmlFlatten2csv', () => {
'gml-extract.csv',
root,
pivot,
headerMap,
null,
['@.streetDescription.en']
headerMap
)
})

Expand All @@ -97,8 +95,7 @@ describe('xmlFlatten2csv', () => {
root,
pivot,
headerMap,
transformFn,
required
transformFn
) {
const sourceFile = path.resolve(__dirname, 'fixtures', inputFilename)
const outputFile = path.resolve(__dirname, 'output', outputFilename)
Expand All @@ -114,8 +111,7 @@ describe('xmlFlatten2csv', () => {
headerMap: headerMap,
transform: transformFn,
namespace: 'strip',
xmllang: 'wrap',
required: required
xmllang: 'wrap'
})

const output = fs.readFileSync(outputFile, { encoding: 'utf8' }).split('\n').map(s => s.trim())
Expand Down
5 changes: 4 additions & 1 deletion test/xml-transform-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ describe('xml-transform-to-csv', () => {
'items',
'$..item',
mapping,
[],
{ xmllang: 'wrap' }
).each(fields => results.push(fields.join()))

Expand All @@ -126,7 +127,8 @@ describe('xml-transform-to-csv', () => {
'$.missing',
'@.description',
'$.price'
]
],
[]
)

try {
Expand Down Expand Up @@ -158,6 +160,7 @@ describe('xml-transform-to-csv', () => {
'items',
'$..item',
conditionalMapping,
[],
{ xmllang: 'wrap' }
).each(fields => results.push(fields.join()))

Expand Down

0 comments on commit e655b15

Please sign in to comment.