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

Feature request: Convert an Object into dot-delimited paths #1

Closed
1 of 2 tasks
maoosi opened this issue Oct 26, 2022 · 5 comments
Closed
1 of 2 tasks

Feature request: Convert an Object into dot-delimited paths #1

maoosi opened this issue Oct 26, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@maoosi
Copy link

maoosi commented Oct 26, 2022

Guidelines

  • Please search other issues to make sure this feature has not already been requested.

Which problem is this feature request solving?

Thanks for the work, very useful library!

One thing I’d love to see added is the ability to convert an Object into dot-delimited paths.

Describe the solution you'd like

Example:

dotate({
  username: 'johndoe',
  profile: { image: 'avatar.jpg', verified: true, views: 5 },
  tags: ['foo', 'bar']
}, { keepArrays })

Result with keepArrays: true:

{
  "username": "johndoe",
  "profile.image": "avatar.jpg",
  "profile.verified": true,
  "profile.views": 5,
  "tags": ["foo", "bar"]
}

Result with keepArrays: false:

{
  "username": "johndoe",
  "profile.image": "avatar.jpg",
  "profile.verified": true,
  "profile.views": 5,
  "tags.0": "foo",
  "tags.1": "bar"
}

Pull request (optional)

  • I can submit a pull request.
@maoosi maoosi added the enhancement New feature or request label Oct 26, 2022
@ehmicky
Copy link
Owner

ehmicky commented Oct 26, 2022

Hi @maoosi,

Thanks for the request!
Good ideas, let me work on it and reach back. 👍

@ehmicky
Copy link
Owner

ehmicky commented Oct 26, 2022

This is now available in wild-wild-utils 4.5.0. 🎉

wild-wild-path focuses on CRUD operations, while wild-wild-utils on functional utilities like the one you described, which is why this was added to that other repository.

The new method is named flatten() and is documented here and there.

import { flatten } from 'wild-wild-utils'

const target = {
  username: 'johndoe',
  profile: { image: 'avatar.jpg', verified: true, views: 5 },
  tags: ['foo', 'bar']
}
flatten(target)
// {
//   "username": "johndoe",
//   "profile.image": "avatar.jpg",
//   "profile.verified": true,
//   "profile.views": 5,
//   "tags.0": "foo",
//   "tags.1": "bar"
// }
flatten(target, { sort: true })
// {
//   "profile.image": "avatar.jpg",
//   "profile.verified": true,
//   "profile.views": 5,
//   "tags.0": "foo",
//   "tags.1": "bar",
//   "username": "johndoe"
// }
flatten(target, { shallowArrays: true })
// {
//   "username": "johndoe",
//   "profile.image": "avatar.jpg",
//   "profile.verified": true,
//   "profile.views": 5,
//   "tags": ["foo", "bar"]
// }

As part of this, I also added a new option shallowArrays to both wild-wild-path and wild-wild-utils. When set to true, it prevents recursing on arrays.

Please note the dot-delimited paths are safe to use with other wild-wild-path and wild-wild-utils methods. For example, if a property key is odd (e.g. is an empty string, or has a dot in it), it will be properly escaped.

I hope this works!

@ehmicky ehmicky closed this as completed Oct 26, 2022
@ehmicky
Copy link
Owner

ehmicky commented Oct 26, 2022

@all-contributors Please add @maoosi for those great ideas.

@allcontributors
Copy link
Contributor

@ehmicky

I've put up a pull request to add @maoosi! 🎉

@maoosi
Copy link
Author

maoosi commented Oct 27, 2022

Wow that was quick @ehmicky, awesome work!

I should now be able to use wild-wild-utils as a dependency to my own OSS project and simplify/strengthen some parts: https://github.com/maoosi/prisma-appsync

Thanks!

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

No branches or pull requests

2 participants