Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 5 commits into from

Conversation

utillity
Copy link

@utillity utillity commented Oct 2, 2017

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

… get a specific array-entry identified by key:value pair
@coveralls
Copy link

coveralls commented Oct 2, 2017

Coverage Status

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

@coveralls
Copy link

coveralls commented Oct 3, 2017

Coverage Status

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

object-path-key-value
@coveralls
Copy link

coveralls commented Oct 3, 2017

Coverage Status

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

@mariocasciaro
Copy link
Owner

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
Copy link
Author

utillity commented Oct 26, 2017 via email

@PAEz
Copy link

PAEz commented Nov 3, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants