object-path icon indicating copy to clipboard operation
object-path copied to clipboard

exists with wildcards

Open pocesar opened this issue 10 years ago • 2 comments

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 ?

pocesar avatar Jul 21 '14 15:07 pocesar

Yes, the idea looks great. I'm just thinking if the API should be called something else. find or search perhaps.

mariocasciaro avatar Jul 21 '14 15:07 mariocasciaro

ok, i will fix something ;)

PS: harder than it looks lol

pocesar avatar Jul 21 '14 16:07 pocesar