Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More โ†’
Socket
Sign inDemoInstall
Socket

visualwidth

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

visualwidth - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

.npmignore

9

package.json
{
"name": "visualwidth",
"version": "0.0.1",
"version": "0.1.0",
"description": "unicode character visual width",

@@ -10,3 +10,3 @@ "main": "visualwidth.js",

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node ./t/test.js"
},

@@ -22,3 +22,6 @@ "repository": {

"author": "Tokuhiro Matsuno <tokuhirom@gmail.com>",
"license": "MIT"
"license": "MIT",
"devDependencies": {
"tape": "^3.0.3"
}
}

@@ -1,30 +0,58 @@

subtest('width', function () {
is(width('ใ‚ใ„ใ†ใˆใŠ'), 10);
is(width('...'), 3);
is(width('Shinjuku'), 8);
is(width('โ€ฆ'), 2);
is(width("\u2026"), 2); // ambiguous char should be full width
var test =require('tape');
var vw = require('../');
test('vw.width', function (t) {
t.equals(vw.width('ใ‚ใ„ใ†ใˆใŠ'), 10);
t.equals(vw.width('...'), 3);
t.equals(vw.width('Shinjuku'), 8);
t.equals(vw.width('โ€ฆ'), 2);
t.equals(vw.width("\u2026"), 2); // ambiguous char should be full width
t.end();
});
subtest('width/surrogate pair', function () {
is(width(String.fromCharCode(0xD800, 0xDC00)), 1); // U+10000 LINEAR B SYLLABLE B008 A (first non-BMP code point) ( half width )
is(width(String.fromCharCode(0xD840, 0xDC0B)), 2);
is(width(String.fromCharCode(0xD869, 0xDEB2)), 2);
is(width(String.fromCharCode(0xD840, 0xDC0B) + String.fromCharCode(0xD869, 0xDEB2)), 4);
test('vw short for vw.width', function (t) {
t.equals(vw.width('ใ‚ใ„ใ†ใˆใŠ'), 10);
t.end();
})
test('vw.width/surrogate pair', function (t) {
t.equals(vw.width(String.fromCharCode(0xD800, 0xDC00)), 1); // U+10000 LINEAR B SYLLABLE B008 A (first non-BMP code point) ( half width )
t.equals(vw.width(String.fromCharCode(0xD840, 0xDC0B)), 2);
t.equals(vw.width(String.fromCharCode(0xD869, 0xDEB2)), 2);
t.equals(vw.width(String.fromCharCode(0xD840, 0xDC0B) + String.fromCharCode(0xD869, 0xDEB2)), 4);
t.end();
});
subtest('truncate', function () {
is(truncate('DOUTOR ๆ–ฐๅฎฟใ‚ขใ‚คใƒฉใƒณใƒ‰ๅบ—', 15, '...'), 'DOUTOR ๆ–ฐๅฎฟ...');
is(truncate('็„กๅฐ่‰ฏๅ“ ใ‚ขใ‚ญใƒใƒปใƒˆใƒชใƒ ', 20, '...'), '็„กๅฐ่‰ฏๅ“ ใ‚ขใ‚ญใƒใƒป...');
is(truncate('VILLAGE VANGUARD ๆธ‹่ฐทๅฎ‡็”ฐๅท', 15, '...'), 'VILLAGE VANG...');
is(truncate('VILLAGE VANGUARD ๆธ‹่ฐทๅฎ‡็”ฐๅท', 15, '...').length, 15);
is(truncate('Shinjuku', 15, 'โ€ฆ'), 'Shinjuku');
is(truncate('ใ‚ใ„ใ†ใˆใŠ', 12, '...'), 'ใ‚ใ„ใ†ใˆใŠ');
is(truncate('ใ‚ใ„ใ†ใˆใŠ', 11, '...'), 'ใ‚ใ„ใ†ใˆใŠ');
is(truncate('ใ‚ใ„ใ†ใˆใŠ', 10, '...'), 'ใ‚ใ„ใ†ใˆใŠ');
is(truncate('ใ‚ใ„ใ†ใˆใŠ', 9, '...'), 'ใ‚ใ„ใ†...');
is(truncate('ใ‚ใ„ใ†ใˆใŠ', 8, '...'), 'ใ‚ใ„...');
is(truncate('ใ‚ใ„ใ†ใˆใŠ', 7, '...'), 'ใ‚ใ„...');
is(truncate('ใ‚ใ„ใ†ใˆใŠ', 6, '...'), 'ใ‚...');
is(truncate('ใ‚ใ„ใ†ใˆใŠ', 5, '...'), 'ใ‚...');
test('truncate', function (t) {
t.equals(vw.truncate('DOUTOR ๆ–ฐๅฎฟใ‚ขใ‚คใƒฉใƒณใƒ‰ๅบ—', 15, '...'), 'DOUTOR ๆ–ฐๅฎฟ...');
t.equals(vw.truncate('็„กๅฐ่‰ฏๅ“ ใ‚ขใ‚ญใƒใƒปใƒˆใƒชใƒ ', 20, '...'), '็„กๅฐ่‰ฏๅ“ ใ‚ขใ‚ญใƒใƒป...');
t.equals(vw.truncate('VILLAGE VANGUARD ๆธ‹่ฐทๅฎ‡็”ฐๅท', 15, '...'), 'VILLAGE VANG...');
t.equals(vw.truncate('VILLAGE VANGUARD ๆธ‹่ฐทๅฎ‡็”ฐๅท', 15, '...').length, 15);
t.equals(vw.truncate('Shinjuku', 15, 'โ€ฆ'), 'Shinjuku');
t.equals(vw.truncate('ใ‚ใ„ใ†ใˆใŠ', 12, '...'), 'ใ‚ใ„ใ†ใˆใŠ');
t.equals(vw.truncate('ใ‚ใ„ใ†ใˆใŠ', 11, '...'), 'ใ‚ใ„ใ†ใˆใŠ');
t.equals(vw.truncate('ใ‚ใ„ใ†ใˆใŠ', 10, '...'), 'ใ‚ใ„ใ†ใˆใŠ');
t.equals(vw.truncate('ใ‚ใ„ใ†ใˆใŠ', 9, '...'), 'ใ‚ใ„ใ†...');
t.equals(vw.truncate('ใ‚ใ„ใ†ใˆใŠ', 8, '...'), 'ใ‚ใ„...');
t.equals(vw.truncate('ใ‚ใ„ใ†ใˆใŠ', 7, '...'), 'ใ‚ใ„...');
t.equals(vw.truncate('ใ‚ใ„ใ†ใˆใŠ', 6, '...'), 'ใ‚...');
t.equals(vw.truncate('ใ‚ใ„ใ†ใˆใŠ', 5, '...'), 'ใ‚...');
t.end();
});
test('terminal characters', function (t) {
t.equals(vw.width('\x1B[0mH', true), 1);
t.equals(vw.width('\x1B[0mH'), 5);
t.equals(vw.width('\x1B[31mH\x1B[0m', true), 1);
t.equals(vw.width('\x1B[31mใ‚\x1B[0m', true), 2);
t.equals(vw.width('\x1B[31mใ‚\x1B[0mB', true), 3);
t.end();
});
test('truncate terminal characters', function (t) {
t.equals(vw.truncate('\x1B[31mHello World', 10, '...', true), '\x1B[31mHello W...');
t.equals(vw.truncate('\x1B[31mHello World\x1B[0m', 10, '...', true), '\x1B[31mHello W...');
t.equals(vw.truncate('\x1B[31mHello World', 10, '\x1B[0m...', true), '\x1B[31mHello W\x1B[0m...');
t.equals(vw.truncate('็งใฎ\x1B[31mใƒขใƒณใ‚นใ‚ฟใƒผ\x1B[0mใƒใƒณใ‚ฟใƒผ', 12, '\x1B[0mโ€ฆ', true), '็งใฎ\x1b[31mใƒขใƒณใ‚น\x1b[0mโ€ฆ');
t.end();
});

@@ -9,5 +9,5 @@ // http://tokuhirom.mit-license.org

if (typeof exports !== 'undefined') {
VisualWidth = exports;
module.exports = VisualWidth = width;
} else {
global.VisualWidth = VisualWidth = {};
global.VisualWidth = VisualWidth = width;
}

@@ -17,6 +17,10 @@ VisualWidth.truncate = truncate;

function width(string) {
function width(string, terminal) {
var counter=0,
i, l, c, cp;
if (terminal) {
string = string.replace(/\x1B\[[0-9;]*m/g, '')
}
for (i=0, l=string.length; i<l; i++) {

@@ -50,22 +54,38 @@ c = string.charCodeAt(i);

function truncate(string, length, suffix) {
function truncate(string, length, suffix, terminal) {
var ret = '',
c, clen,
counter=0,
chars = string.split(''),
chars,
i, l,
slen = width(suffix);
max,
terminalBlock;
if (width(string) <= length) {
if (width(string, terminal) <= length) {
return string;
}
chars = string.split('')
max = length - width(suffix, terminal)
for (i=0, l=chars.length; i<l && counter < length; i++) {
c = chars[i];
clen = width(c);
if (counter + clen + slen > length) {
if (terminalBlock) {
if (c === "m") {
terminalBlock = false
}
ret += c;
continue;
}
if (c === "\x1B") {
terminalBlock = true;
ret += c;
continue;
}
clen = width(c, terminal);
counter += clen;
if (counter > max) {
return ret + suffix;
}
ret += c;
counter += clen;
}

@@ -72,0 +92,0 @@ return ret; // maybe fatal

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with โšก๏ธ by Socket Inc