MOBILE-4368 core: Implement CoreObject.consumeKey utility
parent
f9eb1f8462
commit
0598ec0062
|
@ -30,6 +30,20 @@ export type CoreObjectWithoutUndefined<T> = Pretty<{
|
||||||
*/
|
*/
|
||||||
export class CoreObject {
|
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.
|
* Check if two objects have the same shape and the same leaf values.
|
||||||
*
|
*
|
||||||
|
|
|
@ -16,6 +16,22 @@ import { CoreObject } from '@singletons/object';
|
||||||
|
|
||||||
describe('CoreObject singleton', () => {
|
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', () => {
|
it('compares two values, checking all subproperties if needed', () => {
|
||||||
expect(CoreObject.deepEquals(1, 1)).toBe(true);
|
expect(CoreObject.deepEquals(1, 1)).toBe(true);
|
||||||
expect(CoreObject.deepEquals(1, 2)).toBe(false);
|
expect(CoreObject.deepEquals(1, 2)).toBe(false);
|
||||||
|
|
Loading…
Reference in New Issue