Skip to content
Merged
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ npm i xlsx-import --save
Mapper is a function that transforms values. You can use [built-in mappers](#Mappers) or write your own

```ts
import { upperCaseMapper } from 'xlsx-import/lib/mappers';
import { upperCaseMapper, isEmpty } from 'xlsx-import/lib/mappers';

const config = {
// ...
Expand All @@ -59,6 +59,7 @@ Mapper is a function that transforms values. You can use [built-in mappers](#Map
fields:[
{row: 2, col:1, key:'FirstName'},
{row: 2, col:2, key:'SecondName', mapper: upperCaseMapper},
{row: 2, col:3, key:'ArtistName', mapper: isEmpty},
{row: 3, col:1, key:'Age', mapper: Number.parseInt},
]
},
Expand Down Expand Up @@ -123,6 +124,7 @@ This is `type` related configuration, for more information please study examples
| Exported Name | Description
|-----|-----------
|upperCaseMapper|Transforms string to upper case
|isEmpty|Examines if input is empty

# See also

Expand Down
1 change: 1 addition & 0 deletions src/mappers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ import { stringMapper } from './stringMapper';
/** probably `MAPPER_DEFAULT` should never be changed. Designed for internal usages when mapper isn't precised. */
export const MAPPER_DEFAULT = stringMapper;
export { upperCaseMapper } from './upperCaseMapper';
export { isEmpty } from './isEmpty';
3 changes: 3 additions & 0 deletions src/mappers/isEmpty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ValueMapper } from '../abstracts/ValueMapper';

export const isEmpty: ValueMapper<boolean> = value => value == null || value === '';
18 changes: 17 additions & 1 deletion 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 { upperCaseMapper } from '../../../src/mappers';
import { upperCaseMapper, isEmpty } from '../../../src/mappers';
import { stringMapper } from '../../../src/mappers/stringMapper';

describe('Mappers, unit tests', () => {
Expand Down Expand Up @@ -44,4 +44,20 @@ describe('Mappers, unit tests', () => {
});
});
});

describe('isEmpty', () => {
const dataProvider = [
{ inValue: '', expectedResult: true },
{ inValue: null, expectedResult: true },
{ inValue: ' ', expectedResult: false }, // it is quite problematic case, however it isn't empty
{ inValue: 'null', expectedResult: false },
{ inValue: 'a', expectedResult: false},
{ inValue: 'as d', expectedResult: false },
]
dataProvider.forEach(({inValue, expectedResult}) => {
it(`isEmpty for input "${inValue}" SHOULD return "${expectedResult}"`, () => {
chai.expect(isEmpty(inValue as string)).equal(expectedResult);
});
});
});
});