DEV Community

What would you call this function?

Keff on December 08, 2019

(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 cur...
Collapse
 
karfau profile image
Christian Bewernitz • Edited

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
Keff • Edited

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
Keff

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
Keff

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
 
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
Keff

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?

Collapse
 
siddharthshyniben profile image
Siddharth

getPaths? entries? paths?

Collapse
 
nombrekeff profile image
Keff • Edited

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 :)