It is the fourth article from the polyfill series. If you haven't read the previous article in this polyfill series then you can find those articles here
Today we will learn about the polyfill of the array some method.
The behavior of the already existing method
Let us first see how the some method works in javascript.
It is used to check if at least one element passes the condition given in the callback, if so then it will return true else it will return false.
It can be called in three ways
some((val) => {...} )
some(callback , thisArgs)
some(function() {} , thisArgs)
CASE 1: let array = [1,2,3,4];
array.some((val) => val > 1);
//RESULT
true
CASE 2: let array = [];
array.some((val) => val > 1);
//RESULT
false
CASE 3: let array = [1,2,3];
array.some();
//RESULT
Uncaught TypeError: missing argument 0 when calling function Array.prototype.some
CASE 4: let object = {
length: 3,
0: "a",
1: "b",
2: "c",
}; //using object
console.log(Array.prototype.some.call(arrayLike, (x) => typeof x === "number"));
//RESULT
false
Let's create our custom some method now
//Whenever we will call [].customSome(() => ...)
//this -> []
function customSome(callback) {
//First we will check the type of this
//and we will check if the callback is present or not
//and if callback is present then it should be of type function
if (this === null || this === undefined) {
throw new TypeError('some is not a function')
}
else if (callback && typeof callback !== 'function') {
throw new TypeError('missing argument 0 when calling function Array.prototype.customSome');
}
}
Array.prototype.customSome = customSome;
//Whenever we will call [].customSome(()=> ...)
//this -> []
function customSome(callback) {
//First we will check the type of this
//and we will check if the callback is present or not
//and if callback is present then it should be of type function
if (this === null || this === undefined) {
throw new TypeError('some is not a function')
}
else if (callback && typeof callback !== 'function') {
throw new TypeError('missing argument 0 when calling function Array.prototype.customSome');
}
//Now if we know this is a type of
// object or array, we will find the length
let list = Object(this); //creating a instance of this
let thisArgs = arguments[1] || this; //if this is explicitly passed
let length = this.length || 0;
//Now if we don't have anything to check therefore
//we have to change nothing, just we have to return false
if (!length) return false;
}
Array.prototype.customSome = customSome;
//Whenever we will call [].customSome(()=> ...))
//this -> []
function customSome(callback) {
//First we will check the type of this
//and we will check if the callback is present or not
//and if callback is present then it should be of type function
if (this === null || this === undefined) {
throw new TypeError('some is not a function')
}
else if (callback && typeof callback !== 'function') {
throw new TypeError('missing argument 0 when calling function Array.prototype.customSome');
}
//Now if we know this is a type of
// object or array, we will find the length
let list = Object(this);
let thisArgs = arguments[1] || this; //if this is explicitly passed
let length = this.length || 0;
//Now if we don't have anything to check therefore
//we have to change nothing, just we have to return false
if (!length) return false;
let currentValue = undefined;
let found = false;
//looping over the array
for (let i = 0; i < length; i++) {
currentValue = this[i];
//now we are calling callback function for each value in the array
//if it returns true our search ends there only, as we have to found atleast one match else we have to keep our search on
if (i in list && callback.call(thisArgs, currentValue, i, list)) {
found = true;
break;
}
}
return found; //returning the result
}
Array.prototype.customSome = customSome;
Hope you enjoyed reading, learning and implementing array some polyfill.
If you enjoyed then support by liking and commenting your views.
We will meet with another article, till then Goodbye and happy learning :-)