Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
reduced package size
fixed some issues
improved select, selectmany, todictionary and zip generics
  • Loading branch information
morrisjdev committed Mar 13, 2019
1 parent 299d4d4 commit 7b1fa10
Show file tree
Hide file tree
Showing 79 changed files with 10,021 additions and 347 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
bower_components
/.vscode
/node_modules
/bower_components
9 changes: 9 additions & 0 deletions .idea/Linq4JS.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 34 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -827,33 +827,41 @@ select x => {x.Name}

Supported methods (the methodname and aliases are not case-sensitive)

| Methodname | Alias | Examples |
|-------------------|---------------------------------------------------------------------------------------------------------------------------|----------------------------------|
| Clone | | clone |
| Reverse | | reverse |
| Where | | where x => x.Id > 3 |
| Select | | select x => x.Id |
| Get | | get 4 |
| ForEach | for each | for each x => console.log(x) |
| Count | | count x => x.Id |
| All | | all x => x.Age > 4 |
| Any | | any x => x.Age > 4 |
| Take | | take 3 |
| Skip | | skip 3 |
| Min | | min x => x.Age |
| Max | | max x => x.Age |
| GroupBy | group by | group by x => x.Name |
| Distinct | | distinct x => x.Id |
| FindLastIndex | find last index<br>find index ... last<br>findindex ... last | find index x => x.Age == 3 last |
| FindIndex | find index<br>find first index<br>findfirstindex<br>find index ... first<br>findindex ... first | find index x => x.Age == 3 first |
| OrderByDescending | order by ... descending<br>orderby ... descending<br>orderby descending<br>order by descending<br>orderbydescending | order by x => x.Age descending |
| Methodname | Alias | Examples |
|-------------------|---------------------------------------------------------------------------------------------------------------------------------------|----------------------------------|
| Clone | | clone |
| Reverse | | reverse |
| Contains | | contains 5 |
| Join | | join 5 |
| Sum | | sum |
| Average | | average |
| Where | | where x => x.Id > 3 |
| Select | | select x => x.Id |
| SelectMany | select many<br> select ... many | select many x => x.Pets |
| Get | | get 4 |
| ForEach | for each | for each x => console.log(x) |
| Count | | count x => x.Id |
| All | | all x => x.Age > 4 |
| Any | | any x => x.Age > 4 |
| Take | | take 3 |
| TakeWhile | take while<br> take ... while | take while x => x.Age > 10 |
| Skip | | skip 3 |
| Min | | min x => x.Age |
| Max | | max x => x.Age |
| GroupBy | group by | group by x => x.Name |
| Distinct | | distinct x => x.Id |
| FindLastIndex | find last index<br>find index ... last<br>findindex ... last | find index x => x.Age == 3 last |
| FindIndex | find index<br>find first index<br>findfirstindex<br>find index ... first<br>findindex ... first | find index x => x.Age == 3 first |
| OrderByDescending | order by ... descending<br>orderby ... descending<br>orderby descending<br>order by descending<br>orderbydescending | order by x => x.Age descending |
| OrderBy | order by ... ascending<br>orderby ... ascending<br>orderby ascending<br>order by ascending<br>orderbyascending<br>order by<br>orderby | order by x => x.Age ascending |
| FirstOrDefault | first or default | first or default |
| LastOrDefault | last or default | last or default |
| First | | first |
| Last | | last |
| ThenByDescending | thenby ... descending<br>then by ... descending<br>thenbydescending<br>then by descending | then by x => x.Name descending |
| ThenBy | thenby ... ascending<br>then by ... ascending<br>thenbyascending<br>then byascending<br>thenby<br>then by | then by x => x.Name ascending |
| FirstOrDefault | first or default | first or default |
| LastOrDefault | last or default | last or default |
| SingleOrDefault | single or default | single or default |
| First | | first |
| Last | | last |
| Single | | single |
| ThenByDescending | thenby ... descending<br>then by ... descending<br>thenbydescending<br>then by descending | then by x => x.Name descending |
| ThenBy | thenby ... ascending<br>then by ... ascending<br>thenbyascending<br>then byascending<br>thenby<br>then by | then by x => x.Name ascending |

## Author

Expand Down
12 changes: 7 additions & 5 deletions dev/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,12 @@
enumerable: false
});
}

public static CreateArrayData(array: any[], value: any = {}) {
Object.defineProperty(array, "_linq4js_", {
value: value,
enumerable: false
});
}
}
}

