Skip to content

Commit

Permalink
Merge pull request #36 from DEFRA/fix/stringreplacement-replace-all-flag
Browse files Browse the repository at this point in the history
Added code to replace all instances of a search string
  • Loading branch information
suityou01 authored Oct 3, 2024
2 parents 74a1648 + f1f7a32 commit 9051fa6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ffc-pay-etl-framework",
"version": "0.1.13",
"version": "0.1.14",
"publisher": "Defra",
"main": "dist/cjs/index.js",
"private": false,
Expand Down
7 changes: 6 additions & 1 deletion src/transformers/stringReplaceTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ function StringReplaceTransformer(options){
// @ts-ignore
self.replacements.forEach(r => {
const colIndex = _columns.indexOf(r.column)
chunk[colIndex] = chunk[colIndex].replace(r.find, r.replace)
if(r.all){
chunk[colIndex] = chunk[colIndex].replaceAll(r.find, r.replace)
}
else {
chunk[colIndex] = chunk[colIndex].replace(r.find, r.replace)
}
})

callback(null, chunk)
Expand Down
25 changes: 25 additions & 0 deletions test/transformers/stringReplaceTransformer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,29 @@ describe('stringReplaceTransformer tests', () => {
})
)
})
it('should replace all instances of a search string with a replacement string', (done) => {
const uut = StringReplaceTransformer([{
column: "column2",
find: "'",
replace: "''",
all: true
}])
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)
}
})
)
})
})

0 comments on commit 9051fa6

Please sign in to comment.