feat: filter node

This commit is contained in:
ysandler 2020-05-16 10:59:49 -05:00 committed by root
parent 65a67b34f0
commit fc18a3f66c
3 changed files with 159 additions and 54 deletions

View File

@ -0,0 +1,9 @@
const filterTypes = {
EQUAL: 'EQUAL',
GREATER: 'GREATER',
GREATEREQUAL: 'GREATEREQUAL',
LESSER: 'LESSER',
LESSEREQUAL: 'LESSEREQUAL',
}
export default filterTypes

View File

@ -1,4 +1,5 @@
import Node from '../Node.js'
import filterTypes from '../../constants/filterTypes.js'
class FilterNode extends Node {
constructor (props) {
@ -7,16 +8,24 @@ class FilterNode extends Node {
}
addFilter = params => {
const validation = this._validateFilters(params)
if (validation.status === 'ERR') throw validation
const filterValidation = this._validateFilters(params)
if (filterValidation.status === 'ERR') throw filterValidation
else this.filterParams = {...this.filterParams, ...params}
}
setType = type => {
const typeValidation = this._validateType(type)
if (typeValidation.status === 'ERR') throw typeValidation
else this.type = type
}
export = () => {
let rows = this.tables.map(t => t.getRows() ).flat()
let filters = []
let filters = this._createFilterMethods()
for (let key in this.filterParams) {
const filterMethod = (t) => { return t[key] === this.filterParams[key] }
const filterMethod = t => {
return t[key] === this.filterParams[key]
}
filters.push(filterMethod)
}
@ -28,9 +37,23 @@ class FilterNode extends Node {
}
_assignProps = (props) => {
this.type = 'Filter'
this.filterParams = props.filterParams || []
_assignProps = props => {
this.filterParams = props.filterParams || {}
if (props.type) this.setType(props.type)
}
_createFilterMethods = () => {
let filters = []
for (let key in this.filterParams) {
let filterMethod = {}
if (this.type === filterTypes.EQUAL){
filterMethod = t => {
return t[key] === this.filterParams[key]
}
}
filters.push(filterMethod)
}
return filters
}
_validateFilters = params => {
@ -50,6 +73,23 @@ class FilterNode extends Node {
if (err.error.messages.length > 0) return err
else return { status: 'OK' }
}
_validateType = type => {
const err = {
status: 'ERR',
error: {
label: 'Filter Type is not valid',
messages: []
}
}
if (Object.values(filterTypes).indexOf(type) < 0) {
err.error.messages.push(`Type must be one of: ${Object.keys(filterTypes)}`)
}
if (err.error.messages.length > 0) return err
else return { status: 'OK' }
}
}
export default FilterNode

View File

@ -23,6 +23,106 @@ const exportRows = () => {
return false
}
let filterNode = {}
try {
filterNode = new FilterNode({
id: 'ABC',
label: 'Test Node',
tables: [table],
filterParams: {
data: 'row',
contractor: 'AshBritt'
},
type: 'EQUAL'
})
} catch (err) {
console.log(err)
return false
}
const filteredRows = filterNode.export()
if (JSON.stringify(filteredRows) === JSON.stringify(expectedOutput)) {
return true
} else {
return false
}
}
const addFilter = () => {
const expectedOutput = [
{ id: 'abc', data: 'row', contractor: 'AshBritt' },
{ id: 'XYZ', data: 'row', contractor: 'AshBritt' }
]
let table = {}
try {
table = new Table({
id: 'XYZ',
label: 'Test Table',
rows: [
{ id: 'abc', data: 'row', contractor: 'AshBritt' },
{ id: 'qwe', data: 'lh', contractor: 'AshBritt' },
{ id: 'XYZ', data: 'row', contractor: 'AshBritt' },
{ id: 'XYZ', data: 'row', contractor: 'HeyDay' },
]
})
} catch (err) {
return false
}
let filterNode = {}
try {
filterNode = new FilterNode({
id: 'ABC',
label: 'Test Node',
tables: [table],
filterParams: {
data: 'row',
},
type: 'EQUAL'
})
} catch (err) {
console.log(err)
return false
}
try {
filterNode.addFilter({ contractor: 'AshBritt' })
} catch (err) {
console.log(err)
return false
}
const filteredRows = filterNode.export()
if (JSON.stringify(filteredRows) === JSON.stringify(expectedOutput)) {
return true
} else {
return false
}
}
const setType = () => {
const expectedOutput = [
{ id: 'abc', data: 'row', contractor: 'AshBritt' },
{ id: 'XYZ', data: 'row', contractor: 'AshBritt' }
]
let table = {}
try {
table = new Table({
id: 'XYZ',
label: 'Test Table',
rows: [
{ id: 'abc', data: 'row', contractor: 'AshBritt' },
{ id: 'qwe', data: 'lh', contractor: 'AshBritt' },
{ id: 'XYZ', data: 'row', contractor: 'AshBritt' },
{ id: 'XYZ', data: 'row', contractor: 'HeyDay' },
]
})
} catch (err) {
return false
}
let filterNode = {}
try {
filterNode = new FilterNode({
@ -39,54 +139,8 @@ const exportRows = () => {
return false
}
const filteredRows = filterNode.export()
if (JSON.stringify(filteredRows) === JSON.stringify(expectedOutput)) {
return true
} else {
return false
}
}
const addFilter = () => {
const expectedOutput = [
{ id: 'abc', data: 'row', contractor: 'AshBritt' },
{ id: 'XYZ', data: 'row', contractor: 'AshBritt' }
]
let table = {}
try {
table = new Table({
id: 'XYZ',
label: 'Test Table',
rows: [
{ id: 'abc', data: 'row', contractor: 'AshBritt' },
{ id: 'qwe', data: 'lh', contractor: 'AshBritt' },
{ id: 'XYZ', data: 'row', contractor: 'AshBritt' },
{ id: 'XYZ', data: 'row', contractor: 'HeyDay' },
]
})
} catch (err) {
return false
}
let filterNode = {}
try {
filterNode = new FilterNode({
id: 'ABC',
label: 'Test Node',
tables: [table],
filterParams: {
data: 'row',
}
})
} catch (err) {
console.log(err)
return false
}
try {
filterNode.addFilter({ contractor: 'AshBritt' })
filterNode.setType('EQUAL')
} catch (err) {
console.log(err)
return false
@ -100,7 +154,9 @@ const addFilter = () => {
}
}
export default [
{ name: 'Entity | FilterNode Export Rows', test: exportRows },
{ name: 'Entity | FilterNode Add Filter', test: addFilter },
{ name: 'Entity | FilterNode Set Type', test: setType },
]