Skip to content

Commit

Permalink
removed get() to remove lodash dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
abumq committed Jul 23, 2023
1 parent 2a5b353 commit 1e0782c
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 49 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Unreleased
- Removed `get()` and added it to README to remove dependency from `lodash.get`

## 1.0.3
- Simplified README to understand the features
Expand Down
66 changes: 38 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

<p align="center">
<a aria-label="NPM version" href="https://www.npmjs.com/package/airsync">
<img alt="" src="https://img.shields.io/npm/v/airsync.svg?style=for-the-badge&labelColor=000000&">
<img alt="" src="https://img.shields.io/npm/v/airsync.svg?style=ffor-the-badge&labelColor=000000&">
</a>
<a aria-label="Tests" href="https://github.com/abumq/airsync/actions/workflows/run-tests.yml">
<img alt="" src="https://github.com/abumq/airsync/actions/workflows/run-tests.yml/badge.svg">
</a>
<a aria-label="License" href="https://github.com/abumq/airsync/blob/main/LICENSE">
<img alt="" src="https://img.shields.io/npm/l/airsync?style=for-the-badge&labelColor=000000">
<img alt="" src="https://img.shields.io/npm/l/airsync?style=ffor-the-badge&labelColor=000000">
</a>
</p>

Expand Down Expand Up @@ -157,32 +160,6 @@ Now if you pass in JSON with unresolved promises to the function, it would be co

# Misc Features

## Get Object Value

If you have a function that returns an object, and you want to grab just one specific value from the object, you can use built-in `get` function to do that.

```javascript
const { get } = require("airsync");

const getProfile = async (uid) => ({
name: "John",
age: 45,
father: {
name: "Peter",
},
});

(async () => {
console.log(await get(getProfile(), "father.name")); // output: Peter
console.log(await get(getProfile(), "mother.name", "Steph")); // output: Steph
console.log(await get(getProfile(), "brother.name")); // output: undefined
})();
```

Synopsis: `get(object, path, defaultValue, options)`. The `options` is passed through to `fn()` internally.

**NOTE:** This function uses [lodash.get](https://www.npmjs.com/package/lodash.get) to resolve the JSON path.

## Options

If the first parameter is an object for the `fn()`, that object is used for setting up the options.
Expand Down Expand Up @@ -253,6 +230,39 @@ module.exports = fnExport({

Alternatively, you can do it when importing like in example of `/examples/json.js`. Doing it multiple times does not harm.

## Get Object Value

If you have a function that returns an object, and you want to grab just one specific value from the object, you can use following `get` function to do that.

This function used to be part of library. Since `1.0.4`, the `get()` function is removed (see CHANGELOG). However, if you need it, you can add it as utility in your project.

```js
const lodashGet = require('lodash.get');

// get value from the object using lodash.get
// when object is resolved
const get = (objOrPromise, path, defaultVal, options = {}) => fn(o => lodashGet(o, path) || defaultVal, options)(objOrPromise)
```

You need to add dependency to [lodash.get](https://www.npmjs.com/package/lodash.get) if you need it

```javascript

const getProfile = async (uid) => ({
name: "John",
age: 45,
father: {
name: "Peter",
},
});

(async () => {
console.log(await get(getProfile(), "father.name")); // output: Peter
console.log(await get(getProfile(), "mother.name", "Steph")); // output: Steph
console.log(await get(getProfile(), "brother.name")); // output: undefined
})();
```

# License

```
Expand Down
3 changes: 2 additions & 1 deletion examples/example-utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const snooze = ms => new Promise(resolve => setTimeout(resolve, ms));
const snooze = ms => new Promise(resolve => setTimeout(resolve, ms))

const queryUserInfo = async () => {
//await snooze(800);
Expand Down Expand Up @@ -53,4 +53,5 @@ module.exports = {
thisFnThrows,
queryConfig,
simple,
snooze,
};
27 changes: 24 additions & 3 deletions examples/exec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
const { exec } = require('../src');
const { queryAccountInfo, queryUserInfo } = require('./example-utils');
const { queryAccountInfo, queryUserInfo, snooze } = require('./example-utils');



const queryProfile = async () => {
await snooze(10)
return { id: 1, name: 'Majid Q.', role: 'Software Engineer' }
}

const querySocial = async () => {
await snooze(10)
return { github: 'abumq', linkedin: 'abumq' }
}

const flattenProfile = (profile, social) => {

return {
...profile,
...social,
}
}

(async () => {
console.log(await exec(queryAccountInfo, queryUserInfo()));
})();
const userProfile = await exec(flattenProfile, queryProfile(), querySocial());
console.log(userProfile);
})()
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
"dist/fn.js",
"dist/json.js"
],
"dependencies": {
"lodash.get": "^4.4.2"
},
"devDependencies": {
"babel-minify": "^0.5.1",
"mocha": "^10.2.0",
Expand Down
6 changes: 0 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const get = require('lodash.get');
const { fn, exec, fnExport } = require('./fn');
const { json } = require('./json');

Expand All @@ -30,8 +29,3 @@ module.exports.fnjson = (theSyncFunc) => fn(async (obj, ...anythingElse) => theS
// name exports from json
module.exports.json = json;

// get value from the object using lodash.get
// when object is resolved
module.exports.get = (objOrPromise, path, defaultVal, options = {}) =>
fn(o => get(o, path) || defaultVal, options)
(objOrPromise);
11 changes: 3 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,9 @@ diff@5.0.0:
integrity sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w==

electron-to-chromium@^1.4.431:
version "1.4.467"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.467.tgz#b0660bf644baff7eedea33b8c742fb53ec60e3c2"
integrity sha512-2qI70O+rR4poYeF2grcuS/bCps5KJh6y1jtZMDDEteyKJQrzLOEhFyXCLcHW6DTBjKjWkk26JhWoAi+Ux9A0fg==
version "1.4.468"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.468.tgz#3cbf64ad67d9f12bfe69fefe5eb1935ec4f6ab7a"
integrity sha512-6M1qyhaJOt7rQtNti1lBA0GwclPH+oKCmsra/hkcWs5INLxfXXD/dtdnaKUYQu/pjOBP/8Osoe4mAcNvvzoFag==

emoji-regex@^8.0.0:
version "8.0.0"
Expand Down Expand Up @@ -1155,11 +1155,6 @@ locate-path@^6.0.0:
dependencies:
p-locate "^5.0.0"

lodash.get@^4.4.2:
version "4.4.2"
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
integrity sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==

lodash@^4.17.11:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
Expand Down

0 comments on commit 1e0782c

Please sign in to comment.