Files
utils/src/functions/isDeepWeakEqual.ts

49 lines
1.4 KiB
TypeScript

/**
* Copyright 2024 Resultium LLC
*
* This file is part of @resultium/utils.
*
* @resultium/utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* @resultium/utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RCZ. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Weakly compares two objects
*
* @remarks uses JSON.stringify()
*
* @example
* ```ts
* // prints false
* console.log(isDeepWeakEqual({ a: 1 }, ["lorem"]))
*
* // prints true
* console.log(isDeepWeakEqual({ a: 1 }, { a: 1 }))
*
* // prints true
* // snippet displaying the weak equality of this function
* console.log(
* isDeepWeakEqual(
* { date: "2024-03-12T18:19:50.548Z" },
* { date: new Date(1710267590548) },
* ),
* );
* ```
*
* @param obj1 - first object for comparison
* @param obj2 - second object for comparison
* @returns true if equal
*/
export const isDeepWeakEqual = (obj1: object, obj2: object) =>
JSON.stringify(obj1) === JSON.stringify(obj2);