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, 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