(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?
Latest comments (11)
getPaths
?entries
?paths
?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.
I might share some use cases here when it's working correctly!
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 rubyeach_pairs
it feels like a pretty good name.I was thinking about this a bit, I would consider them something like:
propertyPathMap
,pathValueMap
orpathValuePairs
, 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.Yup, I thought about adding the Deep postfix, maybe I will create the non-deep function as well.
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!
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 :)
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
.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
?Yeah you could do that sure.
Why do you need this function? Maybe that could help you name it. What you're trying to achieve
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.
Any commonalities?