-
-
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.
Issue #24: Add splitMapper, Add splitMapper customizable separator, …
…Add splitMapper itemMapper, Add tests+documentaion+samples
- Loading branch information
Showing
6 changed files
with
208 additions
and
14 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
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
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,11 +1,9 @@ | ||
import { ValueMapper } from '../abstracts/ValueMapper'; | ||
|
||
type JsonMapper = <TJsonResult>(value: string) => TJsonResult | null; | ||
|
||
export const jsonMapper: JsonMapper = <TJsonResult extends any>(value: string): TJsonResult | null => { | ||
try { | ||
return JSON.parse(value) | ||
return JSON.parse(value); | ||
} catch (e) { | ||
return null | ||
return null; | ||
} | ||
} | ||
}; |
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,55 @@ | ||
/** | ||
* @description Configurable, immutable **splitMapper** with possibility to use specific `itemMapper` or `separator`. | ||
* @see https://github.com/Siemienik/xlsx-import/issues/24 | ||
* @see https://github.com/Siemienik/xlsx-import#mappers | ||
* @example standalone usaffe | ||
* ```js | ||
* // import {splitMapper} from ...; | ||
* splitMapper('a,b,c,,d,e'); // ["a", "b", "c", "", "d", "e"] | ||
* splitMapper.itemMapper<boolean>(isFilled)('a,b,c,,d,e'); // [true, true, true, false, true, true] | ||
* splitMapper.itemMapper<boolean>(isFilled).separator('.')('a,b,,c,d'); // [true] | ||
* | ||
* // or: | ||
* const dotMapper = splitMapper.itemMapper<string>(upperCaseMapper).separator('.'); | ||
* dotMapper('a,b,,c,d'); // ["A,B,,C,D"] | ||
* dotMapper('a,b,.,c,d'); // ["A,B,", ",C,D"] | ||
* dotMapper('a.b.c.d'); // ["A", "B", "C", "D"] | ||
* ``` | ||
* | ||
* @example example cfg | ||
* ```js | ||
* { | ||
* key: "interests", | ||
* index:1, | ||
* // map value from: `"Cycling | SKIING | HikiNg"` to: `["cycling", "skiing", "hiking"]` | ||
* mapper: splitMapper.separator(" | ").itemMapper(lowerCaseMapper) | ||
* } | ||
* ``` | ||
*/ | ||
|
||
import { ValueMapper } from '../abstracts/ValueMapper'; | ||
import { stringMapper } from './stringMapper'; | ||
|
||
export type SplitMapper<TItem> = ValueMapper<TItem[]> & { | ||
separator: (separator: string) => SplitMapper<TItem>; | ||
itemMapper: <TMapper>(itemMapper: ValueMapper<TMapper>) => SplitMapper<TMapper>; | ||
}; | ||
|
||
interface ISplitMapperOptions<TItem> { | ||
separator: string; | ||
itemMapper: ValueMapper<TItem>; | ||
} | ||
|
||
const factory = <TItem>(options: Readonly<ISplitMapperOptions<TItem>>): SplitMapper<TItem> => { | ||
const mapper: SplitMapper<TItem> = (value: string) => value.split(options.separator).map(options.itemMapper); | ||
|
||
mapper.separator = separator => factory({ ...options, separator }); | ||
mapper.itemMapper = <TMapper>(itemMapper: ValueMapper<TMapper>) => factory<TMapper>({ ...options, itemMapper }); | ||
|
||
return mapper; | ||
}; | ||
|
||
export const splitMapper: SplitMapper<string> = factory<string>({ | ||
itemMapper: stringMapper, | ||
separator: ',', | ||
}); |
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,96 @@ | ||
import * as chai from 'chai'; | ||
import { isEmpty, lowerCaseMapper, upperCaseMapper } from '../../../src/mappers'; | ||
import { splitMapper } from '../../../src/mappers/splitMapper'; | ||
|
||
describe('UNIT TEST: src/mappers/', () => { | ||
describe('splitMapper default', () => { | ||
const dataProvider = [ | ||
// mappers are designed for string input only, like this: | ||
{inValue: '', expectedResult: ['']}, | ||
{inValue: '0', expectedResult: ['0']}, | ||
{inValue: ',', expectedResult: ['', '']}, | ||
{inValue: '0,0', expectedResult: ['0', '0']}, | ||
{inValue: 'asd,dsa', expectedResult: ['asd','dsa']}, | ||
{inValue: ',asd,dsa', expectedResult: ['','asd','dsa']}, | ||
{inValue: ',asd,dsa,', expectedResult: ['','asd','dsa','']}, | ||
{inValue: ',asd;dsa,', expectedResult: ['','asd;dsa','']}, | ||
{inValue: '[1,2,3]', expectedResult: ['[1','2','3]']}, | ||
|
||
// invalid inputs won't to check | ||
// {inValue: {}, expectedResult: ... }, | ||
]; | ||
|
||
dataProvider.forEach(({inValue, expectedResult}) => { | ||
it(`the default splitMapper for input "${inValue}" SHOULD return "${JSON.stringify(expectedResult)}"`, () => { | ||
chai.expect(splitMapper(inValue)).eql(expectedResult); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('splitMapper custom separator', () => { | ||
const dataProvider = [ | ||
// mappers are designed for string input only, like this: | ||
{inValue: '', inSeparator: ';', expectedResult: ['']}, | ||
{inValue: '0', inSeparator: ';', expectedResult: ['0']}, | ||
{inValue: ';', inSeparator: ';', expectedResult: ['', '']}, | ||
{inValue: '0;0', inSeparator: ';', expectedResult: ['0', '0']}, | ||
{inValue: 'asd;dsa', inSeparator: ';', expectedResult: ['asd','dsa']}, | ||
{inValue: ';asd;dsa', inSeparator: ';', expectedResult: ['','asd','dsa']}, | ||
{inValue: ';asd;dsa;', inSeparator: ';', expectedResult: ['','asd','dsa','']}, | ||
{inValue: ';asd.dsa;', inSeparator: ';', expectedResult: ['','asd.dsa','']}, | ||
{inValue: ',', inSeparator: ';', expectedResult: [',']}, | ||
{inValue: '0,0', inSeparator: ';', expectedResult: ['0,0']}, | ||
{inValue: 'asd,dsa', inSeparator: ';', expectedResult: ['asd,dsa']}, | ||
{inValue: ',asd,dsa', inSeparator: ';', expectedResult: [',asd,dsa']}, | ||
{inValue: ',asd,dsa,', inSeparator: ';', expectedResult: [',asd,dsa,']}, | ||
{inValue: ',asd;dsa,', inSeparator: ';', expectedResult: [',asd','dsa,']}, | ||
{inValue: '[1,2,3]', inSeparator: ';', expectedResult: ['[1,2,3]']}, | ||
{inValue: 'Ala ma kota, a kot ma Ale', inSeparator: ', a ', expectedResult: ['Ala ma kota', 'kot ma Ale']}, | ||
{inValue: 'bbbBBBbbbBBBaaa', inSeparator: 'BBB', expectedResult: ['bbb', 'bbb', 'aaa']}, | ||
|
||
// invalid inputs won't to check | ||
// {inValue: {}, expectedResult: ... }, | ||
]; | ||
dataProvider.forEach(({inValue,inSeparator, expectedResult}) => { | ||
it(`splitMapper with separator ${inSeparator} for input "${inValue}" SHOULD return "${JSON.stringify(expectedResult)}"`, () => { | ||
const mapper = splitMapper.separator(inSeparator); | ||
chai.expect(mapper(inValue)).eql(expectedResult); | ||
}); | ||
}); | ||
}); | ||
describe('splitMapper custom mapper', () => { | ||
const dataProvider = [ | ||
{inValue: 'Ala,ma,kota,,Kot,ma,Ale', inMapper:upperCaseMapper, expectedResult: ['ALA','MA','KOTA','','KOT','MA','ALE']}, | ||
{inValue: 'Ala,ma,kota,,Kot,ma,Ale', inMapper:lowerCaseMapper, expectedResult: ['ala','ma','kota','','kot','ma','ale']}, | ||
{inValue: 'Ala,ma,kota,,Kot,ma,Ale', inMapper:isEmpty, expectedResult: [false,false,false,true,false,false,false]}, | ||
{inValue: 'Ala,ma,kota,,Kot,ma,Ale', inMapper:(v:string) => v.length, expectedResult: [3,2,4,0,3,2,3]}, | ||
]; | ||
dataProvider.forEach(({inValue,inMapper, expectedResult}) => { | ||
it(`splitMapper with separator ${inMapper.constructor.name} for input "${inValue}" SHOULD return "${JSON.stringify(expectedResult)}"`, () => { | ||
const mapper = splitMapper.itemMapper<unknown>(inMapper); | ||
chai.expect(mapper(inValue)).eql(expectedResult); | ||
}); | ||
}); | ||
}); | ||
describe('splitMapper complex / advanced', () => { | ||
it(`Map sentence into matrix word in sentence`, () => { | ||
// assumptions | ||
const input = 'Lorem ipsum dolor sit amet. consectetur adipiscing elit. Nullam placerat massa nec efficir. '; | ||
const expectedResult = [ | ||
['Lorem', 'ipsum', 'dolor', 'sit', 'amet'], | ||
['consectetur', 'adipiscing', 'elit'], | ||
['Nullam', 'placerat', 'massa', 'nec', 'efficir'], | ||
[''] | ||
] | ||
|
||
// building a mapper | ||
const sentenceSplitter = splitMapper.separator('. '); | ||
const wordSplitter = splitMapper.separator(' '); | ||
const wordsInSentencesMapper = sentenceSplitter.itemMapper(wordSplitter); | ||
|
||
// testing | ||
chai.expect(wordsInSentencesMapper(input)).eql(expectedResult); | ||
|
||
}); | ||
}); | ||
}); |
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