Skip to content

Commit e345002

Browse files
author
NotFounds(iori)
committed
update doc
1 parent 8acecfd commit e345002

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

collections/README.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -397,16 +397,21 @@ console.assert(
397397

398398
### single
399399

400-
Returns the value if the given array matching the given predicate exactly once.
400+
Returns the only element in the given collection matching the given predicate.
401+
Returns undefined if there is none.
401402

402403
```ts
403-
import { single } from "./single.ts";
404-
import { assertEquals } from "../testing/asserts.ts";
404+
import { single } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";
405+
import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
405406

406-
const numbers = [ 4, 2, 7, 6 ];
407-
const match = single(numbers, it => it % 2 === 1)
407+
const bookings = [
408+
{ month: 'January', active: false },
409+
{ month: 'March', active: false },
410+
{ month: 'June', active: true },
411+
];
412+
const activeBooking = single(bookings, (it) => it.active);
408413

409-
assertEquals(match, 6);
414+
assertEquals(activeBooking, { month: 'June', active: true });
410415
```
411416

412417
### sortBy

collections/single.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
22

33
/**
4-
* Returns the value if the given array matching the given predicate exactly once
4+
* Returns the only element in the given collection matching the given predicate. Returns undefined if there is none.
55
*
66
* Example:
77
*
88
* ```ts
99
* import { single } from "./single.ts";
1010
* import { assertEquals } from "../testing/asserts.ts";
1111
*
12-
* const numbers = [ 4, 2, 7, 6 ];
13-
* const match = single(numbers, it => it % 2 === 1);
12+
* const bookings = [
13+
* { month: 'January', active: false },
14+
* { month: 'March', active: false },
15+
* { month: 'June', active: true },
16+
* ];
17+
* const activeBooking = single(bookings, (it) => it.active);
1418
*
15-
* assertEquals(match, 6);
19+
* assertEquals(activeBooking, { month: 'June', active: true });
1620
* ```
1721
*/
1822
export function single<T>(

0 commit comments

Comments
 (0)