-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from DEFRA/feature/string-replace-transformer
Added string replacement transformer
- Loading branch information
Showing
9 changed files
with
844 additions
and
1,652 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities. | ||
|
||
version: v1.25.0 | ||
ignore: {} | ||
patch: {} |
Large diffs are not rendered by default.
Oops, something went wrong.
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,85 @@ | ||
const { Etl, Loaders, Transformers, Destinations } = require("ffc-pay-etl-framework") | ||
|
||
let csvFile = `${process.cwd()}/test/fixtures/Monthly_Rainfall.csv` | ||
|
||
const columns = [ | ||
"Dist Code","Year", | ||
"State Code", | ||
"State Name", | ||
"Dist Name", | ||
"JANUARY RAINFALL (Millimeters)", | ||
"FEBRUARY RAINFALL (Millimeters)", | ||
"MARCH RAINFALL (Millimeters)", | ||
"APRIL RAINFALL (Millimeters)", | ||
"MAY RAINFALL (Millimeters)", | ||
"JUNE RAINFALL (Millimeters)", | ||
"JULY RAINFALL (Millimeters)", | ||
"AUGUST RAINFALL (Millimeters)", | ||
"SEPTEMBER RAINFALL (Millimeters)", | ||
"OCTOBER RAINFALL (Millimeters)", | ||
"NOVEMBER RAINFALL (Millimeters)", | ||
"DECEMBER RAINFALL (Millimeters)", | ||
"ANNUAL RAINFALL (Millimeters)" | ||
] | ||
|
||
const etl = new Etl.Etl() | ||
|
||
etl | ||
.loader(Loaders.CSVLoader({path: csvFile, columns: columns})) | ||
.transform(new Transformers.StringReplaceTransformer([ | ||
{ column: "JANUARY RAINFALL (Millimeters)", | ||
find: ".", | ||
replace: "," | ||
}, | ||
{ column: "FEBRUARY RAINFALL (Millimeters)", | ||
find: ".", | ||
replace: "," | ||
}, | ||
{ column: "MARCH RAINFALL (Millimeters)", | ||
find: ".", | ||
replace: "," | ||
}, | ||
{ column: "APRIL RAINFALL (Millimeters)", | ||
find: ".", | ||
replace: "," | ||
}, | ||
{ column: "MAY RAINFALL (Millimeters)", | ||
find: ".", | ||
replace: "," | ||
}, | ||
{ column: "JUNE RAINFALL (Millimeters)", | ||
find: ".", | ||
replace: "," | ||
}, | ||
{ column: "JULY RAINFALL (Millimeters)", | ||
find: ".", | ||
replace: "," | ||
}, | ||
{ column: "AUGUST RAINFALL (Millimeters)", | ||
find: ".", | ||
replace: "," | ||
}, | ||
{ column: "SEPTEMBER RAINFALL (Millimeters)", | ||
find: ".", | ||
replace: "," | ||
}, | ||
{ column: "OCTOBER RAINFALL (Millimeters)", | ||
find: ".", | ||
replace: "," | ||
}, | ||
{ column: "NOVEMBER RAINFALL (Millimeters)", | ||
find: ".", | ||
replace: "," | ||
}, | ||
{ column: "DECEMBER RAINFALL (Millimeters)", | ||
find: ".", | ||
replace: "," | ||
}, | ||
])) | ||
.destination(Destinations.CSVFileDestination({ | ||
fileName: "SoilType_Output.csv", | ||
headers: true, | ||
includeErrors: false, | ||
quotationMarks: true | ||
})) | ||
.pump() |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
const { ToUpperCaseTransformer } = require("./toUpperCaseTransformer") | ||
const { FakerTransformer } = require("./fakerTransformer") | ||
const { StringReplaceTransformer } = require("./stringReplaceTransformer") | ||
|
||
module.exports = { | ||
StringReplaceTransformer, | ||
ToUpperCaseTransformer, | ||
FakerTransformer | ||
} |
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 @@ | ||
const { Transform } = require("node:stream") | ||
|
||
/** | ||
* | ||
* @param {Object} options | ||
* @param {String} options.column | ||
* @returns Writable | ||
*/ | ||
function StringReplaceTransformer(options){ | ||
let self = this | ||
self.replacements = options | ||
|
||
return new Transform({ | ||
readableObjectMode: true, | ||
writableObjectMode: true, | ||
transform(chunk, _, callback){ | ||
const { _columns } = chunk | ||
// @ts-ignore | ||
self.replacements.forEach(r => { | ||
const colIndex = _columns.indexOf(r.column) | ||
chunk[colIndex] = chunk[colIndex].replace(r.find, r.replace) | ||
}) | ||
|
||
callback(null, chunk) | ||
} | ||
}) | ||
} | ||
|
||
module.exports = { | ||
StringReplaceTransformer | ||
} |
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,30 @@ | ||
const { expect } = require("@jest/globals") | ||
const { StringReplaceTransformer } = require("../../src/transformers") | ||
const { Readable, PassThrough } = require("node:stream") | ||
|
||
describe('stringReplaceTransformer tests', () => { | ||
it('should replace a search string with a replacement string', (done) => { | ||
const uut = StringReplaceTransformer([{ | ||
column: "column2", | ||
find: "'", | ||
replace: "''" | ||
}]) | ||
const testData =["a", "o'b", "c"] | ||
testData.errors = [] | ||
testData.rowId = 1 | ||
testData._columns = ["columm1", "column2", "column3"] | ||
const readable = Readable.from([testData]) | ||
readable | ||
.pipe(uut) | ||
.pipe(new PassThrough({ | ||
objectMode: true, | ||
transform(chunk,_,callback){ | ||
expect(chunk.errors.length).toEqual(0) | ||
expect(chunk[1]).toEqual("o''b") | ||
done() | ||
callback(null, chunk) | ||
} | ||
}) | ||
) | ||
}) | ||
}) |