diff --git a/README.md b/README.md index 1de7bc9..f34e2f4 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,36 @@ npm i --save-dev @helpers/siteomat-query yarn add --dev @helpers/siteomat-query ``` -## filter -## limit -## offset -## orderBy +## Filter + +Basic Usage: + +``` +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) +``` diff --git a/index.js b/index.js new file mode 100644 index 0000000..1e4a9d7 --- /dev/null +++ b/index.js @@ -0,0 +1,4 @@ +module.exports = { + Filter: require('./src/filter.js') + orderBy: require('./src/orderBy.js') +} diff --git a/package-lock.json b/package-lock.json index 6af96c8..fc418bf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "0.1.0", "dependencies": { "assign-deep": "^1.0.1", - "lodash.merge": "^4.6.2", "lodash.orderby": "^4.6.0" }, "devDependencies": { @@ -394,20 +393,6 @@ "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", "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": { "version": "2.0.5", "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" } }, - "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": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/lodash.orderby/-/lodash.orderby-4.6.0.tgz", @@ -1307,13 +1287,6 @@ "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", "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": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", @@ -1464,11 +1437,6 @@ "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": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/lodash.orderby/-/lodash.orderby-4.6.0.tgz", diff --git a/package.json b/package.json index c11d356..2edeade 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,6 @@ "description": "A Site-O-Mat Query", "dependencies": { "assign-deep": "^1.0.1", - "lodash.merge": "^4.6.2", "lodash.orderby": "^4.6.0" }, "devDependencies": { diff --git a/src/orderBy.js b/src/orderBy.js new file mode 100644 index 0000000..282d6f6 --- /dev/null +++ b/src/orderBy.js @@ -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 diff --git a/src/reduce.js b/src/reduce.js new file mode 100644 index 0000000..1a23c40 --- /dev/null +++ b/src/reduce.js @@ -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 diff --git a/test.js b/test.js deleted file mode 100644 index c862220..0000000 --- a/test.js +++ /dev/null @@ -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) diff --git a/test/orderBy.js b/test/orderBy.js new file mode 100644 index 0000000..7852e78 --- /dev/null +++ b/test/orderBy.js @@ -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) + }) +}) diff --git a/test/reduce.js b/test/reduce.js new file mode 100644 index 0000000..f0e54c0 --- /dev/null +++ b/test/reduce.js @@ -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) + }) +})