Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Check boxing in other array methods #199

Merged
merged 2 commits into from

1 participant

Jordan Harband
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 69 additions and 22 deletions.
  1. +19 −14 es5-shim.js
  2. +50 −8 tests/spec/s-array.js
33 es5-shim.js
View
@@ -334,19 +334,24 @@ if (!Array.isArray) {
// http://es5.github.com/#x15.4.4.18
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/forEach
+
// Check failure of by-index access of string characters (IE < 9)
// and failure of `0 in boxedString` (Rhino)
-var boxedString = Object("a"),
- splitString = boxedString[0] != "a" || !(0 in boxedString);
-// Check node 0.6.21 bug where third parameter is not boxed
-var boxedForEach = true;
-if (Array.prototype.forEach) {
- Array.prototype.forEach.call("foo", function(item, i, obj) {
- if (typeof obj !== 'object') boxedForEach = false;
- });
-}
+var boxedString = Object("a");
+var splitString = boxedString[0] != "a" || !(0 in boxedString);
+
+var properlyBoxesContext = function properlyBoxed(method) {
+ // Check node 0.6.21 bug where third parameter is not boxed
+ var properlyBoxes = true;
+ if (method) {
+ method.call('foo', function (item, index, context) {
+ if (typeof context !== 'object') { properlyBoxes = false; }
+ });
+ }
+ return !!method && properlyBoxes;
+};
-if (!Array.prototype.forEach || !boxedForEach) {
+if (!Array.prototype.forEach || !properlyBoxesContext(Array.prototype.forEach)) {
Array.prototype.forEach = function forEach(fun /*, thisp*/) {
var object = toObject(this),
self = splitString && _toString(this) == "[object String]" ?
@@ -375,7 +380,7 @@ if (!Array.prototype.forEach || !boxedForEach) {
// ES5 15.4.4.19
// http://es5.github.com/#x15.4.4.19
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map
-if (!Array.prototype.map) {
+if (!Array.prototype.map || !properlyBoxesContext(Array.prototype.map)) {
Array.prototype.map = function map(fun /*, thisp*/) {
var object = toObject(this),
self = splitString && _toString(this) == "[object String]" ?
@@ -401,7 +406,7 @@ if (!Array.prototype.map) {
// ES5 15.4.4.20
// http://es5.github.com/#x15.4.4.20
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter
-if (!Array.prototype.filter) {
+if (!Array.prototype.filter || !properlyBoxesContext(Array.prototype.filter)) {
Array.prototype.filter = function filter(fun /*, thisp */) {
var object = toObject(this),
self = splitString && _toString(this) == "[object String]" ?
@@ -432,7 +437,7 @@ if (!Array.prototype.filter) {
// ES5 15.4.4.16
// http://es5.github.com/#x15.4.4.16
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
-if (!Array.prototype.every) {
+if (!Array.prototype.every || !properlyBoxesContext(Array.prototype.every)) {
Array.prototype.every = function every(fun /*, thisp */) {
var object = toObject(this),
self = splitString && _toString(this) == "[object String]" ?
@@ -458,7 +463,7 @@ if (!Array.prototype.every) {
// ES5 15.4.4.17
// http://es5.github.com/#x15.4.4.17
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
-if (!Array.prototype.some) {
+if (!Array.prototype.some || !properlyBoxesContext(Array.prototype.some)) {
Array.prototype.some = function some(fun /*, thisp */) {
var object = toObject(this),
self = splitString && _toString(this) == "[object String]" ?
58 tests/spec/s-array.js
View
@@ -1,3 +1,5 @@
+var toString = Object.prototype.toString;
+
describe('Array', function() {
var testSubject;
beforeEach(function() {
@@ -76,8 +78,7 @@ describe('Array', function() {
});
describe('strings', function() {
- var str = 'Hello, World!',
- toString = Object.prototype.toString;
+ var str = 'Hello, World!';
it('should iterate all in a string', function() {
actual = [];
Array.prototype.forEach.call(str, function(item, index) {
@@ -93,13 +94,14 @@ describe('Array', function() {
}, o);
expect(actual).toExactlyMatch(str.split(''));
});
- it('should have String object for third argument of callback', function() {
- Array.prototype.forEach.call(str, function(item, index, obj) {
- actual = obj;
- });
- expect(typeof actual).toBe("object");
- expect(toString.call(actual)).toBe("[object String]");
+ });
+ it('should have a boxed object as list argument of callback', function() {
+ var actual;
+ Array.prototype.forEach.call('foo', function(item, index, list) {
+ actual = list;
});
+ expect(typeof actual).toBe('object');
+ expect(toString.call(actual)).toBe('[object String]');
});
});
describe('some', function() {
@@ -196,6 +198,14 @@ describe('Array', function() {
}, o);
expect(actual).toExactlyMatch(expected);
});
+ it('should have a boxed object as list argument of callback', function() {
+ var actual;
+ Array.prototype.some.call('foo', function(item, index, list) {
+ actual = list;
+ });
+ expect(typeof actual).toBe('object');
+ expect(toString.call(actual)).toBe('[object String]');
+ });
});
describe('every', function() {
var actual, expected, numberOfRuns;
@@ -294,6 +304,14 @@ describe('Array', function() {
}, o);
expect(actual).toExactlyMatch(expected);
});
+ it('should have a boxed object as list argument of callback', function() {
+ var actual;
+ Array.prototype.every.call('foo', function(item, index, list) {
+ actual = list;
+ });
+ expect(typeof actual).toBe('object');
+ expect(toString.call(actual)).toBe('[object String]');
+ });
});
describe('indexOf', function() {
@@ -651,6 +669,14 @@ describe('Array', function() {
expect(testSubject).toExactlyMatch(copy);
});
});
+ it('should have a boxed object as list argument of callback', function() {
+ var actual;
+ Array.prototype.filter.call('foo', function(item, index, list) {
+ actual = list;
+ });
+ expect(typeof actual).toBe('object');
+ expect(toString.call(actual)).toBe('[object String]');
+ });
});
describe('map', function() {
var callback;
@@ -767,6 +793,14 @@ describe('Array', function() {
expect(i).toBe(3);
});
});
+ it('should have a boxed object as list argument of callback', function() {
+ var actual;
+ Array.prototype.map.call('foo', function(item, index, list) {
+ actual = list;
+ });
+ expect(typeof actual).toBe('object');
+ expect(toString.call(actual)).toBe('[object String]');
+ });
});
describe('reduce', function() {
@@ -916,6 +950,14 @@ describe('Array', function() {
expect(testSubject.reduce.length).toBe(1);
});
});
+ it('should have a boxed object as list argument of callback', function() {
+ var actual;
+ Array.prototype.reduce.call('foo', function(accumulator, item, index, list) {
+ actual = list;
+ });
+ expect(typeof actual).toBe('object');
+ expect(toString.call(actual)).toBe('[object String]');
+ });
});
describe('reduceRight', function() {
beforeEach(function() {
Something went wrong with that request. Please try again.