npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@the-arj/flat-ts

v0.1.0

Published

TypeScript-first flatten/unflatten for nested objects. Modern ESM+CJS replacement for `flat` with full type inference and prototype-pollution protection.

Readme

flat-ts

TypeScript-first flatten/unflatten for nested objects.
Modern ESM + CJS replacement for flat with full type inference and prototype-pollution protection.

import { flatten, unflatten } from '@the-arj/flat-ts'

const obj = { a: { b: { c: 1 }, d: 'hello' }, e: true }

flatten(obj)
// → { 'a.b.c': 1, 'a.d': 'hello', e: true }
// TypeScript infers: { 'a.b.c': number; 'a.d': string; e: boolean }

unflatten({ 'a.b.c': 1, 'a.d': 'hello', e: true })
// → { a: { b: { c: 1 }, d: 'hello' }, e: true }

Why flat-ts?

| | flat | flat-ts | |---|:---:|:---:| | Built-in TypeScript types | ✗ | ✅ | | Inferred output types | ✗ | ✅ | | ESM (import) | ✗ | ✅ | | CJS (require) | ✅ | ✅ | | Prototype-pollution guard | Partial | ✅ | | Circular reference detection | Silent | ✅ throws | | Diamond-shape support | ✗ | ✅ | | Active maintenance | ✗ (2023) | ✅ |


Install

npm install @the-arj/flat-ts

Requires Node.js ≥ 16.


API

flatten(obj, options?)

Converts a nested object into a single-depth object with delimited keys.

import { flatten } from '@the-arj/flat-ts'

flatten({ a: { b: 1, c: 2 }, d: 3 })
// → { 'a.b': 1, 'a.c': 2, d: 3 }

Options

| Option | Type | Default | Description | |---|---|---|---| | delimiter | string | '.' | Key delimiter | | safe | boolean | false | When true, arrays are preserved as-is | | maxDepth | number | Infinity | Stop recursing beyond this depth | | transformKey | (key: string) => string | — | Applied to every key segment |

// Custom delimiter
flatten({ a: { b: 1 } }, { delimiter: '/' })
// → { 'a/b': 1 }

// Preserve arrays
flatten({ a: [1, 2, 3] }, { safe: true })
// → { a: [1, 2, 3] }

// Limit depth
flatten({ a: { b: { c: 1 } } }, { maxDepth: 1 })
// → { 'a.b': { c: 1 } }

// Transform keys
flatten({ fooBar: { bazQux: 1 } }, { transformKey: k => k.toLowerCase() })
// → { 'foobar.bazqux': 1 }

unflatten(obj, options?)

Restores a flat object back to its nested form.

import { unflatten } from '@the-arj/flat-ts'

unflatten({ 'a.b': 1, 'a.c': 2, d: 3 })
// → { a: { b: 1, c: 2 }, d: 3 }

Numeric path segments automatically produce arrays:

unflatten({ 'a.0': 'x', 'a.1': 'y' })
// → { a: ['x', 'y'] }

Options

| Option | Type | Default | Description | |---|---|---|---| | delimiter | string | '.' | Key delimiter | | object | boolean | false | When true, numeric segments produce objects, not arrays | | overwrite | boolean | false | When true, primitives at a path are silently overwritten | | transformKey | (key: string) => string | — | Applied to every key segment |


TypeScript — Type Inference

flatten infers the output type from your input:

const result = flatten({ user: { name: 'Alice', age: 30 }, active: true })
//    ^? { 'user.name': string; 'user.age': number; active: boolean }

You can also use the exported utility types directly:

import type { FlattenResult, LeafPaths, PathValue } from '@the-arj/flat-ts'

type MyObj = { a: { b: number; c: string }; d: boolean }

type Flat    = FlattenResult<MyObj>         // { 'a.b': number; 'a.c': string; d: boolean }
type Paths   = LeafPaths<MyObj>             // 'a.b' | 'a.c' | 'd'
type ValAtAB = PathValue<MyObj, 'a.b'>      // number

Safety

Prototype pollution

flatten and unflatten silently skip keys named __proto__, constructor, and prototype. No polluted prototypes, ever.

Circular references

const obj: Record<string, unknown> = { a: 1 }
obj.self = obj

flatten(obj) // throws TypeError: flat-ts: circular reference detected at key "self"

Diamond-shaped structures

Shared (non-circular) references are handled correctly without false positives:

const shared = { x: 1 }
flatten({ a: shared, b: shared }) // → { 'a.x': 1, 'b.x': 1 } ✅

Round-trip

import { flatten, unflatten } from '@the-arj/flat-ts'

const original = { a: { b: { c: 1 }, d: 'hi' } }
unflatten(flatten(original))
// → { a: { b: { c: 1 }, d: 'hi' } }  ✅

License

MIT © The-ARJ