Skip to content

Commit 092cc4d

Browse files
Added xOrNull methods.
1 parent 138fdb3 commit 092cc4d

File tree

7 files changed

+96
-2
lines changed

7 files changed

+96
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ sequence.first(predicate);
182182
sequence.last(predicate);
183183
sequence.singleOrDefault(defaultValue, predicate);
184184
sequence.firstOrUndefined(predicate);
185+
sequence.lastOrNull(predicate);
185186
```
186187

187188
A resolution is a transform that takes an `Iterable<T>` and returns `TResult`.

dist-esm/LinqExtendedBase.js

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist-esm/LinqExtendedBase.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/LinqExtendedBase.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ export default abstract class LinqExtendedBase<T, TLinq extends LinqExtendedBase
5151
* @return {T | undefined}
5252
*/
5353
singleOrUndefined(predicate?: PredicateWithIndex<T>): T | undefined;
54+
/**
55+
* Returns the expected single element; otherwise undefined.
56+
* @param {PredicateWithIndex<T>} [predicate]
57+
* @return {T | null}
58+
*/
59+
singleOrNull(predicate?: PredicateWithIndex<T>): T | null;
5460
/**
5561
* Returns the first element of the sequence.
5662
* @param {PredicateWithIndex<T>} [predicate]
@@ -70,6 +76,12 @@ export default abstract class LinqExtendedBase<T, TLinq extends LinqExtendedBase
7076
* @return {T | undefined}
7177
*/
7278
firstOrUndefined(predicate?: PredicateWithIndex<T>): T | undefined;
79+
/**
80+
* Returns the first element of the sequence; otherwise undefined.
81+
* @param {PredicateWithIndex<T>} [predicate]
82+
* @return {T | null}
83+
*/
84+
firstOrNull(predicate?: PredicateWithIndex<T>): T | null;
7385
/**
7486
* Returns the last element of the sequence.
7587
* @param {PredicateWithIndex<T>} [predicate]
@@ -89,4 +101,10 @@ export default abstract class LinqExtendedBase<T, TLinq extends LinqExtendedBase
89101
* @return {T | undefined}
90102
*/
91103
lastOrUndefined(predicate?: PredicateWithIndex<T>): T | undefined;
104+
/**
105+
* Returns the last element of the sequence; otherwise null.
106+
* @param {PredicateWithIndex<T>} [predicate]
107+
* @return {T | null}
108+
*/
109+
lastOrNull(predicate?: PredicateWithIndex<T>): T | null;
92110
}

dist/LinqExtendedBase.js

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/LinqExtendedBase.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/LinqExtendedBase.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ export default abstract class LinqExtendedBase<T, TLinq extends LinqExtendedBase
9191
return singleOrDefault<T, undefined>(undefined)(predicate ? this.where(predicate) : this.source);
9292
}
9393

94+
/**
95+
* Returns the expected single element; otherwise undefined.
96+
* @param {PredicateWithIndex<T>} [predicate]
97+
* @return {T | null}
98+
*/
99+
singleOrNull(predicate?: PredicateWithIndex<T>): T | null {
100+
return singleOrDefault<T, null>(null)(predicate ? this.where(predicate) : this.source);
101+
}
102+
94103
/**
95104
* Returns the first element of the sequence.
96105
* @param {PredicateWithIndex<T>} [predicate]
@@ -119,6 +128,15 @@ export default abstract class LinqExtendedBase<T, TLinq extends LinqExtendedBase
119128
return firstOrDefault<T, undefined>(undefined)(predicate ? this.where(predicate) : this.source);
120129
}
121130

131+
/**
132+
* Returns the first element of the sequence; otherwise undefined.
133+
* @param {PredicateWithIndex<T>} [predicate]
134+
* @return {T | null}
135+
*/
136+
firstOrNull(predicate?: PredicateWithIndex<T>): T | null {
137+
return firstOrDefault<T, null>(null)(predicate ? this.where(predicate) : this.source);
138+
}
139+
122140
/**
123141
* Returns the last element of the sequence.
124142
* @param {PredicateWithIndex<T>} [predicate]
@@ -146,4 +164,13 @@ export default abstract class LinqExtendedBase<T, TLinq extends LinqExtendedBase
146164
lastOrUndefined(predicate?: PredicateWithIndex<T>): T | undefined {
147165
return lastOrDefault<T, undefined>(undefined)(predicate ? this.where(predicate) : this.source);
148166
}
167+
168+
/**
169+
* Returns the last element of the sequence; otherwise null.
170+
* @param {PredicateWithIndex<T>} [predicate]
171+
* @return {T | null}
172+
*/
173+
lastOrNull(predicate?: PredicateWithIndex<T>): T | null {
174+
return lastOrDefault<T, null>(null)(predicate ? this.where(predicate) : this.source);
175+
}
149176
}

0 commit comments

Comments
 (0)