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

to-width

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

to-width - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

34

lib/main.js

@@ -1,3 +0,4 @@

// Generated by CoffeeScript 1.10.0
// Generated by CoffeeScript 2.5.1
(function() {
//###########################################################################################################
var CND, alert, badge, debug, echo, help, info, log, rpr, self, urge, warn, whisper, ƒ;

@@ -29,2 +30,4 @@

//...........................................................................................................
// probes = require './probes'
this._width_of_string = require('string-width');

@@ -40,10 +43,10 @@

//-----------------------------------------------------------------------------------------------------------
this.to_width = ƒ(function(text, width, settings) {
/* Fit text into `width` columns, taking into account ANSI color codes (that take up bytes but not width)
and double-width glyphs such as CJK characters.
*/
/* `WCString` occasionally is off by one, so here we fix that: */
var R, align, ellipsis, old_width, p, padder, ref, ref1, ref2, width_1;
if (!(width >= 2)) {
throw new Error("width must at least be 2, got " + width);
/* Fit text into `width` columns, taking into account ANSI color codes (that take up bytes but not width)
and double-width glyphs such as CJK characters. */
throw new Error(`width must at least be 2, got ${width}`);
}

@@ -58,10 +61,9 @@ padder = (ref = settings != null ? settings['padder'] : void 0) != null ? ref : ' ';

}
//.........................................................................................................
if (old_width > (width_1 = width - 1)) {
/* `WCString` occasionally is off by one, so here we fix that: */
R = (new this._Wcstring(text)).truncate(width_1, '');
R += ellipsis + ((this.width_of(R)) < width_1 ? ellipsis : '');
R += ellipsis + (((this.width_of(R)) < width_1) ? ellipsis : '');
} else {
/* TAINT assuming uncolored, single-width glyph for padding */
//.........................................................................................................
p = width - old_width;

@@ -79,13 +81,13 @@ switch (align) {

default:
throw new Error("expected one of 'left, 'right'. 'center', got " + (rpr(align)));
throw new Error(`expected one of 'left, 'right'. 'center', got ${rpr(align)}`);
}
}
//.........................................................................................................
return R;
});
this.width_of = ƒ((function(_this) {
return function(text) {
return _this._width_of_string(text);
};
})(this));
//-----------------------------------------------------------------------------------------------------------
this.width_of = ƒ((text) => {
return this._width_of_string(text);
});

@@ -92,0 +94,0 @@ }).call(this);

@@ -1,5 +0,6 @@

// Generated by CoffeeScript 1.10.0
// Generated by CoffeeScript 2.5.1
(function() {
var CND, alert, badge, debug, echo, help, include, info, log, ref, rpr, test, to_width, urge, warn, whisper, width_of,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
//###########################################################################################################
var CND, alert, badge, debug, echo, help, include, info, log, rpr, test, to_width, urge, warn, whisper, width_of,
indexOf = [].indexOf;

@@ -30,15 +31,20 @@ CND = require('cnd');

//...........................................................................................................
test = require('guy-test');
ref = require('./main'), to_width = ref.to_width, width_of = ref.width_of;
//...........................................................................................................
({to_width, width_of} = require('./main'));
//===========================================================================================================
// TESTS
//-----------------------------------------------------------------------------------------------------------
this["demo"] = function(T, done) {
var alignments_and_texts, colors, show, stripe;
show = function() {
var align, border, i, j, len, len1, n, ref1, ref2, results, text;
var align, border, i, j, len, len1, n, ref, results, text;
border = CND.blue('│');
ref1 = [2, 3, 4, 10, 20, 30];
ref = [2, 3, 4, 10, 20, 30];
results = [];
for (i = 0, len = ref1.length; i < len; i++) {
n = ref1[i];
for (i = 0, len = ref.length; i < len; i++) {
n = ref[i];
info();

@@ -51,9 +57,9 @@ help(n);

for (j = 0, len1 = alignments_and_texts.length; j < len1; j++) {
ref2 = alignments_and_texts[j], align = ref2[0], text = ref2[1];
[align, text] = alignments_and_texts[j];
text = CND.white(text);
text = to_width(text, n, {
align: align
});
info(border + text + border + ' ' + (CND.blue(width_of(text))));
text = to_width(text, n, {align});
info(border + text + border + ' ' + (CND.blue(width_of(text)))); // + ' ' + ( CND.yellow text.length )
}
// info '╭' + ( '─'.repeat n ) + '╮'
// info '╰' + ( '─'.repeat n ) + '╯'
results.push(info('└' + ('─'.repeat(n)) + '┘'));

@@ -67,7 +73,7 @@ }

return ((function() {
var i, len, ref1, results;
ref1 = Array.from(text);
var i, len, ref, results;
ref = Array.from(text);
results = [];
for (idx = i = 0, len = ref1.length; i < len; idx = ++i) {
glyph = ref1[idx];
for (idx = i = 0, len = ref.length; i < len; idx = ++i) {
glyph = ref[idx];
results.push(colors[idx % 2](glyph));

@@ -80,5 +86,13 @@ }

show();
// debug ' 1 2 3 4'
// debug ' 1234567890123456789012345678901234567890'
// debug ( new TO_WIDTH._Wcstring "a nice test to see the effects of fixed width" ).truncate 30, '*'
// debug ( new TO_WIDTH._Wcstring "北京 (Peking) 位於華北 (North China) 平原的西北边缘 (north-western border area)" ).truncate 30, '*'
// debug ( new TO_WIDTH._Wcstring "北京 (Peking) " + ( CND.white "位於華北" ) + CND.yellow " (North China) 平原的西北边缘 (north-western border area)" ).truncate 30, '*'
//.........................................................................................................
return done();
};
//-----------------------------------------------------------------------------------------------------------
this["demo 2"] = function(T, done) {

@@ -99,6 +113,10 @@ info('abcd', 'abcd'.length, Buffer.byteLength('abcd'), width_of('abcd'));

//===========================================================================================================
// HELPERS
//-----------------------------------------------------------------------------------------------------------
this._prune = function() {
var name, value;
for (name in this) {
value = this[name];
var name, ref, value;
ref = this;
for (name in ref) {
value = ref[name];
if (name.startsWith('_')) {

@@ -114,2 +132,3 @@ continue;

//-----------------------------------------------------------------------------------------------------------
this._main = function() {

@@ -121,9 +140,13 @@ return test(this, {

//###########################################################################################################
if (module.parent == null) {
include = ["demo"];
// @_prune()
this._main();
}
// debug '5562', JSON.stringify key for key in Object.keys @
}).call(this);
//# sourceMappingURL=tests.js.map
{
"name": "to-width",
"version": "1.0.3",
"version": "1.0.4",
"description": "The essential building block for command line tables: truncate & pad strings to given width, taking care of wide characters, accents and ANSI colors",

@@ -39,9 +39,9 @@ "main": "lib/main.js",

"dependencies": {
"cnd": "^4.0.5",
"string-width": "^1.0.1",
"cnd": "^5.3.2",
"string-width": "^4.2.0",
"wcstring": "^2.1.1"
},
"devDependencies": {
"guy-test": "^1.2.3"
"guy-test": "^2.0.1"
}
}

Sorry, the diff of this file is not supported yet

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