From 2d173a6b7af71472971b81c3c424617befec5619 Mon Sep 17 00:00:00 2001 From: ysandler Date: Tue, 28 Jul 2020 20:49:00 -0500 Subject: [PATCH] feat: headers getter from table --- package.json | 1 + src/entities/Table.js | 16 +++++++++++ tests/core/NoduleTests.js | 3 +++ tests/core/nodules/filterNoduleTests.js | 1 + tests/core/tableTests.js | 35 +++++++++++++++++++------ 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index dbd0ef2..689996e 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.1.4", "description": "Dmein is a modern JavaScript Library to create objects that easily mutate data through relationships, filtering, and tranforming the shape of data.", "main": "index.js", + "type": "module", "directories": { "test": "tests" }, diff --git a/src/entities/Table.js b/src/entities/Table.js index e92a510..f8870ce 100644 --- a/src/entities/Table.js +++ b/src/entities/Table.js @@ -10,11 +10,27 @@ class Table { id: this.id, label: this.label, rows: this.rows, + headers: this.headers, type: this.type, isValid: this.isValid } } + get headers () { + const rows = this.rows + + if (!Array.isArray(rows) || rows.length < 1) return [] + + const length = rows.length + let lengthToSlice = 49 + if (length < 50) lengthToSlice = length + const firstSliceOfRows = rows.slice(0, lengthToSlice) + const headersOfSplicedRows = firstSliceOfRows.map(r => Object.keys(r)) + const flatenedHeaders = headersOfSplicedRows.flat() + const uniqueHeaders = Array.from(new Set(flatenedHeaders)) + return uniqueHeaders + } + export = () => this.rows setRows = rows => { diff --git a/tests/core/NoduleTests.js b/tests/core/NoduleTests.js index d0ede39..45fcc76 100644 --- a/tests/core/NoduleTests.js +++ b/tests/core/NoduleTests.js @@ -23,6 +23,7 @@ const getNodeProperties = () => { id: 'XYZ', label: 'Test Table', rows: [{ id: 'abc', data: 'row' }], + headers: [ 'id', 'data' ], type: 'Table', isValid: true } @@ -79,6 +80,7 @@ const importTables = () => { id: 'XYZ', label: 'Test Table', rows: [{ id: 'abc', data: 'row' }], + headers: [ 'id', 'data' ], type: 'Table', isValid: true }], @@ -134,6 +136,7 @@ const setTables = () => { id: 'XYZ', label: 'Test Table', rows: [{ id: 'abc', data: 'row' }], + headers: [ 'id', 'data' ], type: 'Table', isValid: true }], diff --git a/tests/core/nodules/filterNoduleTests.js b/tests/core/nodules/filterNoduleTests.js index 1faad8d..6a02d14 100644 --- a/tests/core/nodules/filterNoduleTests.js +++ b/tests/core/nodules/filterNoduleTests.js @@ -419,6 +419,7 @@ const getAsTable = () => { { id: 'qwe', count: 4, contractor: 'AshBritt' }, { id: 'xyz', count: 2, contractor: 'HeyDay' } ], + headers: [ 'id', 'count', 'contractor' ], type: 'Table', isValid: true } diff --git a/tests/core/tableTests.js b/tests/core/tableTests.js index 9c5a984..59aa0aa 100644 --- a/tests/core/tableTests.js +++ b/tests/core/tableTests.js @@ -17,6 +17,7 @@ const getTableProperties = () => { { id: '2345676', type: 'row', lat: 54, long: 31 }, { id: '2345676', type: 'lh', lat: 31, long: -71.34 } ], + headers: [ "id", "type", "lat", "long" ], type: 'Table', isValid: true } @@ -115,6 +116,7 @@ const createTableWithEverythingButRows = () => { id: 'abc', label: 'Test Label', rows: [], + headers: [], type: 'Table', isValid: true } @@ -171,13 +173,14 @@ const createTableWithRowsAsNotArray = () => { rows: [ { id: '2345676', type: 'lh', lat: 31, long: -71.34 } ], + headers: [ 'id', 'type', 'lat', 'long' ], type: 'Table', isValid: true } try { const table = new Table(input) - const tableProperties = table.getProperties() + const tableProperties = table.getProperties() if (JSON.stringify(tableProperties) === JSON.stringify(expectedOutput)) return true else return false } catch (err) { @@ -205,6 +208,7 @@ const setTableRows = () => { { id: '2345676', type: 'row', lat: 54, long: 31 }, { id: '2345676', type: 'lh', lat: 31, long: -71.34 } ], + headers: [ 'id', 'type', 'lat', 'long' ], type: 'Table', isValid: true } @@ -221,14 +225,29 @@ const setTableRows = () => { } } +const getTableHeaders = () => { + const expectedOutput = ["id", "type", "lat", "long"] + + try { + const table = new Table(input) + const headers = table.headers + if (JSON.stringify(headers) === JSON.stringify(expectedOutput)) return true + else return false + } catch (err) { + console.log(err) + return false + } +} + export default [ { name: 'Entity | Get Table Properties', test: getTableProperties }, { name: 'Entity | Get Table Rows', test: getTableRows }, - { name: 'Entiry | Table With Invalid Props', test: createTableWithNoProps }, - { name: 'Entiry | Table With Only Id', test: createTableWithOnlyId }, - { name: 'Entiry | Table With Only Label', test: createTableWithOnlyLabel }, - { name: 'Entiry | Table With Everything But Rows', test: createTableWithEverythingButRows }, - { name: 'Entiry | Table With Invalid Rows', test: createTableWithInvalidRows }, - { name: 'Entiry | Table With Rows as Not Array', test: createTableWithRowsAsNotArray }, - { name: 'Entiry | Set Table Rows', test: setTableRows } + { name: 'Entity | Table With Invalid Props', test: createTableWithNoProps }, + { name: 'Entity | Table With Only Id', test: createTableWithOnlyId }, + { name: 'Entity | Table With Only Label', test: createTableWithOnlyLabel }, + { name: 'Entity | Table With Everything But Rows', test: createTableWithEverythingButRows }, + { name: 'Entity | Table With Invalid Rows', test: createTableWithInvalidRows }, + { name: 'Entity | Table With Rows as Not Array', test: createTableWithRowsAsNotArray }, + { name: 'Entity | Set Table Rows', test: setTableRows }, + { name: 'Entity | Table Headers Getter', test: getTableHeaders } ] \ No newline at end of file