-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_COMDict.ahk
411 lines (364 loc) · 11.9 KB
/
class_COMDict.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
/**
* Class: COMDict
* A classbased wrapper for working with com based dicts
* that mind the case of its keys
* Attributes:
* dict: the Scripting.Dictionary COM object
* Enum: Class for enumeration within for loops
* Methods:
* __New(keyvalArr := ""): COMDict
* Creates a new instace of the dictionary class
* setAnew(keyvalArr): Void
* sets the dict anew from the handed array
* invert(): COMDict
* returns a new COMDict instance with inverted key/value pairs
* add(key, value): Void
* adds a key value pair but will not update existing on
* item(key): key reference
* returns a specific reference to the given key and its value
* __Get(key): key reference
* convinience wrapper for the this.item(key) method
* CAN HAVE UNWANTED SIDEEFFECTS from not being called
* remove(key): Void
* removes a specific key from the dictionary
* updateKey(key, newKey): Void
* updates a key from the dictionary
* exists(key): Boolean
* checks whether given key exists within the dict
* HasKey(key): Boolean
* convinience wrapper for the this.exists(key) method
* count(): Integer
* retrieves the number of key/value pairs within the dict
* items(): Array
* retrieves all values present within the dictionary
* keys()
* retrieves all keys present within the dictionary
* items()
* removes all key/values pairs within the dictionary
* isCOMDict(subject)
* checks whether the handed subject is of class COMDict
* _setFromArr(keyvalArr)
* adds new key/value pairs from the handed array
* _NewEnum()
* enables enumerated though a for loop
* Remarks:
* this class uses the Scripting.Dictionary COM Object
*/
Class COMDict {
/**
* Method: __New(keyvalArr := "")
* Creates a new instace of the dictionary class
* Params:
* keyvalArr: a dictionary creation formatted array
* defined below the definition of this class
* Return:
* new dictionary instance
*/
__New(keyvalArr := ""){
this.dict := ComObjCreate("Scripting.Dictionary")
this._setFromArr(keyvalArr)
}
/**
* Method: setAnew(keyvalArr)
* sets the dict anew from the handed array
* Params:
* keyvalArr: a dictionary creation formatted array
* defined below the definition of this class
*/
setAnew(keyvalArr){
this.removeAll()
this._setFromArr(keyvalArr)
}
/**
* Method: invert()
* returns a new COMDict instance with inverted key/value pairs
* Return:
* the inverted COMDict instance
*/
invert(){
r := new COMDict()
for k in this.keys()
Try {
i := this.item(k)
r.add(i, k)
}
Catch, e
lol := ""
return, r
}
/**
* Method: add(key, value)
* adds a key value pair but will not update existing on
* Params:
* key: the key to be add
* value: the value to be associated with the key
*/
add(key, value){
this.dict.Add(key, value)
}
/**
* Method: item(key)
* returns a specific reference to the given key and its value
* equivalent to ahks object[key]
* Params:
* key: the key to be referenced
* Return:
* a reference to the key value pair
*/
item(key){
return, this.dict.Item(key)
}
/**
* Method: __Get(key)
* convinience wrapper for the this.item(key) method
* for reference, look at its documentation above
* Params:
* key: the key to be referenced
* Return:
* a reference to the key value pair
* Note:
* In case that any key within your map
* (or value in case you invert a map)
* is named like a method of this class, it would get cause unwanted sideeffects
*/
/*
__Get(key){
return, this.item(key)
}
*/
/**
* Method: remove(key)
* removes a specific key from the dictionary
* Params:
* key: the key to be removed
*/
remove(key){
this.dict.Remove(key)
}
/**
* Method: updateKey(key, newKey)
* updates a key from the dictionary
* Params:
* key: the key to be updated
* newKey: the replacement key to be set
*/
updateKey(key, newKey){
this.dict.Key(key) := newKey
}
/**
* Method: exists(key)
* checks whether given key exists within the dict
* Params:
* key: the key to be checked
* Return:
* boolean, true when key exists
*/
exists(key){
return, this.dict.Exists(key)
}
/**
* Method: HasKey(key)
* convinience wrapper for the this.exists(key) method
* for reference, look at its documentation above
* Params:
* key: the key to be checked
* Return:
* true if key exists within the dictionary
*/
HasKey(key){
return, this.exists(key)
}
/**
* Method: count()
* retrieves the number of key/value pairs within the dict
* Return:
* the number of key/value pairs
*/
count(){
return, this.dict.count()
}
/**
* Method: items()
* retrieves all values present within the dictionary
* Return:
* an standard array of all values
*/
items(){
return, this.dict.Items()
}
/**
* Method: keys()
* retrieves all keys present within the dictionary
* Return:
* an standard array of all keys
*/
keys(){
return, this.dict.Keys()
}
/**
* Method: items()
* removes all key/values pairs within the dictionary
* clears the hole dict
*/
removeAll(){
this.dict.RemoveAll()
}
/**
* Method: isCOMDict(subject)
* checks whether the handed subject is of class COMDict
* Params:
* subject: the subject to check
* Return:
* True if subject is of class COMDict
*/
isCOMDict(subject){
return, (!IsObject(subject))
? False
: subject.base.__Class == "COMDict"
}
/**
* Method: _setFromArr(keyvalArr)
* adds new key/value pairs from the handed array
* Params:
* keyvalArr: a dictionary creation formatted array
* defined below the definition of this class
*/
_setFromArr(keyvalArr){
if(!IsObject(keyvalArr))
return
for _, o in keyvalArr {
if(!(IsObject(o)
&& o.HasKey("key")
&& o.HasKey("val")))
continue
this.add(o.key, o.val)
}
}
/**
* Method: _NewEnum()
* enables enumerated though a for loop
* Params:
* Return:
* COMDict.Enum instance
* Note:
* Do not store this in a var and
* try to then use it for a for loop
* it does not work...
*/
_NewEnum(){
return, new COMDict.Enum(this)
}
/**
* Class: COMDict.Enum
* provides COMDict enumerated though a for loop
* Attributes:
* target: the COMDict instance to enumerate
* key: the keys yet to be enumerated
* Methods:
*/
Class Enum{
/**
* Method: __New(cd)
* creates a new instance of COMDict.Enum
* Params:
* cd: the COMDict instance to enumerate
* Return:
* The COMDict.Enum instance for a for loop
* Note:
* Do not store this in a var and
* try to then use it for a for loop
* it does not work...
*/
__New(cd){
if(!COMDict.isCOMDict(cd))
return, ""
this.target := cd
this.keys := []
for k in cd.keys()
this.keys.push(k)
}
/**
* Method: Next(ByRef k := "", ByRef v := "")
* sets k and v anew with each iteration of the for loop
* Params:
* k: the key to be set
* v: the value to be set
* Return:
* true if the loop should continue
*/
Next(ByRef k := "", ByRef v := ""){
if(!this.keys.count())
return, False
k := this.keys.RemoveAt(1)
v := this.target.item(k)
return, 1
}
}
}
/*
* The dictionary creation formatted array (short DCFA):
* The COMDict class uses a specially formatted standard ahk array
* for easy creation of dictionarys, the DCFA.
* The DCFA is a normal array containing associative arrays.
* These associative arrays have two keys, "key" and "value"
* and should be assigned with their values.
* Example:
dcfa := []
dcfa.Push({"key": "a", "val": "ᴀ"}
, {"key": "b", "val": "ʙ"}
, {"key": "c", "val": "ᴄ"}
, {"key": "d", "val": "ᴅ"}
, {"key": "e", "val": "ᴇ"}
, {"key": "f", "val": "ꜰ"}
, {"key": "g", "val": "ɢ"}
, {"key": "h", "val": "ʜ"}
, {"key": "i", "val": "ɪ"}
, {"key": "j", "val": "ᴊ"}
, {"key": "k", "val": "ᴋ"}
, {"key": "l", "val": "ʟ"}
, {"key": "m", "val": "ᴍ"}
, {"key": "n", "val": "ɴ"}
, {"key": "o", "val": "ᴏ"}
, {"key": "p", "val": "ᴘ"}
, {"key": "q", "val": "ǫ"}
, {"key": "r", "val": "ʀ"}
, {"key": "s", "val": "ꜱ"}
, {"key": "t", "val": "ᴛ"}
, {"key": "u", "val": "ᴜ"}
, {"key": "v", "val": "ᴠ"}
, {"key": "w", "val": "ᴡ"}
, {"key": "x", "val": "ⅹ"}
, {"key": "y", "val": "ʏ"}
, {"key": "z", "val": "ᴢ"})
dcfa.Push({"key": "A", "val": "ᴀ"}
, {"key": "B", "val": "ʙ"}
, {"key": "C", "val": "ᴄ"}
, {"key": "D", "val": "ᴅ"}
, {"key": "E", "val": "ᴇ"}
, {"key": "F", "val": "ꜰ"}
, {"key": "G", "val": "ɢ"}
, {"key": "H", "val": "ʜ"}
, {"key": "I", "val": "ɪ"}
, {"key": "J", "val": "ᴊ"}
, {"key": "K", "val": "ᴋ"}
, {"key": "L", "val": "ʟ"}
, {"key": "M", "val": "ᴍ"}
, {"key": "N", "val": "ɴ"}
, {"key": "O", "val": "ᴏ"}
, {"key": "P", "val": "ᴘ"}
, {"key": "Q", "val": "ǫ"}
, {"key": "R", "val": "ʀ"}
, {"key": "S", "val": "ꜱ"}
, {"key": "T", "val": "ᴛ"}
, {"key": "U", "val": "ᴜ"}
, {"key": "V", "val": "ᴠ"}
, {"key": "W", "val": "ᴡ"}
, {"key": "X", "val": "ⅹ"}
, {"key": "Y", "val": "ʏ"}
, {"key": "Z", "val": "ᴢ"})
SmallCapsDict := new COMDict(dcfa)
for k in SmallCapsDict.Keys()
test .= k " = " SmallCapsDict.item(k) "`n"
MsgBox, % test
return
*/