mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 16:42:56 +00:00
49
src/common.ts
Normal file
49
src/common.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2019 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A recursive partial object
|
||||
*
|
||||
* Copied from https://stackoverflow.com/a/51365037
|
||||
*/
|
||||
export type RecursivePartial<T> = {
|
||||
[P in keyof T]?: T[P] extends Array<infer U> ?
|
||||
Array<RecursivePartial<U>> :
|
||||
T[P] extends object ? RecursivePartial<T[P]> : T[P];
|
||||
};
|
||||
|
||||
/**
|
||||
* Deletes all properties that are undefined from an object
|
||||
* @param obj
|
||||
*/
|
||||
export function deleteUndefinedProperties(obj: any) {
|
||||
// return atomic data types and arrays (recursion anchor)
|
||||
if (typeof obj !== 'object' || Array.isArray(obj)) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
// check each key
|
||||
Object.keys(obj).forEach((key) => {
|
||||
if (typeof obj[key] === 'undefined') {
|
||||
// delete undefined keys
|
||||
delete obj[key];
|
||||
} else {
|
||||
// check recursive
|
||||
obj[key] = deleteUndefinedProperties(obj[key]);
|
||||
}
|
||||
});
|
||||
|
||||
return obj;
|
||||
}
|
||||
Reference in New Issue
Block a user