Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tidy-table syntax CJK characters #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 11 additions & 4 deletions lib/tables.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ pad = require 'pad'
{getAttribute} = require './utils'
{isTextNode, isElementNode} = require './tree-adapter'

utf8CharacterCount = (str) ->
count = 0
for c in str
if c.charCodeAt() > 0x007f then count++
return count

###*
* Find the length of the longest string in an array
* @param {String[]} array Array of strings
###
longestStringInArray = (array) ->
longest = 0
for str in array
len = str.length
len = str.length + utf8CharacterCount(str)
if len > longest then longest = len
return longest

Expand Down Expand Up @@ -89,18 +95,19 @@ extractRows = (node) ->
formatRow = (row, alignments, columnWidths) ->
# apply padding around each cell for alignment and column width
for i in [0...row.length]
columnWidth = columnWidths[i] - utf8CharacterCount(row[i])
row[i] = (
switch alignments[i]
when 'right'
pad(columnWidths[i], row[i])
pad(columnWidth, row[i])
when 'center'
# rounding causes a bias to the left because we can't have half a char
whitespace = columnWidths[i] - row[i].length
whitespace = columnWidth - row[i].length
leftPadded = pad(Math.floor(whitespace / 2) + row[i].length, row[i])
pad(leftPadded, Math.ceil(whitespace / 2) + leftPadded.length)
else
# left is the default alignment when formatting
pad(row[i], columnWidths[i])
pad(row[i], columnWidth)
)

# trimRight is to remove any trailing whitespace added by the padding
Expand Down