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

Feature/73 default json mapper response #201

Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 30 additions & 6 deletions packages/xlsx-import/src/mappers/jsonMapper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
type JsonMapper = <TJsonResult>(value: string) => TJsonResult | null;

export const jsonMapper: JsonMapper = <TJsonResult extends any>(value: string): TJsonResult | null => {
try {
return JSON.parse(value);
} catch (e) {
return null;
type callSignature = <TJsonResult, TDefaultResult>(value: string) => TJsonResult | TDefaultResult;

type JsonMapper = {
default: <TDefaultResult>(defaultResult: TDefaultResult) => JsonMapper
} & callSignature;

interface IJsonMapperOptions {
default: any;
}

const factory = (options: Readonly<IJsonMapperOptions>): JsonMapper => {
const mapper = (json: string) => {
try {
return JSON.parse(json);
} catch (e) {
return options.default
}
}

mapper.default = <TDefaultResult>(defaultResult: TDefaultResult) => factory({
...options,
default: defaultResult
});

return mapper;
};



export const jsonMapper: JsonMapper = factory({
default: null,
});
32 changes: 32 additions & 0 deletions packages/xlsx-import/tests/unit/mappers/jsonMapper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as chai from 'chai';
import { jsonMapper } from '../../../src/mappers/jsonMapper';

describe('UNIT TEST: src/mappers/', () => {
describe('jsonMapper', () => {
const dataProvider = [
{ inValue: '', expectedResult: null },
{ inValue: null, expectedResult: null },
{ inValue: ' ', expectedResult: null },
{ inValue: 'asd', expectedResult: null },
{ inValue: 'null', expectedResult: null },
{ inValue: 'false', expectedResult: false },
{ inValue: 'true', expectedResult: true },
{ inValue: '0', expectedResult: 0 },
{ inValue: '1', expectedResult: 1 },
{ inValue: '"string"', expectedResult: 'string' },
{ inValue: "'string'", expectedResult: null },
{ inValue: '{"a":1}', expectedResult: { a: 1 } },
{ inValue: '{"a":[1]}', expectedResult: { a: [1] } },
];
dataProvider.forEach(({ inValue, expectedResult }) => {
it(`jsonMapper for input "${inValue}" SHOULD return "${JSON.stringify(expectedResult)}"`, () => {
chai.expect(jsonMapper(inValue as string)).is.eql(expectedResult);
});
});

it('Should return default value on error', () => {
const mapper = jsonMapper.default({a: 1});
chai.expect(mapper('invalid')).eql({a: 1});
})
});
});
26 changes: 1 addition & 25 deletions packages/xlsx-import/tests/unit/mappers/stringMapper.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chai from 'chai';
import { isEmpty, isFilled, jsonMapper, lowerCaseMapper, upperCaseMapper } from '../../../src/mappers';
import { isEmpty, isFilled, lowerCaseMapper, upperCaseMapper } from '../../../src/mappers';
import { stringMapper } from '../../../src/mappers/stringMapper';

describe('UNIT TEST: src/mappers/', () => {
Expand Down Expand Up @@ -62,30 +62,6 @@ describe('UNIT TEST: src/mappers/', () => {
});
});

// todo move to jsonMapper.test.ts
describe('jsonMapper', () => {
const dataProvider = [
{ inValue: '', expectedResult: null },
{ inValue: null, expectedResult: null },
{ inValue: ' ', expectedResult: null },
{ inValue: 'asd', expectedResult: null },
{ inValue: 'null', expectedResult: null },
{ inValue: 'false', expectedResult: false },
{ inValue: 'true', expectedResult: true },
{ inValue: '0', expectedResult: 0 },
{ inValue: '1', expectedResult: 1 },
{ inValue: '"string"', expectedResult: 'string' },
{ inValue: "'string'", expectedResult: null },
{ inValue: '{"a":1}', expectedResult: { a: 1 } },
{ inValue: '{"a":[1]}', expectedResult: { a: [1] } },
];
dataProvider.forEach(({ inValue, expectedResult }) => {
it(`jsonMapper for input "${inValue}" SHOULD return "${JSON.stringify(expectedResult)}"`, () => {
chai.expect(jsonMapper(inValue as string)).is.eql(expectedResult);
});
});
});

// todo move to isEmpty.test.ts
describe('isEmpty', () => {
const dataProvider = [
Expand Down