Socket
Socket
Sign inDemoInstall

eastasianwidth

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

eastasianwidth - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

53

eastasianwidth.js

@@ -1,2 +0,4 @@

exports.eastAsianWidth = function(character) {
var eaw = exports;
eaw.eastAsianWidth = function(character) {
var x = character.charCodeAt(0);

@@ -12,11 +14,8 @@ var y = (character.length == 2) ? character.charCodeAt(1) : 0;

if (
(0x3000 == codePoint) ||
if ((0x3000 == codePoint) ||
(0xFF01 <= codePoint && codePoint <= 0xFF60) ||
(0xFFE0 <= codePoint && codePoint <= 0xFFE6)
) {
(0xFFE0 <= codePoint && codePoint <= 0xFFE6)) {
return 'F';
}
if (
(0x20A9 == codePoint) ||
if ((0x20A9 == codePoint) ||
(0xFF61 <= codePoint && codePoint <= 0xFFBE) ||

@@ -27,8 +26,6 @@ (0xFFC2 <= codePoint && codePoint <= 0xFFC7) ||

(0xFFDA <= codePoint && codePoint <= 0xFFDC) ||
(0xFFE8 <= codePoint && codePoint <= 0xFFEE)
) {
(0xFFE8 <= codePoint && codePoint <= 0xFFEE)) {
return 'H';
}
if (
(0x1100 <= codePoint && codePoint <= 0x115F) ||
if ((0x1100 <= codePoint && codePoint <= 0x115F) ||
(0x11A3 <= codePoint && codePoint <= 0x11A7) ||

@@ -70,8 +67,6 @@ (0x11FA <= codePoint && codePoint <= 0x11FF) ||

(0x2B740 <= codePoint && codePoint <= 0x2FFFD) ||
(0x30000 <= codePoint && codePoint <= 0x3FFFD)
) {
(0x30000 <= codePoint && codePoint <= 0x3FFFD)) {
return 'W';
}
if (
(0x0020 <= codePoint && codePoint <= 0x007E) ||
if ((0x0020 <= codePoint && codePoint <= 0x007E) ||
(0x00A2 <= codePoint && codePoint <= 0x00A3) ||

@@ -82,8 +77,6 @@ (0x00A5 <= codePoint && codePoint <= 0x00A6) ||

(0x27E6 <= codePoint && codePoint <= 0x27ED) ||
(0x2985 <= codePoint && codePoint <= 0x2986)
) {
(0x2985 <= codePoint && codePoint <= 0x2986)) {
return 'Na';
}
if (
(0x00A1 == codePoint) ||
if ((0x00A1 == codePoint) ||
(0x00A4 == codePoint) ||

@@ -260,4 +253,3 @@ (0x00A7 <= codePoint && codePoint <= 0x00A8) ||

(0xF0000 <= codePoint && codePoint <= 0xFFFFD) ||
(0x100000 <= codePoint && codePoint <= 0x10FFFD)
) {
(0x100000 <= codePoint && codePoint <= 0x10FFFD)) {
return 'A';

@@ -267,2 +259,19 @@ }

return 'N';
}
};
eaw.characterLength = function(character) {
var code = this.eastAsianWidth(character);
if (code == 'F' || code == 'W' || code == 'A') {
return 2;
} else {
return 1;
}
};
eaw.length = function(string) {
var len = 0;
for (var i = 0; i < string.length; i++) {
len = len + this.characterLength(string.charAt(i));
}
return len;
};
{
"name": "eastasianwidth",
"version": "0.0.1",
"version": "0.1.0",
"description": "Get East Asian Width from a character.",

@@ -11,3 +11,6 @@ "main": "eastasianwidth.js",

"author": "Masaki Komagata",
"license": "MIT"
"license": "MIT",
"devDependencies": {
"mocha": "~1.9.0"
}
}

@@ -7,2 +7,4 @@ # East Asian Width

Original Code is [東アジアの文字幅 (East Asian Width) の判定 - 中途](http://d.hatena.ne.jp/takenspc/20111126#1322252878).
## Install

@@ -9,0 +11,0 @@

var assert = require('assert'),
stringwidth = require('../eastasianwidth');
eaw = require('../eastasianwidth');
describe('eastAsianWidth', function() {
it('Fullwidth', function(){
assert.equal('F', stringwidth.eastAsianWidth('¢'));
assert.equal('F', stringwidth.eastAsianWidth('₩'));
it('Fullwidth', function() {
assert.equal('F', eaw.eastAsianWidth('¢'));
assert.equal('F', eaw.eastAsianWidth('₩'));
});
it('Halfwidth', function(){
assert.equal('H', stringwidth.eastAsianWidth('。'));
assert.equal('H', stringwidth.eastAsianWidth('ᅵ'));
it('Halfwidth', function() {
assert.equal('H', eaw.eastAsianWidth('。'));
assert.equal('H', eaw.eastAsianWidth('ᅵ'));
});
it('Wide', function(){
assert.equal('W', stringwidth.eastAsianWidth('ㄅ'));
assert.equal('W', stringwidth.eastAsianWidth('뀀'));
it('Wide', function() {
assert.equal('W', eaw.eastAsianWidth('ㄅ'));
assert.equal('W', eaw.eastAsianWidth('뀀'));
});
it('Narrow', function(){
assert.equal('Na', stringwidth.eastAsianWidth('¢'));
assert.equal('Na', stringwidth.eastAsianWidth('⟭'));
assert.equal('Na', stringwidth.eastAsianWidth('a'));
it('Narrow', function() {
assert.equal('Na', eaw.eastAsianWidth('¢'));
assert.equal('Na', eaw.eastAsianWidth('⟭'));
assert.equal('Na', eaw.eastAsianWidth('a'));
});
it('Ambiguous', function(){
assert.equal('A', stringwidth.eastAsianWidth('⊙'));
assert.equal('A', stringwidth.eastAsianWidth('①'));
it('Ambiguous', function() {
assert.equal('A', eaw.eastAsianWidth('⊙'));
assert.equal('A', eaw.eastAsianWidth('①'));
});
it('Natural', function(){
assert.equal('N', stringwidth.eastAsianWidth('ب'));
assert.equal('N', stringwidth.eastAsianWidth('ف'));
it('Natural', function() {
assert.equal('N', eaw.eastAsianWidth('ب'));
assert.equal('N', eaw.eastAsianWidth('ف'));
});
})
});
describe('characterLength', function() {
it('Fullwidth', function() {
assert.equal(2, eaw.characterLength('¢'));
assert.equal(2, eaw.characterLength('₩'));
});
it('Halfwidth', function() {
assert.equal(1, eaw.characterLength('。'));
assert.equal(1, eaw.characterLength('ᅵ'));
});
it('Wide', function() {
assert.equal(2, eaw.characterLength('ㄅ'));
assert.equal(2, eaw.characterLength('뀀'));
});
it('Narrow', function() {
assert.equal(1, eaw.characterLength('¢'));
assert.equal(1, eaw.characterLength('⟭'));
assert.equal(1, eaw.characterLength('a'));
});
it('Ambiguous', function() {
assert.equal(2, eaw.characterLength('⊙'));
assert.equal(2, eaw.characterLength('①'));
});
it('Natural', function() {
assert.equal(1, eaw.characterLength('ب'));
assert.equal(1, eaw.characterLength('ف'));
});
});
describe('length', function() {
it('Fullwidth', function() {
assert.equal(10, eaw.length('あいうえお'));
});
it('Halfwidth', function() {
assert.equal(7, eaw.length('abcdefg'));
});
it('Mixed', function() {
assert.equal(19, eaw.length('¢₩。ᅵㄅ뀀¢⟭a⊙①بف'));
});
});
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