@@ -16,20 +16,22 @@ strings, but inspecting the `wcwidth` property is enough. The following code
1616snippet 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,
4254it is meaningless to have two versions while POSIX does for C. The method also
4355accepts a code value that can be obtained by the ` charCodeAt() ` method.
4456
0 commit comments