From 29d9541006e6cca63a2472b00b139c2bcd669e46 Mon Sep 17 00:00:00 2001 From: Ryuichi Inagaki Date: Sat, 18 Jan 2020 19:41:13 +1100 Subject: [PATCH] [#34] Added a test for ObjectStringifier as well --- README.md | 10 +++++++++- src/test/csv-stringifiers/object.test.ts | 20 +++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f591824..2f62977 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,10 @@ console.log(csvStringifier.stringifyRecords(records)); Default: `\n`. Only either LF (`\n`) or CRLF (`\r\n`) is allowed. + * keyDelimiter `` (optional) + + Default: `undefined`. Give this value to specify a path to a value in a nested object. + * alwaysQuote `` (optional) Default: `false`. Set it to `true` to double-quote all fields regardless of their values. @@ -233,7 +237,11 @@ console.log(csvStringifier.stringifyRecords(records)); * recordDelimiter `` (optional) Default: `\n`. Only either LF (`\n`) or CRLF (`\r\n`) is allowed. - + + * keyDelimiter `` (optional) + + Default: `undefined`. Give this value to specify a path to a value in a nested object. + * alwaysQuote `` (optional) Default: `false`. Set it to `true` to double-quote all fields regardless of their values. diff --git a/src/test/csv-stringifiers/object.test.ts b/src/test/csv-stringifiers/object.test.ts index 59c2bc8..f0669ea 100644 --- a/src/test/csv-stringifiers/object.test.ts +++ b/src/test/csv-stringifiers/object.test.ts @@ -5,7 +5,7 @@ import {strictEqual, throws} from 'assert'; describe('ObjectCsvStringifier', () => { const records = [ {FIELD_A: 'VALUE_A1', FIELD_B: 'VALUE_B1'}, - {FIELD_A: 'VALUE_A2', FIELD_B: 'VALUE_B2'} + {FIELD_A: 'VALUE_A2', FIELD_B: 'VALUE_B2', OTHERS: {FIELD_C: 'VALUE_C2'}} ]; describe('When field delimiter is comma', generateTestCases()); @@ -69,6 +69,24 @@ describe('ObjectCsvStringifier', () => { }); }); + describe('When `keyDelimiter` is set', () => { + const stringifier = createObjectCsvStringifier({ + header: [ + {id: 'FIELD_A', title: 'TITLE_A'}, + {id: 'OTHERS/FIELD_C', title: 'TITLE_C'} + ], + keyDelimiter: '/' + }); + + it('uses the title as is', () => { + strictEqual(stringifier.getHeaderString(), 'TITLE_A,TITLE_C\n'); + }); + + it('picks up a value in nested objects', () => { + strictEqual(stringifier.stringifyRecords(records), 'VALUE_A1,\nVALUE_A2,VALUE_C2\n'); + }); + }); + function generateTestCases(fieldDelimiter?: string) { const delim = resolveDelimiterChar(fieldDelimiter); return () => {