Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Safely get and set deep nested properties using dot notation.

License

NotificationsYou must be signed in to change notification settings

g-makarov/dot-path-value

Repository files navigation

Safely get and set deep nested properties using dot notation.

npm versionnpm downloadsnpm minified bundle sizenpm gzip minified bundle sizenpm gzip minified bundle size

Features

  • TypeScript first 🤙
  • Support arrays
  • Tiny
  • No dependencies
  • Utility typesPath andPathValue

If you find this library useful, why not

Buy Me A Coffee

Installation

# using npmnpm install dot-path-value# using pnpmpnpm install dot-path-value# using yarnyarn add dot-path-value

Usage

import{getByPath,setByPath}from'dot-path-value';constobj={a:{b:'hello',d:[{e:'world',}],},};// access through objectgetByPath(obj,'a.b');// outputs 'hello' with type `string`// access through arraygetByPath(obj,'a.d.0.e');// outputs 'world' with type `string`getByPath(obj,'a.d.0');// outputs '{ e: 'world' }' with type `{ e: string }`// also you can pass array as first argumentgetByPath([{a:1}],'0.a');// outputs '1' with type `number`// typescript errorsgetByPath(obj,'a.b.c');// `c` property does not exist// set a property through an objectsetByPath(obj,'a.b','hello there');

Types

dot-path-value exports a few types to ensure the type safety:

TypeDescription
Path<T>converts nested structureT into a string representation of the paths to its properties
PathValue<T, TPath>returns the type of the value at the specified path

Types usage

import{Path,PathValue}from'dot-path-value';constobj={a:{b:'hello',d:[{e:'world',}],},};typeFoo=Path<typeofobj>;// 'a.d' | 'a' | 'a.b' | `a.d.${number}` | `a.d.${number}.e`typeBar=PathValue<typeofobj,'a.b'>;// 'string'

About

Safely get and set deep nested properties using dot notation.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors4

  •  
  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp