feat: transform noide

This commit is contained in:
Joshua Shoemaker 2020-06-17 00:01:25 -05:00
parent be1d1a6333
commit 032520203d
3 changed files with 166 additions and 1 deletions

View File

@ -0,0 +1,56 @@
import Node from '../Node.js'
class TransformNode extends Node {
constructor (props) {
super(props)
this._assignProps(props)
}
export = () => {
const rows = this.tables.map(t => t.export()).flat()
const transformedRows = rows.map(r => {
let mapShape = {}
for (const [ key, value ] of Object.entries(this.structure)) {
mapShape[value] = r[key]
}
return mapShape
})
return transformedRows
}
setStructure = struct => {
const structureValidation = this._validateStructureProps(struct)
if (structureValidation.status === 'ERR') throw structureValidation
else this.structure = struct
}
_assignProps = props => {
if (props.structure) this.setStructure(props.structure)
}
_validateStructureProps = struct => {
const err = {
status: 'ERR',
error: {
label: 'Ptructure Parameters are not valid',
messages: []
}
}
if (!struct) {
err.error.messages.push('No structure provided')
return err
}
for (let key in struct) {
if (typeof struct[key] !== 'string')
err.error.messages.push(`Key [${struct}] is not a String`)
}
if (err.error.messages.length > 0) return err
else return { status: 'OK' }
}
}
export default TransformNode

View File

@ -0,0 +1,107 @@
import Table from '../../../core/entities/Table.js'
import TransformNode from '../../../core/entities/nodes/TransformNode.js'
const transformTable = () => {
const expectedOutput = [
{ identifier: 'abc', ticketType: 'row', contractor: 'AshBritt' },
{ identifier: 'qwe', ticketType: 'lh', contractor: 'AshBritt' },
{ identifier: 'XYZ', ticketType: 'row', contractor: 'AshBritt' },
{ identifier: 'XYZ', ticketType: 'row', contractor: 'HeyDay' },
]
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) {
console.log(err)
return false
}
let transformNode = {}
try {
transformNode = new TransformNode({
id: 'ABC',
label: 'Transform Test',
tables: [table],
structure: {
'id': 'identifier',
'data': 'ticketType',
'contractor': 'contractor'
}
})
} catch (err) {
console.log(err)
return false
}
const transformRows = transformNode.export()
if (JSON.stringify(transformRows) === JSON.stringify(expectedOutput)) return true
else return false
}
const setStructure = () => {
const expectedOutput = [
{ identifier: 'abc', ticketType: 'row', contractor: 'AshBritt' },
{ identifier: 'qwe', ticketType: 'lh', contractor: 'AshBritt' },
{ identifier: 'XYZ', ticketType: 'row', contractor: 'AshBritt' },
{ identifier: 'XYZ', ticketType: 'row', contractor: 'HeyDay' },
]
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) {
console.log(err)
return false
}
let transformNode = {}
try {
transformNode = new TransformNode({
id: 'ABC',
label: 'Transform Test',
tables: [table],
})
} catch (err) {
console.log(err)
return false
}
try {
transformNode.setStructure({
'id': 'identifier',
'data': 'ticketType',
'contractor': 'contractor'
})
} catch (err) {
console.log(err)
return false
}
const transformRows = transformNode.export()
if (JSON.stringify(transformRows) === JSON.stringify(expectedOutput)) return true
else return false
}
export default [
{ name: 'Entities | Transform Node Test', test: transformTable },
{ name: 'Entities | Transform Node Set Structure', test: setStructure }
]

View File

@ -4,6 +4,7 @@ import tableTests from '../tests/core/tableTests.js'
import nodeTests from '../tests/core/nodeTests.js'
import filterNodeTests from '../tests/core/nodes/filterNodeTests.js'
import joinNodeTests from '../tests/core/nodes/joinNodeTests.js'
import transformNodeTests from '../tests/core/nodes/transformNodeTests.js'
function runTestsAndReturnFailures (tests) {
const testTotalCount = tests.length
@ -41,7 +42,8 @@ const testsArray = [
tableTests,
nodeTests,
filterNodeTests,
joinNodeTests
joinNodeTests,
transformNodeTests
]
init (testsArray.flat())