-
Notifications
You must be signed in to change notification settings - Fork 634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(collection): add findSingle #1166
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
820e27b
feat(collections): add single
NotFounds 0f5af03
chore: exec fmt
NotFounds 7472844
docs(collections): add example for single
NotFounds 6c74b61
docs(collections): add description for single
NotFounds 1fa2dbb
docs(collections): fix description for single
NotFounds 95dc34e
Merge branch 'main' into add-collection-single
kt3k 8864057
fix(collections): enable to work short circuit for undefined values
NotFounds a7a2e3a
Merge branch 'main' into add-collection-single
kt3k 64e4cac
chore: rename single -> findSingle
kt3k 4f9c030
chore: fmt
kt3k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,39 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
|
||
/** | ||
* Returns the only element in the given collection matching the given predicate. Returns undefined if there is none or multiple matches for the predicate. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* import { single } from "./single.ts"; | ||
* import { assertEquals } from "../testing/asserts.ts"; | ||
* | ||
* const bookings = [ | ||
* { month: 'January', active: false }, | ||
* { month: 'March', active: false }, | ||
* { month: 'June', active: true }, | ||
* ]; | ||
* const activeBooking = single(bookings, (it) => it.active); | ||
* const inactiveBooking = single(bookings, (it) => !it.active); | ||
* | ||
* assertEquals(activeBooking, { month: "June", active: true }); | ||
* assertEquals(inactiveBooking, undefined); // there are two applicable items | ||
* ``` | ||
*/ | ||
export function single<T>( | ||
array: readonly T[], | ||
predicate: (el: T) => boolean = (_) => true, | ||
): T | undefined { | ||
let match: T | undefined = undefined; | ||
for (const element of array) { | ||
if (predicate(element)) { | ||
if (match !== undefined) { | ||
return undefined; | ||
} | ||
match = element; | ||
} | ||
} | ||
|
||
return match; | ||
} |
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,136 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals } from "../testing/asserts.ts"; | ||
import { single } from "./single.ts"; | ||
|
||
function singleTest<I>( | ||
input: [Array<I>, (element: I) => boolean], | ||
expected: I | undefined, | ||
message?: string, | ||
) { | ||
const actual = single(...input); | ||
assertEquals(actual, expected, message); | ||
} | ||
|
||
function singleDefaultPredicatorTest<I>( | ||
input: Array<I>, | ||
expected: I | undefined, | ||
message?: string, | ||
) { | ||
const actual = single(input); | ||
assertEquals(actual, expected, message); | ||
} | ||
|
||
Deno.test({ | ||
name: "[collections/single] no mutation", | ||
fn() { | ||
const array = [1, 2, 3]; | ||
single(array, (it) => it % 2 === 0); | ||
|
||
assertEquals(array, [1, 2, 3]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/single] empty input", | ||
fn() { | ||
singleTest( | ||
[[], (_) => true], | ||
undefined, | ||
); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/single] only one element", | ||
fn() { | ||
singleTest( | ||
[[42], (_it) => true], | ||
42, | ||
); | ||
singleTest( | ||
[["foo"], (_it) => true], | ||
"foo", | ||
); | ||
singleTest( | ||
[[null], (_it) => true], | ||
null, | ||
); | ||
singleTest( | ||
[[undefined], (_it) => true], | ||
undefined, | ||
); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/single] no matches", | ||
fn() { | ||
singleTest( | ||
[[9, 11, 13], (it) => it % 2 === 0], | ||
undefined, | ||
); | ||
singleTest( | ||
[["foo", "bar"], (it) => it.startsWith("z")], | ||
undefined, | ||
); | ||
singleTest( | ||
[[{ done: false }], (it) => it.done], | ||
undefined, | ||
); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/single] only match", | ||
fn() { | ||
singleTest( | ||
[[9, 12, 13], (it) => it % 2 === 0], | ||
12, | ||
); | ||
singleTest( | ||
[["zap", "foo", "bar"], (it) => it.startsWith("z")], | ||
"zap", | ||
); | ||
singleTest( | ||
[[{ done: false }, { done: true }], (it) => it.done], | ||
{ done: true }, | ||
); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/single] multiple matches", | ||
fn() { | ||
singleTest( | ||
[[9, 12, 13, 14], (it) => it % 2 === 0], | ||
undefined, | ||
); | ||
singleTest( | ||
[["zap", "foo", "bar", "zee"], (it) => it.startsWith("z")], | ||
undefined, | ||
); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "[collections/single] default predicator", | ||
fn() { | ||
singleDefaultPredicatorTest( | ||
[42], | ||
42, | ||
); | ||
singleDefaultPredicatorTest( | ||
["foo"], | ||
"foo", | ||
); | ||
singleDefaultPredicatorTest( | ||
[9, 11, 13], | ||
undefined, | ||
); | ||
singleDefaultPredicatorTest( | ||
["foo", "bar"], | ||
undefined, | ||
); | ||
}, | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alternative:
Returns the element if and only if a single matching element exists to the given condition; otherwise, it returns undefined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"to the given condition" does not make sense to me (not a native speaker though). Maybe slightly modify to
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for comment! Fixed it! 1fa2dbb