mirror of
https://github.com/He4eT/simple-spaceapi.git
synced 2026-05-04 17:37:24 +00:00
SpaceAPI: rewrite isEmpty function
This commit is contained in:
parent
42c322449d
commit
6b85e563a2
1 changed files with 35 additions and 10 deletions
|
|
@ -5,12 +5,13 @@ const SPACE_API_VERSION = '15';
|
|||
|
||||
/* Utils */
|
||||
|
||||
const isEmpty = <A>(x: A): boolean =>
|
||||
x == null
|
||||
? true
|
||||
: Object.keys(x).length === 0
|
||||
? String(x).length === 0
|
||||
: false;
|
||||
const isEmpty = <A>(x: A): boolean => {
|
||||
if (x == null) return true;
|
||||
if (Array.isArray(x)) return x.length === 0;
|
||||
if (typeof x === 'string') return x.trim().length === 0;
|
||||
if (typeof x === 'object') return Object.keys(x).length === 0;
|
||||
return false;
|
||||
};
|
||||
|
||||
const pickFields =
|
||||
(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)) {
|
||||
result.contact = pickFields([
|
||||
const contactDraft = pickFields([
|
||||
'phone',
|
||||
'sip',
|
||||
'irc',
|
||||
|
|
@ -154,6 +175,10 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
|
|||
'mumble',
|
||||
])(hackspace.contact);
|
||||
|
||||
if (!isEmpty(contactDraft)) {
|
||||
result.contact = contactDraft;
|
||||
}
|
||||
|
||||
if (!isEmpty(hackspace.contact.keymasters)) {
|
||||
result.contact.keymasters = hackspace.contact.keymasters.map(
|
||||
pickFields([
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue