wcwidth
Advanced tools
Comparing version 0.0.0 to 1.0.0
39
index.js
@@ -22,18 +22,25 @@ "use strict" | ||
// The following functions define the column width of an ISO 10646 character as follows: | ||
// | ||
// - The null character (U+0000) has a column width of 0. | ||
// - Other C0/C1 control characters and DEL will lead to a return value of -1. | ||
// - Non-spacing and enclosing combining characters (general category code Mn or Me in the | ||
// Unicode database) have a column width of 0. | ||
// - SOFT HYPHEN (U+00AD) has a column width of 1. | ||
// - Other format characters (general category code Cf in the Unicode database) and ZERO WIDTH | ||
// SPACE (U+200B) have a column width of 0. | ||
// - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF) have a column width of 0. | ||
// - Spacing characters in the East Asian Wide (W) or East Asian Full-width (F) category as | ||
// defined in Unicode Technical Report #11 have a column width of 2. | ||
// - All remaining characters (including all printable ISO 8859-1 and WGL4 characters, Unicode | ||
// control characters, etc.) have a column width of 1. | ||
// | ||
// This implementation assumes that characters are encoded in ISO 10646. | ||
/* | ||
* The following functions define the column width of an ISO 10646 | ||
* character as follows: | ||
* - The null character (U+0000) has a column width of 0. | ||
* - Other C0/C1 control characters and DEL will lead to a return value | ||
* of -1. | ||
* - Non-spacing and enclosing combining characters (general category | ||
* code Mn or Me in the | ||
* Unicode database) have a column width of 0. | ||
* - SOFT HYPHEN (U+00AD) has a column width of 1. | ||
* - Other format characters (general category code Cf in the Unicode | ||
* database) and ZERO WIDTH | ||
* SPACE (U+200B) have a column width of 0. | ||
* - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF) | ||
* have a column width of 0. | ||
* - Spacing characters in the East Asian Wide (W) or East Asian | ||
* Full-width (F) category as | ||
* defined in Unicode Technical Report #11 have a column width of 2. | ||
* - All remaining characters (including all printable ISO 8859-1 and | ||
* WGL4 characters, Unicode control characters, etc.) have a column | ||
* width of 1. | ||
* This implementation assumes that characters are encoded in ISO 10646. | ||
*/ | ||
@@ -40,0 +47,0 @@ function wcswidth(str, opts) { |
{ | ||
"name": "wcwidth", | ||
"version": "0.0.0", | ||
"version": "1.0.0", | ||
"description": "Port of C's wcwidth() and wcswidth()", | ||
@@ -14,3 +14,3 @@ "author": "Tim Oxley", | ||
"devDependencies": { | ||
"mocha": "^1.18.2" | ||
"tape": "^2.13.4" | ||
}, | ||
@@ -33,4 +33,4 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "mocha -R spec" | ||
"test": "tape test/*.js" | ||
} | ||
} |
# wcwidth | ||
wcwidth is a simple JavaScript port of wcwidth() implemented in C by Markus Kuhn. | ||
Determine columns needed for a fixed-size wide-character string | ||
---- | ||
wcwidth is a simple JavaScript port of [wcwidth](http://man7.org/linux/man-pages/man3/wcswidth.3.html) implemented in C by Markus Kuhn. | ||
JavaScript port [originally](https://github.com/mycoboco/wcwidth.js) written by Woong Jun <woong.jun@gmail.com> (http://code.woong.org/) | ||
## Example | ||
```js | ||
'한'.length // => 1 | ||
wcwidth('한'); // => 2 | ||
'한글'.length // => 2 | ||
wcwidth('한글'); // => 4 | ||
``` | ||
`wcwidth()` and its string version, `wcswidth()` are defined by IEEE Std | ||
1002.1-2001, a.k.a. POSIX.1-2001, and return the number of columns used | ||
to represent the given wide character and string. | ||
Markus's implementation assumes the wide character given to those | ||
functions to be encoded in ISO 10646, which is almost true for | ||
JavaScript's characters. | ||
[Further explaination here](docs) | ||
## License | ||
MIT |
@@ -0,54 +1,64 @@ | ||
"use strict" | ||
var wcwidth = require('../') | ||
var assert = require('assert') | ||
var test = require('tape') | ||
describe('wcwidth', function() { | ||
it('handles regular strings', function() { | ||
assert.strictEqual(wcwidth('abc'), 3) | ||
}) | ||
test('handles regular strings', function(t) { | ||
t.strictEqual(wcwidth('abc'), 3) | ||
t.end() | ||
}) | ||
it('handles multibyte strings', function() { | ||
assert.strictEqual(wcwidth('字的模块'), 8) | ||
}) | ||
test('handles multibyte strings', function(t) { | ||
t.strictEqual(wcwidth('字的模块'), 8) | ||
t.end() | ||
}) | ||
it('handles multibyte characters mixed with regular characters', function() { | ||
assert.strictEqual(wcwidth('abc 字的模块'), 12) | ||
}) | ||
test('handles multibyte characters mixed with regular characters', function(t) { | ||
t.strictEqual(wcwidth('abc 字的模块'), 12) | ||
t.end() | ||
}) | ||
it('ignores control characters e.g. \\n', function() { | ||
assert.strictEqual(wcwidth('abc\n字的模块\ndef'), 14) | ||
}) | ||
test('ignores control characters e.g. \\n', function(t) { | ||
t.strictEqual(wcwidth('abc\n字的模块\ndef'), 14) | ||
t.end() | ||
}) | ||
it('ignores bad input', function() { | ||
assert.strictEqual(wcwidth(''), 0) | ||
assert.strictEqual(wcwidth(3), 0) | ||
assert.strictEqual(wcwidth({}), 0) | ||
assert.strictEqual(wcwidth([]), 0) | ||
assert.strictEqual(wcwidth(), 0) | ||
}) | ||
test('ignores bad input', function(t) { | ||
t.strictEqual(wcwidth(''), 0) | ||
t.strictEqual(wcwidth(3), 0) | ||
t.strictEqual(wcwidth({}), 0) | ||
t.strictEqual(wcwidth([]), 0) | ||
t.strictEqual(wcwidth(), 0) | ||
t.end() | ||
}) | ||
it('ignores nul (charcode 0)', function() { | ||
assert.strictEqual(wcwidth(String.fromCharCode(0)), 0) | ||
}) | ||
test('ignores nul (charcode 0)', function(t) { | ||
t.strictEqual(wcwidth(String.fromCharCode(0)), 0) | ||
t.end() | ||
}) | ||
it('ignores nul mixed with chars', function() { | ||
assert.strictEqual(wcwidth('a' + String.fromCharCode(0) + '\n字的'), 5) | ||
}) | ||
test('ignores nul mixed with chars', function(t) { | ||
t.strictEqual(wcwidth('a' + String.fromCharCode(0) + '\n字的'), 5) | ||
t.end() | ||
}) | ||
it('can have custom value for nul', function() { | ||
assert.strictEqual(wcwidth.config({ | ||
nul: 10 | ||
})(String.fromCharCode(0) + 'a字的'), 15) | ||
}) | ||
test('can have custom value for nul', function(t) { | ||
t.strictEqual(wcwidth.config({ | ||
nul: 10 | ||
})(String.fromCharCode(0) + 'a字的'), 15) | ||
t.end() | ||
}) | ||
it('can have custom control char value', function() { | ||
assert.strictEqual(wcwidth.config({ | ||
control: 1 | ||
})('abc\n字的模块\ndef'), 16) | ||
}) | ||
test('can have custom control char value', function(t) { | ||
t.strictEqual(wcwidth.config({ | ||
control: 1 | ||
})('abc\n字的模块\ndef'), 16) | ||
t.end() | ||
}) | ||
it('negative custom control chars == -1', function() { | ||
assert.strictEqual(wcwidth.config({ | ||
control: -1 | ||
})('abc\n字的模块\ndef'), -1) | ||
}) | ||
test('negative custom control chars == -1', function(t) { | ||
t.strictEqual(wcwidth.config({ | ||
control: -1 | ||
})('abc\n字的模块\ndef'), -1) | ||
t.end() | ||
}) |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
14010
187
1
34