|
1 | 1 |
|
| 2 | +namespace switchcase { |
| 3 | + export class SwitchContext { |
| 4 | + private Cases: { Match: any, Handler: () => void, IsDefault?: boolean }[] = []; |
| 5 | + private Value: any; |
| 6 | + public IsValueSet: boolean; |
| 7 | + |
| 8 | + addCase(match: any, handler: () => void): void { |
| 9 | + this.Cases.push({ Match: match, Handler: handler }); |
| 10 | + } |
2 | 11 |
|
3 | | -export class SwitchContext { |
4 | | - private Cases: { Match: any, Handler: () => void, IsDefault?: boolean }[] = []; |
5 | | - |
6 | | - addCase(match: any, handler: () => void): void { |
7 | | - this.Cases.push({ Match: match, Handler: handler }); |
8 | | - } |
| 12 | + addDefault(handler: () => void): void { |
| 13 | + this.Cases.push({ Match: null, Handler: handler, IsDefault: true }); |
| 14 | + } |
9 | 15 |
|
10 | | - addDefault(handler: () => void): void { |
11 | | - this.Cases.push({ Match: null, Handler: handler, IsDefault: true }); |
12 | | - } |
| 16 | + execute(): void { |
| 17 | + this.executeFromValue(this.Value); |
| 18 | + } |
13 | 19 |
|
14 | | - execute(value: any): void { |
15 | | - let matched = false; |
16 | | - for (let cb of this.Cases) { |
17 | | - if (cb.Match === value || cb.Match == value) { |
18 | | - cb.Handler(); |
19 | | - matched = true; |
20 | | - break; |
| 20 | + executeFromValue(value: any): void { |
| 21 | + let matched = false; |
| 22 | + for (let cb of this.Cases) { |
| 23 | + if (cb.Match === value || cb.Match == value) { |
| 24 | + cb.Handler(); |
| 25 | + matched = true; |
| 26 | + break; |
| 27 | + } |
21 | 28 | } |
| 29 | + if (!matched) { |
| 30 | + let defaultCase = this.Cases.find(cb => cb.IsDefault); |
| 31 | + if (defaultCase) defaultCase.Handler(); |
| 32 | + } |
| 33 | + //for now, let's keep the cases present, and instead of clearing them (for hygene), |
| 34 | + // let's instead go with a route that only sets them up the first time, and every other |
| 35 | + // time, instead of adding a case, the user would simply detect it already exists |
| 36 | + // and use what's there |
| 37 | + //this.Cases = []; // clear after execution |
22 | 38 | } |
23 | | - if (!matched) { |
24 | | - let defaultCase = this.Cases.find(cb => cb.IsDefault); |
25 | | - if (defaultCase) defaultCase.Handler(); |
| 39 | + |
| 40 | + setValue(value: any): void { |
| 41 | + this.Value = value; |
| 42 | + if (this.Value) |
| 43 | + this.IsValueSet = this.Value; |
| 44 | + else |
| 45 | + this.IsValueSet = false; |
26 | 46 | } |
27 | | - //for now, let's keep the cases present, and instead of clearing them (for hygene), |
28 | | - // let's instead go with a route that only sets them up the first time, and every other |
29 | | - // time, instead of adding a case, the user would simply detect it already exists |
30 | | - // and use what's there |
31 | | - //this.Cases = []; // clear after execution |
32 | | - } |
33 | 47 |
|
34 | | - clearCases(): void { |
35 | | - this.Cases = []; |
36 | | - } |
| 48 | + matches(match: any): boolean { |
| 49 | + return (this.Value == match || this.Value == match); |
| 50 | + } |
37 | 51 |
|
38 | | - /** |
39 | | - * Remove Case (Non-Default Case) |
40 | | - * and return both the success of removal and the removed case. |
41 | | - * |
42 | | - * Parameters: |
43 | | - * @param {any} match |
44 | | - * This is the match property found on your case. For DefaultCase, please see: {@look SwitchContext#removeDefaultCase} |
45 | | - * @returns: |
46 | | - * { removalSuccess: boolean, removed: { Match: any, Handler: () => void, IsDefault?: boolean } } |
47 | | - */ |
48 | | - removeCase(match: any): { RemovalSuccess: boolean, Removed: { Match: any, Handler: () => void, IsDefault?: boolean } } { |
| 52 | + clearCases(): void { |
| 53 | + this.Cases = []; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Remove Case (Non-Default Case) |
| 58 | + * and return both the success of removal and the removed case. |
| 59 | + * |
| 60 | + * Parameters: |
| 61 | + * @param {any} match |
| 62 | + * This is the match property found on your case. For DefaultCase, please see: {@look SwitchContext#removeDefaultCase} |
| 63 | + * @returns: |
| 64 | + * { removalSuccess: boolean, removed: { Match: any, Handler: () => void, IsDefault?: boolean } } |
| 65 | + */ |
| 66 | + removeCase(match: any): { RemovalSuccess: boolean, Removed: { Match: any, Handler: () => void, IsDefault?: boolean } } { |
49 | 67 |
|
50 | | - /* |
51 | | - let toRemove: { Match: any, Handler: () => void, IsDefault?: boolean }; |
52 | | - let matched = false; |
53 | | - |
54 | | - for (let cb in this.Cases) { |
55 | | - if (cb.Match === match || cb.Match == match) { |
56 | | - toRemove = cb; |
57 | | - matched = true; |
58 | | - break; |
| 68 | + /* |
| 69 | + let toRemove: { Match: any, Handler: () => void, IsDefault?: boolean }; |
| 70 | + let matched = false; |
| 71 | + |
| 72 | + for (let cb in this.Cases) { |
| 73 | + if (cb.Match === match || cb.Match == match) { |
| 74 | + toRemove = cb; |
| 75 | + matched = true; |
| 76 | + break; |
| 77 | + } |
59 | 78 | } |
60 | | - } |
61 | | - */ |
| 79 | + */ |
62 | 80 |
|
63 | | - let toRemove: { Match: any, Handler: () => void, IsDefault?: boolean }; |
64 | | - let removeSuccess = false; |
| 81 | + let toRemove: { Match: any, Handler: () => void, IsDefault?: boolean }; |
| 82 | + let removeSuccess = false; |
65 | 83 |
|
66 | | - toRemove = |
67 | | - this.Cases.find(cb => cb.Match === match || cb.Match == match); |
| 84 | + toRemove = |
| 85 | + this.Cases.find(cb => cb.Match === match || cb.Match == match); |
68 | 86 |
|
69 | | - if (toRemove) removeSuccess = this.Cases.removeElement(toRemove); |
70 | | - //if (removeSuccess) return toRemove; |
| 87 | + if (toRemove) removeSuccess = this.Cases.removeElement(toRemove); |
| 88 | + //if (removeSuccess) return toRemove; |
71 | 89 |
|
72 | | - return { RemovalSuccess: removeSuccess, Removed: toRemove }; |
73 | | - |
74 | | - /* |
75 | | - if(match.IsDefault) |
76 | | -
|
77 | | - if (match.IsDefault) { |
78 | | - toRemove = this.Cases.find(cb => cb.IsDefault); |
79 | | - if (toRemove) { |
80 | | - if (this.Cases.removeElement(defaultCase)) return defaultCase; |
| 90 | + return { RemovalSuccess: removeSuccess, Removed: toRemove }; |
| 91 | + |
| 92 | + /* |
| 93 | + if(match.IsDefault) |
| 94 | + |
| 95 | + if (match.IsDefault) { |
| 96 | + toRemove = this.Cases.find(cb => cb.IsDefault); |
| 97 | + if (toRemove) { |
| 98 | + if (this.Cases.removeElement(defaultCase)) return defaultCase; |
| 99 | + } |
81 | 100 | } |
82 | | - } |
83 | | - */ |
| 101 | + */ |
84 | 102 |
|
85 | | - } |
| 103 | + } |
86 | 104 |
|
87 | | - /** |
88 | | - * Remove Default Case |
89 | | - * and return both the success of removal and the removed case |
90 | | - * To remove a specific NON-Default case, please see: {@look SwitchContext#removeCase} |
91 | | - * |
92 | | - * Parameters: |
93 | | - * NONE |
94 | | - * Returns: |
95 | | - * @returns: |
96 | | - * { RemovalSuccess: boolean, Removed: { Match: any, Handler: () => void, IsDefault?: boolean } } |
97 | | - * The success of finding and removing the defaultCaseBlock AND the defaultCaseBlock that was removed. |
98 | | - */ |
99 | | - removeDefaultCase(): |
100 | | - { |
| 105 | + /** |
| 106 | + * Remove Default Case |
| 107 | + * and return both the success of removal and the removed case |
| 108 | + * To remove a specific NON-Default case, please see: {@see SwitchContext#removeCase} |
| 109 | + * |
| 110 | + * Parameters: |
| 111 | + * NONE |
| 112 | + * Returns: |
| 113 | + * @returns: |
| 114 | + * { RemovalSuccess: boolean, Removed: { Match: any, Handler: () => void, IsDefault?: boolean } } |
| 115 | + * The success of finding and removing the defaultCaseBlock AND the defaultCaseBlock that was removed. |
| 116 | + */ |
| 117 | + removeDefaultCase(): { |
101 | 118 | RemovalSuccess: boolean |
102 | 119 | , Removed: { Match: any, Handler: () => void, IsDefault?: boolean } |
103 | 120 | } { |
104 | 121 |
|
105 | 122 | let defaultCaseBlock: { Match: any, Handler: () => void, IsDefault?: boolean }; |
106 | 123 | defaultCaseBlock = this.Cases.find(dcb => dcb.IsDefault); |
107 | 124 |
|
108 | | - let returnValue: { RemovalSuccess: boolean, Removed: { Match: any, Handler: () => void, IsDefault?: boolean } }; |
109 | | - if (defaultCaseBlock) { |
110 | | - returnValue.RemovalSuccess = true; |
111 | | - returnValue.Removed == defaultCaseBlock; |
112 | | - } |
113 | | - else { |
114 | | - returnValue.RemovalSuccess = false; |
115 | | - returnValue.Removed = defaultCaseBlock |
116 | | - } |
| 125 | + let returnValue: { RemovalSuccess: boolean, Removed: { Match: any, Handler: () => void, IsDefault?: boolean } }; |
| 126 | + if (defaultCaseBlock) { |
| 127 | + returnValue.RemovalSuccess = true; |
| 128 | + returnValue.Removed == defaultCaseBlock; |
| 129 | + } |
| 130 | + else { |
| 131 | + returnValue.RemovalSuccess = false; |
| 132 | + returnValue.Removed = defaultCaseBlock |
| 133 | + } |
117 | 134 |
|
118 | | - return returnValue; |
119 | | - } |
| 135 | + return returnValue; |
| 136 | + } |
120 | 137 |
|
121 | | - caseCount(): number { |
122 | | - return this.Cases.length; |
123 | | - } |
124 | | - /** |
125 | | - * class SwitchContext { |
126 | | - * [private] Cases { Match: any, handler: () => void, IsDefault?: boolean }[] |
127 | | - * addCase(match: any, handler: () => void): void; |
128 | | - * addDefault(handler: () => void): void; |
129 | | - * execute(value: any): void; |
130 | | - * caseCount(): number; |
131 | | - * } |
132 | | - */ |
| 138 | + caseCount(): number { |
| 139 | + return this.Cases.length; |
| 140 | + } |
| 141 | + /** |
| 142 | + * class SwitchContext { |
| 143 | + * [private] Cases { Match: any, handler: () => void, IsDefault?: boolean }[] |
| 144 | + * addCase(match: any, handler: () => void): void; |
| 145 | + * addDefault(handler: () => void): void; |
| 146 | + * execute(value: any): void; |
| 147 | + * caseCount(): number; |
| 148 | + * } |
| 149 | + */ |
133 | 150 | } |
134 | | - |
| 151 | +} |
135 | 152 |
|
0 commit comments