Skip to content

Instantly share code, notes, and snippets.

@eliperelman
Forked from 140bytes/LICENSE.txt
Created June 21, 2011 13:21
Show Gist options
  • Save eliperelman/1037838 to your computer and use it in GitHub Desktop.
Save eliperelman/1037838 to your computer and use it in GitHub Desktop.
Array.prototype.some polyfill for 140byt.es

Array.prototype.some polyfill for 140byt.es

Execute an expression for every element in an array and determine whether one of the elements make the expression evaluate truthy. This polyfill adds the some method to the array prototype for environments lower than ECMAScript 5.

Array.prototype.some = [].some || // Use the native every method if available, otherwise:
function (
a, // expression to test each element of the array against
b, // optionally change the 'this' context for the given callback
c, // placeholder iterator variable
d // placeholder variable (stores context of original array)
) {
for (c = 0, d = this; c < d.length; c++) // iterate over all of the array elements
if (a.call(b, d[c], c, d)) // call the given expression, passing in context, value, index, and original array
return !0; // if any expression evaluates true, immediately return since 'some' is true
return !1 // otherwise return false since all callbacks evaluated to false
}
Array.prototype.some=[].some||function(a,b,c,d){for(c=0,d=this;c<d.length;c++)if(a.call(b,d[c],c,d))return!0;return!1}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Eli Perelman <http://eliperelman.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "ArrayPrototypeSomePolyfill",
"description": "Array some polyfill method for ECMAScript versions older than 5.",
"keywords": [
"Array",
"prototype",
"some",
"polyfill",
"ecmascript5"
]
}
<!DOCTYPE html>
<title>Array.prototype.some polyfill</title>
<script>
Array.prototype.some=[].some||function(a,b,c,d){for(c=0,d=this;c<d.length;c++)if(a.call(b,d[c],c,d))return!0;return!1}
var arr = [2,4,6,8,10,12,14,16];
var arr2 = [1,2,3,4,5,6,7,8];
// logs true, since at least 'some' elements are less than 10
console.log(arr.some(function(value, index, myArray) {
return value < 10;
});
// logs false, since none of the values are greater than 20 or none of arr2 values are greater than 10
console.log(arr.some(function(value, index, myArray) {
// this refers to arr2, but myArray is still arr
return this[index] > 10 || value > 20;
}, arr2);
</script>
@jdalton
Copy link

jdalton commented Oct 6, 2011

@atk nice, lots of space left for smth like (115 bytes):

[].some||(Array.prototype.some=function(a,b,c,d){d=this;for(c in d)if(c==+c&&a.call(b,d[c],c,d))return!0;return!1})

which would avoid most problem keys but still allow keys like "03"... OR (125 bytes)

[].some||(Array.prototype.some=function(a,b,c,d){d=this;for(c in d)if(~~c+''==c&&c>-1&&a.call(b,d[c],c,d))return!0;return!1})

which should avoid that edge case issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment