From 032520203db393f1eccf5ef77ee62698a37f4e22 Mon Sep 17 00:00:00 2001 From: Joshua Shoemaker Date: Wed, 17 Jun 2020 00:01:25 -0500 Subject: [PATCH] feat: transform noide --- core/entities/nodes/TransformNode.js | 56 +++++++++++++ tests/core/nodes/transformNodeTests.js | 107 +++++++++++++++++++++++++ tests/index.js | 4 +- 3 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 core/entities/nodes/TransformNode.js create mode 100644 tests/core/nodes/transformNodeTests.js diff --git a/core/entities/nodes/TransformNode.js b/core/entities/nodes/TransformNode.js new file mode 100644 index 0000000..a81c379 --- /dev/null +++ b/core/entities/nodes/TransformNode.js @@ -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 diff --git a/tests/core/nodes/transformNodeTests.js b/tests/core/nodes/transformNodeTests.js new file mode 100644 index 0000000..0eb1a6b --- /dev/null +++ b/tests/core/nodes/transformNodeTests.js @@ -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 } +] diff --git a/tests/index.js b/tests/index.js index de4eb5d..66e5a72 100644 --- a/tests/index.js +++ b/tests/index.js @@ -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()) \ No newline at end of file