Object.defineProperty(Array.prototype, "_linq4js_", {
value: { Order: [] },
enumerable: false
});
12 changes: 7 additions & 5 deletions dev/IArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
/**
* Adds objects to the array
* @param objects The array of objects to add
* @param generateId Auto-generate a property to identify object in later processes
*/
AddRange(objects: T[], generateId?: boolean): T[];

Expand Down Expand Up @@ -162,13 +163,13 @@
* Select the properties for a new array
* @param selector A function (or a function-string) that returns a new object
*/
Select(selector: ((item: T) => any) | string): any[];
Select<Y>(selector: ((item: T) => Y) | string): Y[];

/**
* Select the properties with an array as value and concats them
* @param selector A function (or a function-string) that returns the array with elements to select
*/
SelectMany(selector: ((item: T) => any) | string): any[];
SelectMany<Y>(selector: ((item: T) => Y[]) | string): Y[];

/**
* Limits the number of entries taken
Expand Down Expand Up @@ -310,7 +311,7 @@
* @param array The array to combine
* @param result The function (or function-string) to combine elements
*/
Zip<T, X>(array: X[], result: ((first: T, second: X) => any) | string): any[];
Zip<T, X, Y>(array: X[], result: ((first: T, second: X) => Y) | string): Y[];

/**
* Combines two arrays without duplicates
Expand All @@ -323,5 +324,6 @@
* @param keySelector The selector-function (or function-string) to select property for key
* @param valueSelector A selector-function (or function-string) to select property for value
*/
ToDictionary(keySelector: ((item: T) => any) | string, valueSelector?: ((item: T) => any) | string): any;
}
ToDictionary<Y>(keySelector: ((item: T) => (string|number)) | string, valueSelector?: ((item: T) => Y) | string):
{ [prop: string]: Y, [prop: number]: Y };
}
4 changes: 2 additions & 2 deletions dev/Modules/FindIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
for (let i = 0; i < this.length; i++) {
let obj: T = this[i];

if (filterFunction(obj) === true) {
if (filterFunction(obj)) {
return i;
}
}
Expand All @@ -14,4 +14,4 @@
} else {
throw new Error("Linq4JS: You must define a filter");
}
});
});
20 changes: 8 additions & 12 deletions dev/Modules/First.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
Linq4JS.Helper.NonEnumerable("First", function<T> (this: T[], filter?: ((item: T) => boolean) | string): T {
let result = this;

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

if (result.Any()) {
return result.Get(0);
} else {
throw new Error("Linq4JS: The First Entry was not found");
}
if (result.Any()) {
return result.Get(0);
} else {
if (this.Any()) {
return this.Get(0);
} else {
throw new Error("Linq4JS: The First Entry was not found");
}
throw new Error("Linq4JS: The First Entry was not found");
}
});
});
20 changes: 8 additions & 12 deletions dev/Modules/FirstOrDefault.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
Linq4JS.Helper.NonEnumerable("FirstOrDefault", function<T> (this: T[], filter?: ((item: T) => boolean) | string): (T | null) {
let result = this;

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

if (result.Any()) {
return result.Get(0);
} else {
return null;
}
if (result.Any()) {
return result.Get(0);
} else {
if (this.Any()) {
return this.Get(0);
} else {
return null;
}
return null;
}
});
});
4 changes: 3 additions & 1 deletion dev/Modules/GroupBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
if (selectorFunction(prev) !== selectorFunction(x)) {
newArray.Add(newSub);
newSub = [];
Linq4JS.Helper.CreateArrayData(newSub, {});
newSub._linq4js_.GroupValue = selectorFunction(x);
}
} else {
Linq4JS.Helper.CreateArrayData(newSub, {});
newSub._linq4js_.GroupValue = selectorFunction(x);
}

Expand All @@ -28,4 +30,4 @@
}

return newArray;
});
});
20 changes: 8 additions & 12 deletions dev/Modules/Last.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
Linq4JS.Helper.NonEnumerable("Last", function<T> (this: T[], filter?: ((item: T) => boolean) | string): T {
let result = this;

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

if (result.Any()) {
return result.Get(result.length - 1);
} else {
throw new Error("Linq4JS: The Last Entry was not found");
}
if (result.Any()) {
return result.Get(result.length - 1);
} else {
if (this.Any()) {
return this.Get(this.length - 1);
} else {
throw new Error("Linq4JS: The Last Entry was not found");
}
throw new Error("Linq4JS: The Last Entry was not found");
}
});
});
Loading

0 comments on commit 7b1fa10

Please sign in to comment.