|
1 | 1 | namespace Linq4JS { |
2 | 2 | export class Helper { |
3 | | - static ConvertStringFunction = function (functionString: string): any { |
4 | | - |
5 | | - if (functionString.length == 0) { |
6 | | - throw "Linq4JS: Cannot convert empty string to function"; |
| 3 | + private static ConvertStringFunction(functionString: string): any { |
| 4 | + if (functionString.length === 0) { |
| 5 | + throw new Error("Linq4JS: Cannot convert empty string to function"); |
7 | 6 | } |
8 | 7 |
|
9 | | - let varnameString: string = functionString.substring(0, functionString.indexOf("=>")).replace(" ", "").replace("(", "").replace(")", ""); |
| 8 | + let varnameString: string = functionString |
| 9 | + .substring(0, functionString.indexOf("=>")) |
| 10 | + .replace(" ", "") |
| 11 | + .replace("(", "") |
| 12 | + .replace(")", ""); |
10 | 13 |
|
11 | | - let varnames: Array<string> = varnameString.split(","); |
| 14 | + let varnames: string[] = varnameString.split(","); |
12 | 15 |
|
13 | 16 | let func: string = functionString |
14 | | - .substring(functionString.indexOf("=>") + 2) |
| 17 | + .substring(functionString.indexOf("=>") + ("=>").length) |
15 | 18 | .replace("{", "").replace("}", "") |
16 | 19 | .split(".match(//gi)").join(""); |
17 | 20 |
|
18 | 21 | /*No return outside of quotations*/ |
19 | | - if(func.match(/return(?=([^\"']*[\"'][^\"']*[\"'])*[^\"']*$)/g) == null){ |
| 22 | + if (func.match(/return(?=([^\"']*[\"'][^\"']*[\"'])*[^\"']*$)/g) == null) { |
20 | 23 | func = "return " + func; |
21 | 24 | } |
22 | 25 |
|
23 | 26 | return Function(...varnames, func); |
24 | 27 | } |
25 | 28 |
|
26 | | - static ConvertFunction = function<T> (testFunction: any): T { |
27 | | - |
| 29 | + public static ConvertFunction<T>(testFunction: string | T): T { |
28 | 30 | let result: T; |
29 | 31 |
|
30 | | - if (typeof testFunction == "function") { |
| 32 | + if (typeof testFunction === "function") { |
31 | 33 | result = testFunction; |
32 | | - } |
33 | | - else if (typeof testFunction == "string") { |
| 34 | + } else if (typeof testFunction === "string") { |
34 | 35 | result = Linq4JS.Helper.ConvertStringFunction(testFunction); |
35 | | - } |
36 | | - else { |
37 | | - throw `Linq4JS: Cannot use '${testFunction}' as function`; |
| 36 | + } else { |
| 37 | + throw new Error(`Linq4JS: Cannot use '${testFunction}' as function`); |
38 | 38 | } |
39 | 39 |
|
40 | 40 | return result; |
41 | 41 | } |
42 | 42 |
|
43 | | - static OrderCompareFunction = function<T> (valueSelector: (item: T) => any, a: T, b: T, invert: boolean): number { |
44 | | - let value_a = valueSelector(a); |
45 | | - let value_b = valueSelector(b); |
| 43 | + public static OrderCompareFunction<T>(valueSelector: (item: T) => any, a: T, b: T, invert: boolean): number { |
| 44 | + let value_a: any = valueSelector(a); |
| 45 | + let value_b: any = valueSelector(b); |
46 | 46 |
|
47 | | - let type = typeof value_a; |
| 47 | + let type: string = typeof value_a; |
48 | 48 |
|
49 | | - if (type == "string") { |
| 49 | + if (type === "string") { |
50 | 50 | let value_a_string: string = value_a; |
51 | 51 | value_a_string = value_a_string.toLowerCase(); |
52 | 52 | let value_b_string: string = value_b; |
53 | 53 | value_b_string = value_b_string.toLowerCase(); |
54 | 54 |
|
55 | 55 | if (value_a_string > value_b_string) { |
56 | | - return invert == true ? -1 : 1; |
57 | | - } |
58 | | - else if (value_a_string < value_b_string) { |
59 | | - return invert == true ? 1 : -1; |
60 | | - } |
61 | | - else { |
| 56 | + return invert === true ? -1 : 1; |
| 57 | + } else if (value_a_string < value_b_string) { |
| 58 | + return invert === true ? 1 : -1; |
| 59 | + } else { |
62 | 60 | return 0; |
63 | 61 | } |
64 | 62 |
|
65 | | - } |
66 | | - else if (type == "number") { |
| 63 | + } else if (type === "number") { |
67 | 64 | let value_a_number: number = value_a; |
68 | 65 | let value_b_number: number = value_b; |
69 | 66 |
|
70 | | - return invert == true ? value_b_number - value_a_number : value_a_number - value_b_number; |
71 | | - } |
72 | | - else if (type == "boolean") { |
| 67 | + return invert === true ? value_b_number - value_a_number : value_a_number - value_b_number; |
| 68 | + } else if (type === "boolean") { |
73 | 69 | let value_a_bool: boolean = value_a; |
74 | 70 | let value_b_bool: boolean = value_b; |
75 | 71 |
|
76 | | - if (value_a_bool == value_b_bool) { |
| 72 | + if (value_a_bool === value_b_bool) { |
77 | 73 | return 0; |
78 | | - } |
79 | | - else { |
80 | | - if (invert == true) { |
| 74 | + } else { |
| 75 | + if (invert === true) { |
81 | 76 | return value_a_bool ? 1 : -1; |
82 | | - } |
83 | | - else { |
| 77 | + } else { |
84 | 78 | return value_a_bool ? -1 : 1; |
85 | 79 | } |
86 | 80 | } |
87 | | - } |
88 | | - else { |
89 | | - throw `Linq4JS: Cannot map type '${type}' for compare"`; |
| 81 | + } else { |
| 82 | + throw new Error(`Linq4JS: Cannot map type '${type}' for compare`); |
90 | 83 | } |
91 | 84 | } |
92 | 85 | } |
|
0 commit comments