Skip to content

Commit 120f70a

Browse files
committed
1 parent f5ee98e commit 120f70a

File tree

1 file changed

+87
-8
lines changed

1 file changed

+87
-8
lines changed

src/jquery.counter.js

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* jQuery.counter plugin
33
*
4-
* Copyright (c) 2012 Sophilabs <contact@sophilabs.com>
4+
* Copyright (c) 2012 Sophilabs <hi@sophilabs.com>
55
* MIT License
66
*/
77

@@ -56,7 +56,7 @@
5656
while (digits.length < part.padding) {
5757
digits = '0' + digits;
5858
}
59-
$.each(digits.split(''), function(j, digit) {
59+
$.each(split(digits, ''), function(j, digit) {
6060
animate(e, i, j, digit);
6161
});
6262
i--;
@@ -68,29 +68,108 @@
6868
edigit.attr('class', 'digit digit' + digit + ' digit' + edigit.text() + digit).text(digit);
6969
};
7070

71+
//from http://blog.stevenlevithan.com/archives/cross-browser-split
72+
var split = function(undef) {
73+
74+
var nativeSplit = String.prototype.split,
75+
compliantExecNpcg = /()??/.exec("")[1] === undef, // NPCG: nonparticipating capturing group
76+
self;
77+
78+
self = function (str, separator, limit) {
79+
// If `separator` is not a regex, use `nativeSplit`
80+
if (Object.prototype.toString.call(separator) !== "[object RegExp]") {
81+
return nativeSplit.call(str, separator, limit);
82+
}
83+
var output = [],
84+
flags = (separator.ignoreCase ? "i" : "") +
85+
(separator.multiline ? "m" : "") +
86+
(separator.extended ? "x" : "") + // Proposed for ES6
87+
(separator.sticky ? "y" : ""), // Firefox 3+
88+
lastLastIndex = 0,
89+
// Make `global` and avoid `lastIndex` issues by working with a copy
90+
separator = new RegExp(separator.source, flags + "g"),
91+
separator2, match, lastIndex, lastLength;
92+
str += ""; // Type-convert
93+
if (!compliantExecNpcg) {
94+
// Doesn't need flags gy, but they don't hurt
95+
separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags);
96+
}
97+
/* Values for `limit`, per the spec:
98+
* If undefined: 4294967295 // Math.pow(2, 32) - 1
99+
* If 0, Infinity, or NaN: 0
100+
* If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
101+
* If negative number: 4294967296 - Math.floor(Math.abs(limit))
102+
* If other: Type-convert, then use the above rules
103+
*/
104+
limit = limit === undef ?
105+
-1 >>> 0 : // Math.pow(2, 32) - 1
106+
limit >>> 0; // ToUint32(limit)
107+
while (match = separator.exec(str)) {
108+
// `separator.lastIndex` is not reliable cross-browser
109+
lastIndex = match.index + match[0].length;
110+
if (lastIndex > lastLastIndex) {
111+
output.push(str.slice(lastLastIndex, match.index));
112+
// Fix browsers whose `exec` methods don't consistently return `undefined` for
113+
// nonparticipating capturing groups
114+
if (!compliantExecNpcg && match.length > 1) {
115+
match[0].replace(separator2, function () {
116+
for (var i = 1; i < arguments.length - 2; i++) {
117+
if (arguments[i] === undef) {
118+
match[i] = undef;
119+
}
120+
}
121+
});
122+
}
123+
if (match.length > 1 && match.index < str.length) {
124+
Array.prototype.push.apply(output, match.slice(1));
125+
}
126+
lastLength = match[0].length;
127+
lastLastIndex = lastIndex;
128+
if (output.length >= limit) {
129+
break;
130+
}
131+
}
132+
if (separator.lastIndex === match.index) {
133+
separator.lastIndex++; // Avoid an infinite loop
134+
}
135+
}
136+
if (lastLastIndex === str.length) {
137+
if (lastLength || !separator.test("")) {
138+
output.push("");
139+
}
140+
} else {
141+
output.push(str.slice(lastLastIndex));
142+
}
143+
return output.length > limit ? output.slice(0, limit) : output;
144+
};
145+
146+
return self;
147+
148+
}();
149+
71150
return this.each(function() {
72151
var e = $(this);
73152
var data = e.data('counter') || {};
74153
data.interval = parseInt(options.interval || e.attr('data-interval') || '1000', 10);
75154
data.down = ( options.direction || e.attr('data-direction') || 'down') == 'down';
76155
data.parts = [];
77-
var initial = (options.initial || e.text()).split(/([^0-9]+)/);
156+
var initial = split(options.initial || e.text(), /([^0-9]+)/);
78157
//WARN: Use attr() no data()
79-
var format = (options.format || e.attr('data-format') || "23:59:59").split(/([^0-9]+)/);
158+
var format = split(options.format || e.attr('data-format') || "23:59:59", /([^0-9]+)/);
80159
var stop = options.stop || e.attr('data-stop');
81160
if (stop) {
82-
stop = stop.split(/([^0-9]+)/);
161+
stop = split(stop, /([^0-9]+)/);
83162
}
84163
e.html('');
85164
$.each(format, function(index, value) {
86165
if (/^\d+$/.test(value)) {
87166
var part = {};
88167
part.index = index;
89168
part.padding = (value + '').length;
90-
part.limit = parseFloat(value);
91-
part.value = parseFloat(initial[initial.length - format.length + index] || 0);
169+
part.limit = parseInt(value, 10);
170+
part.value = parseInt(initial[initial.length - format.length + index] || 0, 10);
92171
part.value = part.value > part.limit ? part.limit : part.value;
93-
part.stop = parseFloat(stop ? stop[stop.length - format.length + index] : (data.down ? 0 : part.limit));
172+
part.stop = parseInt(stop ? stop[stop.length - format.length + index] : (data.down ? 0 : part.limit), 10);
94173
part.stop = part.stop > part.limit ? part.limit : part.stop;
95174
part.stop = part.stop < 0 ? 0 : part.stop;
96175
var epart = $('<span>').addClass('part').addClass('part' + index);

0 commit comments

Comments
 (0)