Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ Table.prototype.toString = function (){
- (style['padding-left'] || 0)
- (style['padding-right'] || 0)
, align = options.colAligns[index] || 'left';

return repeat(' ', style['padding-left'] || 0)
+ (length == width ? str :
(length < width
Expand Down
19 changes: 15 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
21 changes: 21 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
};