Skip to content

Commit

Permalink
refactor(File-builder): More to the point function names
Browse files Browse the repository at this point in the history
  • Loading branch information
jezhiggins committed Apr 27, 2020
1 parent 4ae8d0b commit 51de697
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
92 changes: 46 additions & 46 deletions lib/File-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,64 +23,26 @@ class FileBuilder {
this.dirSplits = options.dirSplits
.filter(dirSplit => dirSplit)
.filter(dirSplit => Object.prototype.hasOwnProperty.call(dirSplit, 'columnIndex'))
.map(dirSplit => FileBuilder.getColumnIndexDirSplit(dirSplit.columnIndex, dirSplit.valueToDirMap || {}))
.map(dirSplit => columnIndexDirSplit(dirSplit.columnIndex, dirSplit.valueToDirMap || {}))
}

if (Object.prototype.hasOwnProperty.call(options.fileSplits, 'columnIndex')) {
this.getFileConfigFromCsvLine = FileBuilder.getColumnIndexFileConfigFunction(options.fileSplits.columnIndex, options.fileSplits.valueToFileMap)
this.fileConfigFromCsvLine = columnIndexFileConfigFunction(options.fileSplits.columnIndex, options.fileSplits.valueToFileMap)
}
}

static getColumnIndexDirSplit (columnIndex, valueToDirMap) {
return function columnIndexDirSplit (line) {
let value = line[columnIndex]
if (Object.prototype.hasOwnProperty.call(valueToDirMap, value)) {
value = valueToDirMap[value]
} else if (Object.prototype.hasOwnProperty.call(valueToDirMap, '*')) {
value = valueToDirMap['*']
} else {
value = UNKNOWN_DIR
}
return value
}
}

static getColumnIndexFileConfigFunction (columnIndex, valueToFileMap) {
return function columnIndexFileInfo (line) {
let value = line[columnIndex]
const keys = _.keys(valueToFileMap)
let matchingKey
for (const key of keys) {
const splitKeys = key.split('&')
for (const k of splitKeys) {
if (k === value) {
matchingKey = key
}
}
}
if (Object.prototype.hasOwnProperty.call(valueToFileMap, matchingKey)) {
value = valueToFileMap[matchingKey]
} else if (Object.prototype.hasOwnProperty.call(valueToFileMap, '*')) {
value = valueToFileMap['*']
} else {
value = { filename: UNKNOWN_FILENAME }
}
return value
}
}

getDirPathFromCsvLine (incomingCsvLine) {
dirPathFromCsvLine (incomingCsvLine) {
if (!this.dirSplits) return '.'

const dirParts = this.dirSplits.map(fn => fn(incomingCsvLine))
const dirPath = './' + dirParts.join('/')
return dirPath
}

getWriteStreamInfo (incomingCsvLine, callback) {
writeStreamInfo (incomingCsvLine, callback) {
const _this = this
const dirPath = this.getDirPathFromCsvLine(incomingCsvLine)
const fileConfig = this.getFileConfigFromCsvLine(incomingCsvLine)
const dirPath = this.dirPathFromCsvLine(incomingCsvLine)
const fileConfig = this.fileConfigFromCsvLine(incomingCsvLine)
const key = dirPath + '/' + fileConfig.filename + '.csv'

function createWriteStream () {
Expand Down Expand Up @@ -131,9 +93,9 @@ class FileBuilder {

close () {
Object.values(this.files).forEach(file => file.writeStream.close())
}
} // close

getManifest () {
manifest () {
const manifest = {
outputDirRootPath: this.outputDirRootPath,
started: this.started,
Expand Down Expand Up @@ -183,4 +145,42 @@ class FileBuilder {
}
}

function columnIndexDirSplit (columnIndex, valueToDirMap) {
return function columnIndexDirSplit (line) {
let value = line[columnIndex]
if (Object.prototype.hasOwnProperty.call(valueToDirMap, value)) {
value = valueToDirMap[value]
} else if (Object.prototype.hasOwnProperty.call(valueToDirMap, '*')) {
value = valueToDirMap['*']
} else {
value = UNKNOWN_DIR
}
return value
}
} // columnIndexDirSplit

function columnIndexFileConfigFunction (columnIndex, valueToFileMap) {
return function columnIndexFileInfo (line) {
let value = line[columnIndex]
const keys = _.keys(valueToFileMap)
let matchingKey
for (const key of keys) {
const splitKeys = key.split('&')
for (const k of splitKeys) {
if (k === value) {
matchingKey = key
}
}
}
if (Object.prototype.hasOwnProperty.call(valueToFileMap, matchingKey)) {
value = valueToFileMap[matchingKey]
} else if (Object.prototype.hasOwnProperty.call(valueToFileMap, '*')) {
value = valueToFileMap['*']
} else {
value = { filename: UNKNOWN_FILENAME }
}
return value
}
} // columnIndexFileConfigFunction

module.exports = FileBuilder
2 changes: 1 addition & 1 deletion lib/Writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FileWriter extends Writable {
_write (incomingCsvLine, encoding, callback) {
if (!(this.firstLine && this.skipFirstLine)) {
this.firstLine = false
this.fileBuilder.getWriteStreamInfo(incomingCsvLine, function (err, info) {
this.fileBuilder.writeStreamInfo(incomingCsvLine, function (err, info) {
if (err) {
callback(err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function processFilePaths (sourceFilePaths, options) {

fileBuilder.close()
return [
fileBuilder.getManifest(),
fileBuilder.manifest(),
fileBuilder.outputDirRootPath
]
}
Expand Down

0 comments on commit 51de697

Please sign in to comment.