Conversion of JavaScript's Array methods to AutoHotkey
AutoHotkey lacks built-in iteration helper methods to perform many of the common array behaviors found in other languages. This package ports most of JavaScript's Array object methods to AutoHotkey's Array object. Because Javascript is zero-based and AutoHotkey is not, there are some slight differences.
- concat
- every
- fill
- filter
- find
- findIndex
- forEach
- includes
- indexOf
- join
- lastIndexOf
- map
- reduce
- reduceRight
- reverse
- shift
- slice
- some
- sort
- splice
- toString
- unshift
- values
In a terminal or command line navigated to your project folder:
npm install array.ahk
You may also review or copy the library from ./export.ahk on GitHub
In your code only export.ahk needs to be included:
#Include %A_ScriptDir%\node_modules
#Include array.ahk\export.ahk
msgbox, % [1,2,3].join()
; => "1,2,3"
You may also review or copy the library from ./export.ahk on GitHub; #Incude as you would normally when manually downloading.
The package modifies the Array object when #Included near the top of your script.
Array.<fn>([params*])
; Map to doubled value
arrayInt := [1, 5, 10]
arrayInt.map(func("fn_doubleInt"))
; => [2, 10, 20]
fn_doubleInt(int) {
return int * 2
}
; Map to object property
arrayObj := [{"name": "bob", "age": 22}, {"name": "tom", "age": 51}]
arrayObj.map(func("fn_returnName"))
; => ["bob", "tom"]
fn_returnName(obj) {
return obj.name
}
; Method chaining
arrayObj := [{"name": "bob", "age": 22}, {"name": "tom", "age": 51}]
msgbox, % arrayObj.map(func("fn_returnProp").bind("age"))
.map(func("fn_doubleInt"))
.join(",")
; => "44,102"
fn_returnProp(prop, obj) {
return obj[prop]
}
JavaScript does not use start/end or left/right parameters, and this implementation follows the same approach.
array.sort([params*])
[11,9,5,10,1,6,3,4,7,8,2].sort()
; => [1,2,3,4,5,6,7,8,9,10,11]
[11,9,5,10,1,6,3,4,7,8,2].sort(func("fn_reverse"))
fn_reverse(a, b) {
return b - a
}
; => [11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1])