├── .editorconfig ├── .gitignore ├── LICENSE ├── README.md ├── array-includes.js ├── index.js ├── package-lock.json ├── package.json └── typings.d.ts /.editorconfig: -------------------------------------------------------------------------------- 1 | [*] 2 | indent_size = 2 3 | indent_style = space -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | node_modules/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Kevin Latusinski 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Polyfill Array.prototype.includes 2 | 3 | This is a polyfill for the Array.prototype.includes method based on the code from [MDN](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/includes). 4 | 5 | ## Installation 6 | 7 | ```npm install polyfill-array-includes``` 8 | 9 | ```yarn add polyfill-array-includes``` 10 | 11 | ### Usage 12 | 13 | With ES6: 14 | ``` javascript 15 | import 'polyfill-array-includes'; 16 | ``` 17 | 18 | In Browser: 19 | ``` html 20 | 21 | ``` 22 | -------------------------------------------------------------------------------- /array-includes.js: -------------------------------------------------------------------------------- 1 | if (!Array.prototype.includes) { 2 | Object.defineProperty(Array.prototype, 'includes', { 3 | value: function (searchElement, fromIndex) { 4 | 5 | // 1. Let O be ? ToObject(this value). 6 | if (this == null) { 7 | throw new TypeError('"this" is null or not defined'); 8 | } 9 | 10 | var o = Object(this); 11 | 12 | // 2. Let len be ? ToLength(? Get(O, "length")). 13 | var len = o.length >>> 0; 14 | 15 | // 3. If len is 0, return false. 16 | if (len === 0) { 17 | return false; 18 | } 19 | 20 | // 4. Let n be ? ToInteger(fromIndex). 21 | // (If fromIndex is undefined, this step produces the value 0.) 22 | var n = fromIndex | 0; 23 | 24 | // 5. If n ≥ 0, then 25 | // a. Let k be n. 26 | // 6. Else n < 0, 27 | // a. Let k be len + n. 28 | // b. If k < 0, let k be 0. 29 | var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); 30 | 31 | function sameValueZero(x, y) { 32 | return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y)); 33 | } 34 | 35 | // 7. Repeat, while k < len 36 | while (k < len) { 37 | // a. Let elementK be the result of ? Get(O, ! ToString(k)). 38 | // b. If SameValueZero(searchElement, elementK) is true, return true. 39 | // c. Increase k by 1. 40 | if (sameValueZero(o[k], searchElement)) { 41 | return true; 42 | } 43 | k++; 44 | } 45 | 46 | // 8. Return false 47 | return false; 48 | } 49 | }); 50 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | Array.prototype.includes||Object.defineProperty(Array.prototype,"includes",{value:function(r,e){if(null==this)throw new TypeError('"this" is null or not defined');var t=Object(this),n=t.length>>>0;if(0===n)return!1;var i,o,a=0|e,u=Math.max(0<=a?a:n-Math.abs(a),0);for(;u { 2 | includes(item: T): boolean; 3 | } --------------------------------------------------------------------------------