# safe-flat

> Safely flatten a nested JavaScript object.

Latest version **2.1.0** (published 2023-11-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install safe-flat
pnpm add safe-flat
yarn add safe-flat
bun add safe-flat
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2023-11-30 |
| First published | 2019-04-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 24.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Jessie Barnett |
| Maintainers | jessie-codes |
| Keywords | flat, flatten, json, nested, object, safe, serialize, unflatten |

## Links

- npm: https://www.npmjs.com/package/safe-flat
- Repository: https://github.com/jessie-codes/safe-flat
- Homepage: https://github.com/jessie-codes/safe-flat#readme
- Issues: https://github.com/jessie-codes/safe-flat/issues
- npm.io page: https://npm.io/package/safe-flat

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.1.0 (latest) — 2023-11-30
- 2.0.3 — 2023-11-30
- 2.0.2 — 2021-01-24
- 2.0.1 — 2020-12-31
- 2.0.0 — 2020-10-06
- 1.1.4 — 2020-09-12
- 1.1.3 — 2020-09-12
- 1.1.2 — 2019-10-12
- 1.1.1 — 2019-10-12
- 1.1.0 — 2019-10-09
- 1.0.1 — 2019-05-03
- 1.0.0 — 2019-04-22

## README

# safe-flat
> Safely flatten a nested JavaScript object.

[![NPM](https://nodei.co/npm/safe-flat.png?compact=true)](https://nodei.co/npm/safe-flat/)

[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com)
![Build](https://github.com/jessie-codes/safe-flat/workflows/Build/badge.svg)
[![Coverage](https://coveralls.io/repos/github/jessie-codes/safe-flat/badge.svg?branch=master)](https://coveralls.io/github/jessie-codes/safe-flat?branch=master)
[![Known Vulnerabilities](https://snyk.io/test/github/jessie-codes/safe-flat/badge.svg)](https://snyk.io/test/github/jessie-codes/safe-flat)

## Installation

``` bash
$ npm i safe-flat
```

## Methods

### flatten(obj, [delimiter])

Flattens an object to one level deep. Optionally takes a custom `delimiter`, otherwise uses `.` by default. Circular references within the object will be replaced with `[Circular]`.

``` javascript
const { flatten } = require('safe-flat')

const original = {
    a: {
        b: {
            c: [{
                val: 'one'
            }, {
                val: 'two'
            }],
            d: 'three'
        },
        e: 'four',
    }
}
original.a.b.f = original.a.b
original.a.b.c.push(original.a)

const flat = flatten(original)
/*
{
  'a.b.c.0.val': 'one',
  'a.b.c.1.val': 'two',
  'a.b.c.2': '[Circular]',
  'a.b.d': 'three',
  'a.e': 'four',
  'a.b.f': '[Circular]'
}
*/

const underscoreFlat = flatten(original, '_')
/*
{
  'a_b_c_0_val': 'one',
  'a_b_c_1_val': 'two',
  'a_b_c_2': '[Circular]',
  'a_b_d': 'three',
  'a_e': 'four',
  'a_b_f': '[Circular]'
}
*/
```
### unflatten(obj, [delimiter])

Unflattens an object back to its original nested form. Optionally takes a custom `delimiter`, otherwise uses `.` by default. Circular references denoted by `[Circular]` are treated as Strings.

``` javascript
const { unflatten } = require('safe-flat')

const original = {
    'a.b.c.0.val': 'one',
    'a.b.c.1.val': 'two',
    'a.b.c.2': '[Circular]',
    'a.b.d': 'three',
    'a.e': 'four',
    'a.b.f': '[Circular]'
}


const unflat = unflatten(original)

/*{
  a:{
    b:{
      c:[
        {
          val:'one'
        },
        {
          val:'two'
        },
        '[Circular]'
      ],
      d:'three',
      f:'[Circular]'
    },
    e:'four'
  }
}*/
```

---
_Source: https://npm.io/package/safe-flat · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
