Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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 (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-11-05T10:09:09.000Z (about 4 years ago)
- Last Synced: 2024-11-30T15:41:55.554Z (about 1 month ago)
- Language: TypeScript
- Size: 81.1 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[![NPM Package](https://badge.fury.io/js/get-or-create.svg)](https://www.npmjs.com/package/get-or-create)
[![Build Status](https://travis-ci.org/vilic/get-or-create.svg?branch=master)](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.