This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const deepEqual = (a, b, seen = new WeakSet()) => { | |
if (a === b) return true; | |
if (a == null || b == null) return false; | |
if (typeof a !== typeof b) return false; | |
if (typeof a === 'object' && typeof b === 'object') { | |
if (seen.has(a) || seen.has(b)) return true; | |
seen.add(a); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TreeNode { | |
constructor(value) { | |
this.value = value; | |
this.left = null; | |
this.right = null; | |
} | |
} | |
const pOne = new TreeNode(12); | |
pOne.left = new TreeNode(7); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const promiseAllSettled = (promises) => { | |
return new Promise((resolve) => { | |
const results = new Array(promises.length); | |
let settledCount = 0; | |
promises.forEach((promise, index) => { | |
Promise.resolve(promise) | |
.then((value) => { | |
results[index] = { status: 'fulfilled', value }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const flattenObject = (obj, parentKey = '', result = {}) => { | |
const separator = '.'; | |
for (const key in obj) { | |
if (obj.hasOwnProperty(key)) { | |
const newKey = parentKey ? `${parentKey}${separator}${key}` : key; | |
if (typeof obj[key] === 'object' && obj[key] !== null) { | |
flattenObject(obj[key], newKey, result); | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const memoWithExpiration = (fn, expirationTime) => { | |
const cache = new Map(); | |
return (...args) => { | |
const key = JSON.stringify(args); | |
const now = Date.now(); | |
if (cache.has(key)) { | |
const { value, timestamp } = cache.get(key); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isObject = (obj) => { | |
return typeof obj === 'object' && obj !== null; | |
} | |
const deepMerge = (target, source) => { | |
for (const key in source) { | |
if (source.hasOwnProperty(key)) { | |
const targetValue = target[key]; | |
const sourceValue = source[key]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TreeNode { | |
constructor(value) { | |
this.value = value; | |
this.left = null; | |
this.right = null; | |
} | |
} | |
const treeOne = new TreeNode(1); | |
treeOne.left = new TreeNode(2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rateLimiter = (fn, limit, interval) => { | |
let callCount = 0; | |
let lastResetTime = Date.now(); | |
return (...args) => { | |
const context = this; | |
const now = Date.now(); | |
if (now - lastResetTime > interval) { | |
callCount = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListNode { | |
constructor(value) { | |
this.value = value; | |
this.next = null; | |
} | |
} | |
const listOne = new ListNode(1); | |
listOne.next = new ListNode(2); | |
listOne.next.next = new ListNode(3); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function.prototype.myCall = function(thisArg, ...args) { | |
const context = thisArg != null ? thisArg : (typeof window !== 'undefined' ? window : global); | |
const fnSymbol = Symbol('fn'); | |
context[fnSymbol] = this; | |
const result = context[fnSymbol](...args); | |
delete context[fnSymbol]; |
NewerOlder