https://github.com/simplisticated/deepquery.js
Easy data chains for JavaScript
https://github.com/simplisticated/deepquery.js
Last synced: 9 months ago
JSON representation
Easy data chains for JavaScript
- Host: GitHub
- URL: https://github.com/simplisticated/deepquery.js
- Owner: simplisticated
- License: apache-2.0
- Created: 2019-10-10T09:06:02.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-10-10T10:54:45.000Z (over 6 years ago)
- Last Synced: 2025-06-22T07:05:38.059Z (9 months ago)
- Language: TypeScript
- Size: 12.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## At a Glance
`DeepQuery.js` solves the problem of null element inside of long object chain.
## How to Get Started
Type in Terminal:
`npm install --save @imatyushkin/deep-query`
or, if you prefer `yarn` over `npm`, type:
`yarn add @imatyushkin/deep-query`
## Usage
### Simple Query
Let's assume we have some JavaScript object with complicated structure of nested objects:
```javascript
var obj = {
a: {
b: [
{ val: [1,2,3] },
{ val: [4,5,6] },
{ val: null },
]
}
};
```
Now, we want to obtain a value located very deep in the tree:
```javascript
var regularQuery = obj.a.b[2].val[0] // CRASH 😡
```
We'll get crash because the `val` is `null`, which might be an unexpected case. To avoid this, we could implement a more complicated logic based on data structure:
```javascript
var safeQuery = (() => {
var intermediateResult = obj.a.b[2];
return intermediateResult.val ? intermediateResult.val[0] : null;
})();
```
With `DeepQuery.js` it works significantly easier:
```javascript
import "@imatyushkin/deep-query"
var query = obj.__("a.b.2.val.0") // No crash, just null will be returned 🙂
var anotherQuery = obj.__("a.b.1.val.0") // 4
```
### Filter
Here's example of query with data filter:
```javascript
var data = {
users: [
{
name: "John",
points: [10,9,5]
},
{
name: "Donald",
points: [8, 4, 2]
}
]
}
var pointsOfJohn = data.__("users.name:John.0.points") // [10, 9, 5]
```
### Multiline Query
For convenience, you may use a multiline expression:
```javascript
// Instead of
var pointsOfJohn = data.__("users.name:John.0.points")
// use
var pointsOfJohn = data.__(`
users
.name:John.0
.points
`)
```
## License
`DeepQuery.js` is available under the Apache 2.0 license. See the [LICENSE](./LICENSE) file for more info.