From bf7f9525765beb8a2b1f9a33f2c5d6040c354b66 Mon Sep 17 00:00:00 2001 From: Charlie Benger-Stevenson Date: Thu, 29 Aug 2024 16:18:21 +0100 Subject: [PATCH] Added locale support for fakerTransformer and repo details to package.json --- package.json | 3 +- src/transformers/README.md | 57 +++++++++++++--------- src/transformers/fakerTransformer.js | 9 +++- test/transformers/fakerTransformer.test.js | 33 ++++++++++++- 4 files changed, 76 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index 49728b1..f973fe9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ffc-pay-etl-framework", - "version": "0.0.9", + "version": "0.0.10", "publisher": "Defra", "main": "dist/cjs/index.js", "private": false, @@ -20,6 +20,7 @@ ], "author": "Charlie Benger-Stevenson", "license": "OGL-UK-3.0", + "repository": "https://github.com/DEFRA/ffc-pay-etl-framework", "description": "A framework for creating ETL pipelines in node", "jest": { "setupFiles": [ diff --git a/src/transformers/README.md b/src/transformers/README.md index 20874b2..fc46ea1 100644 --- a/src/transformers/README.md +++ b/src/transformers/README.md @@ -11,33 +11,46 @@ Currently supported transformers are : ### Options -| option | description | -| -------------- | ------------------------------------------------------------- | -| columns | Array of column transformations | +| option | description | +| ------- | ------------------------------------------------------------ | +| columns | Array of column transformations | +| locale | See https://fakerjs.dev/guide/localization for valid locales | ### Usage ```js -FakerTransformer( - { - columns: [ - { - name: "column2", - faker: "company.name" - } - ] - } -) +FakerTransformer({ + columns: [ + { + name: "column2", + faker: "company.name", + }, + ], +}); ``` faker is a string value that can be any of the currently supported [apis](https://fakerjs.dev/api/) -e.g. +e.g. - finance.accountName - person.firstName - randomizer.next +To create UK post code + +```js +FakerTransformer({ + columns: [ + { + name: "column2", + faker: "location.zipCode", + }, + ], + locale: "en_GB", +}); +``` + ### Example src/examples/csv-faker.js @@ -46,20 +59,18 @@ src/examples/csv-faker.js ### Options -| option | description | -| -------------- | ------------------------------------------------------------- | -| column | source column to be transformed | +| option | description | +| ------ | ------------------------------- | +| column | source column to be transformed | ### Usage ```js -ToUpperCaseTransformer( - { - column: "column2" - } -) +ToUpperCaseTransformer({ + column: "column2", +}); ``` ### Example -src/examples/csv-to-upper-case.js \ No newline at end of file +src/examples/csv-to-upper-case.js diff --git a/src/transformers/fakerTransformer.js b/src/transformers/fakerTransformer.js index bfbed3b..fa18822 100644 --- a/src/transformers/fakerTransformer.js +++ b/src/transformers/fakerTransformer.js @@ -1,5 +1,4 @@ const { Transform } = require("node:stream") -const { faker } = require("@faker-js/faker") /** * @@ -12,11 +11,19 @@ const { faker } = require("@faker-js/faker") * } * ] * } + * @param {String} options.locale * @returns StreamReader */ function FakerTransformer(options){ let self = this self.columns = options.columns + let faker; + if(options.locale){ + faker = require(`@faker-js/faker/locale/${options.locale}`).faker + } else { + faker = require('@faker-js/faker').faker + } + function getFaker(fakerType) { return fakerType.split('.').reduce((a,b) => { return Object.keys(a).length === 0 ? faker[b] : a[b] diff --git a/test/transformers/fakerTransformer.test.js b/test/transformers/fakerTransformer.test.js index a2a911f..601b687 100644 --- a/test/transformers/fakerTransformer.test.js +++ b/test/transformers/fakerTransformer.test.js @@ -23,7 +23,38 @@ describe('fakerTransformer tests', () => { objectMode: true, transform(chunk,_,callback){ expect(chunk.errors.length).toEqual(0) - expect(chunk[1]).not.toEqual("B") + expect(chunk[1]).not.toEqual("b") + done() + callback(null, chunk) + } + }) + ) + }) + it('should support locales', (done) => { + const uut = FakerTransformer({ + columns: [ + { + name: "column2", + faker: "location.zipCode" + } + ], + locale: 'en_GB' + }) + const testData =["a", "b", "c"] + testData.errors = [] + testData.rowId = 1 + testData._columns = ["column1", "column2", "column3"] + const readable = Readable.from([testData]) + readable + .pipe(uut) + .pipe(new PassThrough({ + objectMode: true, + transform(chunk,_,callback){ + console.log(chunk) + expect(chunk.errors.length).toEqual(0) + expect(chunk[1]).not.toEqual("b") + const regex = /^([A-Za-z]{2}[\d]{1,2}[A-Za-z]?)[\s]+([\d][A-Za-z]{2})$/ + expect(chunk[1].match(regex)[0]).toEqual(chunk[1]) done() callback(null, chunk) }