New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@ezzylabs/object-path

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

@ezzylabs/object-path

Access nested object properties using paths

latest
Source
npmnpm
Version
0.1.1
Version published
Maintainers
1
Created
Source

@ezzylabs/object-path

Access nested object properties using paths.

Installation

npm i @ezzylabs/object-path

Usage

// Import the module.
import objectPath from '@ezzylabs/object-path'

// Define an object with some properties.
const obj = { a: { b: 'x' } }

// Check if the property at the specified path is defined.
objectPath.has(obj, 'a.b')

// Get the property at the specified path.
objectPath.get(obj, 'a.b')

// Set the property at the specified path.
objectPath.set(obj, 'a.b', 'y')

API

has(obj, path)

Checks if the property at the given path is defined in the obj.

// Object with properties.
const obj = { a: { b: 'x', c: [{ d: 'y' }] } }

// Named properties.
objectPath.has(obj, 'a') // returns true
objectPath.has(obj, 'a.b') // returns true
objectPath.has(obj, 'a.b.x') // returns false; 'x' is not defined

// Array elements.
objectPath.has(obj, 'a.b.c.0') // returns true
objectPath.has(obj, 'a.b.c.0.d') // returns true
objectPath.has(obj, 'a.b.c.1') // returns false; '1' is out of bounds
objectPath.has(obj, 'a.b.c.1.d') // returns false; '1' is out of bounds

// Invalid paths.
objectPath.has(obj) // throws TypeError
objectPath.has(obj, null) // throws TypeError

get(obj, path, fallback)

Gets a property of the obj at the given path, or returns the fallback value if the property is not defined.

// Object with properties.
const obj = { a: { b: 'x', c: [{ d: 'y' }] } }

// Named properties.
objectPath.get(obj, 'a.b') // returns 'x'
objectPath.get(obj, 'a.x') // throws; 'x' is not defined
objectPath.get(obj, 'a.x', 'z') // returns 'z'; 'x' is not defined

// Array elements.
objectPath.get(obj, 'a.c.0.d') // returns 'y'
objectPath.get(obj, 'a.c.1.d') // throws; '1' is out of bounds
objectPath.get(obj, 'a.c.1.d', 'z') // returns 'z'; '1' is out of bounds

// Invalid paths.
objectPath.get(obj) // throws TypeError
objectPath.get(obj, null) // throws TypeError

set(obj, path, value)

Sets a property at the given path.

// Object with properties.
const obj = { a: { b: 'x', c: [{ d: 'y' }] } }

// Named properties.
objectPath.set(obj, 'a.b', 'z') // modifies 'a.b' to 'z'
objectPath.set(obj, 'a.d.f', 'x') // adds 'd.f' properties to obj

// Array elements.
objectPath.set(obj, 'a.c.0', 'x') // modifies 'a.c.0' to 'x'
objectPath.set(obj, 'a.c.1', 'y') // adds 'y' to the array
objectPath.set(obj, 'a.c.3', 'w') // adds 'w' to the array; 'a.c.2' will be undefined

// Invalid paths.
objectPath.set(obj) // throws TypeError; path is not provided
objectPath.set(obj, null) // thorws TypeError; path is not valid 

join(...segments)

Joins multiple path segments into a single path.

// Single segment.
objectPath.join('x') // returns 'x'
objectPath.join(0) // returns '0'
objectPath.join('x.y.z') // returns 'x.y.z'

// Multiple segments.
objectPath.join('x', 'y', 'z') // Returns 'x.y.z'
objectPath.join('x', 0, 'z') // Returns 'x.0.z'
objectPath.join('x.y', 'z') // Returns 'x.y.z'

// Invalid segments.
objectPath.join('x', 'y', null) // throws TypeError; null is not a valid path segment
objectPath.join('x', 7.5, 'z') // throws; '7.5' is not a valid path segment

split(path)

Splits the given path into individual segments.

// Without separators.
objectPath.split('x') // returns ['x']

// With seprators.
objectPath.split('x.y.z') // returns ['x', 'y', 'z']
objectPath.split('x.0.z') // returns ['x', '0', 'z']

// Invalid paths.
objectPath.split() // throws TypeError; undefined cannot be split
objectPath.split(null) // throws TypeError; null cannot be split

Keywords

object

FAQs

Package last updated on 16 Jun 2022

Did you know?

Socket

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