Skip to content

Commit 17bef71

Browse files
committed
Enhanced performance by removing checks and adding small tweaks
1 parent 816f71a commit 17bef71

File tree

1 file changed

+3
-13
lines changed

1 file changed

+3
-13
lines changed

wcwidth.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
* Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
6464
*/
6565

66-
var assert = require('assert');
6766
var _ = require('underscore');
6867

6968

@@ -145,8 +144,6 @@ module.exports = wcwidth = function (option) {
145144
max = combining.length-1;
146145
var mid;
147146

148-
assert(_.isFinite(ucs));
149-
150147
if (ucs < combining[0][0] || ucs > combining[max][1])
151148
return false;
152149
while (max >= min) {
@@ -164,11 +161,6 @@ module.exports = wcwidth = function (option) {
164161

165162
// ucs = 'character' or charCode
166163
var wcwidth = function (ucs) {
167-
assert((_.isString(ucs) && ucs.length === 1) || _.isFinite(ucs));
168-
169-
if (_.isString(ucs))
170-
ucs = ucs.charCodeAt(0);
171-
172164
// test for 8-bit control characters
173165
if (ucs === 0)
174166
return option.nul;
@@ -197,11 +189,11 @@ module.exports = wcwidth = function (option) {
197189
};
198190

199191
var wcswidth = function (str) {
200-
var i, n, s = 0;
192+
var i, l, n, s = 0;
201193

202194
if (_.isString(str))
203-
for (i = 0; i < str.length; i++) {
204-
if ((n = wcwidth(str.charAt(i))) < 0)
195+
for (i=0, l=str.length; i < l; i++) {
196+
if ((n = wcwidth(str.charCodeAt(i))) < 0)
205197
return -1;
206198
s += n;
207199
}
@@ -211,8 +203,6 @@ module.exports = wcwidth = function (option) {
211203
return s;
212204
};
213205

214-
assert(_.isUndefined(option) || _.isObject(option));
215-
216206
option = _.extend({
217207
nul: 0,
218208
control: -1

0 commit comments

Comments
 (0)