feat: differnt types for FilterNode
This commit is contained in:
parent
df66bdecae
commit
a23336387b
@ -26,7 +26,8 @@ try {
|
|||||||
filterParams: {
|
filterParams: {
|
||||||
siteName: 'Seewee Road DMS',
|
siteName: 'Seewee Road DMS',
|
||||||
fieldMonitorUserCertificationNumber: '9YW6ZAY'
|
fieldMonitorUserCertificationNumber: '9YW6ZAY'
|
||||||
}
|
},
|
||||||
|
type: 'EQUAL'
|
||||||
})
|
})
|
||||||
console.log(filterNode.tables[0].getRows().length)
|
console.log(filterNode.tables[0].getRows().length)
|
||||||
console.log(filterNode.export().length)
|
console.log(filterNode.export().length)
|
||||||
|
@ -22,12 +22,6 @@ class FilterNode extends Node {
|
|||||||
export = () => {
|
export = () => {
|
||||||
let rows = this.tables.map(t => t.getRows() ).flat()
|
let rows = this.tables.map(t => t.getRows() ).flat()
|
||||||
let filters = this._createFilterMethods()
|
let filters = this._createFilterMethods()
|
||||||
for (let key in this.filterParams) {
|
|
||||||
const filterMethod = t => {
|
|
||||||
return t[key] === this.filterParams[key]
|
|
||||||
}
|
|
||||||
filters.push(filterMethod)
|
|
||||||
}
|
|
||||||
|
|
||||||
filters.forEach(f => {
|
filters.forEach(f => {
|
||||||
rows = rows.filter(f)
|
rows = rows.filter(f)
|
||||||
@ -36,21 +30,29 @@ class FilterNode extends Node {
|
|||||||
return rows
|
return rows
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
_assignProps = props => {
|
_assignProps = props => {
|
||||||
this.filterParams = props.filterParams || {}
|
this.filterParams = props.filterParams || {}
|
||||||
if (props.type) this.setType(props.type)
|
if (props.type) this.setType(props.type)
|
||||||
}
|
}
|
||||||
|
|
||||||
_createFilterMethods = () => {
|
_createFilterMethods = () => {
|
||||||
|
const typeValidation = this._validateType(this.type)
|
||||||
|
if (typeValidation.status !== 'OK') throw typeValidation
|
||||||
|
|
||||||
let filters = []
|
let filters = []
|
||||||
for (let key in this.filterParams) {
|
for (let key in this.filterParams) {
|
||||||
let filterMethod = {}
|
let filterMethod = {}
|
||||||
if (this.type === filterTypes.EQUAL){
|
if (this.type === filterTypes.EQUAL)
|
||||||
filterMethod = t => {
|
filterMethod = t => t[key] === this.filterParams[key]
|
||||||
return t[key] === this.filterParams[key]
|
else if (this.type === filterTypes.GREATER)
|
||||||
}
|
filterMethod = t => t[key] > this.filterParams[key]
|
||||||
}
|
else if (this.type === filterTypes.GREATEREQUAL)
|
||||||
|
filterMethod = t => t[key] >= this.filterParams[key]
|
||||||
|
else if (this.type === filterTypes.LESSER)
|
||||||
|
filterMethod = t => t[key] < this.filterParams[key]
|
||||||
|
else if (this.type === filterTypes.LESSEREQUAL)
|
||||||
|
filterMethod = t => t[key] <= this.filterParams[key]
|
||||||
|
|
||||||
filters.push(filterMethod)
|
filters.push(filterMethod)
|
||||||
}
|
}
|
||||||
return filters
|
return filters
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import FilterNode from '../../../core/entities/nodes/FilterNode.js'
|
import FilterNode from '../../../core/entities/nodes/FilterNode.js'
|
||||||
import Table from '../../../core/entities/Table.js'
|
import Table from '../../../core/entities/Table.js'
|
||||||
|
|
||||||
const exportRows = () => {
|
const equalFilter = () => {
|
||||||
const expectedOutput = [
|
const expectedOutput = [
|
||||||
{ id: 'abc', data: 'row', contractor: 'AshBritt' },
|
{ id: 'abc', data: 'row', contractor: 'AshBritt' },
|
||||||
{ id: 'XYZ', data: 'row', contractor: 'AshBritt' }
|
{ id: 'XYZ', data: 'row', contractor: 'AshBritt' }
|
||||||
@ -101,6 +101,55 @@ const addFilter = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const exportWithoutTypeErrorHandle = () => {
|
||||||
|
const expectedOutput = {
|
||||||
|
status: 'ERR',
|
||||||
|
error: {
|
||||||
|
label: 'Filter Type is not valid',
|
||||||
|
messages: ['Type must be one of: EQUAL,GREATER,GREATEREQUAL,LESSER,LESSEREQUAL']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let table = {}
|
||||||
|
try {
|
||||||
|
table = new Table({
|
||||||
|
id: 'XYZ',
|
||||||
|
label: 'Test Table',
|
||||||
|
rows: [
|
||||||
|
{ id: 'abc', data: 'row', contractor: 'AshBritt' }
|
||||||
|
]
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
let filterNode = {}
|
||||||
|
try {
|
||||||
|
filterNode = new FilterNode({
|
||||||
|
id: 'ABC',
|
||||||
|
label: 'Test Node',
|
||||||
|
tables: [table],
|
||||||
|
filterParams: {
|
||||||
|
data: 'row',
|
||||||
|
contractor: 'AshBritt'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
filterNode.export()
|
||||||
|
} catch (err) {
|
||||||
|
if (JSON.stringify(err) === JSON.stringify(expectedOutput)) {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const setType = () => {
|
const setType = () => {
|
||||||
const expectedOutput = [
|
const expectedOutput = [
|
||||||
{ id: 'abc', data: 'row', contractor: 'AshBritt' },
|
{ id: 'abc', data: 'row', contractor: 'AshBritt' },
|
||||||
@ -154,9 +203,221 @@ const setType = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const greaterFilter = () => {
|
||||||
|
const expectedOutput = [
|
||||||
|
{ id: 'abc', count: 5, contractor: 'AshBritt' },
|
||||||
|
{ id: 'XYZ', count: 8, contractor: 'AshBritt' }
|
||||||
|
]
|
||||||
|
|
||||||
|
let table = {}
|
||||||
|
try {
|
||||||
|
table = new Table({
|
||||||
|
id: 'XYZ',
|
||||||
|
label: 'Test Table',
|
||||||
|
rows: [
|
||||||
|
{ id: 'abc', count: 5, contractor: 'AshBritt' },
|
||||||
|
{ id: 'qwe', count: 4, contractor: 'AshBritt' },
|
||||||
|
{ id: 'XYZ', count: 8, contractor: 'AshBritt' },
|
||||||
|
{ id: 'xyz', count: 2, contractor: 'HeyDay' },
|
||||||
|
]
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
let filterNode = {}
|
||||||
|
try {
|
||||||
|
filterNode = new FilterNode({
|
||||||
|
id: 'ABC',
|
||||||
|
label: 'Test Node',
|
||||||
|
tables: [table],
|
||||||
|
filterParams: {
|
||||||
|
count: 4
|
||||||
|
},
|
||||||
|
type: 'GREATER'
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
let filteredRows = []
|
||||||
|
try {
|
||||||
|
filteredRows = filterNode.export()
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (JSON.stringify(filteredRows) === JSON.stringify(expectedOutput)) {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const greaterEqualFilter = () => {
|
||||||
|
const expectedOutput = [
|
||||||
|
{ id: 'abc', count: 5, contractor: 'AshBritt' },
|
||||||
|
{ id: 'qwe', count: 4, contractor: 'AshBritt' },
|
||||||
|
{ id: 'XYZ', count: 8, contractor: 'AshBritt' }
|
||||||
|
]
|
||||||
|
|
||||||
|
let table = {}
|
||||||
|
try {
|
||||||
|
table = new Table({
|
||||||
|
id: 'XYZ',
|
||||||
|
label: 'Test Table',
|
||||||
|
rows: [
|
||||||
|
{ id: 'abc', count: 5, contractor: 'AshBritt' },
|
||||||
|
{ id: 'qwe', count: 4, contractor: 'AshBritt' },
|
||||||
|
{ id: 'XYZ', count: 8, contractor: 'AshBritt' },
|
||||||
|
{ id: 'xyz', count: 2, contractor: 'HeyDay' },
|
||||||
|
]
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
let filterNode = {}
|
||||||
|
try {
|
||||||
|
filterNode = new FilterNode({
|
||||||
|
id: 'ABC',
|
||||||
|
label: 'Test Node',
|
||||||
|
tables: [table],
|
||||||
|
filterParams: {
|
||||||
|
count: 4
|
||||||
|
},
|
||||||
|
type: 'GREATEREQUAL'
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
let filteredRows = []
|
||||||
|
try {
|
||||||
|
filteredRows = filterNode.export()
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (JSON.stringify(filteredRows) === JSON.stringify(expectedOutput)) {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const lesserFilter = () => {
|
||||||
|
const expectedOutput = [
|
||||||
|
{ id: 'xyz', count: 2, contractor: 'HeyDay' }
|
||||||
|
]
|
||||||
|
|
||||||
|
let table = {}
|
||||||
|
try {
|
||||||
|
table = new Table({
|
||||||
|
id: 'XYZ',
|
||||||
|
label: 'Test Table',
|
||||||
|
rows: [
|
||||||
|
{ id: 'abc', count: 5, contractor: 'AshBritt' },
|
||||||
|
{ id: 'qwe', count: 4, contractor: 'AshBritt' },
|
||||||
|
{ id: 'XYZ', count: 8, contractor: 'AshBritt' },
|
||||||
|
{ id: 'xyz', count: 2, contractor: 'HeyDay' },
|
||||||
|
]
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
let filterNode = {}
|
||||||
|
try {
|
||||||
|
filterNode = new FilterNode({
|
||||||
|
id: 'ABC',
|
||||||
|
label: 'Test Node',
|
||||||
|
tables: [table],
|
||||||
|
filterParams: {
|
||||||
|
count: 4
|
||||||
|
},
|
||||||
|
type: 'LESSER'
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
let filteredRows = []
|
||||||
|
try {
|
||||||
|
filteredRows = filterNode.export()
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (JSON.stringify(filteredRows) === JSON.stringify(expectedOutput)) {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const lesserEqualFilter = () => {
|
||||||
|
const expectedOutput = [
|
||||||
|
{ id: 'qwe', count: 4, contractor: 'AshBritt' },
|
||||||
|
{ id: 'xyz', count: 2, contractor: 'HeyDay' }
|
||||||
|
]
|
||||||
|
|
||||||
|
let table = {}
|
||||||
|
try {
|
||||||
|
table = new Table({
|
||||||
|
id: 'XYZ',
|
||||||
|
label: 'Test Table',
|
||||||
|
rows: [
|
||||||
|
{ id: 'abc', count: 5, contractor: 'AshBritt' },
|
||||||
|
{ id: 'qwe', count: 4, contractor: 'AshBritt' },
|
||||||
|
{ id: 'XYZ', count: 8, contractor: 'AshBritt' },
|
||||||
|
{ id: 'xyz', count: 2, contractor: 'HeyDay' },
|
||||||
|
]
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
let filterNode = {}
|
||||||
|
try {
|
||||||
|
filterNode = new FilterNode({
|
||||||
|
id: 'ABC',
|
||||||
|
label: 'Test Node',
|
||||||
|
tables: [table],
|
||||||
|
filterParams: {
|
||||||
|
count: 4
|
||||||
|
},
|
||||||
|
type: 'LESSEREQUAL'
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
let filteredRows = []
|
||||||
|
try {
|
||||||
|
filteredRows = filterNode.export()
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (JSON.stringify(filteredRows) === JSON.stringify(expectedOutput)) {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default [
|
export default [
|
||||||
{ name: 'Entity | FilterNode Export Rows', test: exportRows },
|
{ name: 'Entity | FilterNode Equal Filter', test: equalFilter },
|
||||||
|
{ name: 'Entity | FilterNode GREATER Filter', test: greaterFilter },
|
||||||
|
{ name: 'Entity | FilterNode GREATEREQUAL Filter', test: greaterEqualFilter },
|
||||||
|
{ name: 'Entity | FilterNode LESSER Filter', test: lesserFilter },
|
||||||
|
{ name: 'Entity | FilterNode LESSEREQUAL Filter', test: lesserEqualFilter },
|
||||||
{ name: 'Entity | FilterNode Add Filter', test: addFilter },
|
{ name: 'Entity | FilterNode Add Filter', test: addFilter },
|
||||||
{ name: 'Entity | FilterNode Set Type', test: setType },
|
{ name: 'Entity | FilterNode Set Type', test: setType },
|
||||||
|
{ name: 'Entity | FilterNode Export Without Type Error', test: exportWithoutTypeErrorHandle },
|
||||||
]
|
]
|
Loading…
x
Reference in New Issue
Block a user