An open API service indexing awesome lists of open source software.

https://github.com/doowb/stringify-keys

Build an array of key paths from an object.
https://github.com/doowb/stringify-keys

dot-notation expand javascript key key-paths keys object objects paths stringify

Last synced: 8 days ago
JSON representation

Build an array of key paths from an object.

Awesome Lists containing this project

README

        

# stringify-keys [![NPM version](https://img.shields.io/npm/v/stringify-keys.svg?style=flat)](https://www.npmjs.com/package/stringify-keys) [![NPM monthly downloads](https://img.shields.io/npm/dm/stringify-keys.svg?style=flat)](https://npmjs.org/package/stringify-keys) [![NPM total downloads](https://img.shields.io/npm/dt/stringify-keys.svg?style=flat)](https://npmjs.org/package/stringify-keys) [![Linux Build Status](https://img.shields.io/travis/doowb/stringify-keys.svg?style=flat&label=Travis)](https://travis-ci.org/doowb/stringify-keys)

> Build an array of key paths from an object.

Please consider following this project's author, [Brian Woodward](https://github.com/doowb), and consider starring the project to show your :heart: and support.

## Install

Install with [npm](https://www.npmjs.com/):

```sh
$ npm install --save stringify-keys
```

See the [Release History](#release-history) for changes.

## Usage

```js
const stringify = require('stringify-keys');

let obj = { a: 'a', b: { c: { d: { e: 'f' } } } };
console.log(stringify(obj));
//=> [ 'a', 'b.c.d.e' ]
```

Include values in the result:

```js
console.log(stringify(obj, { values: true }));
//=> { a: 'a', 'b.c.d.e': 'f' }
```

Keys with dots are automatically escaped with backslashes (this can be [customized](#optionsescape)):

```js
let obj = { 'a.b.c': { d: 'e' } };
console.log(stringify(obj));
//=> [ 'a\\.b\\.c.d' ]

console.log(stringify(obj, { values: true }));
//=> { 'a\\.b\\.c.d': 'e' }
```

Objects with arrays return the array indices as part of the paths:

```js
let obj = { a: 'a', b: [{ c: { d: 'e' } }, { f: { g: 'h' } }] };

console.log(stringify(obj));
//=> [ 'a', 'b.0.c.d', 'b.1.f.g' ]

console.log(stringify(obj, { values: true }));
//=> { a: 'a', 'b.0.c.d': 'e', 'b.1.f.g': 'h' }
```

## Options

### options.separator

**Type**: `string`

**Default**: `.`

Custom separator to use for creating object paths (`a.b.c`):

**Example**

```js
let obj = { 'a.b.c': { d: 'e' } };
console.log(stringify(obj, { separator: '/' }));
//=> [ 'a.b.c/d' ]

console.log(stringify(obj, { separator: '/', values: true }));
//=> { 'a.b.c/d': 'e' }
```

### options.escape

**Type**: `function`

**Default**: adds `\\` before dots

Custom function to use for escaping keys.

**Example**

```js
let obj = { 'a.b.c': { d: 'e' } };
let escape = str => str.split('.').join('/');

console.log(stringify(obj, { escape }));
//=> [ 'a/b/c.d' ]

console.log(stringify(obj, { escape, values: true }));
//=> { 'a/b/c.d': 'e' }
```

## Release History

### v3.0

* Redundant (parent) keys are no longer included in the output. Thus `{ a: { b: 'c' } }` now returns `['a.b']` instead of `['a', 'a.b']`.

### v2.0

* Added support for traversing into arrays.

## About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

```sh
$ npm install && npm test
```

Building docs

_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_

To generate the readme, run the following command:

```sh
$ npm install -g verbose/verb#dev verb-generate-readme && verb
```

### Related projects

You might also be interested in these projects:

* [expand-hash](https://www.npmjs.com/package/expand-hash): Recursively expands property keys with dot-notation into objects. | [homepage](https://github.com/doowb/expand-hash "Recursively expands property keys with dot-notation into objects.")
* [expand-object](https://www.npmjs.com/package/expand-object): Expand a string into a JavaScript object using a simple notation. Use the CLI or… [more](https://github.com/jonschlinkert/expand-object) | [homepage](https://github.com/jonschlinkert/expand-object "Expand a string into a JavaScript object using a simple notation. Use the CLI or as a node.js lib.")
* [glob-object](https://www.npmjs.com/package/glob-object): Filter an object using glob patterns and dot notation. | [homepage](https://github.com/jonschlinkert/glob-object "Filter an object using glob patterns and dot notation.")
* [kind-of](https://www.npmjs.com/package/kind-of): Get the native type of a value. | [homepage](https://github.com/jonschlinkert/kind-of "Get the native type of a value.")

### Contributors

| **Commits** | **Contributor** |
| --- | --- |
| 19 | [doowb](https://github.com/doowb) |
| 17 | [jonschlinkert](https://github.com/jonschlinkert) |
| 1 | [contra](https://github.com/contra) |

### Author

**Brian Woodward**

* [GitHub Profile](https://github.com/doowb)
* [Twitter Profile](https://twitter.com/doowb)
* [LinkedIn Profile](https://linkedin.com/in/woodwardbrian)

### License

Copyright © 2019, [Brian Woodward](https://github.com/doowb).
Released under the [MIT License](LICENSE).

***

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on January 22, 2019._