-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/73 default json mapper response (#201)
* feat: added customizable default value for jsonMapper * reafactor: better typings and format
- Loading branch information
1 parent
faebc52
commit 721e696
Showing
3 changed files
with
63 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,31 @@ | ||
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 JsonMapperSignature<TDefaultResult> = <TJsonResult>(value: string) => TJsonResult | TDefaultResult; | ||
|
||
export type JsonMapper<TJsonResult> = JsonMapperSignature<TJsonResult> & { | ||
default: <TDefaultResult>(value: TDefaultResult) => JsonMapper<TDefaultResult>; | ||
}; | ||
|
||
interface IJsonMapperOptions<TDefaultResult> { | ||
default: TDefaultResult; | ||
} | ||
|
||
const factory = <TDefaultResult>(options: Readonly<IJsonMapperOptions<TDefaultResult>>): JsonMapper<TDefaultResult> => { | ||
const mapper: JsonMapper<TDefaultResult> = <TJsonResult>(json: string): TJsonResult | TDefaultResult => { | ||
try { | ||
return JSON.parse(json); | ||
} catch (e) { | ||
return options.default; | ||
} | ||
}; | ||
|
||
mapper.default = <TJSonResult>(defaultResult: TJSonResult) => | ||
factory<TJSonResult>({ | ||
...options, | ||
default: defaultResult, | ||
}); | ||
|
||
return mapper; | ||
}; | ||
|
||
export const jsonMapper = factory({ | ||
default: null, | ||
}); |
32 changes: 32 additions & 0 deletions
32
packages/xlsx-import/tests/unit/mappers/jsonMapper.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters