-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckObjectInstanceOfClass.leetcode.js
More file actions
45 lines (37 loc) · 1.5 KB
/
checkObjectInstanceOfClass.leetcode.js
File metadata and controls
45 lines (37 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* @param {any} object
* @param {any} classFunction
* @return {boolean}
*/
const checkIfInstanceOf = (obj, classFunction) => {
const invalidObj = theyAreNotPresent(obj, classFunction);
if (invalidObj) return !invalidObj;
return Object(obj) instanceof classFunction;
// return obj?.__proto__ === classFunction?.prototype ? true :
// obj?.__proto__?.__proto__ === classFunction?.prototype ? true :
// obj?.__proto__?.__proto__?.__proto__ === classFunction?.prototype ? true : false;
};
const isObjectType = (obj) => {
return typeof obj === 'object' && obj !== null && !Array.isArray(obj);
};
const theyAreNotPresent = (obj1, obj2) => {
return (obj1 === null || obj2 === null ||
obj1 === undefined || typeof obj2 !== 'function');
};
const removeDuplicatedValues = (arr) => {
// When filter to get the first ocurrence of each element in the array, then sort ascending
return arr.filter((item, index) => {
return arr.indexOf(item) === index;
}).sort((a, b) => a - b);
};
const checkForPalindrome = (text) => {
const textToLower = text.toLowerCase();
const reverseText = textToLower.split("").reverse().join("");
return Object.is(reverseText, textToLower);
};
const factorialNumberByRecursion = (currentNumber) => {
if (currentNumber <= 1) return 1;
return currentNumber * factorialNumberByRecursion(currentNumber - 1);
};
module.exports = { checkIfInstanceOf, removeDuplicatedValues, checkForPalindrome,
factorialNumberByRecursion };