feat: headers getter from table
This commit is contained in:
parent
c3269fcfda
commit
fb3bb45a48
@ -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"
|
||||
},
|
||||
|
@ -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 => {
|
||||
|
@ -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
|
||||
}],
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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,6 +173,7 @@ const createTableWithRowsAsNotArray = () => {
|
||||
rows: [
|
||||
{ id: '2345676', type: 'lh', lat: 31, long: -71.34 }
|
||||
],
|
||||
headers: [ 'id', 'type', 'lat', 'long' ],
|
||||
type: 'Table',
|
||||
isValid: true
|
||||
}
|
||||
@ -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 }
|
||||
]
|
Loading…
x
Reference in New Issue
Block a user