https://github.com/vilicvane/get-or-create
A simple utility to get or create nested property and element in place.
https://github.com/vilicvane/get-or-create
Last synced: about 1 month ago
JSON representation
A simple utility to get or create nested property and element in place.
- Host: GitHub
- URL: https://github.com/vilicvane/get-or-create
- Owner: vilicvane
- Created: 2019-02-27T16:09:01.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-11-05T10:09:09.000Z (over 4 years ago)
- Last Synced: 2025-03-24T00:54:53.540Z (4 months ago)
- Language: TypeScript
- Size: 81.1 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/get-or-create)
[](https://travis-ci.org/vilic/get-or-create)# Get or Create
A simple utility to get or create nested property and element in place.
## Install
```sh
yarn add get-or-create
```## Usage
```ts
import getOrCreate from 'get-or-create';let data = {};
let entry = getOrCreate(data)
.property('foo', [])
.element(element => element.id === 'abc', {id: 'abc'})
.property('bar', [])
.element(element => element.id === 'def', {id: 'def'})
.exec();entry.value = 123;
```Mutated `data`:
```json
{
"foo": [
{
"id": "abc",
"bar": [
{
"id": "def",
"value": 123
}
]
}
]
}
```## License
MIT License.