SpaceAPI: rewrite isEmpty function

This commit is contained in:
He4eT 2025-05-29 08:43:00 +02:00
commit 6b85e563a2

View file

@ -5,12 +5,13 @@ const SPACE_API_VERSION = '15';
/* Utils */ /* Utils */
const isEmpty = <A>(x: A): boolean => const isEmpty = <A>(x: A): boolean => {
x == null if (x == null) return true;
? true if (Array.isArray(x)) return x.length === 0;
: Object.keys(x).length === 0 if (typeof x === 'string') return x.trim().length === 0;
? String(x).length === 0 if (typeof x === 'object') return Object.keys(x).length === 0;
: false; return false;
};
const pickFields = const pickFields =
(fields: Array<string>) => (fields: Array<string>) =>
@ -63,9 +64,29 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
], ],
}); });
result.space = hackspace.space; /* */
result.logo = absoluteURL(origin)(hackspace?.logo.url);
result.url = hackspace.url; if (isEmpty(hackspace)) {
return {};
}
/* */
if (!isEmpty(hackspace.space)) {
result.space = hackspace.space;
}
/* */
if (!isEmpty(hackspace.logo)) {
result.logo = absoluteURL(origin)(hackspace.logo?.url);
}
/* */
if (!isEmpty(hackspace.url)) {
result.url = hackspace.url;
}
/* */ /* */
@ -136,7 +157,7 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
/* */ /* */
if (!isEmpty(hackspace.contact)) { if (!isEmpty(hackspace.contact)) {
result.contact = pickFields([ const contactDraft = pickFields([
'phone', 'phone',
'sip', 'sip',
'irc', 'irc',
@ -154,6 +175,10 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
'mumble', 'mumble',
])(hackspace.contact); ])(hackspace.contact);
if (!isEmpty(contactDraft)) {
result.contact = contactDraft;
}
if (!isEmpty(hackspace.contact.keymasters)) { if (!isEmpty(hackspace.contact.keymasters)) {
result.contact.keymasters = hackspace.contact.keymasters.map( result.contact.keymasters = hackspace.contact.keymasters.map(
pickFields([ pickFields([