Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "wcwidth.js", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A JavaScript porting of C's wcwidth() and wcswidth()", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -19,4 +19,5 @@ wcwidth.js: A JavaScript Porting of Markus Kuhn's wcwidth() Implementation | ||
var wcwidth = require('wcwidth')({ | ||
nul: 0, | ||
control: -1 | ||
nul: 0, | ||
control: -1, | ||
monkeypatch: true | ||
}); // equivalent to var wcwidth = require('wcwidth')(); | ||
@@ -28,8 +29,9 @@ | ||
The argument `{ nul: 0, control: -1 }` (which are the default values, in fact) | ||
tells `wcwidth.js` to return 0 for the NUL character and -1 for non-printable | ||
control characters. Setting a negative value to `nul` or `control` makes the | ||
`wcwidth` property set to -1 for any string that contains NUL or control | ||
characters respectively. If you plan to replace each control character with, | ||
say, `???` when printing, you can 'require' `wcwidth.js` as follows: | ||
The argument `{ nul: 0, control: -1, monkeypatch: true }` (which are the | ||
default values, in fact) tells `wcwidth.js` to return 0 for the NUL character | ||
and -1 for non-printable control characters. Setting a negative value to `nul` | ||
or `control` makes the `wcwidth` property set to -1 for any string that | ||
contains NUL or control characters respectively. If you plan to replace each | ||
control character with, say, `???` when printing, you can 'require' | ||
`wcwidth.js` as follows: | ||
@@ -43,3 +45,13 @@ var wcwidth = require('wcwidth')({ | ||
`wcwidth.js` also provides a methods. Since JavaScript has no character type, | ||
The last option `monkeypatch` allows `wcwidth.js` to monkey-patch | ||
`String.prototype` to provide the getter `wcwidth`. Even if it is convenient to | ||
have a getter that looks like the native one, it is sometimes unwanted as | ||
adding a getter into `String.prototype` may break node.js's module system; you | ||
are not guaranteed to have the version your code `require`s through the getter | ||
if other modules you're using also depend on other versions of `wcwidth.js` | ||
(thanks to [timoxley](https://github.com/timoxley) for the information). By | ||
setting `monkeypatch` to `false`, `wcwidth.js` touches no global object and | ||
provides no getter but a callable method explained below. | ||
`wcwidth.js` also provides a method. Since JavaScript has no character type, | ||
it is meaningless to have two versions while POSIX does for C. The method also | ||
@@ -46,0 +58,0 @@ accepts a code value that can be obtained by the `charCodeAt()` method. |
@@ -66,3 +66,2 @@ /* | ||
var assert = require('assert'); | ||
var _ = require('underscore'); | ||
@@ -88,3 +87,3 @@ | ||
// | ||
// optional option = { null: width, control: width } | ||
// optional option = { null: width, control: width, monkeypatch: boolean } | ||
module.exports = wcwidth = function (option) { | ||
@@ -150,4 +149,2 @@ // sorted list of non-overlapping intervals of non-spacing characters | ||
assert(_.isFinite(ucs)); | ||
if (ucs < combining[0][0] || ucs > combining[max][1]) | ||
@@ -170,7 +167,2 @@ return false; | ||
var wcwidth = function (ucs) { | ||
assert((_.isString(ucs) && ucs.length === 1) || _.isFinite(ucs)); | ||
if (_.isString(ucs)) | ||
ucs = ucs.charCodeAt(0); | ||
// test for 8-bit control characters | ||
@@ -204,7 +196,7 @@ if (ucs === 0) | ||
var wcswidth = function (str) { | ||
var i, n, s = 0; | ||
var i, l, n, s = 0; | ||
if (_.isString(str)) | ||
for (i = 0; i < str.length; i++) { | ||
if ((n = wcwidth(str.charAt(i))) < 0) | ||
for (i=0, l=str.length; i < l; i++) { | ||
if ((n = wcwidth(str.charCodeAt(i))) < 0) | ||
return -1; | ||
@@ -219,12 +211,12 @@ s += n; | ||
assert(_.isUndefined(option) || _.isObject(option)); | ||
option = _.extend({ | ||
nul: 0, | ||
control: -1 | ||
nul: 0, | ||
control: -1, | ||
monkeypatch: true | ||
}, option); | ||
String.prototype.__defineGetter__('wcwidth', function () { | ||
return wcswidth(this); | ||
}); | ||
if (option.monkeypatch) | ||
String.prototype.__defineGetter__('wcwidth', function () { | ||
return wcswidth(this); | ||
}); | ||
@@ -247,11 +239,15 @@ return wcswidth; | ||
for (var i = 0; i < test.length; i++) | ||
console.log(test[i], test[i].length, test[i].wcwidth); | ||
console.log(test[i], test[i].length, ww(test[i]), test[i].wcwidth); | ||
console.log(ww('한글')); | ||
console.log(ww('한'.charCodeAt(0))); | ||
}()); | ||
*/ | ||
/* | ||
(function () { | ||
var ww = wcwidth({ | ||
nul: 1, | ||
control: 1 | ||
nul: 1, | ||
control: 1, | ||
monkeypatch: false | ||
}); | ||
@@ -267,3 +263,3 @@ var test = [ | ||
for (var i = 0; i < test.length; i++) | ||
console.log(test[i], test[i].length, test[i].wcwidth); | ||
console.log(test[i], test[i].length, ww(test[i]), test[i].wcwidth); | ||
console.log(ww('한글')); | ||
@@ -270,0 +266,0 @@ console.log(ww('한'.charCodeAt(0))); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17829
66
240