Skip to content

Commit a3c47b8

Browse files
committed
oops
2 parents 00d95e9 + cc4ab8a commit a3c47b8

10 files changed

+372
-4
lines changed

README.md

+61
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,37 @@
33

44
Despite AutoHotKey (1.1.28 at the moment) supporting Arrays and Function Objects, there are no handy built-in methods for parsing Array data. This project ports most of JavaScript's Array object methods over to AutoHotKey.
55

6+
<<<<<<< HEAD
67
### Ported Methods
8+
=======
9+
AHK supports [objects](https://autohotkey.com/docs/objects/Object.htm), [arrays](https://autohotkey.com/docs/Objects.htm#Table_of_Contents), and [function objects](https://autohotkey.com/docs/objects/Functor.htm). But it lacks built in methods for functional operation on arrays, like `Array.map()`. JavaScript's Array object has some handy methods; so I ported them over.
10+
11+
Since AHK is also prototypical in nature, these could have been added to a base object. For simplicity's sake these are global functions. All methods are prefixed with `array_`.
12+
13+
### Usage
14+
15+
Just include the `array_.ahk` file and pass functors as arguments when needed. For instance, to double all values in an array:
16+
17+
array := [1,2,3,4]
18+
result := array_map(array, func("double"))
19+
double(item) {
20+
return item * 2
21+
}
22+
msgbox % array_toString(result) ;outputs "[2,4,6,8]"
23+
24+
or getting fancy with a partial:
25+
26+
array := [12,3,44,9]
27+
result := array_map(array, func("multiply").bind(2))
28+
multiply(a, b) {
29+
return a * b
30+
}
31+
msgbox % array_toString(result) ;outputs "[24,6,88,18]"
32+
33+
34+
### Stolen Methods
35+
36+
>>>>>>> origin/master
737
* concat
838
* every
939
* fill
@@ -27,7 +57,10 @@ Despite AutoHotKey (1.1.28 at the moment) supporting Arrays and Function Objects
2757
* toString
2858
* unshift
2959

60+
### Sort?
61+
Considered implementing a `array_sort` quicksort; but currently no immediate need.
3062

63+
<<<<<<< HEAD
3164
### Installation
3265
There are two options for using importing these functions, either as global functions stored in **array_.ahk** or by extending the built-in Array object stored in **array_base.ahk**. The sorting function in both files depends on the object defined in **array_quicksort.ahk**. This object must be present for sorting to work properly.
3366

@@ -122,3 +155,31 @@ Class Array_QuickSort {
122155
A quick and dirty test suite was crafted to make sure these were functioning properly. Each function/method has a test case in the **/tests/** directory. The test runner UI has two tabs representing each library's test results:
123156

124157
![Tester Results Screenshot](tester/ui_preview.png)
158+
=======
159+
### Tests
160+
Nothing is truely complete without testing, so each `array_<method>` has a test case file in the `tests/` directory.
161+
162+
Built a few primitive [test classes](test_suite/tester.ahk) to collect test cases. Management of this test class is located in [testrunner.ahk](testrunner.ahk). Results are output to an edit control in a new window.
163+
164+
Sample of [test runner results](testrunner_results.txt):
165+
166+
01/22/17 - 19:23:53
167+
-Conversion of JavaScript's Array methods to AutoHotkey-
168+
169+
array_some(array, callback)
170+
[PASS] Detect one even number
171+
[PASS] Detect one odd number
172+
[PASS] Fails to find large enough number
173+
174+
array_splice(ByRef array, start, deleteCount:=-1, args*)
175+
[PASS] Starting position only: array
176+
[PASS] Starting position only: spliced
177+
[PASS] Starting position with delete: array
178+
[PASS] Starting position with delete: spliced
179+
[PASS] Starting position no delete: array
180+
[PASS] Starting position no delete: spliced
181+
[PASS] Starting position with delete and args: array
182+
[PASS] Starting position with delete and args: spliced
183+
[PASS] Starting position no delete and args: array
184+
[PASS] Starting position no delete and args: spliced
185+
>>>>>>> origin/master

array_.ahk

+63-4
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ array_fill(array, value, start:=0, end:=0) {
4747

4848
loop, % (last - begin)
4949
array[begin + A_Index] := value
50+
<<<<<<< HEAD
5051

5152
return array
53+
=======
54+
>>>>>>> origin/master
5255
}
5356

5457

@@ -167,7 +170,11 @@ array_lastIndexOf(array, searchElement, fromIndex:=0) {
167170

168171
; https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
169172
array_map(array, callback) {
173+
<<<<<<< HEAD
170174
global
175+
=======
176+
177+
>>>>>>> origin/master
171178
results := []
172179

173180
for index, element in array
@@ -181,10 +188,18 @@ array_map(array, callback) {
181188
; -->[A,B,C,D,E,F]
182189
array_reduce(array, callback, initialValue:="__NULL__") {
183190

191+
<<<<<<< HEAD
184192
arrLen := array.Length()
185193

186194
; initialValue not defined
187195
if (initialValue == "__NULL__") {
196+
=======
197+
idxOffset := 0
198+
arrLen := array.Length()
199+
200+
; No initialValue passed
201+
if (initialValue == "IAMNULL") {
202+
>>>>>>> origin/master
188203

189204
if (arrLen < 1) {
190205
; Empty array with no intial value
@@ -194,6 +209,7 @@ array_reduce(array, callback, initialValue:="__NULL__") {
194209
; Single item array with no initial value
195210
return array[1]
196211
}
212+
<<<<<<< HEAD
197213

198214
; Starting value is last element
199215
initialValue := array[1]
@@ -203,6 +219,12 @@ array_reduce(array, callback, initialValue:="__NULL__") {
203219

204220
; Set index A_Index+1 each iteration
205221
idxOffset := 1
222+
=======
223+
else {
224+
idxOffset := 1
225+
initialValue := array[1]
226+
}
227+
>>>>>>> origin/master
206228

207229
} else {
208230
; initialValue defined
@@ -219,10 +241,17 @@ array_reduce(array, callback, initialValue:="__NULL__") {
219241
idxOffset := 0
220242
}
221243

244+
<<<<<<< HEAD
222245
; if no initialValue is passed, use first index as starting value and reduce
223246
; array starting at index n+1. Otherwise, use initialValue as staring value
224247
; and start at arrays first index.
225248
Loop, % iterations
249+
=======
250+
; if no initialValue is passed, reduce passed array starting at second
251+
; index. Otherwise use initialValue as reduction target and start at array's
252+
; first index.
253+
Loop, % (arrLen - idxOffset)
254+
>>>>>>> origin/master
226255
{
227256
adjIndex := A_Index + idxOffset
228257
initialValue := callback.Call(initialValue, array[adjIndex], adjIndex, array)
@@ -238,6 +267,7 @@ array_reduceRight(array, callback, initialValue:="__NULL__") {
238267

239268
arrLen := array.Length()
240269

270+
<<<<<<< HEAD
241271
; initialValue not defined
242272
if (initialValue == "__NULL__") {
243273

@@ -247,16 +277,34 @@ array_reduceRight(array, callback, initialValue:="__NULL__") {
247277
}
248278
else if (arrLen == 1) {
249279
; Single item array with no initial value
280+
=======
281+
; No initialValue passed
282+
if (initialValue == "IAMNULL") {
283+
284+
if (arrLen < 1)
285+
throw "Empty array with no intial value, derp"
286+
else if (arrLen == 1)
287+
>>>>>>> origin/master
250288
return array[1]
251289
}
252290

291+
<<<<<<< HEAD
253292
; Starting value is last element
254293
initialValue := array[arrLen]
255294

256295
; Loop n-1 times (starting at n-1 element)
257296
iterations := arrLen - 1
258297

259298
; Set index A_Index-1 each iteration
299+
=======
300+
; Start at end
301+
initialValue := array[arrLen]
302+
303+
; Loop n-1 times (start at n-1 element)
304+
iterations := arrLen - 1
305+
306+
; Start at n-1 element (Keep 1 based index notation)
307+
>>>>>>> origin/master
260308
idxOffset := 0
261309

262310
} else {
@@ -269,13 +317,20 @@ array_reduceRight(array, callback, initialValue:="__NULL__") {
269317
; Loop n times (start at n element)
270318
iterations := arrLen
271319

320+
<<<<<<< HEAD
272321
; Set index A_Index each iteration
273322
idxOffset := 1
274323
}
275324

276325
; If no initialValue is passed, use last index as starting value and reduce
277326
; array starting at index n-1. Otherwise, use initialValue as starting value
278327
; and start at arrays last index.
328+
=======
329+
; Start at n element (Adjust to 0 based index notation)
330+
idxOffset := 1
331+
}
332+
333+
>>>>>>> origin/master
279334
Loop, % iterations
280335
{
281336
adjIndex := arrLen - (A_Index - idxOffset)
@@ -359,8 +414,13 @@ array_some(array, callback) {
359414
; https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
360415
array_sort(array, compare_fn:=0) {
361416

417+
<<<<<<< HEAD
362418
; Quicksort
363419
Array_QuickSort.Call(array, compare_fn)
420+
=======
421+
;Quicksort
422+
throw "Not implemented"
423+
>>>>>>> origin/master
364424

365425
return array
366426
}
@@ -407,12 +467,11 @@ array_splice(ByRef array, start, deleteCount:=-1, args*) {
407467
; https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString
408468
array_toString(array) {
409469

410-
str := "["
411-
470+
str := ""
412471
for i,v in array
413-
str .= v (i < array.Length() ? ", " : "")
472+
str .= v (i < array.Length() ? "," : "")
414473

415-
return str "]"
474+
return "[" str "]"
416475
}
417476

418477

test_suite/example.png

27.3 KB
Loading

test_suite/tester.ahk

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class TestRunner {
2+
3+
groups := []
4+
5+
__New(title) {
6+
this.title := title
7+
}
8+
9+
newGroup(title) {
10+
11+
tests := new TestGroup(title)
12+
this.groups.push(tests)
13+
14+
return tests
15+
}
16+
17+
getAllTestResults() {
18+
19+
FormatTime, CurrentDateTime,, MM/dd/yy - HH:mm:ss
20+
results := [CurrentDateTime, this.title, ""]
21+
22+
for i, tests in this.groups
23+
results.push(tests.getTestResults())
24+
25+
return array_join(results, "`n")
26+
}
27+
}
28+
29+
30+
class TestGroup {
31+
32+
tests := []
33+
34+
__New(title) {
35+
this.title := title
36+
}
37+
38+
newTest(desc, result) {
39+
40+
this.tests.push(new Test(desc, result))
41+
}
42+
43+
getTestResults() {
44+
45+
results := [this.title]
46+
47+
for i, test in this.tests
48+
results.push(test.getResult())
49+
50+
return array_join(results, "`n") "`n"
51+
}
52+
}
53+
54+
class Test {
55+
56+
__New(desc, result) {
57+
this.desc := desc
58+
this.result := result
59+
}
60+
61+
getResult() {
62+
63+
return "[" (this.result ? "PASS" : "----") "] " this.desc
64+
}
65+
}

testrunner.ahk

+6
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ tester := new TestRunner("-explicit array_<fn> functions from 'array_.ahk'-")
3737
#Include, array_some.test.ahk
3838
#Include, array_sort.test.ahk
3939
#Include, array_splice.test.ahk
40+
#Include, array_toString.test.ahk
4041
#Include, array_unshift.test.ahk
4142

43+
<<<<<<< HEAD
4244
; Setup 'array_' results
4345
Gui, Tab, array_
4446
gui, add, edit, r30 w375, % tester.getAllTestResults()
@@ -83,6 +85,10 @@ gui, add, edit, r30 w375, % tester.getAllTestResults()
8385

8486

8587
; Show Gui
88+
=======
89+
; Show results
90+
gui, add, edit, r30, % tester.getAllTestResults()
91+
>>>>>>> origin/master
8692
gui, show
8793

8894
return

0 commit comments

Comments
 (0)