hughsk / hughsk/flat

Possible to allow key's with '.'s in them?

Open
#96 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.8k
Forks
197
PR merge metrics
No merged PRs in 30d

Description

I'm looking for a way to preserve keys that have `.`s in to use the same syntax as lodash's set/get.

Ideally something like this:
```js
// Input
{
some: {
'key.with.dots': value
}
}

// Output
{
some['key.with.dots']: value
}
```
The problem I've found with using the `transformKey` option is they still get the `.` joining them.

I have this:
```js
const flatData = flatten(data, {
transformKey: key => (key.includes('.') ? `['${key}']` : key),
});
```

But the output looks like this. (extra `.` after `some`)
```js
{
some.['key.with.dots']: value
}
```

I've worked around the issue for now by looping through the flattend data and doing a string replace on `.[` with `[`.

```js
const fixedData = Object.entries(flatData).reduce((acc, [key, value]) => {
const newKey = key.includes('.[') ? key.replace('.[', '[') : key;
acc[newKey] = value;
return acc;
}, {});
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.