Skip to content

Commit

Permalink
Add a compare object
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispuska committed Mar 31, 2024
1 parent ebe490e commit be49f0d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
7 changes: 4 additions & 3 deletions packages/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export { getClasses } from "./classnames"
export { compareObjects } from "./objects"
export { copyValue } from "./clipboard"
export { hexToRgb, rgbToHex } from "./colors"
export { debounce } from "./debounce"
export { getClasses } from "./classnames"
export { getSlug } from "./slug"
export { hexToRgb, rgbToHex } from "./colors"
export { isUUID, getUUID } from "./uuid"
export { sanitizeText, removeMarkdown, middleEllipsis, removeNewlines } from "./strings"
export { throttle } from "./throttle"
export { isUUID, getUUID } from "./uuid"
export { validateUrl, validateEmail } from "./validations"
29 changes: 29 additions & 0 deletions packages/utils/objects.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, it, expect } from "vitest"
import { compareObjects } from './objects'

describe('compareObjects', () => {
it('should return true for identical objects', () => {
const obj1 = { a: 1, b: 2 }
const obj2 = { a: 1, b: 2 }
expect(compareObjects(obj1, obj2)).toBeTruthy()
})

it('should return false for different objects', () => {
const obj1 = { a: 1, b: 2 }
const obj2 = { a: 2, b: 3 }
expect(compareObjects(obj1, obj2)).toBeFalsy()
})

it('should handle null and undefined', () => {
const obj1 = { a: 1, b: 2 }
expect(compareObjects(obj1, null as unknown as any)).toBeFalsy()
expect(compareObjects(null as unknown as any, obj1)).toBeFalsy()
expect(compareObjects(undefined as unknown as any, undefined as unknown as any)).toBeTruthy()
})

it('should not depend on property order', () => {
const obj1 = { a: 1, b: 2 }
const obj2 = { b: 2, a: 1 }
expect(compareObjects(obj1, obj2)).toBeTruthy()
})
})
18 changes: 18 additions & 0 deletions packages/utils/objects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const _orderObjectProperties = (obj: object): object => {
if (!obj || typeof obj !== 'object' || Array.isArray(obj)) return obj

return Object.keys(obj).sort().reduce((acc, key) => {
acc[key] = _orderObjectProperties(obj[key])

return acc
}, {})
}

export const compareObjects = (first: unknown, second: unknown): boolean => {
if (!first || !second) return first === second

const orderedFirst = _orderObjectProperties(first)
const orderedSecond = _orderObjectProperties(second)

return JSON.stringify(orderedFirst) === JSON.stringify(orderedSecond)
}
4 changes: 2 additions & 2 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@heliosgraphics/utils",
"version": "5.3.5",
"version": "5.3.6",
"type": "module",
"author": "03b8 <[email protected]>",
"license": "MIT",
"private": false,
"description": "Universal javascript helpers",
"description": "Universal Javascript helpers",
"main": "index.ts",
"engines": {
"npm": ">=10.2.4",
Expand Down

0 comments on commit be49f0d

Please sign in to comment.