main
HerrHase 2 years ago
parent cb910b3f18
commit f576ba8903

@ -15,7 +15,36 @@ npm i --save-dev @helpers/siteomat-query
yarn add --dev @helpers/siteomat-query yarn add --dev @helpers/siteomat-query
``` ```
## filter ## Filter
## limit
## offset Basic Usage:
## orderBy
```
import { Filter } from '@helpers/siteomat-query'
const filter = new Filter(options)
const isValid = filter.validate(result)
```
## Reduce
Basic Usage:
```
import { reduce } from '@helpers/siteomat-query'
results = reduce({
limit: 5
}, results)
```
## OrderBy
Basic Usage:
```
import { orderBy } from '@helpers/siteomat-query'
results = orderBy([
-published_at
], results)
```

@ -0,0 +1,4 @@
module.exports = {
Filter: require('./src/filter.js')
orderBy: require('./src/orderBy.js')
}

32
package-lock.json generated

@ -9,7 +9,6 @@
"version": "0.1.0", "version": "0.1.0",
"dependencies": { "dependencies": {
"assign-deep": "^1.0.1", "assign-deep": "^1.0.1",
"lodash.merge": "^4.6.2",
"lodash.orderby": "^4.6.0" "lodash.orderby": "^4.6.0"
}, },
"devDependencies": { "devDependencies": {
@ -394,20 +393,6 @@
"integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==",
"dev": true "dev": true
}, },
"node_modules/fsevents": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz",
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
"dev": true,
"hasInstallScript": true,
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
}
},
"node_modules/get-caller-file": { "node_modules/get-caller-file": {
"version": "2.0.5", "version": "2.0.5",
"resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz",
@ -613,11 +598,6 @@
"url": "https://github.com/sponsors/sindresorhus" "url": "https://github.com/sponsors/sindresorhus"
} }
}, },
"node_modules/lodash.merge": {
"version": "4.6.2",
"resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz",
"integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ=="
},
"node_modules/lodash.orderby": { "node_modules/lodash.orderby": {
"version": "4.6.0", "version": "4.6.0",
"resolved": "https://registry.npmjs.org/lodash.orderby/-/lodash.orderby-4.6.0.tgz", "resolved": "https://registry.npmjs.org/lodash.orderby/-/lodash.orderby-4.6.0.tgz",
@ -1307,13 +1287,6 @@
"integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==",
"dev": true "dev": true
}, },
"fsevents": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz",
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
"dev": true,
"optional": true
},
"get-caller-file": { "get-caller-file": {
"version": "2.0.5", "version": "2.0.5",
"resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz",
@ -1464,11 +1437,6 @@
"p-locate": "^5.0.0" "p-locate": "^5.0.0"
} }
}, },
"lodash.merge": {
"version": "4.6.2",
"resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz",
"integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ=="
},
"lodash.orderby": { "lodash.orderby": {
"version": "4.6.0", "version": "4.6.0",
"resolved": "https://registry.npmjs.org/lodash.orderby/-/lodash.orderby-4.6.0.tgz", "resolved": "https://registry.npmjs.org/lodash.orderby/-/lodash.orderby-4.6.0.tgz",

@ -14,7 +14,6 @@
"description": "A Site-O-Mat Query", "description": "A Site-O-Mat Query",
"dependencies": { "dependencies": {
"assign-deep": "^1.0.1", "assign-deep": "^1.0.1",
"lodash.merge": "^4.6.2",
"lodash.orderby": "^4.6.0" "lodash.orderby": "^4.6.0"
}, },
"devDependencies": { "devDependencies": {

@ -0,0 +1,28 @@
const lodashOrderBy = require('lodash.orderby')
/**
* order results by keys
*
* @param {object} options
* @param {object} results
*
* @return {object}
*
*/
function orderBy(options, results) {
options.forEach((key, index) => {
let direction = 'asc'
if (key.charAt(0) === '-') {
key = key.slice(1, key.length)
direction = 'desc'
}
results = lodashOrderBy(results, key, direction)
})
return results
}
module.exports = orderBy

@ -0,0 +1,34 @@
const lodashOrderBy = require('lodash.orderby')
const assign = require('assign-deep')
/**
* reduce results
*
* @param {object} options
* @param {object} results
*
* @return {object}
*
*/
function reduce(options, results) {
options = assign({
offset: 0,
limit: results.length
}, options)
// check if offset exists and limit is smaller then length
if (options.offset > 0 && options.limit < results.length) {
options.limit += options.offset
// if new limit is more then length, then set to length
if (options.limit > results.length) {
options.limit = results.length
}
}
// offset and limit
return results.slice(options.offset, options.limit)
}
module.exports = reduce

@ -1,13 +0,0 @@
const Filter = require('./src/filter.js')
const filter = new Filter({
view: {
_eq: 'post.nj'
}
})
const v = filter.validate({
view: 'post.njk'
})
console.log(v)

@ -0,0 +1,43 @@
const { assert } = require('chai')
const fs = require('fs')
describe('OrderBy', function () {
// get function
const orderBy = require('./../src/orderBy.js')
// check results
it('desc', function() {
const options = [
'-index'
]
const data = orderBy(options, [
{ index: 0 },
{ index: 1 },
{ index: 2 },
{ index: 3 },
{ index: 4 }
])
assert.equal(data[0].index, 4)
})
it('asc', function() {
const options = [
'index'
]
const data = orderBy(options, [
{ index: 0 },
{ index: 1 },
{ index: 2 },
{ index: 3 },
{ index: 4 }
])
assert.equal(data[0].index, 0)
})
})

@ -0,0 +1,64 @@
const { assert } = require('chai')
const fs = require('fs')
describe('Filter', function () {
// get function
const reduce = require('./../src/reduce.js')
// check results
it('reduce, limit', function() {
const options = {
limit: 2
}
const data = reduce(options, [
{ index: 0 },
{ index: 1 },
{ index: 2 },
{ index: 3 },
{ index: 4 }
])
assert.equal(data.length, 2)
})
// check results
it('reduce, offset', function() {
const options = {
offset: 2
}
const data = reduce(options, [
{ index: 0 },
{ index: 1 },
{ index: 2 },
{ index: 3 },
{ index: 4 }
])
assert.equal(data[0].index, 2)
})
// check results
it('reduce, limit / offset', function() {
const options = {
offset: 2,
limit: 3
}
const data = reduce(options, [
{ index: 0 },
{ index: 1 },
{ index: 2 },
{ index: 3 },
{ index: 4 }
])
assert.equal(data[0].index, 2)
assert.equal(data.length, 3)
})
})
Loading…
Cancel
Save