Skip to content

Commit

Permalink
added single and singleordefault
Browse files Browse the repository at this point in the history
  • Loading branch information
morrisjdev committed Apr 29, 2017
1 parent 9be47b4 commit 392cb39
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 3 deletions.
2 changes: 2 additions & 0 deletions dev/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@
new EvaluateCommand("OrderBy", "orderby {x} ascending", "order by {x} ascending", "orderbyascending {x}", "order by ascending {x}", "orderby {x}", "order by {x}"),
new EvaluateCommand("FirstOrDefault", "firstordefault {x}", "first or default {x}", "firstordefault", "first or default"),
new EvaluateCommand("LastOrDefault", "lastordefault {x}", "last or default {x}", "lastordefault", "last or default"),
new EvaluateCommand("SingleOrDefault", "singleordefault {x}", "single or default {x}", "singleordefault", "single or default"),
new EvaluateCommand("First", "first {x}", "first"),
new EvaluateCommand("Last", "last {x}", "last"),
new EvaluateCommand("Single", "single {x}", "single"),
new EvaluateCommand("ThenByDescending", "thenby {x} descending", "then by {x} descending", "thenbydescending {x}", "then by descending {x}"),
new EvaluateCommand("ThenBy", "thenby {x} ascending", "then by {x} ascending", "thenbyascending {x}", "then by ascending {x}", "thenby {x}", "then by {x}")
];
Expand Down
12 changes: 12 additions & 0 deletions dev/IArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@
*/
FirstOrDefault(filter?: ((item: T) => boolean) | string): (T | null);

/**
* Returns the only item of the array - Throws an exception if not exactly one item is in array
* @param filter If set the function returns the only item that matches the filter
*/
Single(filter?: ((item: T) => boolean) | string): T;

/**
* Returns the only item of the array - Throws an exception if not only one item is in array
* @param filter If set the function returns the only item that matches the filter
*/
SingleOrDefault(filter?: ((item: T) => boolean) | string): (T | null);

/**
* Returns the last item of the array - Throws an exception if no item was found
* @param filter If set the function returns the last item that matches the filter
Expand Down
19 changes: 19 additions & 0 deletions dev/Modules/Single.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Array.prototype.Single = function<T> (this: T[], filter?: ((item: T) => boolean) | string): (T | null) {
let that: T[] = this;

if (filter != null) {
let result: T[] = that.Where(filter);

if (result.Count() === 1) {
return result.Get(0);
} else {
throw new Error("Linq4JS: The array does not contain exactly one element");
}
} else {
if (that.Count() === 1) {
return that.Get(0);
} else {
throw new Error("Linq4JS: The array does not contain exactly one element");
}
}
};
27 changes: 27 additions & 0 deletions dev/Modules/SingleOrDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Array.prototype.SingleOrDefault = function<T> (this: T[], filter?: ((item: T) => boolean) | string): (T | null) {
let that: T[] = this;

if (filter != null) {
let result: T[] = that.Where(filter);

if (result.Count() === 1) {
return result.Get(0);
} else {
if (result.Count() > 1) {
throw new Error("Linq4JS: The array contains more than one element");
} else {
return null;
}
}
} else {
if (that.Count() === 1) {
return that.Get(0);
} else {
if (that.Count() > 1) {
throw new Error("Linq4JS: The array contains more than one element");
} else {
return null;
}
}
}
};
10 changes: 10 additions & 0 deletions dist/linq4js.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ interface Array<T> {
* @param filter If set the function returns the first item that matches the filter
*/
FirstOrDefault(filter?: ((item: T) => boolean) | string): (T | null);
/**
* Returns the only item of the array - Throws an exception if not exactly one item is in array
* @param filter If set the function returns the only item that matches the filter
*/
Single(filter?: ((item: T) => boolean) | string): T;
/**
* Returns the only item of the array - Throws an exception if not only one item is in array
* @param filter If set the function returns the only item that matches the filter
*/
SingleOrDefault(filter?: ((item: T) => boolean) | string): (T | null);
/**
* Returns the last item of the array - Throws an exception if no item was found
* @param filter If set the function returns the last item that matches the filter
Expand Down
56 changes: 55 additions & 1 deletion dist/linq4js.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/linq4js.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "linq4js",
"version": "2.1.6",
"version": "2.1.7",
"description": "Linq methods for JavaScript/TypeScript for working with Arrays",
"main": "dist/linq4js.js",
"typings": "dist/linq4js.d.ts",
Expand Down
56 changes: 56 additions & 0 deletions test/all.js

Large diffs are not rendered by default.

0 comments on commit 392cb39

Please sign in to comment.