-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move objectUtils to @studio/pure-functions (#13938)
- Loading branch information
Showing
9 changed files
with
133 additions
and
118 deletions.
There are no files selected for viewing
88 changes: 74 additions & 14 deletions
88
frontend/libs/studio-pure-functions/src/ObjectUtils/ObjectUtils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,84 @@ | ||
import { ObjectUtils } from './ObjectUtils'; | ||
|
||
describe('deepCopy', () => { | ||
it('should create a deep copy of an object', () => { | ||
const originalObject = { | ||
test: 'Test', | ||
test2: 25, | ||
}; | ||
describe('objectUtils', () => { | ||
describe('deepCopy', () => { | ||
it('should create a deep copy of an object', () => { | ||
const originalObject = { | ||
test: 'Test', | ||
test2: 25, | ||
}; | ||
|
||
const copiedObject = ObjectUtils.deepCopy(originalObject); | ||
const copiedObject = ObjectUtils.deepCopy(originalObject); | ||
|
||
expect(copiedObject).toEqual(originalObject); | ||
expect(copiedObject).not.toBe(originalObject); | ||
expect(copiedObject).toEqual(originalObject); | ||
expect(copiedObject).not.toBe(originalObject); | ||
}); | ||
|
||
it('should create a deep copy of an array', () => { | ||
const originalArray = [1, 2, [3, 4]]; | ||
|
||
const copiedArray = ObjectUtils.deepCopy(originalArray); | ||
|
||
expect(copiedArray).toEqual(originalArray); | ||
expect(copiedArray).not.toBe(originalArray); | ||
}); | ||
}); | ||
|
||
describe('areObjectsEqual', () => { | ||
it('Returns true if objects are equal', () => { | ||
expect(ObjectUtils.areObjectsEqual({}, {})).toBe(true); | ||
expect(ObjectUtils.areObjectsEqual({ a: 1 }, { a: 1 })).toBe(true); | ||
expect(ObjectUtils.areObjectsEqual({ a: 1, b: 2 }, { a: 1, b: 2 })).toBe(true); | ||
expect(ObjectUtils.areObjectsEqual({ a: 1, b: 2, c: 3 }, { a: 1, b: 2, c: 3 })).toBe(true); | ||
}); | ||
|
||
it('Returns false if objects are not equal', () => { | ||
expect(ObjectUtils.areObjectsEqual({ a: 1 }, { a: 2 })).toBe(false); | ||
expect(ObjectUtils.areObjectsEqual({ a: 1, b: 2 }, { a: 1, b: 3 })).toBe(false); | ||
expect(ObjectUtils.areObjectsEqual({ a: 1, b: 2, c: 3 }, { a: 1, b: 2, c: 4 })).toBe(false); | ||
}); | ||
|
||
it('should return true for two empty objects', () => { | ||
expect(ObjectUtils.areObjectsEqual({}, {})).toBe(true); | ||
}); | ||
|
||
it('should return true for identical objects (reference equality)', () => { | ||
const obj1 = { a: 1, b: 'test' }; | ||
expect(ObjectUtils.areObjectsEqual(obj1, obj1)).toBe(true); | ||
}); | ||
|
||
it('should return false if the length of the objects are not equally length', () => { | ||
expect(ObjectUtils.areObjectsEqual({ a: 1, b: 2 }, { a: 1 })).toBe(false); | ||
}); | ||
}); | ||
|
||
it('should create a deep copy of an array', () => { | ||
const originalArray = [1, 2, [3, 4]]; | ||
describe('mapByProperty', () => { | ||
const property = 'id'; | ||
const value1 = 'value1'; | ||
const value2 = 'value2'; | ||
const value3 = 'value3'; | ||
const object1 = { [property]: value1 }; | ||
const object2 = { [property]: value2, otherProperty: 'Some irrelevant value' }; | ||
const object3 = { [property]: value3, otherProperty: 'Another irrelevant value' }; | ||
|
||
const copiedArray = ObjectUtils.deepCopy(originalArray); | ||
it('Maps an array of objects to a key-value pair object, where the key is the value of the property', () => { | ||
const objectList = [object1, object2, object3]; | ||
expect(ObjectUtils.mapByProperty(objectList, property)).toEqual({ | ||
[value1]: object1, | ||
[value2]: object2, | ||
[value3]: object3, | ||
}); | ||
}); | ||
}); | ||
|
||
expect(copiedArray).toEqual(originalArray); | ||
expect(copiedArray).not.toBe(originalArray); | ||
describe('flattenObjectValues', () => { | ||
it('Flattens the values of an object', () => { | ||
const object = { | ||
a: 'value1', | ||
b: 'value2', | ||
c: 'value3', | ||
}; | ||
expect(ObjectUtils.flattenObjectValues(object)).toEqual(['value1', 'value2', 'value3']); | ||
}); | ||
}); | ||
}); |
44 changes: 44 additions & 0 deletions
44
frontend/libs/studio-pure-functions/src/ObjectUtils/ObjectUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,47 @@ | ||
import { type KeyValuePairs } from '../types/KeyValuePairs'; | ||
|
||
export class ObjectUtils { | ||
static deepCopy = <T>(value: T) => JSON.parse(JSON.stringify(value)) as T; | ||
|
||
/** | ||
* Checks if two objects are equal (shallow comparison). | ||
* @param obj1 The first object. | ||
* @param obj2 The second object. | ||
* @returns True if the objects are equal and false otherwise. | ||
*/ | ||
static areObjectsEqual = <T extends object>(obj1: T, obj2: T): boolean => { | ||
if (Object.keys(obj1).length !== Object.keys(obj2).length) return false; | ||
for (const key in obj1) { | ||
if (obj1[key] !== obj2[key]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
/** | ||
* Maps an array of objects to a key-value pair object, where the key is the value of the given property. | ||
* @param objectList | ||
* @param property | ||
* @returns An object with the values of the given property as keys and the objects as values. | ||
*/ | ||
static mapByProperty = <T extends object>( | ||
objectList: T[], | ||
property: keyof T, | ||
): KeyValuePairs<T> => { | ||
return Object.fromEntries(objectList.map((object) => [object[property], object])); | ||
}; | ||
|
||
/** | ||
* Flattens the values of an object. | ||
* @param object The object to flatten. | ||
* @returns An array of the values of the object. | ||
*/ | ||
static flattenObjectValues = <T extends object>(object: T): string[] => { | ||
return Object.entries(object) | ||
.map(([, value]) => { | ||
return value; | ||
}) | ||
.flat(); | ||
}; | ||
} |
3 changes: 3 additions & 0 deletions
3
frontend/libs/studio-pure-functions/src/types/KeyValuePairs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface KeyValuePairs<T = any> { | ||
[key: string]: T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters