https://github.com/boneskull/to-keypath
Convert an array of object keys to a keypath string
https://github.com/boneskull/to-keypath
Last synced: 10 months ago
JSON representation
Convert an array of object keys to a keypath string
- Host: GitHub
- URL: https://github.com/boneskull/to-keypath
- Owner: boneskull
- License: apache-2.0
- Created: 2024-11-18T21:17:52.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-08T05:41:19.000Z (10 months ago)
- Last Synced: 2025-04-08T06:29:00.847Z (10 months ago)
- Language: TypeScript
- Size: 611 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# to-keypath
> Convert a string keypath from an array of keys
## Usage
```js
import {toKeypath} from 'to-keypath';
let path = ['a', 'b', 'c'];
let keypath = toKeypath(path); // 'a.b.c'
// valid integers are represented in brackets
// strings which cannot be used w/ dot notation are wrapped in brackets & double-quotes
path = ['a', '0', '.c'];
keypath = toKeypath(path); // 'a[0].["."].c'
```
`to-keypath` is distributed as a dual ESM/CJS package, courtesy of
[tshy](https://github.com/isaacs/tshy).
## Motivation
There are tens of packages that do the inverse of this operation. Others return
a value within an object when provided a string or string array. The reason for
this, presumably, is that the most common relevant use case for these "keypath"
strings is _access_ or _assignment_ to some deeply-nested property within an
object.
**This package does not do any of that.** `to-keypath` just gives you a string.
It does not consider the data structure to which the string would be applied,
returns no values and performs no validation.
It's probably useless to you _unless_ your program _outputs_ the resulting
string (my use case) _or_ you are consuming an ill-conceived API which a string
_only_. If the latter, please read the following warning:
> [!WARNING]
>
> If you're trying to pass the result of `to-keypath`'s value to another API,
> understand that this package is _not_ meant to be used as such a workaround;
> _you are probably making a mistake_ and are _strongly discouraged_ from using
> `to-keypath` in such a way.
## License
©️ 2024 [Christopher "boneskull" Hiller](https://github.com/boneskull). Licensed Apache-2.0