Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
/ dotprop Public archive

Safely access or modify object properties with dot-notated path

Notifications You must be signed in to change notification settings

intrnl/dotprop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dotprop

Safely access or modify object properties with dot-notated path

Installation

  • Install it with your package manager of choice
    • npm: npm i @intrnl/dotprop
    • pnpm: pnpm i @intrnl/dotprop
    • yarn yarn add @intrnl/dotprop

Usage

import { get, set, del, has } from '@intrnl/dotprop';

let obj = {
  foo: {
    bar: 'baz',
    'baz.baz': 'foo foo',
  },
};

// Get property
get(obj, 'foo.bar') // -> 'baz'
get(obj, 'foo.foo.bar') // -> undefined
get(obj, 'foo.foo.bar', 'default value') // -> 'default value'
get(obj, 'foo.baz\\.baz') // 'foo foo'

// Set property
set(obj, 'baz', 'foo') // -> 'foo'
obj // -> { ..., baz: 'foo' }

set(obj, 'bar.bar.foo', 'horse') // -> 'horse'
obj // -> { ..., bar: { bar: { foo: 'horse' } } }

// Delete property
del(obj, 'bar') // -> true
obj // -> { foo: { ... }, baz: 'foo' }

// Has property
has(obj, 'bar') // -> false
has(obj, 'foo.baz\\.baz') // -> true

About

Safely access or modify object properties with dot-notated path

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages