parent
							
								
									cb910b3f18
								
							
						
					
					
						commit
						f576ba8903
					
				@ -0,0 +1,4 @@
 | 
				
			|||||||
 | 
					module.exports = {
 | 
				
			||||||
 | 
						Filter: require('./src/filter.js')
 | 
				
			||||||
 | 
						orderBy: require('./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
 | 
				
			||||||
@ -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…
					
					
				
		Reference in new issue