Skip to content

Commit 0bf8e23

Browse files
committed
Performance improvements
1 parent 9b4ea4f commit 0bf8e23

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

build/smart-number-inputs.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
3030
return element.addEventListener(event, callback);
3131
};
3232

33+
var forEach = function forEach(list, callback) {
34+
for (var i = 0; i < list.length; i++) {
35+
callback(list[i]);
36+
}
37+
};
38+
3339
var ARROW_UP = 38;
3440
var ARROW_DOWN = 40;
3541
var WHITESPACE = /(\s+)/;
@@ -55,27 +61,23 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
5561
};
5662

5763
var enable = function enable(element) {
58-
if (isArray(element)) {
59-
return element.forEach(enable);
60-
}
61-
62-
if (element instanceof NodeList) {
63-
return Array.prototype.forEach.call(element, enable);
64+
if (isArray(element) || element instanceof NodeList) {
65+
return forEach(element, enable);
6466
}
6567

6668
on(element, 'keydown', eventHandler, false);
6769
};
6870

6971
var modify = function modify(value, start, end, addition) {
70-
var segments = value.toString().split(WHITESPACE);
72+
var segments = value.split(WHITESPACE);
7173

7274
// Will transform selection to encapsulate affected segments
7375
var newStart = null;
7476
var newEnd = null;
7577

7678
var character = 0;
7779

78-
for (var i = 0, len = segments.length, originalValue; i < len; i++) {
80+
for (var i = 0, originalValue; i < segments.length; i++) {
7981
originalValue = segments[i];
8082
character += originalValue.length;
8183

build/smart-number-inputs.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
const on = (element, event, callback) => element.addEventListener(event, callback);
1515

16+
const forEach = (list, callback) => {
17+
for(var i = 0; i < list.length; i++) callback(list[i]);
18+
};
19+
1620
const ARROW_UP = 38;
1721
const ARROW_DOWN = 40;
1822
const WHITESPACE = /(\s+)/;
@@ -38,27 +42,23 @@
3842
};
3943

4044
const enable = element => {
41-
if(isArray(element)) {
42-
return element.forEach(enable);
43-
}
44-
45-
if(element instanceof NodeList) {
46-
return Array.prototype.forEach.call(element, enable);
45+
if(isArray(element) || element instanceof NodeList) {
46+
return forEach(element, enable);
4747
}
4848

4949
on(element, 'keydown', eventHandler, false);
5050
};
5151

5252
const modify = (value, start, end, addition) => {
53-
const segments = value.toString().split(WHITESPACE);
53+
const segments = value.split(WHITESPACE);
5454

5555
// Will transform selection to encapsulate affected segments
5656
let newStart = null;
5757
let newEnd = null;
5858

5959
let character = 0;
6060

61-
for(let i = 0, len = segments.length, originalValue; i < len; i++) {
61+
for(let i = 0, originalValue; i < segments.length; i++) {
6262
originalValue = segments[i];
6363
character += originalValue.length;
6464

test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ const test = require('tape');
44
const modify = require('./src').modify;
55

66
test('value', t => {
7-
t.equal(modify(20, 0, 0, 1).value, '21');
8-
97
t.equal(modify('20', 0, 0, 1).value, '21');
108
t.equal(modify('20', 1, 1, 1).value, '21');
119
t.equal(modify('20', 2, 2, 1).value, '21');

0 commit comments

Comments
 (0)