https://github.com/hyperweb-io/nested-obj
https://github.com/hyperweb-io/nested-obj
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/hyperweb-io/nested-obj
- Owner: hyperweb-io
- License: mit
- Created: 2024-03-31T05:09:40.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-31T05:21:03.000Z (about 2 years ago)
- Last Synced: 2025-07-18T11:44:24.579Z (11 months ago)
- Language: TypeScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 7
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nested-obj
`nested-obj` is a simple and lightweight JavaScript utility library for safely accessing and modifying nested properties in objects using string paths.
## Features
- **Get**: Safely access nested properties in an object.
- **Set**: Set a value at a specific path in an object. Does not overwrite if the value is undefined.
- **Has**: Check if a specific path exists within an object.
## Installation
```bash
npm install nested-obj
```
## Usage
### Get
Retrieve a nested property value from an object.
```ts
import objectPath from 'nested-obj';
const obj = {
user: {
name: 'John Doe',
address: {
street: '123 Main St',
city: 'Anytown'
}
}
};
const userName = objectPath.get(obj, 'user.name');
console.log(userName); // 'John Doe'
```
### Set
Set a value at a specific path in an object. If any part of the path does not exist, it will be created.
```ts
objectPath.set(obj, 'user.address.zip', '12345');
console.log(obj.user.address.zip); // '12345'
```
### Has
Check if a path exists within an object.
```ts
const hasCity = objectPath.has(obj, 'user.address.city');
console.log(hasCity); // true
```
## Running Tests
To run tests, execute the following command:
```sh
npm test
```
or for continuous
```sh
npm test:watch
```