-
Notifications
You must be signed in to change notification settings - Fork 10
/
forEachRight.ahk
55 lines (44 loc) · 1.43 KB
/
forEachRight.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
forEachRight(param_collection,param_iteratee:="__identity") {
if (!isObject(param_collection)) {
this._internal_ThrowException()
}
; prepare
shorthand := this._internal_detectShorthand(param_iteratee, param_collection)
if (shorthand != false) {
param_iteratee := this._internal_createShorthandfn(param_iteratee, param_collection)
}
collectionClone := this.reverse(this.cloneDeep(param_collection))
; create
; run against every value in the collection
for key, value in collectionClone {
if (this.isFunction(param_iteratee)) {
vIteratee := param_iteratee.call(value, key, collectionClone)
}
; exit iteration early by explicitly returning false
if (vIteratee == false) {
return collectionClone
}
}
return param_collection
}
; tests
; omit
assert.label("order")
obj := []
A.forEachRight([1, 2], func("fn_forEachRightFuncGlobal"))
assert.test(obj, [3, 2])
fn_forEachRightFuncGlobal(value)
{
global
obj.push(value + 1)
}
assert.label("alias")
assert.test(A.eachRight([1, 2], func("fn_forEachRightFuncGlobal")), [1, 2])
assert.label("default .identity argument")
assert.test(A.forEachRight([1, 2], func("fn_forEachRightFunc")), [1, 2])
assert.label("with own method")
assert.test(A.forEach(["apple", "banana", "cherry"], A.size()), ["apple", "banana", "cherry"])
assert.label("no mutation")
arr := ["apple", "banana", "cherry"]
A.forEach(["apple", "banana", "cherry"], A.size())
assert.test(arr, ["apple", "banana", "cherry"])