A tweet-sized ES5-compatible polyfill for Array.prototype.lastIndexOf based on a similar function in my own tiny.js
-
-
Save atk/1034458 to your computer and use it in GitHub Desktop.
polyfill an ES5-compatibile Array.prototype.lastIndexOf where needed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// avoid overwriting Array.prototype.lastIndexOf | |
// otherwise it becomes enumerable, which leads to | |
// errors in Chrome | |
[].lastIndexOf || (Array.prototype.lastIndexOf = function( | |
a, // item to be found | |
b // index placeholder | |
) { | |
for ( | |
// initialize index | |
b=this.length; | |
// if the index decreased by one is not already -1 | |
// index is not set (sparse array) | |
// and the item at index is not identical to the searched one | |
~--b && (!(b in this) || this[b] !== a);); | |
// return index of last found item or -1 | |
return b | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[].lastIndexOf||(Array.prototype.lastIndexOf=function(a,b){for(b=this.length;~--b&&(!(b in this)||this[b]!==a););return b}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Alex Kloss <[email protected]> | |
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "lastIndexOf", | |
"description": "polyfill an ES5-compatibile Array.prototype.lastIndexOf where needed.", | |
"keywords": [ | |
"array", | |
"lastIndexof", | |
"es5", | |
"polyfill" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: <b>0,1,2,-1</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var lastIndexOf = | |
[].lastIndexOf||(Array.prototype.lastIndexOf=function(a,b){for(b=this.length;~--b&&(!(b in this)||this[b]!==a););return b}) | |
var testdata=[1,2,3,5]; | |
document.getElementById( "ret" ).innerHTML = [lastIndexOf.call(testdata,1), lastIndexOf.call(testdata,2), lastIndexOf.call(testdata,3), lastIndexOf.call(testdata,4)]; | |
</script> |
Seems we don't need b?
function(a,c){for(c=this.length;this[--c]!==a&&~c;);return c}
Now we have as much ES5-compatibility as possible; the length is resolved correctly, sparse arrays are taken care of and the Chrome bug is resolved by the more secure shim logic.
This is still missing fromIndex support.
What about
function(a,b){for(++b>0?0:b=this.length+~~b;~--b&&(!(b in this)||this[b]!==a););return b}
?
Updated.
I think it still has room to be shortened.
@tsaniel: nice one! I think that'll work!
By the way, i wonder if
[].lastIndexOf||(Array.prototype.lastIndexOf=...)
and
[].lastIndexOf?0:Array.prototype.lastIndexOf=...
are equal.
@tsaniel It's not clamping the fromIndex
correctly:
5: If argument fromIndex was passed let n be ToInteger(fromIndex); else let n be len.
6: If n ≥ 0, then let k be min(n, len – 1).
7: Else, n < 0
---- a. Let k be len - abs(n).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Despite the description,
polyfill an ES5-compatibile Array.prototype.lastIndexOf where needed.
, this is not an ES5 compatible fallback because it lacks sparse array support and resolves thelength
incorrectly. Also browsers like Chrome have a bug whereArray.prototype.lastIndexOf = [].lastIndexOf
will causeArray#lastIndexOf
to become enumerable.