-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow to use pre-condition and condition on auto mapping pairs
- Loading branch information
Showing
4 changed files
with
98 additions
and
34 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { MapperConfiguration, MappingPair } from '../src'; | ||
|
||
describe('Auto mapping - map using', () => { | ||
|
||
class SourceValue { | ||
code: number; | ||
} | ||
|
||
class Source { | ||
value: SourceValue[]; | ||
} | ||
|
||
class DestinationValue { | ||
code: string; | ||
} | ||
|
||
class Destination { | ||
value: DestinationValue[]; | ||
} | ||
|
||
const SourceToDestination = new MappingPair(Source, Destination); | ||
const ValueMap = new MappingPair<SourceValue, DestinationValue>(); | ||
|
||
const mapper = new MapperConfiguration(cfg => { | ||
cfg.createAutoMap(SourceToDestination, { | ||
value: opt => opt.mapFromUsing(src => src.value, ValueMap) | ||
}); | ||
|
||
cfg.createMap(ValueMap, { | ||
code: opt => opt.mapFrom(x => x.code.toString()) | ||
}); | ||
}).createMapper(); | ||
|
||
it('should correctly handle auto map with nested pair', () => { | ||
const source = new Source(); | ||
source.value = [{ code: 123 }]; | ||
|
||
const destination = mapper.map(SourceToDestination, source); | ||
|
||
expect(destination.value).toEqual([{ code: '123' }]); | ||
}); | ||
}); |
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,39 @@ | ||
import { MapperConfiguration, MappingPair } from '../../src'; | ||
|
||
describe('Auto map with pre condition', () => { | ||
interface Source { | ||
id: number; | ||
} | ||
|
||
interface Destination { | ||
id: number; | ||
} | ||
|
||
const SourceToDestination = new MappingPair<Source, Destination>(); | ||
|
||
const mapper = new MapperConfiguration(cfg => { | ||
cfg.createAutoMap(SourceToDestination, { | ||
id: opt => opt.preCondition(src => src.id > 2) | ||
}); | ||
}).createMapper(); | ||
|
||
it('should not map property when precondition is not met', () => { | ||
const source: Source = { | ||
id: 1 | ||
}; | ||
|
||
const destination = mapper.map(SourceToDestination, source); | ||
expect(destination).toEqual({}); | ||
}); | ||
|
||
it('should map when precondition is met', () => { | ||
const source: Source = { | ||
id: 3 | ||
}; | ||
|
||
const destination = mapper.map(SourceToDestination, source); | ||
expect(destination).toEqual({ | ||
id: 3 | ||
}); | ||
}); | ||
}); |