From a1718290346fa467c58d7461365dac251d4e3585 Mon Sep 17 00:00:00 2001 From: Charlie Benger-Stevenson Date: Thu, 3 Oct 2024 09:04:03 +0100 Subject: [PATCH 1/2] Added code to replace all instances of a search string --- src/transformers/stringReplaceTransformer.js | 7 +++++- .../stringReplaceTransformer.test.js | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/transformers/stringReplaceTransformer.js b/src/transformers/stringReplaceTransformer.js index 1dac0f3..6fa35c8 100644 --- a/src/transformers/stringReplaceTransformer.js +++ b/src/transformers/stringReplaceTransformer.js @@ -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) diff --git a/test/transformers/stringReplaceTransformer.test.js b/test/transformers/stringReplaceTransformer.test.js index 45228c3..44f955d 100644 --- a/test/transformers/stringReplaceTransformer.test.js +++ b/test/transformers/stringReplaceTransformer.test.js @@ -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) + } + }) + ) + }) }) \ No newline at end of file From f1f7a320a497c57d94b2062cc6bfadfeb654c4d0 Mon Sep 17 00:00:00 2001 From: Charlie Benger-Stevenson Date: Thu, 3 Oct 2024 09:06:45 +0100 Subject: [PATCH 2/2] version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 156e670..056db4a 100644 --- a/package.json +++ b/package.json @@ -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,