8000 Added optional value in first and last #1553 (#1556) · rplansky/immutable-js@37ae5be · GitHub
[go: up one dir, main page]

Skip to content

Commit 37ae5be

Browse files
Yasser Hussainleebyron
authored andcommitted
Added optional value in first and last immutable-js#1553 (immutable-js#1556)
* Added optional value in first and last * Changes:- 1. Added test cases for no optional arg scenario 2. Updated test case descriptions * Added type defs. Updated docs.
1 parent 11588ac commit 37ae5be

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

__tests__/Seq.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@
1010
import { isCollection, isIndexed, Seq } from '../';
1111

1212
describe('Seq', () => {
13+
it('returns undefined if empty and first is called without default argument', () => {
14+
expect(Seq().first()).toBeUndefined();
15+
});
16+
17+
it('returns undefined if empty and last is called without default argument', () => {
18+
expect(Seq().last()).toBeUndefined();
19+
});
20+
21+
it('returns default value if empty and first is called with default argument', () => {
22+
expect(Seq().first({})).toEqual({});
23+
});
24+
25+
it('returns default value if empty and last is called with default argument', () => {
26+
expect(Seq().last({})).toEqual({});
27+
});
28+
1329
it('can be empty', () => {
1430
expect(Seq().size).toBe(0);
1531
});

src/CollectionImpl.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ mixin(Collection, {
371371
.findKey(predicate, context);
372372
},
373373

374-
first() {
375-
return this.find(returnTrue);
374+
first(notSetValue) {
375+
return this.find(returnTrue, null, notSetValue);
376376
},
377377

378378
flatMap(mapper, context) {
@@ -423,10 +423,10 @@ mixin(Collection, {
423423
.toIndexedSeq();
424424
},
425425

426-
last() {
426+
last(notSetValue) {
427427
return this.toSeq()
428428
.reverse()
429-
.first();
429+
.first(notSetValue);
430430
},
431431

432432
lastKeyOf(searchValue) {
@@ -627,8 +627,8 @@ mixin(IndexedCollection, {
627627
return entry ? entry[0] : -1;
628628
},
629629

630-
first() {
631-
return this.get(0);
630+
first(notSetValue) {
631+
return this.get(0, notSetValue);
632632
},
633633

634634
flatten(depth) {
@@ -671,8 +671,8 @@ mixin(IndexedCollection, {
671671
return Range(0, this.size);
672672
},
673673

674-
last() {
675-
return this.get(-1);
674+
last(notSetValue) {
675+
return this.get(-1, notSetValue);
676676
},
677677

678678
skipWhile(predicate, context) {

type-definitions/Immutable.d.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3810,15 +3810,20 @@ declare module Immutable {
38103810
contains(value: V): boolean;
38113811

38123812
/**
3813-
* The first value in the Collection.
3813+
* In case the `Collection` is not empty returns the first element of the
3814+
* `Collection`.
3815+
* In case the `Collection` is empty returns the optional default
3816+
* value if provided, if no default value is provided returns undefined.
38143817
*/
3815-
first(): V | undefined;
3818+
first<NSV>(notSetValue?: NSV): V | NSV;
38163819

38173820
/**
3818-
* The last value in the Collection.
3821+
* In case the `Collection` is not empty returns the last element of the
3822+
* `Collection`.
3823+
* In case the `Collection` is empty returns the optional default
3824+
* value if provided, if no default value is provided returns undefined.
38193825
*/
3820-
last(): V | undefined;
3821-
3826+
last<NSV>(notSetValue?: NSV): V | NSV;
38223827

38233828
// Reading deep values
38243829

type-definitions/immutable.js.flow

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ declare class _Collection<K, +V> /*implements ValueObject*/ {
6363
has(key: K): boolean;
6464
includes(value: V): boolean;
6565
contains(value: V): boolean;
66-
first(): V | void;
67-
last(): V | void;
66+
first<NSV>(notSetValue?: NSV): V | NSV;
67+
last<NSV>(notSetValue?: NSV): V | NSV;
6868

6969
hasIn(keyPath: Iterable<mixed>): boolean;
7070

0 commit comments

Comments
 (0)
0