Skip to content

Instantly share code, notes, and snippets.

View ToTheHit's full-sized avatar

Dmitry Ushakov ToTheHit

  • Nizhny Novgorod, Russia
  • 09:23 (UTC +03:00)
View GitHub Profile
const compareObjects = (obj1, obj2) => {
for (let p in obj1) {
if (obj1.hasOwnProperty(p) !== obj2.hasOwnProperty(p)) return false;
switch (typeof (obj1[p])) {
case 'object':
if (!compareObjects(obj1[p], obj2[p])) return false;
break;
case 'function':
if (typeof (obj2[p]) === 'undefined' || (p !== 'compare' && obj1[p].toString() !== obj2[p].toString())) return false;
break;
import { useState, useEffect } from 'react';
export default function useDebounce(value, delay) {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(
() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
import { useState, useEffect, useRef } from 'react';
export default function useThrottle(value, limit) {
const [throttledValue, setThrottledValue] = useState(value);
const lastRan = useRef(Date.now());
useEffect(
() => {
const handler = setTimeout(function() {
if (Date.now() - lastRan.current >= limit) {
const getCorrectWord = (count, words = []) => {
const cases = [2, 0, 1, 1, 1, 2];
return words[(count % 100 > 4 && count % 100 < 20) ? 2 : cases[(count % 10 < 5) ? count % 10 : 5]];
}
/*
EXAMPLE
const minutes = 33;
const message = `До приезда осталось ${minutes} ${getCorrectWord(minutes, ['минута', 'минуты', 'минут'])}`
const numberOperators = ['$lt', '$gt', '$gte', '$lte', '$eq', '$ne'];
// TODO: Добавить поддержку otherOperators
const otherOperators = ['$eq', '$ne'];
/*
Пример объекта на вход:
const valueRaw = {
prepayment: {
value: {
$gte: '10000',
$lte: '20000'