Skip to content

Commit 4ba17a2

Browse files
committed
Provides option to avoid monkey-patching String.prototype (#4)
1 parent 17bef71 commit 4ba17a2

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

README.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@ strings, but inspecting the `wcwidth` property is enough. The following code
1616
snippet shows how to use `wcwidth.js`:
1717

1818
var wcwidth = require('wcwidth')({
19-
nul: 0,
20-
control: -1
19+
nul: 0,
20+
control: -1,
21+
monkeypatch: true
2122
}); // equivalent to var wcwidth = require('wcwidth')();
2223

2324
console.log("한글".wcwidth); // prints 4
2425
console.log("\0".wcwidth); // prints 0
2526
console.log("\t".wcwidth); // prints -1
2627

27-
The argument `{ nul: 0, control: -1 }` (which are the default values, in fact)
28-
tells `wcwidth.js` to return 0 for the NUL character and -1 for non-printable
29-
control characters. Setting a negative value to `nul` or `control` makes the
30-
`wcwidth` property set to -1 for any string that contains NUL or control
31-
characters respectively. If you plan to replace each control character with,
32-
say, `???` when printing, you can 'require' `wcwidth.js` as follows:
28+
The argument `{ nul: 0, control: -1, monkeypatch: true }` (which are the
29+
default values, in fact) tells `wcwidth.js` to return 0 for the NUL character
30+
and -1 for non-printable control characters. Setting a negative value to `nul`
31+
or `control` makes the `wcwidth` property set to -1 for any string that
32+
contains NUL or control characters respectively. If you plan to replace each
33+
control character with, say, `???` when printing, you can 'require'
34+
`wcwidth.js` as follows:
3335

3436
var wcwidth = require('wcwidth')({
3537
control: 3
@@ -38,7 +40,17 @@ say, `???` when printing, you can 'require' `wcwidth.js` as follows:
3840
console.log("\t".wcwidth); // prints 3
3941
console.log("\0".wcwidth); // prints 0
4042

41-
`wcwidth.js` also provides a methods. Since JavaScript has no character type,
43+
The last option `monkeypatch` allows `wcwidth.js` to monkey-patch
44+
`String.prototype` to provide the getter `wcwidth`. Even if it is convenient to
45+
have a getter that looks like the native one, it is sometimes unwanted as
46+
adding a getter into `String.prototype` may break node.js's module system; you
47+
are not guaranteed to have the version your code `require`s through the getter
48+
if other modules you're using also depend on other versions of `wcwidth.js`
49+
(thanks to [timoxley](https://github.com/timoxley) for the information). By
50+
setting `monkeypatch` to `false`, `wcwidth.js` touches no global object and
51+
provides no getter but a callable method explained below.
52+
53+
`wcwidth.js` also provides a method. Since JavaScript has no character type,
4254
it is meaningless to have two versions while POSIX does for C. The method also
4355
accepts a code value that can be obtained by the `charCodeAt()` method.
4456

wcwidth.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ var _ = require('underscore');
8383
//
8484
// This implementation assumes that characters are encoded in ISO 10646.
8585
//
86-
// optional option = { null: width, control: width }
86+
// optional option = { null: width, control: width, monkeypatch: boolean }
8787
module.exports = wcwidth = function (option) {
8888
// sorted list of non-overlapping intervals of non-spacing characters
8989
// generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c"
@@ -204,13 +204,15 @@ module.exports = wcwidth = function (option) {
204204
};
205205

206206
option = _.extend({
207-
nul: 0,
208-
control: -1
207+
nul: 0,
208+
control: -1,
209+
monkeypatch: true
209210
}, option);
210211

211-
String.prototype.__defineGetter__('wcwidth', function () {
212-
return wcswidth(this);
213-
});
212+
if (option.monkeypatch)
213+
String.prototype.__defineGetter__('wcwidth', function () {
214+
return wcswidth(this);
215+
});
214216

215217
return wcswidth;
216218
};
@@ -228,15 +230,19 @@ module.exports = wcwidth = function (option) {
228230
];
229231
230232
for (var i = 0; i < test.length; i++)
231-
console.log(test[i], test[i].length, test[i].wcwidth);
233+
console.log(test[i], test[i].length, ww(test[i]), test[i].wcwidth);
232234
console.log(ww('한글'));
233235
console.log(ww('한'.charCodeAt(0)));
234236
}());
237+
*/
235238

239+
240+
/*
236241
(function () {
237242
var ww = wcwidth({
238-
nul: 1,
239-
control: 1
243+
nul: 1,
244+
control: 1,
245+
monkeypatch: false
240246
});
241247
var test = [
242248
'다섯글자임',
@@ -247,7 +253,7 @@ module.exports = wcwidth = function (option) {
247253
];
248254
249255
for (var i = 0; i < test.length; i++)
250-
console.log(test[i], test[i].length, test[i].wcwidth);
256+
console.log(test[i], test[i].length, ww(test[i]), test[i].wcwidth);
251257
console.log(ww('한글'));
252258
console.log(ww('한'.charCodeAt(0)));
253259
}());

0 commit comments

Comments
 (0)