Skip to content

Commit

Permalink
feat: table: add Table.FormatColumn()
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Aug 13, 2021
1 parent c1058ff commit f13d362
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions data/table/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ func (tbl *Table) Pivot(colCount uint, haveColumns bool) (Table, error) {
return newTbl, nil
}

// FormatColumn takes a function to format all cell values.
func (tbl *Table) FormatColumn(colIdx uint, conv func(cellVal string) (string, error)) error {
colInt := int(colIdx)
for i, row := range tbl.Rows {
if colInt >= len(row) {
return fmt.Errorf("row [%d] is len [%d] without col index [%d]", i, len(row), colInt)
}
newVal, err := conv(row[colInt])
if err != nil {
return err
}
tbl.Rows[i][colInt] = newVal
}
return nil
}

// String writes the table out to a CSV string.
func (tbl *Table) String(comma rune, useCRLF bool) (string, error) {
var b bytes.Buffer
Expand Down

0 comments on commit f13d362

Please sign in to comment.