File tree 2 files changed +19
-10
lines changed
2 files changed +19
-10
lines changed Original file line number Diff line number Diff line change @@ -397,16 +397,21 @@ console.assert(
397
397
398
398
### single
399
399
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.
401
402
402
403
``` 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" ;
405
406
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 );
408
413
409
- assertEquals (match , 6 );
414
+ assertEquals (activeBooking , { month: ' June ' , active: true } );
410
415
```
411
416
412
417
### sortBy
Original file line number Diff line number Diff line change 1
1
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
2
2
3
3
/**
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.
5
5
*
6
6
* Example:
7
7
*
8
8
* ```ts
9
9
* import { single } from "./single.ts";
10
10
* import { assertEquals } from "../testing/asserts.ts";
11
11
*
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);
14
18
*
15
- * assertEquals(match, 6 );
19
+ * assertEquals(activeBooking, { month: 'June', active: true } );
16
20
* ```
17
21
*/
18
22
export function single < T > (
You can’t perform that action at this time.
0 commit comments