-
-
Notifications
You must be signed in to change notification settings - Fork 25
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
Add support for surrogate pairs and full width characters #20
Add support for surrogate pairs and full width characters #20
Conversation
index.js
Outdated
if (visible + charLength <= cols) { | ||
rows[rows.length - 1] += x; | ||
} else { | ||
rows.push(x); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, maybe we should set visible = 0
here too since it's reset.
index.js
Outdated
|
||
if (visible >= cols && i < word.length - 1) { | ||
if (visible >= cols && i < arr.length - 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this should probably be visible === cols
to avoid duplicating the if
statement above.
index.js
Outdated
for (let i = 0, word; (word = words[i]) !== undefined; i++) { | ||
for (const item of Array.from(words).entries()) { | ||
const i = item[0]; | ||
const x = item[1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use a more descriptive variable than x
here? Since the scope is large, it makes it hard to read the code.
|
||
// For each newline, invoke the method separately | ||
module.exports = (str, cols, opts) => { | ||
return String(str) | ||
.normalize() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why normalize
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To normalize unicode characters that creates the same characters but with different code points and possibly different lengths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like this which both produces ñ
:
'\xF1' === 'n\u0303'
//=> false
test.js
Outdated
t.is(m('a\ud83c\ude00bc', 2, {hard: true}), 'a\n\ud83c\ude00\nbc'); | ||
t.is(m('a\ud83c\ude00bc\ud83c\ude00d\ud83c\ude00', 2, {hard: true}), 'a\n\ud83c\ude00\nbc\n\ud83c\ude00\nd\n\ud83c\ude00'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use uppercase escapes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think it looks good! Some small remarks.
index.js
Outdated
|
||
if (ESCAPES.indexOf(y) !== -1) { | ||
const code = parseFloat(/\d[^m]*/.exec(pre.slice(j, j + 4))); | ||
if (ESCAPES.indexOf(x) !== -1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe make ESCAPES
a Set
and use ESCAPES.has(x)
here?
index.js
Outdated
const y = pre[j]; | ||
for (const item of Array.from(pre).entries()) { | ||
const i = item[0]; | ||
const x = item[1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we call x
char
or something (like in the wrapWord
function)? A variable named x
doesn't say much.
I really appreciate you fixing this Kevin. 🍰 |
Thanks 🎈 |
Also added some small code tweaks for consistency :).
Fixes #10 and fixes #11.