-
Notifications
You must be signed in to change notification settings - Fork 72
/
ObjectTools.ahk
517 lines (479 loc) · 11.1 KB
/
ObjectTools.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
;Example: zip([a,b,c],[d,e,f]) = [[a,d],[b,e],[c,f]]
zip(lists*)
{
zipped := []
Loop % min(map(lists,"objMaxIndex"))
zipped.Insert(get(lists, A_Index))
return zipped
}
;Applies a function to a list of values and returns all results of the function as a list.
map(list, function, args*)
{
results := []
for index, value in list
results.Insert(function.(value, args*))
return results
}
;returns all entries from "list" for which "function" returns true
filter(list,function,args*)
{
filtered := []
for index, value in list
if(function.(value, args*))
filtered.Insert(value)
return filtered
}
;Successively applies "function" to two elements from "list" and their results and returns the final result.
reduce(list, function, args*)
{
reduced := list.clone()
while(reduced.MaxIndex() > 1)
reduced.Insert(function.(reduced.Remove(), reduced.Remove(), args*))
return reduced
}
;combines a list to a string of values separated by a separator
join(list, separator = "`n")
{
result := ""
for index, value in list
result .= (A_Index != 1 ? separator : "") value
return result
}
;Splits a string into a list of strings using a separator
split(string, separator = "`n")
{
result := []
Loop, parse, string, %separator%
result.Insert(A_LoopField)
return result
}
;returns a subset of properties from objects in a list.
;Example: get([{x:1, y:2}, {x:3, y:4}], "x") = [1,3]
;Example: get([{x:1, y:2, z:3}, {x:4, y:5, z:6}], "x", "z") = [{x:1,z:3},{x:4,z:6}]
get(list, keys*)
{
values := []
if(keys.MaxIndex() > 1)
for index, value in list
{
result := []
for index2, key in keys
result[key] := value[key]
values.Insert(result)
}
else
for index, value in list
values.Insert(value[keys[1]])
return values.MaxIndex() ? values : ""
}
;Gets a subset of items from a list where listEntry[KeyOrValue] = value or listEntry = KeyOrValue
GetAll(list, KeyOrValue, value = "")
{
values := []
for index, VarOrObject in list
{
if(IsObject(VarOrObject))
{
if(VarOrObject[KeyOrValue] = value)
values.Insert(VarOrObject)
}
else
if(VarOrObject = KeyOrValue)
values.Insert(VarOrObject)
}
return values.MaxIndex() ? values : ""
}
;Minimum from a list of values
min(values*)
{
for index, value in values
if(A_Index = 1 || value < min)
min := value
return min
}
;Maximum from a list of values
max(values*)
{
for index, value in values
if(A_Index = 1 || value > max)
max := value
return max
}
;Example: reversed([a,b,c]) = [c,b,a]
reversed(list)
{
if(IsObject(list))
{
reversed := []
MaxIndex := list.MaxIndex()
Loop % MaxIndex
reversed.Insert(list[MaxIndex - A_Index + 1])
return reversed
}
else
{
reversed := ""
Loop, parse, list
reversed := A_LoopField reversed
return reversed
}
}
;Sorts a list of integers or strings. Does not support lists of objects
;Example: sorted([b,c,a]) = [a,b,c]
;Example: sorted([3,7,1]) = [1,3,7]
sorted(list, Callback = "")
{
if(IsObject(list))
{
if(IsObject(first := list[list.MinIndex()]))
return []
escapedStringList := join(map(list, "escape"), ",")
if first is number
Sort, escapedStringList, % "N D" (IsFunc(Callback) ? " F " Callback : "")
else
Sort, escapedStringList, % "N" (IsFunc(Callback) ? " F " Callback : "")
return split(unescape(escapedStringList), ",")
}
}
;Sorts an array by one of the members keys
ArraySort(object, key, order = "Down")
{
static obj, k, o
;Called by user
if(order = "Up" || order = "Down")
{
obj := object
k := key
o := order
SortString := ""
Loop % obj.MaxIndex()
SortString .= (A_Index = 1 ? "" : "`n") A_Index
Sort, SortString, % "F ArraySort"
obj := ""
k := ""
o := ""
sorted := Array()
Loop, Parse, SortString, `n
sorted.Insert(object[A_LoopField])
return sorted
}
else ;Called by Sort command
{
if(obj[object][k] != obj[key][k])
return (o = "Down" && obj[object][k] < obj[key][k]) || (o = "Up" && obj[object][k] > obj[key][k]) ? 1 : -1
else
return 0
}
}
;Escapes all occurences of "separator" in "string" with "EscapeChar". This allows to use "separator" as a separator for string lists
escape(string, escapechar = "\", separator = ",")
{
StringReplace, string, string, escapechar, escapechar escapechar, 1
StringReplace, string, string, separator, escapechar separator, 1
return string
}
;Unescapes all occurences of "separator" which are escaped by "escapechar" (and "escapechar" itself)
unescape(string, escapechar = "\", separator = ",")
{
StringReplace, string, string, escapechar escapechar, Chr(1), 1
StringReplace, string, string, escapechar separator, separator, 1
StringReplace, string, string, Chr(1), escapechar, 1
return string
}
;Example: range(10,90,30) = [10, 40, 70]
range(start, end, step = 1)
{
index := start
range := []
;Wrong step direction -> infinite results -> return empty list
if(step > 0 && end < start || step < 0 && end > start)
return []
while(step > 0 && index <= end || step < 0 && index >= end)
{
range.Insert(index)
index += step
}
return range
}
;Example: unique([a,b,a,c,b]) = [a,b,c]
;Example: unique("abadcac") = "abdc"
unique(list)
{
if(IsObject(list))
{
unique := []
for key1, value1 in list
{
found := false
for key2, value2 in list
if(key1 != key2 && value1 = value2)
{
found := true
break
}
if(!found)
unique[key1] := value1
}
return unique
}
else
{
unique := ""
Loop, parse, list
{
index := A_Index
char1 := A_LoopField
found := false
Loop, parse, list
if(index != A_Index && char1 = A_LoopField)
{
found := true
break
}
if(!found)
unique .= char1
}
return unique
}
}
;Example: slice([1,2,3,4], 2, 4) = [2,3]
;Example: slice([1,2,3,4], "", 4) = [1,2,3]
;Example: slice([1,2,3,4], 2, "") = [2,3,4]
;Example: slice([1,2,3,4], -2, "") = [3,4]
;Example: slice([1,2,3,4], 2, -1) = [2,3]
;Example: slice([1,2,3,4], 2, 20) = [2,3,4]
;Example: slice("12345", -4, -2) = "23"
slice(list, start = "", end = "")
{
if(IsObject(list))
{
sliced := []
if(!start)
start := 1
if(!end)
end := list.MaxIndex() + 1
if(start < 0)
start := list.MaxIndex() + start + 1
if(end < 0)
end := list.MaxIndex() + end + 1
Loop % end - start
sliced.Insert(list[A_Index + start - 1])
return sliced
}
else
{
sliced := ""
if(!start)
start := 1
len := StrLen(list)
if(!end)
end := len + 1
if(start < 0)
start := len + start + 1
if(end < 0)
end := len + end + 1
return SubStr(list, start, end - start)
}
}
;Example: repeat([1,2],3) = [1,2,1,2,1,2]
;Example: repeat("hello", 3) = "hellohellohello"
repeat(list, times)
{
if(IsObject(list))
{
repeated := []
Loop %times%
repeated := extend(repeated,repeated)
return repeated
}
else
{
repeated := ""
Loop %times%
repeated .= list
return repeated
}
}
;Example: extend([1,2],[3,4]) = [1,2,3,4]
extend(list, list2)
{
extended := list.Clone()
Loop % list2.MaxIndex()
extended.Insert(list2[A_Index])
return extended
}
;Checks if an object contains subitems which have a specific key-value pair or a specific value
Any(Object, KeyOrValue, value = "")
{
for k,v in Object
if(IsObject(v))
{
if(v[KeyOrValue] = value)
return true
}
else
{
if(v = KeyOrValue)
return true
}
return false
}
;Counts the subitems of an object which have a specific key-value pair or a specific value
Count(Object, KeyOrValue, value = "")
{
count := 0
for k,v in Object
if(IsObject(v))
{
if(v[KeyOrValue] = value)
count++
}
else
{
if(v = KeyOrValue)
count++
}
return count
}
;Class Array
;{
; __new(items*)
; {
; for index, item in items
; this.Insert(item)
; }
; static Any := func("Any")
; static Count := func("Count")
; append(items*)
; {
; for index, item in items
; this.Insert(item)
; }
; insertRange(pos, items*)
; {
; for index, item in items
; this.Insert(pos + A_Index - 1, item)
; }
; clear()
; {
; Loop % this.MaxIndex()
; this.remove()
; }
; ;Removes the first occurence of an item in this list. Returns the item itself.
; removeItem(item)
; {
; for index, element in this
; if(item = element)
; {
; this.remove(index)
; return item
; }
; }
; ;Removes all occurences of an item in this list. Returns the item itself.
; removeAll(item)
; {
; pos := 1
; Loop % this.MaxIndex()
; {
; if(this[pos] = item)
; this.remove(pos)
; else
; pos++
; }
; return item
; }
; ;Functions for retrieving items/indices:
; indexOf(element)
; {
; for index, item in this
; if(item = element)
; return index
; }
; indexOfEquals(element)
; {
; for index, item in this
; if(item = element || IsFunc(item.equals) && item.equals(element))
; return index
; }
; ;Finds an item of an array which has member variables specified by "conditions"
; ;"conditions" is of the form [key1, value1, key2, value2, ...]
; ;Example to get the key of an object in the list representing the active window from a list of window objects: WindowObjects.findIndex("hwnd", WinExist("A"))
; findIndex(conditions*)
; {
; for index, item in this
; {
; fulfilled := true
; Loop % conditions.MaxIndex() / 2
; if(item[conditions[(A_Index - 1) * 2 + 1]] != conditions[A_Index * 2])
; {
; fulfilled := false
; break
; }
; if(fulfilled)
; return index
; }
; }
; ;Finds all items of an array which have member variables specified by "conditions"
; ;"conditions" is of the form [key1, value1, key2, value2, ...]
; ;Example to get the keys of all objects in the list representing notepad windows from a list of window objects: WindowObjects.findAllIndices("class", "Notepad")
; findAllIndices(conditions*)
; {
; findings := []
; for index, item in this
; {
; fulfilled := true
; Loop % conditions.MaxIndex() / 2
; if(item[conditions[(A_Index - 1) * 2 + 1]] != conditions[A_Index * 2])
; {
; fulfilled := false
; break
; }
; if(fulfilled)
; findings.Insert(index)
; }
; return findings
; }
; ;Finds an item of an array which has member variables specified by "conditions"
; ;"conditions" is of the form [key1, value1, key2, value2, ...]
; ;Example to get the object representing the active window from a list of window objects: WindowObjects.findItem("hwnd", WinExist("A"))
; findItem(conditions*)
; {
; for index, item in this
; {
; fulfilled := true
; Loop % conditions.MaxIndex() / 2
; if(item[conditions[(A_Index - 1) * 2 + 1]] != conditions[A_Index * 2])
; {
; fulfilled := false
; break
; }
; if(fulfilled)
; return item
; }
; }
; ;Finds all items of an array which have member variables specified by "conditions"
; ;"conditions" is of the form [key1, value1, key2, value2, ...]
; ;Example to get all objects representing notepad windows from a list of window objects: WindowObjects.findAllItems("class", "Notepad")
; findAllItems(conditions*)
; {
; findings := []
; for index, item in this
; {
; fulfilled := true
; Loop % conditions.MaxIndex() / 2
; if(item[conditions[(A_Index - 1) * 2 + 1]] != conditions[A_Index * 2])
; {
; fulfilled := false
; break
; }
; if(fulfilled)
; findings.Insert(item)
; }
; return findings
; }
; ;See <slice()>
; slice(start = "", end = "")
; {
; return slice(this, start, end)
; }
;}