MOBILE-4368 core: Implement CoreObject.consumeKey utility

main
Dani Palou 2023-06-22 11:30:15 +02:00 committed by Alfonso Salces
parent f9eb1f8462
commit 0598ec0062
2 changed files with 30 additions and 0 deletions

View File

@ -30,6 +30,20 @@ export type CoreObjectWithoutUndefined<T> = Pretty<{
*/
export class CoreObject {
/**
* Returns a value of an object and deletes it from the object.
*
* @param obj Object.
* @param key Key of the value to consume.
* @returns Whether objects are equal.
*/
static consumeKey<T, K extends keyof T>(obj: T, key: K): T[K] {
const value = obj[key];
delete obj[key];
return value;
}
/**
* Check if two objects have the same shape and the same leaf values.
*

View File

@ -16,6 +16,22 @@ import { CoreObject } from '@singletons/object';
describe('CoreObject singleton', () => {
it('consumes object keys', () => {
const object = {
foo: 'a',
bar: 'b',
baz: 'c',
};
const fooValue = CoreObject.consumeKey(object, 'foo');
const bazValue = CoreObject.consumeKey(object, 'baz');
expect(fooValue).toEqual('a');
expect(bazValue).toEqual('c');
expect(object.bar).toEqual('b');
expect(Object.keys(object)).toEqual(['bar']);
});
it('compares two values, checking all subproperties if needed', () => {
expect(CoreObject.deepEquals(1, 1)).toBe(true);
expect(CoreObject.deepEquals(1, 2)).toBe(false);