Skip to content

Instantly share code, notes, and snippets.

View jsgrill's full-sized avatar

JS-Прожарка jsgrill

View GitHub Profile
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);
class TreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
const pOne = new TreeNode(12);
pOne.left = new TreeNode(7);
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 };
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 {
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);
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];
class TreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
const treeOne = new TreeNode(1);
treeOne.left = new TreeNode(2);
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;
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);
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];