mariocasciaro / object-path

A tiny JavaScript utility to access deep properties using a path (for Node and the Browser)
MIT License
1.06k stars 84 forks source link

added key:value notation support to getShallowProperty() #90

Closed utillity closed 7 years ago

utillity commented 7 years ago

added key:value notation support to getShallowProperty(), allowing to get a specific array-entry identified by key:value pair

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.008%) to 99.73% when pulling 98655b96090b84faba859912c4b8a4d220029947 on utillity:master into 99d9d30087493f6def258ddfb45d34029f5ce4eb on mariocasciaro:master.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.009%) to 99.731% when pulling e6125c936a9ddd74b493693d03afb4a22c242fe5 on utillity:master into 99d9d30087493f6def258ddfb45d34029f5ce4eb on mariocasciaro:master.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.009%) to 99.731% when pulling 639bd44de64f13cac6ffd79ccfdc83148aef9fa0 on utillity:master into 99d9d30087493f6def258ddfb45d34029f5ce4eb on mariocasciaro:master.

mariocasciaro commented 7 years ago

Hi @utillity , great the proactivity, thanks! However, this feature seems addressing a quite unusual requirement and it introduces more complexity into the code, so I cannot include it into the main library.

utillity commented 7 years ago

No prob, just wanted to share the code. I do believe there should be an easier way to traverse arrays, apart from their index (which can change without notice), though. Tilli

From: Mario Casciaro notifications@github.com Reply-To: mariocasciaro/object-path reply@reply.github.com Date: Thursday, 26 October 2017 at 17:46 To: mariocasciaro/object-path object-path@noreply.github.com Cc: Tilfried Weissenberger tilli@weissenberger.at, Mention mention@noreply.github.com Subject: Re: [mariocasciaro/object-path] added key:value notation support to getShallowProperty() (#90)

Hi @utillityhttps://github.com/utillity , great the proactivity, thanks! However, this feature seems addressing a quite unusual requirement and it introduces more complexity into the code, so I cannot include it into the main library.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/mariocasciaro/object-path/pull/90#issuecomment-339710003, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AFSu9pB2DUszzsymXFusIywzCJGkOZQpks5swKlKgaJpZM4PrEVq.

PAEz commented 4 years ago

I wanted something like this as well. The way I did it was to add a find/search function. This way the main functions can stay simple (one of the big reasons I picked this to work with). See mine below. It can search an array or an object and return any element that can resolve a "has" against "path" if there is no "value", or a "get" against "value". If "value" is an array it will check to see if any of the values in "value" match. If "index" is true it will return the elements indexs/keys instead. And "all" indicates whether to return all matches or just the first one it finds.

// Doesnt take into account inherited properties like object-path does
// I was only interested in enumarable properties, so it uses Object.keys
function find(obj, what, all, index, value) {
    let result = [];
    const isArray = Array.isArray(obj);
    const keys = isArray ? undefined : Object.keys(obj);
    for (let i = 0, end = isArray ? obj.length : keys.length; i < end; i++) {
        if (!all && result.length) return result[0];
        const element = obj[isArray ? i : keys[i]];
        if (objectPath.has(element, what)) {
            if (arguments.length == 4) {
                result.push(index ? isArray ? i : keys[i] : element);
                continue;
            }
            let foundValue = objectPath.get(element, what);
            if (!Array.isArray(value)) {
                if (foundValue === value) result.push(index ? isArray ? i : keys[i] : element);
                continue;
            } else for (let j = 0; j < value.length; j++) {
                if (value[j] == foundValue) result.push(index ? isArray ? i : keys[i] : element);
                if (!all && result.length) return result[0];
            }
        }
    }
    return result
}

I used this in my EmitterObject.... https://gist.github.com/PAEz/b6fc1687e4e963796f189d539d8d9d0c ... still working on it, dont judge me ;P