Skip to content

Commit 21ccac0

Browse files
author
cbojar
committed
Move browser shims into a separate file so they can be included as needed.
1 parent 6f2b748 commit 21ccac0

File tree

3 files changed

+100
-6
lines changed

3 files changed

+100
-6
lines changed

browser-shims.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
if (!String.prototype.trim) {
2+
String.prototype.trim = function () {
3+
return this.replace(/^\s+|\s+$/gm, '');
4+
};
5+
}
6+
7+
if (!Array.prototype.indexOf) {
8+
Array.prototype.indexOf = function (searchElement, fromIndex) {
9+
if ( this === undefined || this === null ) {
10+
throw new TypeError( '"this" is null or not defined' );
11+
}
12+
13+
var length = this.length >>> 0; // Hack to convert object.length to a UInt32
14+
15+
fromIndex = +fromIndex || 0;
16+
17+
if (Math.abs(fromIndex) === Infinity) {
18+
fromIndex = 0;
19+
}
20+
21+
if (fromIndex < 0) {
22+
fromIndex += length;
23+
if (fromIndex < 0) {
24+
fromIndex = 0;
25+
}
26+
}
27+
28+
for (;fromIndex < length; fromIndex++) {
29+
if (this[fromIndex] === searchElement) {
30+
return fromIndex;
31+
}
32+
}
33+
34+
return -1;
35+
};
36+
}
37+
38+
if (!Array.prototype.filter)
39+
{
40+
Array.prototype.filter = function(fun /*, thisArg */)
41+
{
42+
if (this === undefined || this === null)
43+
throw new TypeError();
44+
45+
var t = Object(this);
46+
var len = t.length >>> 0;
47+
if (typeof fun !== "function")
48+
throw new TypeError();
49+
50+
var res = [];
51+
var thisArg = arguments.length >= 2 ? arguments[1] : undefined;
52+
for (var i = 0; i < len; i++)
53+
{
54+
if (i in t)
55+
{
56+
var val = t[i];
57+
58+
// NOTE: Technically this should Object.defineProperty at
59+
// the next index, as push can be affected by
60+
// properties on Object.prototype and Array.prototype.
61+
// But that method's new, and collisions should be
62+
// rare, so use the more-compatible alternative.
63+
if (fun.call(thisArg, val, i, t))
64+
res.push(val);
65+
}
66+
}
67+
68+
return res;
69+
};
70+
}
71+
72+
if (!Array.prototype.map)
73+
{
74+
Array.prototype.map = function(fun /*, thisArg */)
75+
{
76+
if (this === undefined || this === null)
77+
throw new TypeError();
78+
79+
var t = Object(this);
80+
var len = t.length >>> 0;
81+
if (typeof fun !== "function")
82+
throw new TypeError();
83+
84+
var res = new Array(len);
85+
var thisArg = arguments.length >= 2 ? arguments[1] : undefined;
86+
for (var i = 0; i < len; i++)
87+
{
88+
// NOTE: Absolute correctness would demand Object.defineProperty
89+
// be used. But this method is fairly new, and failure is
90+
// possible only if Object.prototype or Array.prototype
91+
// has a property |i| (very unlikely), so use a less-correct
92+
// but more portable alternative.
93+
if (i in t)
94+
res[i] = fun.call(thisArg, t[i], i, t);
95+
}
96+
97+
return res;
98+
};
99+
}

parse-names-test.html

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<div id="qunit"></div>
99
<div id="qunit-fixture"></div>
1010
<script src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
11+
<script src="browser-shims.js"></script>
1112
<script src="parse-names.js"></script>
1213
<script>
1314
var names = [

parse-names.js

-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
// ported to JavaScript by Mark Pemburn (pemburnia.com)
44
// released under Apache 2.0 license
55

6-
if (!String.prototype.trim) {
7-
String.prototype.trim = function () {
8-
return this.replace(/^\s+|\s+$/gm, '');
9-
};
10-
}
11-
126
var NameParse = (function(){
137
function NameParse() {
148
return NameParse;

0 commit comments

Comments
 (0)