Skip to content

Commit 9969b70

Browse files
authored
Merge pull request #43 from Gontrum/40-add-jsonMapper-into-mappers
#40 Add jsonMapper into mappers
2 parents 68e6110 + 2de79b2 commit 9969b70

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ This is `type` related configuration, for more information please study examples
128128
|-----|-----------
129129
|upperCaseMapper|Transforms string to upper case
130130
|lowerCaseMapper|Transforms string to lower case
131+
|jsonMapper|Transforms a json string to a TJsonResponse or to null if parsing was not possible
131132
|isEmpty|Examines if input is empty
132133
|isFilled|Examines if input is not empty
133134

src/mappers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export { upperCaseMapper } from './upperCaseMapper';
66
export { lowerCaseMapper } from './lowerCaseMapper';
77
export { isEmpty } from './isEmpty';
88
export { isFilled } from './isFilled';
9+
export { jsonMapper } from './jsonMapper';

src/mappers/jsonMapper.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ValueMapper } from '../abstracts/ValueMapper';
2+
3+
type JsonMapper = <TJsonResult>(value: string) => TJsonResult | null;
4+
5+
export const jsonMapper: JsonMapper = <TJsonResult extends any>(value: string): TJsonResult | null => {
6+
try {
7+
return JSON.parse(value)
8+
} catch (e) {
9+
return null
10+
}
11+
}

tests/unit/mappers/stringMapper.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import * as chai from 'chai';
2-
import { isEmpty, isFilled, lowerCaseMapper, upperCaseMapper } from '../../../src/mappers';
2+
import {
3+
isEmpty,
4+
isFilled,
5+
jsonMapper,
6+
lowerCaseMapper,
7+
upperCaseMapper
8+
} from '../../../src/mappers';
39
import { stringMapper } from '../../../src/mappers/stringMapper';
410

511
describe('Mappers, unit tests', () => {
@@ -60,6 +66,29 @@ describe('Mappers, unit tests', () => {
6066
});
6167
});
6268

69+
describe('jsonMapper', () => {
70+
const dataProvider = [
71+
{ inValue: '', expectedResult: null },
72+
{ inValue: null, expectedResult: null },
73+
{ inValue: ' ', expectedResult: null },
74+
{ inValue: 'asd', expectedResult: null },
75+
{ inValue: 'null', expectedResult: null },
76+
{ inValue: 'false', expectedResult: false },
77+
{ inValue: 'true', expectedResult: true },
78+
{ inValue: '0', expectedResult: 0},
79+
{ inValue: '1', expectedResult: 1},
80+
{ inValue: '"string"', expectedResult: 'string'},
81+
{ inValue: '\'string\'', expectedResult: null},
82+
{ inValue: '{"a":1}', expectedResult: {a:1}},
83+
{ inValue: '{"a":[1]}', expectedResult: {a:[1]}},
84+
]
85+
dataProvider.forEach(({inValue, expectedResult}) => {
86+
it(`jsonMapper for input "${inValue}" SHOULD return "${JSON.stringify(expectedResult)}"`, () => {
87+
chai.expect(jsonMapper(inValue as string)).is.eql(expectedResult);
88+
});
89+
});
90+
});
91+
6392
describe('isEmpty', () => {
6493
const dataProvider = [
6594
{ inValue: '', expectedResult: true },

0 commit comments

Comments
 (0)