lamed_table
Version:
Light table implementation
130 lines (123 loc) • 4.7 kB
JavaScript
// Comment out in test functions
console.log(`Starting ${__filename}...`) // comment line to remove simple logging
/* ------------------------------------------------------
* tableRows.js
* Purpose: The purpose of this module is to manage table rows
* Date Created: 2019/12/12
* Created by : Perez Lamed van Niekerk
--------------------------------------------------------- */
/* jshint esversion: 6 */
const _test = require('lamed_test')
const { Ok, notOk, notOk_Then, Equal, notEqual, con, testAND } = _test // eslint-disable-line
// con.setupChalk(require('chalk'))
// con.traceSet(0)
const _cols = require('./tableCols')
/**
* Add row to the table
* @param {object} table - the table object
* @param {array} items - string array of items to add to the table
*/
function rowAdd (table, items) {
// ['item1', 'item2', 'item3']
const colCount = table.cols.length
const itemCount = items.length
if (itemCount !== colCount) throw new Error(`in tableRowAdd; Table require '${colCount}' items but row has '${itemCount}' items in '${items}'.\n`)
table.rows.push(items)
}
/**
* Append values from a list into a table. Assume table with two columns [id, column]. column can be anything
* @param {table} table - The table to append the values to
* @param {array} list - List of items to append to the table
* @param {number} id - The id value to use. Default is -1 and means auto increment id
* @param {bool} noId - If true then the table will not have an id field
*/
function TableArrayAppend (table, list, id = -1, noId = false) { // eslint-disable-line
const autoId = (id === -1)
for (let ii = 0; ii < list.length; ii++) {
const item = list[ii]
if (noId) rowAdd(table, [item])
else {
if (autoId) id = ii + 1 // No id was given -> let us calculate one
rowAdd(table, [id, item])
}
}
}
/**
* Put an value into a row at heading location
* @param {object} table - the table object
* @param {array} row - the row array
* @param {string} colNameOrNumber - the column name
* @param {string} value - the value to add
*/
function rowSet (table, row, colNameOrNumber, value) {
// Check the row and ensure all colls are present in the row
// con.log({row})
if (Array.isArray(row) === false) throw new Error(`'row' parameter must be an array and not '${row}'\n`)
if (row.length !== table.cols.length) {
while (row.length < table.cols.length) row.push('') // Add missing
while (row.length > table.cols.length) row.pop() // Remove extra
}
const col = _cols.colsNumber(table.cols, colNameOrNumber)
row[col] = value
con.trace({ row }, 1)
}
/**
* Remove extra rows from the table
* @param {table} table - The table
* @param {int} rowCount - The maximum allowed rows in the table
* @returns {void} - NA
*/
function trim (table, rowCount = -1) {
if (rowCount === -1 || rowCount > table.rows.length) return
table.rows = table.rows.slice(0, rowCount)
}
/**
* Find if a string is included in a specific column
* @param {object} table - Table object
* @param {string} colName - Column name
* @param {string} value - value to search for
* @param {bool} findFirst - if true, return the first positive
* @returns {string, array, undefined} - If nothing find -> undefined; If findFirst -> string else return row[]
*/
function rowFind_Include (table, colName, value, findFirst = true) { // eslint-disable-line
// con.setupChalk(require('chalk'))
// con.traceSet(0)
const result = []
const colNo = _cols.colsNumber(table.cols, colName)
value = value.toLowerCase()
for (let ii = 0; ii < table.rows.length; ii++) {
const row = table.rows[ii]
const item = row[colNo]
if (item.toLowerCase().includes(value)) {
if (findFirst) return row // <--------------------------------------
else result.push(row)
}
}
if (result !== []) return result
}
/**
* Manages table rows
* @param {object} table - The table object
*/
function Rows (data) {
this.DATA = data
}
/* {{rowAdd}} */
Rows.prototype.Add = function (items) {
rowAdd(this.DATA, items)
}
/* {{rowSet}} */
Rows.prototype.Set = function (row, colNameOrNumber, value) {
// con.log({row, colName, value})
rowSet(this.DATA, row, colNameOrNumber, value)
}
/* {{rowFind_Include}} */
Rows.prototype.Include = function (colName, value, findFirst = true) {
return rowFind_Include(this.DATA, colName, value, findFirst)
}
/* {{trim}} */
Rows.prototype.trim = function (rowCount = -1) {
trim(this.DATA, rowCount)
}
// Exports --------------------------
module.exports = { Rows, TableArrayAppend }