Skip to content

Commit f13d362

Browse files
committed
feat: table: add Table.FormatColumn()
1 parent c1058ff commit f13d362

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

data/table/format.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ func (tbl *Table) Pivot(colCount uint, haveColumns bool) (Table, error) {
5656
return newTbl, nil
5757
}
5858

59+
// FormatColumn takes a function to format all cell values.
60+
func (tbl *Table) FormatColumn(colIdx uint, conv func(cellVal string) (string, error)) error {
61+
colInt := int(colIdx)
62+
for i, row := range tbl.Rows {
63+
if colInt >= len(row) {
64+
return fmt.Errorf("row [%d] is len [%d] without col index [%d]", i, len(row), colInt)
65+
}
66+
newVal, err := conv(row[colInt])
67+
if err != nil {
68+
return err
69+
}
70+
tbl.Rows[i][colInt] = newVal
71+
}
72+
return nil
73+
}
74+
5975
// String writes the table out to a CSV string.
6076
func (tbl *Table) String(comma rune, useCRLF bool) (string, error) {
6177
var b bytes.Buffer

0 commit comments

Comments
 (0)