From 1af3a22ff72891201762ff13f3fbac7b509c4bb2 Mon Sep 17 00:00:00 2001 From: liu_yang Date: Fri, 24 Dec 2021 17:34:00 +0800 Subject: [PATCH 1/3] feat: add Chinese support --- lib/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index c8d1790..3624e6c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -243,11 +243,12 @@ Table.prototype.toString = function (){ - (style['padding-left'] || 0) - (style['padding-right'] || 0) , align = options.colAligns[index] || 'left'; - + // statistics of chinese characters + var chineseCharacters = str.match(/[\u4e00-\u9fa5]/gi)?.length || 0; return repeat(' ', style['padding-left'] || 0) + (length == width ? str : (length < width - ? pad(str, ( width + (str.length - length) ), ' ', align == 'left' ? 'right' : + ? pad(str, ( width + (str.length - length - chineseCharacters) ), ' ', align == 'left' ? 'right' : (align == 'middle' ? 'both' : 'left')) : (truncater ? truncate(str, width, truncater) : str)) ) From 709db8ae9ceefe4e08fca094f3fbc557c874cd72 Mon Sep 17 00:00:00 2001 From: liu_yang Date: Fri, 24 Dec 2021 17:47:29 +0800 Subject: [PATCH 2/3] fix: fix syntax problems --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 3624e6c..29a07df 100644 --- a/lib/index.js +++ b/lib/index.js @@ -244,7 +244,7 @@ Table.prototype.toString = function (){ - (style['padding-right'] || 0) , align = options.colAligns[index] || 'left'; // statistics of chinese characters - var chineseCharacters = str.match(/[\u4e00-\u9fa5]/gi)?.length || 0; + var chineseCharacters = (str.match(/[\u4e00-\u9fa5]/gi) || {}).length || 0; return repeat(' ', style['padding-left'] || 0) + (length == width ? str : (length < width From 433323a5db34eef1bd7725084b5447951bbeabea Mon Sep 17 00:00:00 2001 From: liu_yang Date: Mon, 27 Dec 2021 11:01:22 +0800 Subject: [PATCH 3/3] fix: optimize code and add test code --- lib/index.js | 4 +--- lib/utils.js | 19 +++++++++++++++---- test/index.test.js | 21 +++++++++++++++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/index.js b/lib/index.js index 29a07df..ea3c368 100644 --- a/lib/index.js +++ b/lib/index.js @@ -243,12 +243,10 @@ Table.prototype.toString = function (){ - (style['padding-left'] || 0) - (style['padding-right'] || 0) , align = options.colAligns[index] || 'left'; - // statistics of chinese characters - var chineseCharacters = (str.match(/[\u4e00-\u9fa5]/gi) || {}).length || 0; return repeat(' ', style['padding-left'] || 0) + (length == width ? str : (length < width - ? pad(str, ( width + (str.length - length - chineseCharacters) ), ' ', align == 'left' ? 'right' : + ? pad(str, ( width + (str.length - length) ), ' ', align == 'left' ? 'right' : (align == 'middle' ? 'both' : 'left')) : (truncater ? truncate(str, width, truncater) : str)) ) diff --git a/lib/utils.js b/lib/utils.js index 1cb8072..2c39e2d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -78,9 +78,20 @@ exports.options = options; // // see: http://en.wikipedia.org/wiki/ANSI_escape_code // -exports.strlen = function(str){ +exports.strlen = function (str) { var code = /\u001b\[(?:\d*;){0,5}\d*m/g; - var stripped = ("" + str).replace(code,''); + var stripped = ("" + str).replace(code, ""); var split = stripped.split("\n"); - return split.reduce(function (memo, s) { return (s.length > memo) ? s.length : memo }, 0); -} + // statistics of chinese characters + var chineseCharacters = + typeof str === "string" ? (str.match(/[\u4e00-\u9fa5]/gi) || []).length : 0; + return split.reduce(function (memo, s) { + if (s.length > memo) { + if (chineseCharacters) { + return s.length + chineseCharacters; + } + return s.length; + } + return memo; + }, 0); +}; diff --git a/test/index.test.js b/test/index.test.js index eaed6a2..403e22d 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -282,4 +282,25 @@ module.exports = { table.should.exist; }, + + 'test chinese width property': function (){ + var table = new Table({ + head: ['中文'], + style: { + head: [], + border: [] + } + }); + table.width.should.eql(8); + }, + 'test mixed chinese and english width property': function (){ + var table = new Table({ + head: ['中文abc123'], + style: { + head: [], + border: [] + } + }); + table.width.should.eql(14); + }, };