Used this today for something I thought was cool. My app has a large array of objects and each object has 8 or so pieces of data stored as key/value pairs.
I need to output all of these objects in two ways. One acts like a database and holds all the data. the other needs all the objects, but not all the fields of data; about 5 of them. So as not to confuse other people in the department, I need to not include that extra data in this mini-output. Both of these are written to a files if it matters.
In regular plain ahk I could see accomplishing this in two ways.
1) Looping the entire array and manually picking out the data to be saved to the new object, easily a page of code to manually copy the data. Many lines of
newObject.key := oldObject.key or as many is needed.
2) Making a clone of the array and using
newObject.Delete(Key) for each item to be removed. If the objects ever get another field, I'll need to remember to delete it here or it'll endup in the mini-output.
With biga.ahk;
.pick can be used to pick out only the key/values we want, then
.map performs that action over the entire array
of objects.
Since ahk does not have anonymous functions, a helper function
fn_declutterUsers is used. One could specify
global A here since it needs access to biga.ahk at the global scope, but in this example I've decided to access the superglobal object
biga in an attempt to keep the helper function brief.
Code: Select all
A := new biga() ;requires https://www.npmjs.com/package/biga.ahk
users := [{name: "Ronald", passwordhash: "kuhfiuahsdoijasd", note: "cool"}
, {name: "Geralt", passwordhash: "jksafd8398usadaj", note: "chill"}]
declutteredData := A.map(users, Func("fn_declutterUsers"))
; => [{name: "Ronald", note: "cool"}, {name: "Geralt", note: "chill"}]
fn_declutterUsers(inputObj)
{
return biga.pick(inputObj, ["name", "note"])
}
for example simplicity, only the "passwordhash" has been left out of the output; but this scales if you had a really cluttered object like in my app.
I will be looking at adding
.omit; which works in the opposite fashion of
.pick. Instead of listing the keys to be copied to the new object; the keys to be omitted from the object are specified. For example if you wanted to output everything
except sensitive data like a password or internal note.