Skip to content

Commit

Permalink
fixed some issues, applied improvements, added SelectMany
Browse files Browse the repository at this point in the history
  • Loading branch information
morrisjdev committed Jun 22, 2018
1 parent ae2cf06 commit e48820f
Show file tree
Hide file tree
Showing 72 changed files with 2,118 additions and 1,184 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ array.Where("x => x.Name == 'Max'");

Also a complete procedure as an sql-like string is supported

```
array.Evaluate("select x => x.Id sum");
```

```
clone
reverse
Expand Down Expand Up @@ -638,6 +642,17 @@ array.Select("i => {C: i.Value}");
array.Select("i => {C = i.Value}");
```

### SelectMany

Select the properties with an array as value and concats them

```javascript
var array = [["item1", "item2"], ["item1", "item2"]];

//["item1", "item2", "item1", "item2"]
array.SelectMany("i => i");
```

### Take

Limits the number of entries taken
Expand Down
35 changes: 18 additions & 17 deletions demo/demo.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"use strict";
var User = (function () {
function User(_id, _firstName, _name, _age) {
function User(_id, _firstName, _name, _age, _pets) {
this.Id = _id;
this.FirstName = _firstName;
this.Name = _name;
this.Age = _age;
this.Pets = _pets;
}
return User;
}());
Expand Down Expand Up @@ -77,22 +78,22 @@ console.log(array.FindLastIndex(x => x == "item2"));
//console.log(test);
//console.log(test.Count(x => x.Id > 3));
var userArray = [
new User(1, "Brenda", "Thompson", 49),
new User(2, "Kelly", "Grady", 62),
new User(3, "Lavina", "Baskin", 34),
new User(4, "Corey", "Medina", 53),
new User(5, "Walter", "Pankey", 61),
new User(6, "Virginia", "Ayala", 54),
new User(7, "Allison", "Israel", 38),
new User(8, "Christine", "Starkey", 19),
new User(9, "Robert", "Humphreys", 22),
new User(10, "Daniel", "Stanley", 85),
new User(11, "Frank", "Brown", 73),
new User(12, "Juan", "Barnhart", 56),
new User(13, "Timothy", "Olson", 29),
new User(14, "Christina", "Holland", 81),
new User(15, "Albert", "Dunn", 58),
new User(16, "Kelly", "Grady", 48)
new User(1, "Brenda", "Thompson", 49, ["Dog", "Cat"]),
new User(2, "Kelly", "Grady", 62, ["Dog", "Cat"]),
new User(3, "Lavina", "Baskin", 34, ["Dog", "Cat"]),
new User(4, "Corey", "Medina", 53, ["Dog", "Cat"]),
new User(5, "Walter", "Pankey", 61, ["Dog", "Cat"]),
new User(6, "Virginia", "Ayala", 54, ["Dog", "Cat"]),
new User(7, "Allison", "Israel", 38, ["Dog", "Cat"]),
new User(8, "Christine", "Starkey", 19, ["Dog", "Cat"]),
new User(9, "Robert", "Humphreys", 22, ["Dog", "Cat"]),
new User(10, "Daniel", "Stanley", 85, ["Dog", "Cat"]),
new User(11, "Frank", "Brown", 73, ["Dog", "Cat"]),
new User(12, "Juan", "Barnhart", 56, ["Dog", "Cat"]),
new User(13, "Timothy", "Olson", 29, ["Dog", "Cat"]),
new User(14, "Christina", "Holland", 81, ["Dog", "Cat"]),
new User(15, "Albert", "Dunn", 58, ["Dog", "Cat"]),
new User(16, "Kelly", "Grady", 48, ["Dog", "Cat"])
];
window.onload = function () {
arrayPreview.innerHTML = JSON.stringify(userArray);
Expand Down
22 changes: 20 additions & 2 deletions dev/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@

for (let cmd of this.Commands) {
for (let split of cmd.SplitRegex) {
while (true) {
while (true) {
let result = split.exec(command);
if (result != null) {
splitIndexes.push(result.index);
Expand Down Expand Up @@ -150,13 +150,19 @@
public static Commands: EvaluateCommand[] = [
new EvaluateCommand("Clone", "clone"),
new EvaluateCommand("Reverse", "reverse"),
new EvaluateCommand("Contains", "contains {x}"),
new EvaluateCommand("Join", "join {x}"),
new EvaluateCommand("Sum", "sum {x}", "sum"),
new EvaluateCommand("Average", "average {x}", "average"),
new EvaluateCommand("Where", "where {x}"),
new EvaluateCommand("SelectMany", "selectmany {x}", "select many {x}", "select {x} many"),
new EvaluateCommand("Select", "select {x}"),
new EvaluateCommand("Get", "get {x}"),
new EvaluateCommand("ForEach", "foreach {x}", "for each {x}"),
new EvaluateCommand("Count", "count", "count {x}"),
new EvaluateCommand("All", "all {x}"),
new EvaluateCommand("Any", "any {x}", "any"),
new EvaluateCommand("TakeWhile", "take while {x}", "take {x} while", "takewhile {x}"),
new EvaluateCommand("Take", "take {x}"),
new EvaluateCommand("Skip", "skip {x}"),
new EvaluateCommand("Min", "min {x}", "min"),
Expand All @@ -176,5 +182,17 @@
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}")
];

public static NonEnumerable(name: string, value: Function) {
Object.defineProperty(Array.prototype, name, {
value: value,
enumerable: false
});
}
}
}
}

Object.defineProperty(Array.prototype, "_linq4js_", {
value: { Order: [] },
enumerable: false
});
9 changes: 7 additions & 2 deletions dev/IArray.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
interface Array<T> {
Order: Linq4JS.OrderEntry[];
GroupValue: any;
_linq4js_: { Order: Linq4JS.OrderEntry[], GroupValue: any };

/**
* Executes actions defined in the command-string
Expand Down Expand Up @@ -165,6 +164,12 @@
*/
Select(selector: ((item: T) => any) | string): any[];

/**
* 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[];

/**
* Limits the number of entries taken
* @param count The count of elements taken
Expand Down
14 changes: 6 additions & 8 deletions dev/Modules/Add.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
Array.prototype.Add = function<T> (this: T[], object: T, generateId?: boolean): T[] {
let that: T[] = this;

Linq4JS.Helper.NonEnumerable("Add", function<T> (this: T[], object: T, generateId?: boolean): T[] {
if (object != null) {
if (generateId === true) {
let newIndex: number;

let castedObject: Linq4JS.GeneratedEntity = object as any;
let last: Linq4JS.GeneratedEntity = that.Where((x: any) => x._GeneratedId_ != null).OrderBy((x: any) => x._GeneratedId_).LastOrDefault() as any;
let last: Linq4JS.GeneratedEntity = this.Where((x: any) => x._GeneratedId_ != null).OrderBy((x: any) => x._GeneratedId_).LastOrDefault() as any;
if (last != null) {
newIndex = last._GeneratedId_ != null ? last._GeneratedId_ : 1;

while (that.Any(function(x: any) {
while (this.Any(function(x: any) {
return (x as Linq4JS.GeneratedEntity)._GeneratedId_ === newIndex;
})) {
newIndex++;
Expand All @@ -22,8 +20,8 @@
}
}

that.push(object);
this.push(object);
}

return that;
};
return this;
});
4 changes: 2 additions & 2 deletions dev/Modules/AddRange.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Array.prototype.AddRange = function<T> (this: T[], objects: T[], generateId: boolean): T[] {
Linq4JS.Helper.NonEnumerable("AddRange", function<T> (this: T[], objects: T[], generateId: boolean): T[] {
let that: T[] = this;

objects.ForEach(function (x: T) {
that.Add(x, generateId);
});

return that;
};
});
8 changes: 3 additions & 5 deletions dev/Modules/Aggregate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
Array.prototype.Aggregate = function<T> (this: T[], method: ((result: any, item: T) => any) | string, startVal?: any): string {
let that: T[] = this;

Linq4JS.Helper.NonEnumerable("Aggregate", function<T> (this: T[], method: ((result: any, item: T) => any) | string, startVal?: any): string {
let result: any;

if (startVal != null) {
Expand All @@ -11,9 +9,9 @@

let methodFunction = Linq4JS.Helper.ConvertFunction<(result: any, item: T) => any>(method);

that.ForEach(function(x){
this.ForEach(function(x){
result = methodFunction(result, x);
});

return result;
};
});
8 changes: 3 additions & 5 deletions dev/Modules/All.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Array.prototype.All = function<T> (this: T[], filter: ((item: T) => boolean) | string): boolean {
let that: T[] = this;

return that.Count(filter) === that.Count();
};
Linq4JS.Helper.NonEnumerable("All", function<T> (this: T[], filter: ((item: T) => boolean) | string): boolean {
return this.Count(filter) === this.Count();
});
8 changes: 3 additions & 5 deletions dev/Modules/Any.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Array.prototype.Any = function<T> (this: T[], filter?: ((item: T) => boolean) | string): boolean {
let that: T[] = this;

return that.Count(filter) > 0;
};
Linq4JS.Helper.NonEnumerable("Any", function<T> (this: T[], filter?: ((item: T) => boolean) | string): boolean {
return this.Count(filter) > 0;
});
8 changes: 3 additions & 5 deletions dev/Modules/Average.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
Array.prototype.Average = function <T>(this: T[], selector?: ((item: T) => any) | string, filter?: ((item: T) => boolean) | string): number {
let that: T[] = this;

Linq4JS.Helper.NonEnumerable("Average", function <T>(this: T[], selector?: ((item: T) => any) | string, filter?: ((item: T) => boolean) | string): number {
let result: number = 0;
let array: any[] = that;
let array: any[] = this;

if (filter != null) {
array = array.Where(filter);
Expand All @@ -17,4 +15,4 @@
});

return result / array.Count();
};
});
8 changes: 3 additions & 5 deletions dev/Modules/Clone.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
Array.prototype.Clone = function<T> (this: T[]): T[] {
let that: T[] = this;

Linq4JS.Helper.NonEnumerable("Clone", function<T> (this: T[]): T[] {
let newArray: T[] = [];

for (let obj of that) {
for (let obj of this) {
newArray.Add(obj);
}

return newArray;
};
});
8 changes: 3 additions & 5 deletions dev/Modules/Concat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Array.prototype.Concat = function<T> (this: T[], array: T[]): T[] {
let that: T[] = this;
that = that.concat(array);
return that;
};
Linq4JS.Helper.NonEnumerable("Concat", function<T> (this: T[], array: T[]): T[] {
return this.concat(array);
});
8 changes: 3 additions & 5 deletions dev/Modules/Contains.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
Array.prototype.Contains = function<T> (this: T[], object: T): boolean {
let that: T[] = this;

return that.Any(function(x){
Linq4JS.Helper.NonEnumerable("Contains", function<T> (this: T[], object: T): boolean {
return this.Any(function(x){
return x === object;
});
};
});
10 changes: 4 additions & 6 deletions dev/Modules/Count.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
Array.prototype.Count = function <T>(this: T[], filter?: ((item: T) => boolean) | string): number {
let that: T[] = this;

Linq4JS.Helper.NonEnumerable("Count", function <T>(this: T[], filter?: ((item: T) => boolean) | string): number {
if (filter != null) {
return that.Where(filter).length;
return this.Where(filter).length;
} else {
return that.length;
return this.length;
}
};
});
8 changes: 4 additions & 4 deletions dev/Modules/Distinct.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Array.prototype.Distinct = function<T> (this: T[], valueSelector?: ((item: T) => any) | string): T[] {
Linq4JS.Helper.NonEnumerable("Distinct", function<T> (this: T[], valueSelector?: ((item: T) => any) | string): T[] {
let that: T[] = this;

if (valueSelector != null) {
let valueSelectorFunction = Linq4JS.Helper.ConvertFunction<(item: T) => any>(valueSelector);

return that.Where((x, i) => {
return this.Where((x, i) => {
return that.FindIndex(y => valueSelectorFunction(y) === valueSelectorFunction(x)) === i;
});
} else {
return that.Where((x, i) => {
return this.Where((x, i) => {
return that.FindIndex(y => y === x) === i;
});
}
};
});
8 changes: 3 additions & 5 deletions dev/Modules/Evaluate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
Array.prototype.Evaluate = function<T> (this: T[], command: string): any {
let that: T[] = this;

Linq4JS.Helper.NonEnumerable("Evaluate", function<T> (this: T[], command: string): any {
let commandParts: string[] = Linq4JS.Helper.SplitCommand(command);

let computeObject: any = that;
let computeObject: any = this;

for (let cmd of commandParts) {
let cmdResult = Linq4JS.Helper.MatchCommand(cmd);
Expand All @@ -12,4 +10,4 @@ Array.prototype.Evaluate = function<T> (this: T[], command: string): any {
}

return computeObject;
};
});
10 changes: 4 additions & 6 deletions dev/Modules/FindIndex.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
Array.prototype.FindIndex = function<T> (this: T[], filter: ((item: T) => boolean) | string): number {
let that: T[] = this;

Linq4JS.Helper.NonEnumerable("FindIndex", function<T> (this: T[], filter: ((item: T) => boolean) | string): number {
if (filter != null) {
let filterFunction = Linq4JS.Helper.ConvertFunction<(item: T) => boolean>(filter);

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

if (filterFunction(obj) === true) {
return i;
Expand All @@ -16,4 +14,4 @@
} else {
throw new Error("Linq4JS: You must define a filter");
}
};
});
Loading

0 comments on commit e48820f

Please sign in to comment.