Skip to content

Commit ec04cbd

Browse files
committed
feat: add @stdlib/plot/table/unicode
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
1 parent b4c12b7 commit ec04cbd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+5714
-0
lines changed

lib/node_modules/@stdlib/plot/table/unicode/README.md

Lines changed: 407 additions & 0 deletions
Large diffs are not rendered by default.

lib/node_modules/@stdlib/plot/table/unicode/benchmark/benchmark.js

Lines changed: 495 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var Float64Array = require( '@stdlib/array/float64' );
27+
var array = require( '@stdlib/ndarray/array' );
28+
var pkg = require( './../package.json' ).name;
29+
var UnicodeTable = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} rows - number of rows
39+
* @param {PositiveInteger} columns - number of columns
40+
* @param {UnicodeSparkline} table - table instance
41+
* @returns {Function} benchmark function
42+
*/
43+
function createBenchmark( rows, columns, table ) {
44+
return benchmark;
45+
46+
/**
47+
* Generates table data.
48+
*
49+
* @private
50+
* @returns {ndarray} table data
51+
*/
52+
function data() {
53+
var data;
54+
var i;
55+
56+
data = new Float64Array( rows * columns );
57+
for ( i = 0; i < data.length; i++ ) {
58+
data[ i ] = randu();
59+
}
60+
data = array( data, {
61+
'shape': [ rows, columns ]
62+
});
63+
return data;
64+
}
65+
66+
/**
67+
* Generates table headers.
68+
*
69+
* @private
70+
* @returns {Float64Array} table data
71+
*/
72+
function headers() {
73+
var data;
74+
var i;
75+
76+
data = new Float64Array( columns );
77+
for ( i = 0; i < data.length; i++ ) {
78+
data[ i ] = randu();
79+
}
80+
return data;
81+
}
82+
83+
/**
84+
* Benchmark function.
85+
*
86+
* @private
87+
* @param {Benchmark} b - benchmark instance
88+
*/
89+
function benchmark( b ) {
90+
var str;
91+
var i;
92+
93+
b.tic();
94+
for ( i = 0; i < b.iterations; i++ ) {
95+
str = table.setData( data(), headers() ).render();
96+
if ( typeof str !== 'string' ) {
97+
b.fail( 'should return a string' );
98+
}
99+
}
100+
b.toc();
101+
if ( typeof str !== 'string' ) {
102+
b.fail( 'should return a string' );
103+
}
104+
b.pass( 'benchmark finished' );
105+
b.end();
106+
}
107+
}
108+
109+
110+
// MAIN //
111+
112+
/**
113+
* Main execution sequence.
114+
*
115+
* @private
116+
*/
117+
function main() {
118+
var columns;
119+
var table;
120+
var rows;
121+
var min;
122+
var max;
123+
var f;
124+
var i;
125+
var j;
126+
127+
min = 1; // 5^min
128+
max = 3; // 5^max
129+
130+
for ( i = min; i <= max; i++ ) {
131+
for ( j = min; j <= max; j++ ) {
132+
rows = pow( 5, i );
133+
columns = pow( 5, j );
134+
135+
table = new UnicodeTable();
136+
f = createBenchmark( rows, columns, table );
137+
bench( pkg+':render:rows='+rows+',columns='+columns, f );
138+
}
139+
}
140+
}
141+
142+
main();
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
2+
{{alias}}( [data,] [headers,] [options] )
3+
Returns a Unicode table instance.
4+
5+
Parameters
6+
----------
7+
data: Object|Array<Object>|Array<Array>|MatrixLike (optional)
8+
Table data.
9+
10+
headers: Collection (optional)
11+
Table headers.
12+
13+
options: Object (optional)
14+
Table options.
15+
16+
options.alignment: string (optional)
17+
Datum's cell alignment. Default: 'right'.
18+
19+
options.borders: string (optional)
20+
Border characters. Default: '─ │ ─ │'.
21+
22+
options.cellPadding: NonNegativeInteger (optional)
23+
Cell padding. Default: 1.
24+
25+
options.columnSeparator: string (optional)
26+
Column separator character. Default: '│'.
27+
28+
options.corners: string (optional)
29+
Corner characters. Default: '┌ ┐ ┘ └'.
30+
31+
options.headerSeparator: string (optional)
32+
Header separator character. Default: '─'.
33+
34+
options.joints: string (optional)
35+
Joint characters. Default: '┼ ┬ ┤ ┴ ├'.
36+
37+
options.marginX: NonNegativeInteger (optional)
38+
Horizontal output margin. Default: 0.
39+
40+
options.marginY: NonNegativeInteger (optional)
41+
Vertical output margin. Default: 0.
42+
43+
options.maxCellWidth: NonNegativeInteger (optional)
44+
Maximum cell width (excluding padding). Default: FLOAT64_MAX.
45+
46+
options.maxOutputWidth: NonNegativeInteger (optional)
47+
Maximum output width (including margin). Default: FLOAT64_MAX.
48+
49+
options.rowSeparator: string (optional)
50+
Row separator character. Default: 'None'.
51+
52+
Returns
53+
-------
54+
table.addRow( row )
55+
Adds a row to table data.
56+
57+
table.alignment
58+
Datum's cell alignment.
59+
60+
table.borders
61+
Border characters.
62+
63+
table.cellPadding
64+
Cell Padding.
65+
66+
table.columnSeparator
67+
Column separator character.
68+
69+
table.corners
70+
Corner characters.
71+
72+
table.getData()
73+
Gets table data and headers.
74+
75+
table.headerSeparator
76+
Header separator character.
77+
78+
table.joints
79+
Joint characters.
80+
81+
table.marginX
82+
Horizontal output margin.
83+
84+
table.marginY
85+
Vertical output margin.
86+
87+
table.maxCellWidth
88+
Maximum cell width.
89+
90+
table.maxOutputWidth
91+
Maximum output width.
92+
93+
table.render()
94+
Renders a table.
95+
96+
table.rowSeparator
97+
Row separator character.
98+
99+
table.setData( data, [headers] )
100+
Sets table data and headers
101+
102+
Examples
103+
--------
104+
> var data = [ [ 45, 33, 'hello' ], [ 32.54, true, null ] ];
105+
> var headers = [ 'col1', 'col2', 'col3' ];
106+
> var table = new {{alias}}( data, headers );
107+
> table.render()
108+
'...'
109+
110+
See Also
111+
--------
112+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var randu = require( '@stdlib/random/base/randu' );
22+
var Float64Array = require( '@stdlib/array/float64' );
23+
var array = require( '@stdlib/ndarray/array' );
24+
var ctor = require( './../lib' );
25+
26+
var headers;
27+
var table;
28+
var data;
29+
var str;
30+
var i;
31+
32+
// Generate some random data...
33+
data = new Float64Array( 50 );
34+
for ( i = 0; i < data.length; i++ ) {
35+
data[ i ] = randu() * 100.0;
36+
}
37+
data = array( data, {
38+
'shape': [ 10, 5 ]
39+
});
40+
41+
// Generate headers...
42+
headers = new Float64Array( 5 );
43+
for ( i = 0; i < headers.length; i++ ) {
44+
headers[ i ] = randu() * 100.0;
45+
}
46+
47+
// Create a table:
48+
table = ctor( data, headers );
49+
50+
// Render the table:
51+
str = table.render();
52+
console.log( str );
53+
// => '...'
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var format = require( '@stdlib/string/format' );
24+
var isCollection = require( '@stdlib/assert/is-collection' );
25+
var parse2dArray = require( './parser/parse_2d_array.js' );
26+
27+
28+
// MAIN //
29+
30+
/**
31+
* Adds a row to table data.
32+
*
33+
* @private
34+
* @param {Collection} row - row to add
35+
* @throws {TypeError} must provide a collection with equal number of columns to existing data
36+
* @returns {UnicodeTable} class instance
37+
*/
38+
function addRow( row ) {
39+
/* eslint-disable no-invalid-this */
40+
var parsed;
41+
42+
if ( !isCollection ) {
43+
throw new TypeError( format( 'invalid argument. Must provide a collection. Value: `%s`.', row ) );
44+
}
45+
// If data doesn't already exist, parse row as new data...
46+
if ( !this._data || !this._data.length ) {
47+
parsed = parse2dArray( [ row ], this._headers );
48+
this._data = parsed.data;
49+
this._headers = parsed.headers;
50+
this._columnWidths = parsed.columnWidths;
51+
return this;
52+
}
53+
if ( this._data[ 0 ].length !== row.length ) {
54+
throw new TypeError( format( 'invalid argument. Must provide a collection with equal number of columns to existing data. Value: `%s`.', row ) );
55+
}
56+
this._data.push( row );
57+
return this;
58+
}
59+
60+
61+
// EXPORTS //
62+
63+
module.exports = addRow;

0 commit comments

Comments
 (0)