Skip to content

Commit 18ac355

Browse files
committed
convert: Add new .convert.tableToHtml
1 parent 2767bc0 commit 18ac355

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/convert.q

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// Type Conversion Functions
2-
// Copyright (c) 2015 - 2017 Sport Trades Ltd
2+
// Copyright (c) 2015 - 2020 Sport Trades Ltd
33

44
// Documentation: https://github.com/BuaBook/kdb-common/wiki/convert.q
55

66
.require.lib `type;
77

8+
89
/ @returns (Timespan) The supplied milliseconds in timespan form
910
.convert.msToTimespan:{
1011
:`timespan$1e6*x;
@@ -53,3 +54,33 @@
5354
.convert.genericListToString:{[list]
5455
:(raze/) .type.ensureString@/:list;
5556
};
57+
58+
/ Converts a kdb table into a HTML <table> representation of it
59+
/ @param tbl (Table) A table with all values of the table convertable to string by '.type.ensureString'
60+
/ @returns (String) A HTML version of the table
61+
/ @throws IllegalArgumentException If the parameter is not a table
62+
/ @throws MixedListColumnsNotSupportedException If any mixed list columns are present in the table
63+
/ @see .type.ensureString
64+
.convert.tableToHtml:{[tbl]
65+
if[not .type.isTable tbl;
66+
'"IllegalArgumentException";
67+
];
68+
69+
if[.type.isKeyedTable tbl;
70+
tbl:0!tbl;
71+
];
72+
73+
badColumns:where .type.isMixedList each .Q.V tbl;
74+
badColumns:badColumns except where .type.isString each badColumns#first tbl;
75+
76+
if[0 < count badColumns;
77+
'"MixedListColumnsNotSupportedException (Columns: ",.convert.listToString[badColumns],")";
78+
];
79+
80+
header:.h.htc[`thead;] .h.htc[`tr;] raze .h.htc[`th;] each .type.ensureString each cols tbl;
81+
82+
body:"\n" sv { .h.htc[`tr;] raze .h.htc[`td;] each .type.ensureString each x } each tbl;
83+
84+
:"\n",.h.htc[`table;] header,"\n",body;
85+
};
86+

0 commit comments

Comments
 (0)