Skip to content

Commit f924e5f

Browse files
committed
added FindLastIndex, fixed issue in package.json
1 parent c6cc701 commit f924e5f

File tree

14 files changed

+115
-37
lines changed

14 files changed

+115
-37
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ var array = ["item1", "item2", "item3", "item4", "no"];
8989
array.FindIndex("x => x == 'item3'");
9090
```
9191

92+
### FindLastIndex
93+
94+
Gets the index of the last item found by a filter
95+
96+
```javascript
97+
var array = ["item1", "item2", "item3", "item2", "item4", "no"];
98+
99+
//3
100+
array.FindIndex("x => x == 'item2'");
101+
```
102+
92103
### Get
93104

94105
Gets the item with the index

bower.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "linq4js",
33
"description": "Linq methods for JavaScript/TypeScript for working with Arrays",
4-
"main": "dist/Linq4JS.js",
4+
"main": "dist/linq4js.js",
55
"authors": [
66
"Morris Janatzek"
77
],
@@ -23,8 +23,7 @@
2323
"demo",
2424
".gitignore",
2525
".npmignore",
26-
"gulpfile.js",
27-
"package.json"
26+
"gulpfile.js"
2827
],
2928
"devDependencies": {
3029
"qunit": "^2.1.1"

demo/demo.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,23 @@ console.log(result);
4242
var array = ["item1", "item2", "item3", "item2", "item4"];
4343
var res2 = array.TakeWhile('(x, s) => x != "item2" || s.c < 1', 's => s.c = 0', '(x, s) => x == "item2" && s.c++');
4444
console.log(res2);
45+
console.log(array.FindLastIndex(function (x) { return x == "item2"; }));
46+
//let test: Array<Linq4JS.Entity> = [new testClass("test", 5, 1), new testClass("test5", 3, 2)];
47+
//console.log(test);
48+
//console.log("Foreach");
49+
//test.ForEach(x => console.log(x.Name));
50+
//console.log("Foreach End");
51+
//console.log(test.Take(1));
52+
//console.log(test.Skip(1));
53+
//test.Update(new testClass("test1235234234234", 3, 1), "x => x.OtherId");
54+
//console.log(test);
55+
//console.log(test.Where(x => x.Id == 3));
56+
//console.log(test.Any(x => x.Id == 78));
57+
//console.log(test.First(x => x.Id == 3));
58+
//console.log(test.FirstOrDefault());
59+
//console.log(test.Last(x => x.Id == 3));
60+
//console.log(test.LastOrDefault(x => x.Id == 44));
61+
//console.log(test.Select(x => new displayClass(x)));
62+
//test.Remove(new testClass("test55", 3, 1), x => x.OtherId);
63+
//console.log(test);
64+
//console.log(test.Count(x => x.Id > 3));

demo/js/app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ let res2 = array.TakeWhile('(x, s) => x != "item2" || s.c < 1', 's => s.c = 0',
6161

6262
console.log(res2);
6363

64+
console.log(array.FindLastIndex(x => x == "item2"));
65+
6466
//let test: Array<Linq4JS.Entity> = [new testClass("test", 5, 1), new testClass("test5", 3, 2)];
6567
//console.log(test);
6668

dev/IArray.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
*/
1414
FindIndex(filter: ((item: T) => boolean) | string): number;
1515

16+
/**
17+
* Gets the index of the last item found by a filter
18+
* @param filter A function (or function-string) that returns a boolean when matching element was found
19+
*/
20+
FindLastIndex(filter: ((item: T) => boolean) | string): number;
21+
1622
/**
1723
* Gets the item with the index
1824
* @param index Item index
@@ -23,7 +29,7 @@
2329
* Executes a method for each item in the array
2430
* @param action A function (or function-string) that gets executed for each element. If it returns false the loop stops.
2531
*/
26-
ForEach(action: ((item: T) => boolean | any) | string): Array<T>;
32+
ForEach(action: ((item: T, index?: number) => boolean | any) | string): Array<T>;
2733

2834
/**
2935
* Updates an object in the array
@@ -78,7 +84,7 @@
7884
* Searches for all items in array that match the given filter
7985
* @param filter A function (or function-string) that returns a boolean when matching element was found
8086
*/
81-
Where(filter: ((item: T) => boolean) | string): Array<T>;
87+
Where(filter: ((item: T, index?: number) => boolean) | string): Array<T>;
8288

8389
/**
8490
* Takes items in a specific range

dev/Modules/Add.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
let that: Array<T> = this;
33

44
if (object != null) {
5-
65
if (generateId == true) {
76
let newIndex: number;
87

dev/Modules/Distinct.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
if(valueSelector != null){
55
let valueSelectorFunction = Linq4JS.Helper.ConvertFunction<(item: T) => any>(valueSelector);
66

7-
return that.filter((value, index, self) => {
8-
return self.FindIndex(x => valueSelectorFunction(x) == valueSelectorFunction(value)) == index;
9-
});
7+
return that.Where((x, i) => {
8+
return that.FindIndex(y => valueSelectorFunction(y) == valueSelectorFunction(x)) == i;
9+
})
1010
}
1111
else{
12-
return that.filter((value, index, self) => {
13-
return self.indexOf(value) == index;
14-
});
12+
return that.Where((x, i) => {
13+
return that.FindIndex(y => y == x) == i;
14+
})
1515
}
1616
};

dev/Modules/FindLastIndex.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Array.prototype.FindLastIndex = function<T> (filter: ((item: T) => boolean) | string): number {
2+
let that: Array<T> = this;
3+
4+
if (filter != null) {
5+
let filterFunction = Linq4JS.Helper.ConvertFunction<(item: T) => boolean>(filter);
6+
7+
for (let i = that.length - 1; i >= 0; i--) {
8+
let obj: T = that[i];
9+
10+
if (filterFunction(obj) == true) {
11+
return i;
12+
}
13+
}
14+
15+
return -1;
16+
}
17+
else {
18+
throw "Linq4JS: You must define a filter";
19+
}
20+
};

dev/Modules/ForEach.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
Array.prototype.ForEach = function<T> (action: ((item: T) => boolean | any) | string): Array<T> {
1+
Array.prototype.ForEach = function<T> (action: ((item: T, index?: number) => boolean | any) | string): Array<T> {
22
let that: Array<T> = this;
33

4-
let actionFunction = Linq4JS.Helper.ConvertFunction<(item: T) => boolean | any>(action);
4+
let actionFunction = Linq4JS.Helper.ConvertFunction<(item: T, index?: number) => boolean | any>(action);
55

6-
for (let obj of that) {
7-
let result = actionFunction(obj);
6+
for (let i = 0; i < that.length; i++) {
7+
let result = actionFunction(that[i], i);
88

99
if(result != null && result == true){
1010
break;

dev/Modules/Where.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
Array.prototype.Where = function<T> (filter: ((item: T) => boolean) | string): Array<T> {
1+
Array.prototype.Where = function<T> (filter: ((item: T, index?: number) => boolean) | string): Array<T> {
22
let that: Array<T> = this;
33

44
if (filter != null) {
5-
let filterFunction = Linq4JS.Helper.ConvertFunction<(item: T) => boolean>(filter);
5+
let filterFunction = Linq4JS.Helper.ConvertFunction<(item: T, index?: number) => boolean>(filter);
66

77
let newArray: Array<T> = new Array<T>();
88

99
for (let i = 0; i < that.length; i++) {
1010
let obj: T = that[i];
1111

12-
if (filterFunction(obj) == true) {
12+
if (filterFunction(obj, i) == true) {
1313
newArray.push(obj);
1414
}
1515
}

0 commit comments

Comments
 (0)