Skip to content

Commit 44c6492

Browse files
committed
improved select, added sql-like syntax
1 parent 2696145 commit 44c6492

File tree

17 files changed

+819
-86
lines changed

17 files changed

+819
-86
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"rules": {
33
"no-cond-assign": "error",
44
"no-console": "error",
5-
"no-constant-condition": "error",
5+
//"no-constant-condition": "error",
66
"no-control-regex": "error",
77
"no-dupe-args": "error",
88
"no-dupe-keys": "error",

README.md

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Linq4JS [![Build Status](https://travis-ci.org/morrisjdev/Linq4JS.svg?branch=master)](https://travis-ci.org/morrisjdev/Linq4JS)
22

3-
Linq methods for JavaScript/TypeScript for working with Arrays
3+
Linq methods for JavaScript/TypeScript for working with arrays
44

5-
This simple Extension works with array of complex Objects as well as simple arrays of strings etc. The whole thing is written in TypeScript but also usable in JavaScript
5+
This simple extension works with array of complex objects as well as simple arrays of strings etc. The whole thing is written in TypeScript but also usable in JavaScript
66

77
## Advantages
88

9-
This Extension is lightweight and fast and you can use your Lambda-Expression-Syntax to work with Arrays. The methods are mostly identically to .NET methods.
9+
This extension is lightweight and fast and you can use your Lambda-Expression-Syntax to work with arrays. The methods are mostly identically to .NET methods.
1010

11-
As Expressions you can use the normal Function-Syntax:
11+
As expressions you can use the normal Function-Syntax:
1212

1313
```javascript
1414
array.Where(function(x){
@@ -28,7 +28,21 @@ or Lambda-Expressions as Strings (this Syntax works in IE):
2828
array.Where("x => x.Name == 'Max'");
2929
```
3030

31-
Conclusion:
31+
### SQL-Like
32+
33+
Also a complete procedure as an sql-like string is supported
34+
35+
```
36+
clone
37+
reverse
38+
where x => x.Age > 70
39+
order by x => x.Name
40+
then by x => x.FirstName descending
41+
for each x => console.log(x)
42+
select x => {x.Name}
43+
```
44+
45+
### Conclusion
3246

3347
* Works with multiple Browsers (even IE)
3448
* Angular Support (event directly in Views if using Strings as Expression-Syntax)
@@ -557,6 +571,20 @@ array.Select("i => i.Value");
557571
array.Select("i => {Custom: i.Id, Name: i.Value}");
558572
```
559573

574+
When using the string syntax it is also possible to assign objects by the following methods
575+
```javascript
576+
var array = [{Id: 1, Value: "item1"}, {Id: 2, Value: "item2"}];
577+
578+
//[{Value: item1}, {Value: item2}]
579+
array.Select("i => {i.Value}");
580+
581+
//[{C: item1}, {C: item2}]
582+
array.Select("i => {C: i.Value}");
583+
584+
//[{C: item1}, {C: item2}]
585+
array.Select("i => {C = i.Value}");
586+
```
587+
560588
### Take
561589

562590
Limits the number of entries taken
@@ -707,6 +735,58 @@ array.Distinct("x => x");
707735
array.Distinct();
708736
```
709737

738+
### Evaluate
739+
740+
Evaluates SQL-String for array
741+
742+
```javascript
743+
var array = [...];
744+
745+
array.Evaluate("...");
746+
```
747+
748+
Example SQL-string
749+
750+
```
751+
clone
752+
reverse
753+
where x => x.Age > 70
754+
order by x => x.Name
755+
then by x => x.FirstName descending
756+
for each x => console.log(x)
757+
select x => {x.Name}
758+
```
759+
760+
Supported methods (the methodname and aliases are not case-sensitive)
761+
762+
| Methodname | Alias | Examples |
763+
|-------------------|---------------------------------------------------------------------------------------------------------------------------|----------------------------------|
764+
| Clone | | clone |
765+
| Reverse | | reverse |
766+
| Where | | where x => x.Id > 3 |
767+
| Select | | select x => x.Id |
768+
| Get | | get 4 |
769+
| ForEach | for each | for each x => console.log(x) |
770+
| Count | | count x => x.Id |
771+
| All | | all x => x.Age > 4 |
772+
| Any | | any x => x.Age > 4 |
773+
| Take | | take 3 |
774+
| Skip | | skip 3 |
775+
| Min | | min x => x.Age |
776+
| Max | | max x => x.Age |
777+
| GroupBy | group by | group by x => x.Name |
778+
| Distinct | | distinct x => x.Id |
779+
| FindLastIndex | find last index<br>find index ... last<br>findindex ... last | find index x => x.Age == 3 last |
780+
| FindIndex | find index<br>find first index<br>findfirstindex<br>find index ... first<br>findindex ... first | find index x => x.Age == 3 first |
781+
| OrderByDescending | order by ... descending<br>orderby ... descending<br>orderby descending<br>order by descending<br>orderbydescending | order by x => x.Age descending |
782+
| 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 |
783+
| FirstOrDefault | first or default | first or default |
784+
| LastOrDefault | last or default | last or default |
785+
| First | | first |
786+
| Last | | last |
787+
| ThenByDescending | thenby ... descending<br>then by ... descending<br>thenbydescending<br>then by descending | then by x => x.Name descending |
788+
| ThenBy | thenby ... ascending<br>then by ... ascending<br>thenbyascending<br>then byascending<br>thenby<br>then by | then by x => x.Name ascending |
789+
710790
## Author
711791

712792
[Morris Janatzek](http://morrisj.net) ([morrisjdev](https://github.com/morrisjdev))

demo/app.ts

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
}
1313
}
1414

15-
class displayClass {
15+
/*class displayClass {
1616
TestProp: string;
1717
1818
constructor(_in: User) {
1919
this.TestProp = `${_in.FirstName} ${_in.Name}`;
2020
}
21-
}
21+
}*/
2222

23-
class Helper {
23+
/*class Helper {
2424
static users: Array<User>;
2525
2626
static draw = function () {
@@ -43,9 +43,9 @@ window.onload = function () {
4343
Helper.users = Helper.users.Clone();
4444
4545
Helper.draw();
46-
};
46+
};*/
4747

48-
let test: Array<User> = [new User(1, "Max", "Mustermann", 18), new User(2, "John", "Doe", 89), new User(2, "John", "Doe", 18)];
48+
/*let test: Array<User> = [new User(1, "Max", "Mustermann", 18), new User(2, "John", "Doe", 89), new User(2, "John", "Doe", 18)];
4949
console.log(test.Where(x => x.Age > 10));
5050
5151
test.Add(new User(5, "User", "Test", 12), true);
@@ -62,6 +62,7 @@ let res2 = array.TakeWhile('(x, s) => x != "item2" || s.c < 1', 's => s.c = 0',
6262
console.log(res2);
6363
6464
console.log(array.FindLastIndex(x => x == "item2"));
65+
*/
6566

6667
//let test: Array<Linq4JS.Entity> = [new testClass("test", 5, 1), new testClass("test5", 3, 2)];
6768
//console.log(test);
@@ -87,4 +88,41 @@ console.log(array.FindLastIndex(x => x == "item2"));
8788
//test.Remove(new testClass("test55", 3, 1), x => x.OtherId);
8889
//console.log(test);
8990

90-
//console.log(test.Count(x => x.Id > 3));
91+
//console.log(test.Count(x => x.Id > 3));
92+
93+
let userArray: User[] = [
94+
new User(1, "Brenda", "Thompson", 49),
95+
new User(2, "Kelly", "Grady", 62),
96+
new User(3, "Lavina", "Baskin", 34),
97+
new User(4, "Corey", "Medina", 53),
98+
new User(5, "Walter", "Pankey", 61),
99+
new User(6, "Virginia", "Ayala", 54),
100+
new User(7, "Allison", "Israel", 38),
101+
new User(8, "Christine", "Starkey", 19),
102+
new User(9, "Robert", "Humphreys", 22),
103+
new User(10, "Daniel", "Stanley", 85),
104+
new User(11, "Frank", "Brown", 73),
105+
new User(12, "Juan", "Barnhart", 56),
106+
new User(13, "Timothy", "Olson", 29),
107+
new User(14, "Christina", "Holland", 81),
108+
new User(15, "Albert", "Dunn", 58),
109+
new User(16, "Kelly", "Grady", 48)
110+
];
111+
112+
window.onload = function(){
113+
arrayPreview.innerHTML = JSON.stringify(userArray);
114+
resultdisplay.innerHTML = JSON.stringify(userArray);
115+
};
116+
117+
let updateFunction = function() {
118+
try {
119+
let newArray = userArray.Clone();
120+
121+
resultdisplay.innerHTML = JSON.stringify(newArray.Evaluate(sqltext.value));
122+
error.innerHTML = "";
123+
} catch (ex) {
124+
error.innerHTML = ex;
125+
}
126+
};
127+
128+
let obj = "test";

demo/demo.js

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,55 @@ var User = (function () {
88
}
99
return User;
1010
}());
11-
var displayClass = (function () {
12-
function displayClass(_in) {
13-
this.TestProp = _in.FirstName + " " + _in.Name;
11+
/*class displayClass {
12+
TestProp: string;
13+
14+
constructor(_in: User) {
15+
this.TestProp = `${_in.FirstName} ${_in.Name}`;
1416
}
15-
return displayClass;
16-
}());
17-
var Helper = (function () {
18-
function Helper() {
17+
}*/
18+
/*class Helper {
19+
static users: Array<User>;
20+
21+
static draw = function () {
22+
23+
let innerContent: string = "";
24+
25+
Helper.users.Select("u => new displayClass(u)").ForEach(function (u: displayClass) {
26+
innerContent += `User: ${u.TestProp} <br>`;
27+
});
28+
29+
let contentObj = document.getElementById("content");
30+
31+
contentObj.innerHTML = innerContent;
1932
}
20-
return Helper;
21-
}());
22-
Helper.draw = function () {
23-
var innerContent = "";
24-
Helper.users.Select("u => new displayClass(u)").ForEach(function (u) {
25-
innerContent += "User: " + u.TestProp + " <br>";
26-
});
27-
var contentObj = document.getElementById("content");
28-
contentObj.innerHTML = innerContent;
29-
};
33+
}
34+
3035
window.onload = function () {
31-
Helper.users = new Array();
36+
Helper.users = new Array<User>();
37+
3238
Helper.users = Helper.users.Clone();
39+
3340
Helper.draw();
34-
};
35-
var test = [new User(1, "Max", "Mustermann", 18), new User(2, "John", "Doe", 89), new User(2, "John", "Doe", 18)];
36-
console.log(test.Where(function (x) { return x.Age > 10; }));
41+
};*/
42+
/*let test: Array<User> = [new User(1, "Max", "Mustermann", 18), new User(2, "John", "Doe", 89), new User(2, "John", "Doe", 18)];
43+
console.log(test.Where(x => x.Age > 10));
44+
3745
test.Add(new User(5, "User", "Test", 12), true);
38-
var result = test.Aggregate(function (x, y) {
46+
47+
let result = test.Aggregate((x, y) => {
3948
return x += y.Age;
4049
});
50+
4151
console.log(result);
42-
var array = ["item1", "item2", "item3", "item2", "item4"];
43-
var res2 = array.TakeWhile('(x, s) => x != "item2" || s.c < 1', 's => s.c = 0', '(x, s) => x == "item2" && s.c++');
52+
53+
let array = ["item1", "item2", "item3", "item2", "item4"];
54+
let res2 = array.TakeWhile('(x, s) => x != "item2" || s.c < 1', 's => s.c = 0', '(x, s) => x == "item2" && s.c++');
55+
4456
console.log(res2);
45-
console.log(array.FindLastIndex(function (x) { return x == "item2"; }));
57+
58+
console.log(array.FindLastIndex(x => x == "item2"));
59+
*/
4660
//let test: Array<Linq4JS.Entity> = [new testClass("test", 5, 1), new testClass("test5", 3, 2)];
4761
//console.log(test);
4862
//console.log("Foreach");
@@ -61,4 +75,37 @@ console.log(array.FindLastIndex(function (x) { return x == "item2"; }));
6175
//console.log(test.Select(x => new displayClass(x)));
6276
//test.Remove(new testClass("test55", 3, 1), x => x.OtherId);
6377
//console.log(test);
64-
//console.log(test.Count(x => x.Id > 3));
78+
//console.log(test.Count(x => x.Id > 3));
79+
var userArray = [
80+
new User(1, "Brenda", "Thompson", 49),
81+
new User(2, "Kelly", "Grady", 62),
82+
new User(3, "Lavina", "Baskin", 34),
83+
new User(4, "Corey", "Medina", 53),
84+
new User(5, "Walter", "Pankey", 61),
85+
new User(6, "Virginia", "Ayala", 54),
86+
new User(7, "Allison", "Israel", 38),
87+
new User(8, "Christine", "Starkey", 19),
88+
new User(9, "Robert", "Humphreys", 22),
89+
new User(10, "Daniel", "Stanley", 85),
90+
new User(11, "Frank", "Brown", 73),
91+
new User(12, "Juan", "Barnhart", 56),
92+
new User(13, "Timothy", "Olson", 29),
93+
new User(14, "Christina", "Holland", 81),
94+
new User(15, "Albert", "Dunn", 58),
95+
new User(16, "Kelly", "Grady", 48)
96+
];
97+
window.onload = function () {
98+
arrayPreview.innerHTML = JSON.stringify(userArray);
99+
resultdisplay.innerHTML = JSON.stringify(userArray);
100+
};
101+
var updateFunction = function () {
102+
try {
103+
var newArray = userArray.Clone();
104+
resultdisplay.innerHTML = JSON.stringify(newArray.Evaluate(sqltext.value));
105+
error.innerHTML = "";
106+
}
107+
catch (ex) {
108+
error.innerHTML = ex;
109+
}
110+
};
111+
var obj = "test";

demo/index.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
<body>
1212
<h1>TypeScript HTML App</h1>
1313

14-
<div id="content"></div>
14+
<div id="arrayPreview"></div><br>
15+
16+
<textarea id="sqltext" cols="200" rows="15" onchange="updateFunction()" onkeyup="updateFunction()" placeholder="Custom command"></textarea>
17+
18+
<div id="error"></div>
19+
20+
<h3>Result</h3>
21+
22+
<div id="resultdisplay">
23+
24+
</div>
1525
</body>
16-
</html>
26+
</html>

dev/EvaluateCommand.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace Linq4JS {
2+
export class EvaluateCommand {
3+
public Command: string;
4+
public SplitRegex: RegExp[] = [];
5+
public Finder: RegExp[] = [];
6+
7+
constructor(command: string, ...identifier: string[]) {
8+
this.Command = command;
9+
10+
for (let id of identifier) {
11+
let sSplitRegex: string;
12+
let sFinder: string;
13+
14+
if (id.indexOf("{x}") !== -1) {
15+
if (id.indexOf("{x}") === id.length - 3) {
16+
sSplitRegex = "\\b" + id.replace(" {x}", "") + "\\b";
17+
sFinder = "\\b" + id.replace(" {x}", "\\b (.*)");
18+
} else {
19+
sSplitRegex = "\\b" + id.replace(" {x}", "\\b .*? \\b") + "\\b";
20+
sFinder = "\\b" + id.replace(" {x} ", "\\b (.*) \\b") + "\\b";
21+
}
22+
} else {
23+
sSplitRegex = "\\b" + id + "\\b";
24+
sFinder = "\\b" + id + "\\b";
25+
}
26+
27+
this.Finder.push(new RegExp(sFinder, "i"));
28+
this.SplitRegex.push(new RegExp(sSplitRegex, "gi"));
29+
}
30+
}
31+
}
32+
33+
export class EvaluateCommandResult {
34+
public Command: string;
35+
public DynamicFunction: string;
36+
37+
constructor(cmd: string, fn: string) {
38+
this.Command = cmd;
39+
this.DynamicFunction = fn;
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)