Skip to content

Commit 392cb39

Browse files
committed
added single and singleordefault
1 parent 9be47b4 commit 392cb39

File tree

9 files changed

+183
-3
lines changed

9 files changed

+183
-3
lines changed

dev/Helper.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,10 @@
169169
new EvaluateCommand("OrderBy", "orderby {x} ascending", "order by {x} ascending", "orderbyascending {x}", "order by ascending {x}", "orderby {x}", "order by {x}"),
170170
new EvaluateCommand("FirstOrDefault", "firstordefault {x}", "first or default {x}", "firstordefault", "first or default"),
171171
new EvaluateCommand("LastOrDefault", "lastordefault {x}", "last or default {x}", "lastordefault", "last or default"),
172+
new EvaluateCommand("SingleOrDefault", "singleordefault {x}", "single or default {x}", "singleordefault", "single or default"),
172173
new EvaluateCommand("First", "first {x}", "first"),
173174
new EvaluateCommand("Last", "last {x}", "last"),
175+
new EvaluateCommand("Single", "single {x}", "single"),
174176
new EvaluateCommand("ThenByDescending", "thenby {x} descending", "then by {x} descending", "thenbydescending {x}", "then by descending {x}"),
175177
new EvaluateCommand("ThenBy", "thenby {x} ascending", "then by {x} ascending", "thenbyascending {x}", "then by ascending {x}", "thenby {x}", "then by {x}")
176178
];

dev/IArray.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,18 @@
135135
*/
136136
FirstOrDefault(filter?: ((item: T) => boolean) | string): (T | null);
137137

138+
/**
139+
* Returns the only item of the array - Throws an exception if not exactly one item is in array
140+
* @param filter If set the function returns the only item that matches the filter
141+
*/
142+
Single(filter?: ((item: T) => boolean) | string): T;
143+
144+
/**
145+
* Returns the only item of the array - Throws an exception if not only one item is in array
146+
* @param filter If set the function returns the only item that matches the filter
147+
*/
148+
SingleOrDefault(filter?: ((item: T) => boolean) | string): (T | null);
149+
138150
/**
139151
* Returns the last item of the array - Throws an exception if no item was found
140152
* @param filter If set the function returns the last item that matches the filter

dev/Modules/Single.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Array.prototype.Single = function<T> (this: T[], filter?: ((item: T) => boolean) | string): (T | null) {
2+
let that: T[] = this;
3+
4+
if (filter != null) {
5+
let result: T[] = that.Where(filter);
6+
7+
if (result.Count() === 1) {
8+
return result.Get(0);
9+
} else {
10+
throw new Error("Linq4JS: The array does not contain exactly one element");
11+
}
12+
} else {
13+
if (that.Count() === 1) {
14+
return that.Get(0);
15+
} else {
16+
throw new Error("Linq4JS: The array does not contain exactly one element");
17+
}
18+
}
19+
};

dev/Modules/SingleOrDefault.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Array.prototype.SingleOrDefault = function<T> (this: T[], filter?: ((item: T) => boolean) | string): (T | null) {
2+
let that: T[] = this;
3+
4+
if (filter != null) {
5+
let result: T[] = that.Where(filter);
6+
7+
if (result.Count() === 1) {
8+
return result.Get(0);
9+
} else {
10+
if (result.Count() > 1) {
11+
throw new Error("Linq4JS: The array contains more than one element");
12+
} else {
13+
return null;
14+
}
15+
}
16+
} else {
17+
if (that.Count() === 1) {
18+
return that.Get(0);
19+
} else {
20+
if (that.Count() > 1) {
21+
throw new Error("Linq4JS: The array contains more than one element");
22+
} else {
23+
return null;
24+
}
25+
}
26+
}
27+
};

dist/linq4js.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ interface Array<T> {
142142
* @param filter If set the function returns the first item that matches the filter
143143
*/
144144
FirstOrDefault(filter?: ((item: T) => boolean) | string): (T | null);
145+
/**
146+
* Returns the only item of the array - Throws an exception if not exactly one item is in array
147+
* @param filter If set the function returns the only item that matches the filter
148+
*/
149+
Single(filter?: ((item: T) => boolean) | string): T;
150+
/**
151+
* Returns the only item of the array - Throws an exception if not only one item is in array
152+
* @param filter If set the function returns the only item that matches the filter
153+
*/
154+
SingleOrDefault(filter?: ((item: T) => boolean) | string): (T | null);
145155
/**
146156
* Returns the last item of the array - Throws an exception if no item was found
147157
* @param filter If set the function returns the last item that matches the filter

dist/linq4js.js

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

dist/linq4js.min.js

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "linq4js",
3-
"version": "2.1.6",
3+
"version": "2.1.7",
44
"description": "Linq methods for JavaScript/TypeScript for working with Arrays",
55
"main": "dist/linq4js.js",
66
"typings": "dist/linq4js.d.ts",

test/all.js

Lines changed: 56 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)