Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce way to get return-value of first of many functions th…
…at matches an input-value
- Loading branch information
Showing
7 changed files
with
83 additions
and
0 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
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,3 @@ | ||
export function firstMatchValue<TInput, TOutput>( | ||
...functions: ((input: TInput) => TOutput | undefined)[] | ||
): (input: TInput) => TOutput | undefined; |
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,11 @@ | ||
export const firstMatchValue = | ||
(...functions) => | ||
data => { | ||
for (const f of functions) { | ||
const value = f(data); | ||
|
||
if (value !== undefined) { | ||
return value; | ||
} | ||
} | ||
}; |
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,11 @@ | ||
import { expectType } from 'tsd'; | ||
import { firstMatchValue as firstMatchValueFor } from './firstMatchValue'; | ||
|
||
const firstMatchValue = firstMatchValueFor( | ||
x => 'some-string', | ||
x => 'some-other-string', | ||
); | ||
|
||
const actual = firstMatchValue(true); | ||
|
||
expectType<string | undefined>(actual); |
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,53 @@ | ||
import { firstMatchValue as firstMatchValueFor } from './firstMatchValue'; | ||
import { constant, get } from 'lodash/fp'; | ||
|
||
describe('firstMatchValue', () => { | ||
describe('given array of functions used for finding', () => { | ||
let firstMatchValue; | ||
let finderMock; | ||
|
||
beforeEach(() => { | ||
finderMock = jest.fn(); | ||
|
||
firstMatchValue = firstMatchValueFor( | ||
constant(undefined), | ||
get('someProperty'), | ||
finderMock, | ||
); | ||
}); | ||
|
||
describe('when called with data containing a match', () => { | ||
let actual; | ||
|
||
beforeEach(() => { | ||
actual = firstMatchValue({ someProperty: 'some-value' }); | ||
}); | ||
|
||
it('returns the matched value from the matcher of highest priority', () => { | ||
expect(actual).toBe('some-value'); | ||
}); | ||
|
||
it('does not call matchers of less priority than the one matching', () => { | ||
expect(finderMock).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('when called with data not containing a match', () => { | ||
let actual; | ||
|
||
beforeEach(() => { | ||
actual = firstMatchValue({ someOtherProperty: 'irrelevant' }); | ||
}); | ||
|
||
it('returns undefined', () => { | ||
expect(actual).toBeUndefined(); | ||
}); | ||
|
||
it('calls all matchers', () => { | ||
expect(finderMock).toHaveBeenCalledWith({ | ||
someOtherProperty: 'irrelevant', | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |