Skip to content

Commit

Permalink
Build: Add lib/array-polyfills.js
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Jan 23, 2023
1 parent f68708f commit 0a23e45
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/array-polyfills.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// TODO: Add polyfill for Array.from

if (!Array.prototype.includes) {
Array.prototype.includes = function(searchElement, fromIndex) {
'use strict';
if (!this) {
throw new TypeError('Array.prototype.includes called on null or undefined');
}

return this.indexOf(searchElement, fromIndex) !== -1;
}
}

if (!Array.prototype.every) {
Array.prototype.every = function(func) {
'use strict';
let result = true;

this.forEach((item, index) => {
if (!result) {
return;
} else if (!func(item, index, this)) {
result = false;
}
});

return result;
}
}

if (!Array.prototype.some) {
Array.prototype.some = function(func) {
'use strict';
let result = false;

this.forEach((item, index) => {
if (result) {
return;
} else if (func(item, index, this)) {
result = true;
}
});

return result;
}
}

export default Array;

0 comments on commit 0a23e45

Please sign in to comment.