2020-10-30 11:37:00 +00:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-01-14 11:58:51 +00:00
|
|
|
export type CoreObjectWithoutEmpty<T> = {
|
|
|
|
[k in keyof T]: T[k] extends undefined | null ? never : T[k];
|
|
|
|
};
|
|
|
|
|
2020-10-30 11:37:00 +00:00
|
|
|
/**
|
|
|
|
* Singleton with helper functions for objects.
|
|
|
|
*/
|
|
|
|
export class CoreObject {
|
|
|
|
|
2021-05-05 15:43:40 +00:00
|
|
|
/**
|
|
|
|
* Check if two objects have the same shape and the same leaf values.
|
|
|
|
*
|
|
|
|
* @param a First object.
|
|
|
|
* @param b Second object.
|
|
|
|
* @return Whether objects are equal.
|
|
|
|
*/
|
|
|
|
static deepEquals(a: unknown, b: unknown): boolean {
|
|
|
|
return JSON.stringify(a) === JSON.stringify(b);
|
|
|
|
}
|
|
|
|
|
2020-10-30 11:37:00 +00:00
|
|
|
/**
|
2021-01-14 11:58:51 +00:00
|
|
|
* Check whether the given object is empty.
|
|
|
|
*
|
|
|
|
* @param object Object.
|
|
|
|
* @return Whether the given object is empty.
|
|
|
|
*/
|
|
|
|
static isEmpty(object: Record<string, unknown>): boolean {
|
|
|
|
return Object.keys(object).length === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new object without the specified keys.
|
|
|
|
*
|
|
|
|
* @param obj Object.
|
|
|
|
* @param keys Keys to remove from the new object.
|
|
|
|
* @return New object without the specified keys.
|
|
|
|
*/
|
2021-03-02 09:51:23 +00:00
|
|
|
static without<T, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> {
|
2021-01-14 11:58:51 +00:00
|
|
|
const newObject: T = { ...obj };
|
|
|
|
|
|
|
|
for (const key of keys) {
|
|
|
|
delete newObject[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return newObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new object without empty values (null or undefined).
|
2020-10-30 11:37:00 +00:00
|
|
|
*
|
2021-01-14 11:58:51 +00:00
|
|
|
* @param obj Objet.
|
|
|
|
* @return New object without empty values.
|
2020-10-30 11:37:00 +00:00
|
|
|
*/
|
2021-01-14 11:58:51 +00:00
|
|
|
static withoutEmpty<T>(obj: T): CoreObjectWithoutEmpty<T> {
|
|
|
|
const cleanObj = {};
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(obj)) {
|
|
|
|
if (value === null || value === undefined) {
|
|
|
|
continue;
|
2020-10-30 11:37:00 +00:00
|
|
|
}
|
2021-01-14 11:58:51 +00:00
|
|
|
|
|
|
|
cleanObj[key] = value;
|
2020-10-30 11:37:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 11:58:51 +00:00
|
|
|
return cleanObj as CoreObjectWithoutEmpty<T>;
|
2020-10-30 11:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|