object-path
object-path copied to clipboard
exists with wildcards
So, I was checking for a cache entry, and the cache keys are completly random, but I knew the id of the cache item, basically it was like this:
cache = {
activities: {
'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad': [
{'id': 1 /*...*/},
{'id': 9 /*...*/},
/* ...etc */
]
}
};
I could just iterate through all activities, and check each one of the ids, but then I realized that I could use an 'exists' function that could 'ignore' parts of the path (using a *
and check for the existance of the deep path).
So, for that cache I'd do objectPath.exists(cache, 'activities.*.*.id', 1)
the last value is a comparision. If omitted, it just check of the path !== undefined. It's a nice addition to the library, what do you think @mariocasciaro ?
Yes, the idea looks great. I'm just thinking if the API should be called something else. find
or search
perhaps.
ok, i will fix something ;)
PS: harder than it looks lol