Socket
Socket
Sign inDemoInstall

nested-object-access

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    nested-object-access

TypeScript-powered util to work with nested objects using dot-separated keys


Version published
Weekly downloads
286
increased by42.29%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Nested Object Access

TypeScript-powered util to work with nested objects using dot-separated keys.

This package exports the following stuff:

Helper type: NestedKeys

Returns a union of all dot-separated paths to entries in a nested object.

type TestDict = {
  b: "bb";
  c: { d: "dd"; w: "ww"; e: { p: "pp" } };
  t: { l: "ww"; e: { q: "pp" } };
  i: { d: "DD"; o: "oo" };
};

// Default functionality
// "b" | "c" | "t" | "i" | "c.d" | "c.w" | "c.e.p" | "t.e.q" | "t.l" | "i.d" | "i.o" | "c.e" | "t.e"
type TestKeys = NestedKeys<TestDict>;
// Get only the paths to primitive values
// "b" | "c.d" | "c.w" | "c.e.p" | "t.e.q" | "t.l" | "i.d" | "i.o"
type TestKeysPr = NestedKeys<TestDict, "primitives">;
// Get only the paths to nested objects
// "c" | "t" | "i" | "c.e" | "t.e"
type TestKeysObj = NestedKeys<TestDict, "objects">;
// Get only up to a certain depth
// "b" | "c" | "t" | "i" | "c.d" | "c.w" | "c.e" | "t.e" | "t.l" | "i.d" | "i.o"
type TestKeysWithDepth = NestedKeys<TestDict, any, 2>;

Helper type: RetrieveNested

Returns the type that is at a dot-separated path in an object.

type TestDict2 = { c: { e: { p: "pp" } } }

// { e: { p: "pp"; }; }
type TestNestedPath1 = RetrieveNested<TestDict2, "c">;
// { p: "pp"; }
type TestNestedPath2 = RetrieveNested<TestDict2, "c.e">;
// "pp"
type TestNestedPath3 = RetrieveNested<TestDict2, "c.e.p">;

Function: getNested

Function version of RetrieveNested.

import { getNested } from "nested-object-access";
const testDict = { c: { e: { p: "pp" } } };
console.log(getNested("c.e.p")); // "pp"

TODO

  • You can currently only pass a type Sth = {...} into the helper functions, not interface Sth {...} because of missing index signature...

Keywords

FAQs

Last updated on 10 Sep 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc