From fb050c4520f3e1d56fe981908bf97dd7af8de1d3 Mon Sep 17 00:00:00 2001 From: Yann Pringault Date: Wed, 25 May 2016 11:50:53 -0700 Subject: [PATCH] Add Array.prototype.includes polyfill Summary: Add `Array.prototype.includes` polyfill. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes I had all my `includes` running well on iOS 9 but when switching to Android `includes` threw an error. [The compatibility table](http://kangax.github.io/compat-table/esnext/#test-Array.prototype.includes_Array.prototype.includes) shows that is not supported on Android yet as well as iOS 6-8. With Chrome debugging it's working on both environment. Closes https://github.com/facebook/react-native/pull/7756 Reviewed By: davidaurelio Differential Revision: D3346873 Pulled By: vjeux fbshipit-source-id: 2e17d29992873fbe4448b962df0423e516455b4b --- .../Resolver/polyfills/Array.prototype.es6.js | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/react-packager/src/Resolver/polyfills/Array.prototype.es6.js b/react-packager/src/Resolver/polyfills/Array.prototype.es6.js index 2752aab5..7e2078f8 100644 --- a/react-packager/src/Resolver/polyfills/Array.prototype.es6.js +++ b/react-packager/src/Resolver/polyfills/Array.prototype.es6.js @@ -53,3 +53,39 @@ if (!Array.prototype.find) { } }); } + +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes +if (!Array.prototype.includes) { + Object.defineProperty(Array.prototype, 'includes', { + enumerable: false, + writable: true, + configurable: true, + value: function (searchElement) { + var O = Object(this); + var len = parseInt(O.length) || 0; + if (len === 0) { + return false; + } + var n = parseInt(arguments[1]) || 0; + var k; + if (n >= 0) { + k = n; + } else { + k = len + n; + if (k < 0) { + k = 0; + } + } + var currentElement; + while (k < len) { + currentElement = O[k]; + if (searchElement === currentElement || + (searchElement !== searchElement && currentElement !== currentElement)) { + return true; + } + k++; + } + return false; + } + }); +}