loading...

What would you call this function?

nombrekeff profile image Manolo Edge ・1 min read

(WWYD) What Would You Do - #0

I'm working on a little js library with some functions I use a lot and are not present in other libs I currently use.

I recently needed a function that receives an object and returns an array with it's property paths and values:

flatten({ foo: { bar: 'Salmon' }, name: 'John' });

This will return:

[
    {
        "path": "foo.bar",
        "value": "Salmon"
    },
    {
        "path": "name",
        "value": "John"
    },
    {
        "path": "foo",
        "value": [
            {
                "path": "foo.bar",
                "value": "Salmon"
            }
        ]
    }
]

I've been changing the name of the function quite a bit, and the best I could come up with is flatten, as it return the object as a 1 dimension array of path-value pairs, although I don't feel it fully expresses what it does.

I'm kinda curious on what you would call it? And why?

Discussion

pic
Editor guide
Collapse
karfau profile image
Christian Bewernitz

After reading all the existing comments I can contribute some more ideas:

The method doesn't only extract path(s), and in lodash there is already a toPairs: it returns "tuples":
[[path, value], [path, value]].
Which is very similar (if not identical) to what Object.entries is doing.

Since you are returning a list of objects, what would you call the type of these objects to differentiate them from the existing paradigm of "pairs"/"entries"?
In other languages "entries" are also typed as
{key: string, value: any}
which is only one property away from what you have.

One other important difference to the methods mentioned above, is that your method is doing this recursively. I have seen the post-fix "Deep" in method names to describe that behaviour. (It would actually mean there would be a great deal of type safety from a plain object, in case you plan to provide types for this.)

Hope it helps and also curious about some example use cases for this method.

Collapse
nombrekeff profile image
Manolo Edge Author

Thanks for the extended explanation.

The .toPairs method is really quite similar, the difference is I return an array of objects instead of tuples. I was considering this name, after seeing the lodash method, and the ruby each_pairs it feels like a pretty good name.

Since you are returning a list of objects, what would you call the type of these objects to differentiate them from the existing paradigm of "pairs"/"entries"?

I was thinking about this a bit, I would consider them something like: propertyPathMap, pathValueMap or pathValuePairs, but I feel they don't express it 100%. Maybe it's just a matter of educating the library users, on what this is and what it means.

One other important difference to the methods mentioned above, is that your method is doing this recursively. I have seen the post-fix "Deep" in method names to describe that behaviour.

Yup, I thought about adding the Deep postfix, maybe I will create the non-deep function as well.

(It would actually mean there would be a great deal of type safety from a plain object, in case you plan to provide types for this.)

What do you mean with this? I'm not that experienced with advanced typing.

PD: You are the only one that has realized it's recursive. It might of been a good idea to have explained it in the main Post.

Thanks again!

Collapse
nombrekeff profile image
Manolo Edge Author

I might share some use cases here when it's working correctly!

Collapse
pencillr profile image
Richard Lenkovits

Cool stuff. I would name it by looking at what it does functionally, not methodically. So I would name it this way, something like: propertyPath, extractPath, accessMap.

Collapse
nombrekeff profile image
Manolo Edge Author

Nice approach, I was thinking about it a bit, but couldn't come up with any good ones.
Is there any reason not to pluralize it, like: extractPaths?

Collapse
pencillr profile image
Richard Lenkovits

Yeah you could do that sure.

Collapse
citizen428 profile image
Michael Kohl

Not sure if it helps, but Ruby has an iterator method called each_pair for hashes, so how about something like toPairs?

Collapse
nombrekeff profile image
Manolo Edge Author

Oh cool, thanks, I will check it out, it feels pretty good.

EDIT:
I've checked it out, it's pretty close. Cool, thanks for the info :)

Collapse
seanmclem profile image
Seanmclem

Why do you need this function? Maybe that could help you name it. What you're trying to achieve

Collapse
nombrekeff profile image
Manolo Edge Author

It could be a nice approach, problem is, I have different use cases for it, and I don't want to couple it to one use case.

Collapse
seanmclem profile image
Seanmclem

Any commonalities?