Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a change to handle when it receives stringified json. #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/lib/field-stringifier.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {Field} from './record'
import { Field } from './record'

const DEFAULT_FIELD_DELIMITER = ','
const VALID_FIELD_DELIMITERS = [DEFAULT_FIELD_DELIMITER, ';']

export abstract class FieldStringifier {
constructor(public readonly fieldDelimiter: string) {}
constructor(public readonly fieldDelimiter: string) { }

abstract stringify(value?: Field): string

Expand All @@ -20,13 +20,29 @@ export abstract class FieldStringifier {
class DefaultFieldStringifier extends FieldStringifier {
stringify(value?: Field): string {
if (this.isEmpty(value)) return ''

const str = String(value)

if (this.isJSON(str)) return str

return this.needsQuote(str) ? this.quoteField(str) : str
}

private needsQuote(str: string): boolean {
return str.includes(this.fieldDelimiter) || str.includes('\r') || str.includes('\n') || str.includes('"')
}

private isJSON(str: string): boolean {
if (typeof str !== 'string') return false;
try {
const result = JSON.parse(str);
const type = Object.prototype.toString.call(result);
return type === '[object Object]'
|| type === '[object Array]';
} catch (err) {
return false;
}
}
}

class ForceQuoteFieldStringifier extends FieldStringifier {
Expand All @@ -44,4 +60,4 @@ function _validateFieldDelimiter(delimiter: string): void {
if (VALID_FIELD_DELIMITERS.indexOf(delimiter) === -1) {
throw new Error(`Invalid field delimiter \`${delimiter}\` is specified`)
}
}
}
14 changes: 11 additions & 3 deletions src/test/field-stringifier.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {resolveDelimiterChar} from './helper/delimiter'
import {createFieldStringifier} from '../lib/field-stringifier'
import {strictEqual} from 'assert'
import { resolveDelimiterChar } from './helper/delimiter'
import { createFieldStringifier } from '../lib/field-stringifier'
import { strictEqual } from 'assert'

describe('DefaultFieldStringifier', () => {

Expand Down Expand Up @@ -85,6 +85,14 @@ describe('DefaultFieldStringifier', () => {
strictEqual(stringifier.stringify(obj), 'Name: OBJECT_NAME')
})

it('does not change a stringified object', () => {
const obj = {
json: JSON.stringify({ "test": { "type1": "apples", "isObject": true } }),
toString: function () { return this.json }
}
strictEqual(stringifier.stringify(obj), `{"test":{"type1":"apples","isObject":true}}`)
})

it(`wraps a toString-ed field value with double quote if the value contains "${delim}"`, () => {
const obj = {
name: `OBJECT${delim}NAME`,
Expand Down