@@ -2,12 +2,13 @@ import * as R from './functions'
22import repeat from 'core-js/library/fn/string/repeat'
33
44const len = ( val ) => typeof val === 'undefined' ? 0 : ( '' + val ) . length
5+ const arrLen = ( arr ) => arr . length
6+ const arrMax = ( arr ) => R . apply ( Math . max , arr )
7+ const matrixCol = ( matrix ) => ( colNr ) => R . pluck ( colNr , matrix )
58const padString = ( character ) => ( width ) => ! width ? '' : repeat ( character , width )
69const spacePad = padString ( ' ' )
7- const stringifyRows = ( rows ) => {
8- if ( ! Array . isArray ( rows ) || ! rows . length ) return [ ]
9- return rows . map ( ( row ) => row . map ( JSON . stringify ) )
10- }
10+ const stringifyArray = R . cMap ( JSON . stringify )
11+ const stringifyRows = ( rows ) => R . EitherArray ( rows ) . fold ( ( ) => null , R . cMap ( stringifyArray ) )
1112const insertColSeparators = ( arr ) => '│' + arr . join ( '│' ) + '│'
1213const getTopSeparatorLine = ( colWidths ) => getSeparatorLine ( '═' , '╒' , '╤' , '╕' , colWidths )
1314const getThickSeparatorLine = ( colWidths ) => getSeparatorLine ( '═' , '╞' , '╪' , '╡' , colWidths )
@@ -20,19 +21,23 @@ const getSeparatorLine = (horChar, leftChar, crossChar, rightChar, colWidths) =>
2021}
2122
2223const colWidths = ( maxWidth , minWidth , input ) => {
23- if ( ! Array . isArray ( input ) ) {
24- return 0
25- }
26- return input [ 0 ] . map ( ( _ , i ) => {
27- const tCol = R . pluck ( i , input ) . map ( ( col ) => len ( col ) )
28- const measuredMax = Math . max ( R . apply ( Math . max , tCol ) , minWidth )
29- return measuredMax > maxWidth && maxWidth > 0 ? maxWidth : measuredMax
30- } )
24+ const inputEither = R . EitherArray ( input )
25+ const columnAtIndex = matrixCol ( input )
26+ const normalizeWidth = ( w ) => Math . min ( Math . max ( w , minWidth ) , ( maxWidth || Infinity ) )
27+ return inputEither
28+ . map ( ( r ) => R . head ( r ) [ 0 ] ) // Grab title row
29+ . map ( arrLen ) // Get the number of columns
30+ . map ( R . array ) // Create a new array with same number of columns
31+ . map ( R . cMap ( columnAtIndex ) ) // Populate new array with columns from input
32+ . map ( R . cMap ( R . cMap ( len ) ) ) // Measure the width of every column of every row
33+ . map ( R . cMap ( arrMax ) ) // Grab the max width of every column
34+ . map ( R . cMap ( normalizeWidth ) ) // Normalize width to be within limits
35+ . fold ( ( ) => [ 0 ] , R . id ) // default to 0
3136}
3237
3338const rowHeights = ( maxWidth , input ) => {
3439 return input . map ( ( row ) => {
35- const maxLen = R . apply ( Math . max , row . map ( ( col ) => len ( col ) ) )
40+ const maxLen = arrMax ( row . map ( len ) )
3641 const numLines = Math . ceil ( maxLen / maxWidth )
3742 return numLines
3843 } )
@@ -88,5 +93,5 @@ export default {
8893 serializeData : ( rows ) => stringifyRows ( rows ) ,
8994 tableFromSerializedData : ( serializedRows , maxColumnWidth = 30 ) => main ( serializedRows , maxColumnWidth ) ,
9095 table : ( rows , maxColumnWidth = 30 ) => main ( stringifyRows ( rows ) , maxColumnWidth ) ,
91- maxColumnWidth : ( rows ) => R . apply ( Math . max , colWidths ( 0 , 0 , stringifyRows ( rows ) ) )
96+ maxColumnWidth : ( rows ) => arrMax ( colWidths ( 0 , 0 , stringifyRows ( rows ) ) )
9297}
0 commit comments