Skip to content

Commit ffaf377

Browse files
committed
Implementation.
1 parent 7f72537 commit ffaf377

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"max-statements-per-line": [2, { "max": 2 }],
99
"max-statements": [2, 12],
1010
"new-cap": [2, { "capIsNewExceptions": ["RequireObjectCoercible", "ToObject", "ToUint32", "IsCallable"] }],
11-
"no-magic-numbers": 0
11+
"no-magic-numbers": 0,
12+
"strict": 1
1213
}
1314
}

implementation.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
var ES = require('es-abstract/es5');
4+
var bind = require('function-bind');
5+
var isString = require('is-string');
6+
7+
// Check failure of by-index access of string characters (IE < 9)
8+
// and failure of `0 in boxedString` (Rhino)
9+
var boxedString = Object('a');
10+
var splitString = boxedString[0] !== 'a' || !(0 in boxedString);
11+
12+
var strSplit = bind.call(Function.call, String.prototype.split);
13+
14+
module.exports = function every(callbackfn) {
15+
var O = ES.ToObject(this);
16+
var self = splitString && isString(O) ? strSplit(O, '') : O;
17+
var len = ES.ToUint32(self.length);
18+
var T;
19+
if (arguments.length > 1) {
20+
T = arguments[1];
21+
}
22+
23+
// If no callback function or if callback is not a callable function
24+
if (!ES.IsCallable(callbackfn)) {
25+
throw new TypeError('Array.prototype.every callback must be a function');
26+
}
27+
28+
for (var i = 0; i < len; i++) {
29+
if (i in self && !(typeof T === 'undefined' ? callbackfn(self[i], i, O) : callbackfn.call(T, self[i], i, O))) {
30+
return false;
31+
}
32+
}
33+
return true;
34+
};

index.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
var define = require('define-properties');
4+
var ES = require('es-abstract/es6');
5+
6+
var implementation = require('./implementation');
7+
var getPolyfill = require('./polyfill');
8+
var polyfill = getPolyfill();
9+
var shim = require('./shim');
10+
11+
var slice = Array.prototype.slice;
12+
13+
// eslint-disable-next-line no-unused-vars
14+
var boundEveryShim = function every(array, callbackfn) {
15+
ES.RequireObjectCoercible(array);
16+
return polyfill.apply(array, slice.call(arguments, 1));
17+
};
18+
define(boundEveryShim, {
19+
getPolyfill: getPolyfill,
20+
implementation: implementation,
21+
shim: shim
22+
});
23+
24+
module.exports = boundEveryShim;

polyfill.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var implementation = require('./implementation');
2+
3+
module.exports = function getPolyfill() {
4+
if (typeof Array.prototype.every === 'function') {
5+
var hasPrimitiveContextInStrict = [1].every(function () {
6+
'use strict';
7+
return typeof this === 'string' && this === 'x';
8+
}, 'x');
9+
if (hasPrimitiveContextInStrict) {
10+
return Array.prototype.every;
11+
}
12+
}
13+
return implementation;
14+
};

shim.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
var define = require('define-properties');
4+
var getPolyfill = require('./polyfill');
5+
6+
module.exports = function shimArrayPrototypeEvery() {
7+
var polyfill = getPolyfill();
8+
define(
9+
Array.prototype,
10+
{ every: polyfill },
11+
{ every: function () { return Array.prototype.every !== polyfill; } }
12+
);
13+
return polyfill;
14+
};

0 commit comments

Comments
 (0)