Comparing version 1.0.9 to 1.0.10
@@ -7,2 +7,4 @@ "use strict"; | ||
limit = (s, n) => s && ((s.length <= n) ? s : (s.substr (0, n - 1) + '…')), | ||
asColumns = (rows, cfg_) => { | ||
@@ -37,3 +39,3 @@ | ||
relativeWidths = maxWidths.map (w => w / totalWidth), | ||
maxTotalWidth = cfg.maxTotalWidth - (cfg.delimiter.length * maxWidths.length), | ||
maxTotalWidth = cfg.maxTotalWidth - (cfg.delimiter.length * (maxWidths.length - 1)), | ||
excessWidth = Math.max (0, totalWidth - maxTotalWidth), | ||
@@ -52,3 +54,3 @@ computedWidths = zip ([cfg.minColumnWidths, maxWidths, relativeWidths], | ||
? (str + ' '.repeat (w)) | ||
: (str.slice (0, w))).join (cfg.delimiter)) | ||
: (limit (str, str.length + w))).join (cfg.delimiter)) | ||
} | ||
@@ -66,5 +68,5 @@ }, | ||
const colNames = [...new Set (arr.map (O.keys).reduce ((a, b) => [...a, ...b], []))], | ||
columns = [colNames, ...arr.map (o => colNames.map (key => o[key]))], | ||
lines = asColumns (columns, O.assign ({ minColumnWidths: colNames.map (n => n.length) }, cfg)) | ||
const colNames = [...new Set ([].concat (...arr.map (O.keys)))], | ||
columns = [colNames, ...arr.map (o => colNames.map (key => o[key]))], | ||
lines = asColumns (columns, cfg) | ||
@@ -78,4 +80,4 @@ return [lines[0], '-'.repeat (lines[0].length), ...lines.slice (1)].join ('\n') | ||
module.exports = asTable ({ maxTotalWidth: 120 }) | ||
module.exports = asTable ({ maxTotalWidth: Number.MAX_SAFE_INTEGER }) | ||
{ | ||
"name": "as-table", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "A simple function to print objects as ASCII tables", | ||
@@ -5,0 +5,0 @@ "main": "as-table.js", |
25
test.js
@@ -20,7 +20,7 @@ "use strict"; | ||
assert.equal (asTable.configure ({ maxTotalWidth: 25, delimiter: ' | ' }) (testData), | ||
assert.equal (asTable.configure ({ maxTotalWidth: 22, delimiter: ' | ' }) (testData), | ||
'qwe | 12345 | zxcv\n' + | ||
'qwert | 12 | zxcv\n' + | ||
'qwert | 12345 | z ') | ||
'qwe | 1234… | zxc…\n' + | ||
'qwer… | 12 | zxc…\n' + | ||
'qwer… | 1234… | z ') | ||
}) | ||
@@ -42,4 +42,19 @@ | ||
' null 44 ') | ||
}), | ||
}) | ||
it ('maxTotalWidth correctly handles object field names', () => { | ||
assert.equal ( | ||
asTable.configure ({ maxTotalWidth: 15 }) ([{ | ||
'0123456789': '0123456789', | ||
'abcdefxyzw': 'abcdefxyzw' }]), | ||
'01234… abcde…\n' + | ||
'--------------\n' + | ||
'01234… abcde…' | ||
) | ||
}) | ||
it ('everything renders as singleline', () => { | ||
@@ -46,0 +61,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8593
101