72 lines
1.5 KiB
TypeScript
72 lines
1.5 KiB
TypeScript
import { Column, ColumnType } from './models/column'
|
|
|
|
export enum TableType {
|
|
INPUT = 'In',
|
|
OUTPUT = 'Out'
|
|
}
|
|
|
|
export interface TableInterface {
|
|
id: number | undefined
|
|
name: string | undefined
|
|
data: Array<Object>
|
|
columns: Array<Column>
|
|
type: TableType | undefined
|
|
}
|
|
|
|
export class Table implements TableInterface {
|
|
public id: number | undefined
|
|
public name: string | undefined
|
|
public data: Array<any>
|
|
public columns: Array<Column> = []
|
|
public type: TableType | undefined
|
|
|
|
public static fromPlainObject(obj: any) {
|
|
obj.columns = obj.columns.map((column: object) => {
|
|
return Column.fromPlainObject(column)
|
|
})
|
|
|
|
return Object.assign(new Table(), obj)
|
|
}
|
|
|
|
constructor(
|
|
id?: number,
|
|
name?: string,
|
|
type?: TableType,
|
|
data?: Array<Object>,
|
|
columns?: Array<Column>
|
|
) {
|
|
this.id = id
|
|
this.name = name
|
|
this.type = type
|
|
|
|
this.data = data || [{}]
|
|
this.columns = columns || []
|
|
}
|
|
|
|
public getNextColumnId(): number {
|
|
let highestIdColumn: any = this.columns.sort(
|
|
(cA: any, cB: any) => cA.id - cB.id
|
|
)[this.columns.length - 1]
|
|
return (highestIdColumn && highestIdColumn.id + 1) || 0
|
|
}
|
|
|
|
public addColumn(column: Column) {
|
|
this.columns.push(column)
|
|
|
|
this.data.forEach((row: any) => {
|
|
if (column.name) {
|
|
row[column.name] = column.type === ColumnType.string ? '' : null
|
|
}
|
|
})
|
|
|
|
return column
|
|
}
|
|
|
|
public removeColumn(colInd: any) {
|
|
this.data.forEach((row) => {
|
|
delete row[this.columns[colInd].name!]
|
|
})
|
|
this.columns.splice(colInd, 1)
|
|
}
|
|
}
|