From 366ced74f11a6cad30e18fc04af579ab9ddbb6ec Mon Sep 17 00:00:00 2001 From: Andrey Shulga Date: Sun, 17 Mar 2024 22:55:18 +0200 Subject: [PATCH] refactoring --- .stylelintcache | 2 +- src/helpers/cloneDeep.ts | 65 ------------------------------------ src/helpers/isEqual.ts | 44 ------------------------ src/helpers/merge.ts | 21 ------------ src/helpers/set.ts | 26 --------------- src/helpers/trim.ts | 16 --------- src/helpers/types/indexed.ts | 2 -- 7 files changed, 1 insertion(+), 175 deletions(-) delete mode 100644 src/helpers/cloneDeep.ts delete mode 100644 src/helpers/isEqual.ts delete mode 100644 src/helpers/merge.ts delete mode 100644 src/helpers/set.ts delete mode 100644 src/helpers/trim.ts delete mode 100644 src/helpers/types/indexed.ts diff --git a/.stylelintcache b/.stylelintcache index 30e9588a5..21f4c010a 100644 --- a/.stylelintcache +++ b/.stylelintcache @@ -1 +1 @@ -[{"C:\\1\\middle.messenger.praktikum.yandex\\src\\pages\\chat\\styles.scss":"1","C:\\1\\middle.messenger.praktikum.yandex\\src\\pages\\registration\\styles.scss":"2","C:\\1\\middle.messenger.praktikum.yandex\\src\\components\\Input\\styles.scss":"3","C:\\1\\middle.messenger.praktikum.yandex\\src\\pages\\login\\styles.scss":"4","C:\\1\\middle.messenger.praktikum.yandex\\src\\pages\\account\\styles.scss":"5","C:\\1\\middle.messenger.praktikum.yandex\\src\\components\\Form\\styles.scss":"6","C:\\1\\middle.messenger.praktikum.yandex\\src\\components\\Link\\styles.scss":"7","C:\\1\\middle.messenger.praktikum.yandex\\src\\pages\\error\\styles.scss":"8","C:\\1\\middle.messenger.praktikum.yandex\\src\\components\\Modal\\styles.scss":"9","C:\\1\\middle.messenger.praktikum.yandex\\src\\pages\\account\\components\\AccountPhoto\\styles.scss":"10","C:\\1\\middle.messenger.praktikum.yandex\\src\\components\\Button\\styles.scss":"11","C:\\1\\middle.messenger.praktikum.yandex\\src\\components\\Message\\styles.scss":"12"},{"size":11756,"mtime":1710702679458,"hashOfConfig":"13"},{"size":135,"mtime":1708892428481,"hashOfConfig":"13"},{"size":853,"mtime":1708907080241,"hashOfConfig":"13"},{"size":128,"mtime":1708892428478,"hashOfConfig":"13"},{"size":3868,"mtime":1709391740708,"hashOfConfig":"13"},{"size":851,"mtime":1708907078157,"hashOfConfig":"13"},{"size":390,"mtime":1708907040933,"hashOfConfig":"13"},{"size":395,"mtime":1708892428472,"hashOfConfig":"13"},{"size":1152,"mtime":1710584696300,"hashOfConfig":"13"},{"size":895,"mtime":1710102023778,"hashOfConfig":"13"},{"size":1159,"mtime":1709970950492,"hashOfConfig":"13"},{"size":1167,"mtime":1710702879251,"hashOfConfig":"13"},"1v6j01t"] \ No newline at end of file +[{"C:\\1\\middle.messenger.praktikum.yandex\\src\\pages\\chat\\styles.scss":"1","C:\\1\\middle.messenger.praktikum.yandex\\src\\pages\\registration\\styles.scss":"2","C:\\1\\middle.messenger.praktikum.yandex\\src\\components\\Input\\styles.scss":"3","C:\\1\\middle.messenger.praktikum.yandex\\src\\pages\\login\\styles.scss":"4","C:\\1\\middle.messenger.praktikum.yandex\\src\\pages\\account\\styles.scss":"5","C:\\1\\middle.messenger.praktikum.yandex\\src\\components\\Form\\styles.scss":"6","C:\\1\\middle.messenger.praktikum.yandex\\src\\components\\Link\\styles.scss":"7","C:\\1\\middle.messenger.praktikum.yandex\\src\\pages\\error\\styles.scss":"8","C:\\1\\middle.messenger.praktikum.yandex\\src\\components\\Modal\\styles.scss":"9","C:\\1\\middle.messenger.praktikum.yandex\\src\\pages\\account\\components\\AccountPhoto\\styles.scss":"10","C:\\1\\middle.messenger.praktikum.yandex\\src\\components\\Button\\styles.scss":"11","C:\\1\\middle.messenger.praktikum.yandex\\src\\components\\Message\\styles.scss":"12","C:\\1\\middle.messenger.praktikum.yandex\\src\\assets\\scss\\styles.scss":"13"},{"size":11756,"mtime":1710702679458,"hashOfConfig":"14"},{"size":228,"mtime":1710703881691,"hashOfConfig":"15"},{"size":853,"mtime":1708907080241,"hashOfConfig":"14"},{"size":128,"mtime":1708892428478,"hashOfConfig":"14"},{"size":3868,"mtime":1709391740708,"hashOfConfig":"14"},{"size":851,"mtime":1708907078157,"hashOfConfig":"14"},{"size":390,"mtime":1708907040933,"hashOfConfig":"14"},{"size":395,"mtime":1708892428472,"hashOfConfig":"14"},{"size":1152,"mtime":1710584696300,"hashOfConfig":"14"},{"size":895,"mtime":1710703881682,"hashOfConfig":"15"},{"size":1159,"mtime":1709970950492,"hashOfConfig":"14"},{"size":1167,"mtime":1710702879251,"hashOfConfig":"14"},{"size":277,"mtime":1710703851132,"hashOfConfig":"15"},"1v6j01t","4ibpip"] \ No newline at end of file diff --git a/src/helpers/cloneDeep.ts b/src/helpers/cloneDeep.ts deleted file mode 100644 index b66e8dc31..000000000 --- a/src/helpers/cloneDeep.ts +++ /dev/null @@ -1,65 +0,0 @@ -function cloneDeep(obj: T) { - return (function _cloneDeep( - item: T - ): T | Date | Set | Map | object | T[] { - // Handle: - // * null - // * undefined - // * boolean - // * number - // * string - // * symbol - // * function - if (item === null || typeof item !== "object") { - return item; - } - - if (item instanceof Date) { - return new Date(item.valueOf()); - } - - if (item instanceof Array) { - let copy: unknown[] = []; - - item.forEach((_, i) => (copy[i] = _cloneDeep(item[i]))); - - return copy; - } - - if (item instanceof Set) { - let copy = new Set(); - - item.forEach((v) => copy.add(_cloneDeep(v))); - - return copy; - } - - if (item instanceof Map) { - let copy = new Map(); - - item.forEach((v, k) => copy.set(k, _cloneDeep(v))); - - return copy; - } - - if (item instanceof Object) { - let copy: Record = {}; - - Object.getOwnPropertySymbols(item).forEach( - (s) => (copy[s] = _cloneDeep(item[s as keyof object])) - ); - - // Handle: - // * Object.name (other) - Object.keys(item).forEach( - (k) => (copy[k] = _cloneDeep(item[k as keyof object])) - ); - - return copy; - } - - throw new Error(`Unable to copy object: ${item}`); - })(obj); -} - -export default cloneDeep; diff --git a/src/helpers/isEqual.ts b/src/helpers/isEqual.ts deleted file mode 100644 index cbacff675..000000000 --- a/src/helpers/isEqual.ts +++ /dev/null @@ -1,44 +0,0 @@ -import Indexed from "./types/indexed"; - -type PlainObject = Indexed; - -function isPlainObject(value: unknown): value is PlainObject { - return ( - typeof value === "object" && - value !== null && - value.constructor === Object && - Object.prototype.toString.call(value) === "[object Object]" - ); -} - -function isArray(value: unknown): value is [] { - return Array.isArray(value); -} - -function isArrayOrObject(value: unknown) { - return isPlainObject(value) || isArray(value); -} - -function isEqual(lhs: PlainObject, rhs: PlainObject) { - if (Object.keys(lhs).length !== Object.keys(rhs).length) { - return false; - } - - for (const [key, value] of Object.entries(lhs)) { - const rightValue = rhs[key]; - if (isArrayOrObject(value) && isArrayOrObject(rightValue)) { - if (isEqual(value as PlainObject, rightValue as PlainObject)) { - continue; - } - return false; - } - - if (value !== rightValue) { - return false; - } - } - - return true; -} - -export default isEqual; diff --git a/src/helpers/merge.ts b/src/helpers/merge.ts deleted file mode 100644 index 4c17ebc3d..000000000 --- a/src/helpers/merge.ts +++ /dev/null @@ -1,21 +0,0 @@ -import Indexed from "./types/indexed"; - -export default function merge(lhs: Indexed, rhs: Indexed): Indexed { - for (let p in rhs) { - if (!rhs.hasOwnProperty(p)) { - continue; - } - - try { - if (rhs[p]?.constructor === Object) { - rhs[p] = merge(lhs[p] as Indexed, rhs[p] as Indexed); - } else { - lhs[p] = rhs[p]; - } - } catch (e) { - lhs[p] = rhs[p]; - } - } - - return lhs; -} diff --git a/src/helpers/set.ts b/src/helpers/set.ts deleted file mode 100644 index 90fe65816..000000000 --- a/src/helpers/set.ts +++ /dev/null @@ -1,26 +0,0 @@ -import Indexed from "./types/indexed"; -import merge from "./merge"; - -function set( - object: Indexed | unknown, - path: string, - value: unknown -): Indexed | unknown { - if (typeof object !== "object" || object === null) { - return object; - } - - if (typeof path !== "string") { - throw new Error("path must be string"); - } - - const result = path.split(".").reduceRight( - (acc, key) => ({ - [key]: acc, - }), - value as Indexed - ); - return merge(object as Indexed, result); -} - -export default set; diff --git a/src/helpers/trim.ts b/src/helpers/trim.ts deleted file mode 100644 index 558f4a6a2..000000000 --- a/src/helpers/trim.ts +++ /dev/null @@ -1,16 +0,0 @@ -export default function trim(string: string, chars?: string) { - let str = " " + string + " "; - - if (str && chars === undefined) { - return string.trim(); - } - - if (!str || !chars) { - return string || ""; - } - - const regFirst = new RegExp(` ${chars}`, "gi"); - const regSecond = new RegExp(`${chars} `, "gi"); - - return str.replace(regFirst, "").replace(regSecond, "").trim(); -} diff --git a/src/helpers/types/indexed.ts b/src/helpers/types/indexed.ts deleted file mode 100644 index 9f90bd176..000000000 --- a/src/helpers/types/indexed.ts +++ /dev/null @@ -1,2 +0,0 @@ -type Indexed = Record; -export default Indexed;