feat: headers getter from table
This commit is contained in:
		
							parent
							
								
									c3269fcfda
								
							
						
					
					
						commit
						fb3bb45a48
					
				@ -3,6 +3,7 @@
 | 
				
			|||||||
  "version": "0.1.4",
 | 
					  "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.",
 | 
					  "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",
 | 
					  "main": "index.js",
 | 
				
			||||||
 | 
					  "type": "module",
 | 
				
			||||||
  "directories": {
 | 
					  "directories": {
 | 
				
			||||||
    "test": "tests"
 | 
					    "test": "tests"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
				
			|||||||
@ -10,11 +10,27 @@ class Table {
 | 
				
			|||||||
      id: this.id,
 | 
					      id: this.id,
 | 
				
			||||||
      label: this.label,
 | 
					      label: this.label,
 | 
				
			||||||
      rows: this.rows,
 | 
					      rows: this.rows,
 | 
				
			||||||
 | 
					      headers: this.headers,
 | 
				
			||||||
      type: this.type,
 | 
					      type: this.type,
 | 
				
			||||||
      isValid: this.isValid
 | 
					      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
 | 
					  export = () => this.rows
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  setRows = rows => {
 | 
					  setRows = rows => {
 | 
				
			||||||
 | 
				
			|||||||
@ -23,6 +23,7 @@ const getNodeProperties = () => {
 | 
				
			|||||||
        id: 'XYZ',
 | 
					        id: 'XYZ',
 | 
				
			||||||
        label: 'Test Table',
 | 
					        label: 'Test Table',
 | 
				
			||||||
        rows: [{ id: 'abc', data: 'row' }],
 | 
					        rows: [{ id: 'abc', data: 'row' }],
 | 
				
			||||||
 | 
					        headers: [ 'id', 'data' ],
 | 
				
			||||||
        type: 'Table',
 | 
					        type: 'Table',
 | 
				
			||||||
        isValid: true
 | 
					        isValid: true
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
@ -79,6 +80,7 @@ const importTables = () => {
 | 
				
			|||||||
      id: 'XYZ',
 | 
					      id: 'XYZ',
 | 
				
			||||||
      label: 'Test Table',
 | 
					      label: 'Test Table',
 | 
				
			||||||
      rows: [{ id: 'abc', data: 'row' }],
 | 
					      rows: [{ id: 'abc', data: 'row' }],
 | 
				
			||||||
 | 
					      headers: [ 'id', 'data' ],
 | 
				
			||||||
      type: 'Table',
 | 
					      type: 'Table',
 | 
				
			||||||
      isValid: true
 | 
					      isValid: true
 | 
				
			||||||
    }],
 | 
					    }],
 | 
				
			||||||
@ -134,6 +136,7 @@ const setTables = () => {
 | 
				
			|||||||
      id: 'XYZ',
 | 
					      id: 'XYZ',
 | 
				
			||||||
      label: 'Test Table',
 | 
					      label: 'Test Table',
 | 
				
			||||||
      rows: [{ id: 'abc', data: 'row' }],
 | 
					      rows: [{ id: 'abc', data: 'row' }],
 | 
				
			||||||
 | 
					      headers: [ 'id', 'data' ],
 | 
				
			||||||
      type: 'Table',
 | 
					      type: 'Table',
 | 
				
			||||||
      isValid: true
 | 
					      isValid: true
 | 
				
			||||||
    }],
 | 
					    }],
 | 
				
			||||||
 | 
				
			|||||||
