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

wcwidth.js

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wcwidth.js - npm Package Compare versions

Comparing version 1.0.2 to 1.1.2

128

index.js

@@ -5,92 +5,90 @@ /*

"use strict"
const combining = require('./combining');
var defaults = require('defaults')
const DEFAULTS = {
nul: 0,
control: 0,
};
var combining = require('./combining')
var DEFAULTS = {
nul: 0,
control: 0
}
function bisearch(ucs) {
var min = 0
var max = combining.length - 1
var mid
let min = 0;
let max = combining.length - 1;
let mid;
if (ucs < combining[0][0] || ucs > combining[max][1]) return false
if (ucs < combining[0][0] || ucs > combining[max][1]) return false;
while (max >= min) {
mid = Math.floor((min + max) / 2)
if (ucs > combining[mid][1]) min = mid + 1
else if (ucs < combining[mid][0]) max = mid - 1
else return true
mid = Math.floor((min + max) / 2);
if (ucs > combining[mid][1]) min = mid + 1;
else if (ucs < combining[mid][0]) max = mid - 1;
else return true;
}
return false
return false;
}
function wcwidth(ucs, opts) {
// test for 8-bit control characters
if (ucs === 0) return opts.nul
if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0)) return opts.control
// test for 8-bit control characters
if (ucs === 0) return opts.nul;
if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0)) return opts.control;
// binary search in table of non-spacing characters
if (bisearch(ucs)) return 0
// binary search in table of non-spacing characters
if (bisearch(ucs)) return 0;
// if we arrive here, ucs is not a combining or C0/C1 control character
return 1 +
(ucs >= 0x1100 &&
(ucs <= 0x115f || // Hangul Jamo init. consonants
ucs == 0x2329 || ucs == 0x232a ||
(ucs >= 0x2e80 && ucs <= 0xa4cf &&
ucs != 0x303f) || // CJK ... Yi
(ucs >= 0xac00 && ucs <= 0xd7a3) || // Hangul Syllables
(ucs >= 0xf900 && ucs <= 0xfaff) || // CJK Compatibility Ideographs
(ucs >= 0xfe10 && ucs <= 0xfe19) || // Vertical forms
(ucs >= 0xfe30 && ucs <= 0xfe6f) || // CJK Compatibility Forms
(ucs >= 0xff00 && ucs <= 0xff60) || // Fullwidth Forms
(ucs >= 0xffe0 && ucs <= 0xffe6) ||
(ucs >= 0x20000 && ucs <= 0x2fffd) ||
(ucs >= 0x30000 && ucs <= 0x3fffd)));
// if we arrive here, ucs is not a combining or C0/C1 control character
return 1 +
(
ucs >= 0x1100 && (
ucs <= 0x115f || // Hangul Jamo init. consonants
ucs == 0x2329 || ucs == 0x232a ||
(ucs >= 0x2e80 && ucs <= 0xa4cf && ucs != 0x303f) || // CJK ... Yi
(ucs >= 0xac00 && ucs <= 0xd7a3) || // Hangul Syllables
(ucs >= 0xf900 && ucs <= 0xfaff) || // CJK Compatibility Ideographs
(ucs >= 0xfe10 && ucs <= 0xfe19) || // Vertical forms
(ucs >= 0xfe30 && ucs <= 0xfe6f) || // CJK Compatibility Forms
(ucs >= 0xff00 && ucs <= 0xff60) || // Fullwidth Forms
(ucs >= 0xffe0 && ucs <= 0xffe6) ||
(ucs >= 0x20000 && ucs <= 0x2fffd) ||
(ucs >= 0x30000 && ucs <= 0x3fffd)
)
);
}
function wcswidth(str, opts) {
var h, l
var s = 0, n
let h;
let l;
let s = 0;
let n;
if (typeof str !== 'string') return wcwidth(str, opts)
if (typeof str !== 'string') return wcwidth(str, opts);
for (var i = 0; i < str.length; i++) {
h = str.charCodeAt(i)
if (h >= 0xd800 && h <= 0xdbff) {
l = str.charCodeAt(++i)
if (l >= 0xdc00 && l <= 0xdfff) h = (h-0xd800)*0x400 + (l-0xdc00)+0x10000
else i--
}
n = wcwidth(h, opts)
if (n < 0) return -1
s += n
for (let i = 0; i < str.length; i++) {
h = str.charCodeAt(i);
if (h >= 0xd800 && h <= 0xdbff) {
l = str.charCodeAt(++i);
if (l >= 0xdc00 && l <= 0xdfff) {
h = (h - 0xd800) * 0x400 + (l - 0xdc00) + 0x10000;
} else {
i--;
}
}
n = wcwidth(h, opts);
if (n < 0) return -1;
s += n;
}
return s
return s;
}
module.exports = (str) => wcswidth(str, DEFAULTS);
module.exports = function wcwidth(str) {
return wcswidth(str, DEFAULTS)
}
module.exports.config = (opts = {}) => {
opts = {
...DEFAULTS,
...opts,
};
module.exports.config = function(opts) {
opts = Object.assign({}, DEFAULTS, opts || {})
return function wcwidth(str) {
return wcswidth(str, opts)
}
}
return (str) => wcswidth(str, opts);
};
// end of wcwidth.js
{
"name": "wcwidth.js",
"version": "1.0.2",
"version": "1.1.2",
"description": "a javascript porting of C's wcwidth()",

@@ -33,2 +33,4 @@ "author": {

"devDependencies": {
"eslint": "^8.41.0",
"eslint-config-google": "^0.14.0",
"tape": "^4.2.0"

@@ -40,3 +42,3 @@ },

"engines": {
"node": ">=4.9.1"
"node": ">=16.15.1"
},

@@ -43,0 +45,0 @@ "keywords": [

wcwidth.js: a javascript porting of C's wcwidth()
=================================================
> _Stick to version `1.0.2` if need to support node.js <`16.15.1`._
`wcwidth.js` is a simple javascript porting of `wcwidth()` implemented in C

@@ -21,2 +23,3 @@ [by Markus Kuhn](http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c).

an ISO 10646 character as follows:
- the null character (`U+0000`) has a column width of `opts.null` (whose

@@ -54,7 +57,7 @@ default value is 0);

var wcwidth = require('wcwidth.js')
const wcwidth = require('wcwidth.js');
wcwidth('한글') // 4
wcwidth('\0') // 0; NUL
wcwidth('\t') // 0; control characters
wcwidth('한글'); // 4
wcwidth('\0'); // 0; NUL
wcwidth('\t'); // 0; control characters

@@ -65,9 +68,9 @@ If you plan to replace `NUL` or control characters with, say, `???` before

var mywidth = wcwidth.config({
nul: 3,
control: 3
const mywidth = wcwidth.config({
nul: 3,
control: 3,
})
mywidth('\0\f') // 6
mywidth('한\t글') // 7
mywidth('\0\f'); // 6
mywidth('한\t글'); // 7

@@ -77,9 +80,9 @@ Setting these options to -1 gives a function that returns -1 for a string

mywidth = wcwidth.config({
nul: 0,
control: -1
})
const mywidth = wcwidth.config({
nul: 0,
control: -1,
});
mywidth('java\0script') // 10
mywidth('java\tscript') // -1
mywidth('java\0script'); // 10
mywidth('java\tscript'); // -1

@@ -91,6 +94,7 @@ This is useful when detecting if a string has non-printable characters.

String.prototype.__defineGetter__('wcwidth', function () {
return wcwidth(this);
})
'한글'.wcwidth // 4
String.prototype.__defineGetter__(
'wcwidth',
function () { return wcswidth(this.valueOf()) },
);
'한글'.wcwidth; // 4

@@ -101,4 +105,4 @@ JavaScript has no character type, thus meaningless to have two versions of

wcwidth('한') // prints 2
wcwidth('글'.charCodeAt(0)) // prints 2
wcwidth('한'); // prints 2
wcwidth('글'.charCodeAt(0)); // prints 2

@@ -105,0 +109,0 @@ `INSTALL.md` explains how to build and install the library. For the copyright

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