From 51de697ee51f09dfbd189ad1bfcb61c46ac5f94e Mon Sep 17 00:00:00 2001 From: Jez Higgins Date: Mon, 27 Apr 2020 11:13:26 +0100 Subject: [PATCH] refactor(File-builder): More to the point function names --- lib/File-builder.js | 92 ++++++++++++++++++++++----------------------- lib/Writer.js | 2 +- lib/index.js | 2 +- 3 files changed, 48 insertions(+), 48 deletions(-) diff --git a/lib/File-builder.js b/lib/File-builder.js index 5795129..9a36851 100644 --- a/lib/File-builder.js +++ b/lib/File-builder.js @@ -23,53 +23,15 @@ 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)) @@ -77,10 +39,10 @@ class FileBuilder { 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 () { @@ -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, @@ -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 diff --git a/lib/Writer.js b/lib/Writer.js index b13211f..1b562a9 100644 --- a/lib/Writer.js +++ b/lib/Writer.js @@ -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 { diff --git a/lib/index.js b/lib/index.js index 7a82a5f..3aa27c3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -42,7 +42,7 @@ async function processFilePaths (sourceFilePaths, options) { fileBuilder.close() return [ - fileBuilder.getManifest(), + fileBuilder.manifest(), fileBuilder.outputDirRootPath ] }