https://github.com/mdor/js-extensions
https://github.com/mdor/js-extensions
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/mdor/js-extensions
- Owner: MDOR
- License: mit
- Created: 2022-09-03T13:54:03.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-02-10T19:01:23.000Z (over 3 years ago)
- Last Synced: 2025-02-26T02:48:43.644Z (over 1 year ago)
- Language: JavaScript
- Size: 468 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Description
Set of JavasScript utilities, modular, performant, and easy to use.
## Install
**npm**
```sh
npm i @mdor/js-extensions -S
npm install @mdor/js-extensions --save
```
**yarn**
```sh
yarn add @mdor/js-extensions
yarn workspace @mdor/js-extensions
```
## Documentation
### Object
- #### getFrom
Extracts a property from any given input, if the property is not present, then a fallback value will be returned.
```js
const user = {
id: "123123456",
active: true,
attributes: {
name: "Joe",
},
trades: [
{
id: "werwe224",
bill: 123454,
},
],
};
const userName = getFrom("attributes.name")(user);
const userFirstBill = getFrom('attributes.trades.[0]["bill"]')(user);
```
#### `setInto`
Set or override a property in any given input, if the path is not present, then it will be created.
```js
const user = {
id: "123123456",
active: true,
attributes,
};
setInto("attributes.name", "Marco")(user);
// equivalent setInto(["attributes", "name"], "Marco")(user);
// output Joe
setInto('attributes.trades.[0]["bill"]', 100)(user);
// equivalent setInto(["attributes", "trades", 0, "bill"], 100)(user);
// output 123454
setInto("attributes.trades.1.bill", 0)(user);
// equivalent setInto(["attributes", "trades", 1, "bill"], 0)(user);
// output 0
```