@ -419,6 +419,7 @@ const getAsTable = () => {
 | 
				
			|||||||
      { id: 'qwe', count: 4, contractor: 'AshBritt' },
 | 
					      { id: 'qwe', count: 4, contractor: 'AshBritt' },
 | 
				
			||||||
      { id: 'xyz', count: 2, contractor: 'HeyDay' }
 | 
					      { id: 'xyz', count: 2, contractor: 'HeyDay' }
 | 
				
			||||||
    ],
 | 
					    ],
 | 
				
			||||||
 | 
					    headers: [ 'id', 'count', 'contractor' ],
 | 
				
			||||||
    type: 'Table',
 | 
					    type: 'Table',
 | 
				
			||||||
    isValid: true
 | 
					    isValid: true
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
@ -17,6 +17,7 @@ const getTableProperties = () => {
 | 
				
			|||||||
      { id: '2345676', type: 'row', lat: 54, long: 31 },
 | 
					      { id: '2345676', type: 'row', lat: 54, long: 31 },
 | 
				
			||||||
      { id: '2345676', type: 'lh', lat: 31, long: -71.34 }
 | 
					      { id: '2345676', type: 'lh', lat: 31, long: -71.34 }
 | 
				
			||||||
    ],
 | 
					    ],
 | 
				
			||||||
 | 
					    headers: [ "id", "type", "lat", "long" ],
 | 
				
			||||||
    type: 'Table',
 | 
					    type: 'Table',
 | 
				
			||||||
    isValid: true
 | 
					    isValid: true
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -115,6 +116,7 @@ const createTableWithEverythingButRows = () => {
 | 
				
			|||||||
    id: 'abc',
 | 
					    id: 'abc',
 | 
				
			||||||
    label: 'Test Label',
 | 
					    label: 'Test Label',
 | 
				
			||||||
    rows: [],
 | 
					    rows: [],
 | 
				
			||||||
 | 
					    headers: [],
 | 
				
			||||||
    type: 'Table',
 | 
					    type: 'Table',
 | 
				
			||||||
    isValid: true
 | 
					    isValid: true
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -171,6 +173,7 @@ const createTableWithRowsAsNotArray = () => {
 | 
				
			|||||||
    rows: [
 | 
					    rows: [
 | 
				
			||||||
      { id: '2345676', type: 'lh', lat: 31, long: -71.34 }
 | 
					      { id: '2345676', type: 'lh', lat: 31, long: -71.34 }
 | 
				
			||||||
    ],
 | 
					    ],
 | 
				
			||||||
 | 
					    headers: [ 'id', 'type', 'lat', 'long' ],
 | 
				
			||||||
    type: 'Table',
 | 
					    type: 'Table',
 | 
				
			||||||
    isValid: true
 | 
					    isValid: true
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -205,6 +208,7 @@ const setTableRows = () => {
 | 
				
			|||||||
      { id: '2345676', type: 'row', lat: 54, long: 31 },
 | 
					      { id: '2345676', type: 'row', lat: 54, long: 31 },
 | 
				
			||||||
      { id: '2345676', type: 'lh', lat: 31, long: -71.34 }
 | 
					      { id: '2345676', type: 'lh', lat: 31, long: -71.34 }
 | 
				
			||||||
    ],
 | 
					    ],
 | 
				
			||||||
 | 
					    headers: [ 'id', 'type', 'lat', 'long' ],
 | 
				
			||||||
    type: 'Table',
 | 
					    type: 'Table',
 | 
				
			||||||
    isValid: true
 | 
					    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 [
 | 
					export default [
 | 
				
			||||||
  { name: 'Entity | Get Table Properties', test: getTableProperties },
 | 
					  { name: 'Entity | Get Table Properties', test: getTableProperties },
 | 
				
			||||||
  { name: 'Entity | Get Table Rows', test: getTableRows },
 | 
					  { name: 'Entity | Get Table Rows', test: getTableRows },
 | 
				
			||||||
  { name: 'Entiry | Table With Invalid Props', test: createTableWithNoProps },
 | 
					  { name: 'Entity | Table With Invalid Props', test: createTableWithNoProps },
 | 
				
			||||||
  { name: 'Entiry | Table With Only Id', test: createTableWithOnlyId },
 | 
					  { name: 'Entity | Table With Only Id', test: createTableWithOnlyId },
 | 
				
			||||||
  { name: 'Entiry | Table With Only Label', test: createTableWithOnlyLabel },
 | 
					  { name: 'Entity | Table With Only Label', test: createTableWithOnlyLabel },
 | 
				
			||||||
  { name: 'Entiry | Table With Everything But Rows', test: createTableWithEverythingButRows },
 | 
					  { name: 'Entity | Table With Everything But Rows', test: createTableWithEverythingButRows },
 | 
				
			||||||
  { name: 'Entiry | Table With Invalid Rows', test: createTableWithInvalidRows },
 | 
					  { name: 'Entity | Table With Invalid Rows', test: createTableWithInvalidRows },
 | 
				
			||||||
  { name: 'Entiry | Table With Rows as Not Array', test: createTableWithRowsAsNotArray },
 | 
					  { name: 'Entity | Table With Rows as Not Array', test: createTableWithRowsAsNotArray },
 | 
				
			||||||
  { name: 'Entiry | Set Table Rows', test: setTableRows }
 | 
					  { name: 'Entity | Set Table Rows', test: setTableRows },
 | 
				
			||||||
 | 
					  { name: 'Entity | Table Headers Getter', test: getTableHeaders }
 | 
				
			||||||
]
 | 
					]
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user