-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a26713
commit 8b9f9b2
Showing
8 changed files
with
134 additions
and
4 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,38 @@ | ||
--- | ||
title: selectFirst | ||
group: 'Array' | ||
description: Array find + map | ||
--- | ||
|
||
## Basic usage | ||
|
||
Returns the mapped value for the first element that satisfies the specified condition--else undefined. | ||
If the filter is omitted, returns the first non-nullish mapped value. | ||
|
||
```ts | ||
import * as _ from 'radashi' | ||
|
||
const fish = [ | ||
{ | ||
name: 'Marlin', | ||
weight: 105, | ||
source: 'ocean', | ||
}, | ||
{ | ||
name: 'Bass', | ||
weight: 8, | ||
source: 'lake', | ||
}, | ||
{ | ||
name: 'Trout', | ||
weight: 13, | ||
source: 'lake', | ||
}, | ||
] | ||
|
||
_.selectFirst( | ||
fish, | ||
f => f.weight, | ||
f => f.source === 'lake', | ||
) // => 8 | ||
``` |
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,29 @@ | ||
/** | ||
* Select performs a find + map operation, short-circuiting on the first | ||
* element that satisfies the prescribed condition. If condition is omitted, | ||
* will select the first mapped value which is non-nullish. | ||
* | ||
* ```ts | ||
* selectFirst( | ||
* [1, 2, 3, 4], | ||
* x => x * x, | ||
* x => x > 2 | ||
* ) | ||
* // => 9 | ||
* ``` | ||
*/ | ||
export function selectFirst<T, U>( | ||
array: readonly T[], | ||
mapper: (item: T, index: number) => U, | ||
condition?: (item: T, index: number) => boolean, | ||
): U | undefined { | ||
if (!array) { | ||
return undefined | ||
} | ||
let foundIndex = -1 | ||
const found = array.find((item, index) => { | ||
foundIndex = index | ||
return condition ? condition(item, index) : mapper(item, index) != null | ||
}) | ||
return found === undefined ? undefined : mapper(found, foundIndex) | ||
} |
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,60 @@ | ||
import * as _ from 'radashi' | ||
|
||
const cast = <T = any[]>(value: any): T => value | ||
|
||
describe('selectFirst function', () => { | ||
test('does not fail on bad input', () => { | ||
expect( | ||
_.selectFirst( | ||
cast(null), | ||
x => x, | ||
x => x, | ||
), | ||
).toBeUndefined() | ||
expect( | ||
_.selectFirst( | ||
cast(undefined), | ||
x => x, | ||
x => x, | ||
), | ||
).toBeUndefined() | ||
}) | ||
test('returns mapped result of first value that meets the condition', () => { | ||
const list = [ | ||
{ group: 'a', word: 'hello' }, | ||
{ group: 'b', word: 'bye' }, | ||
{ group: 'a', word: 'oh' }, | ||
{ group: 'b', word: 'hey' }, | ||
{ group: 'c', word: 'ok' }, | ||
] | ||
const result = _.selectFirst( | ||
list, | ||
x => x.word, | ||
x => x.group === 'b', | ||
) | ||
expect(result).toEqual('bye') | ||
}) | ||
test('does not fail on empty input list', () => { | ||
const list: any[] = [] | ||
const result = _.selectFirst( | ||
list, | ||
(x: any) => x.word, | ||
x => x.group === 'a', | ||
) | ||
expect(result).toBeUndefined() | ||
}) | ||
test('works with index', () => { | ||
const letters = ['a', 'b', 'c', 'd'] | ||
const result = _.selectFirst( | ||
letters, | ||
(l, idx) => `${l}${idx}`, | ||
(_, idx) => idx > 1, | ||
) | ||
expect(result).toEqual('c2') | ||
}) | ||
test('works without a condition callback, filtering nullish mapped values', () => { | ||
const list = [{ a: null }, { a: undefined }, { b: 2 }, { a: 1 }, { a: 3 }] | ||
const result = _.selectFirst(list, el => el.a) | ||
expect(result).toEqual(1) | ||
}) | ||
}) |
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