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 */
|
/* 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 }) => ({
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/* */
|
||||||
|
|
||||||
|
if (isEmpty(hackspace)) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
/* */
|
||||||
|
|
||||||
|
if (!isEmpty(hackspace.space)) {
|
||||||
result.space = hackspace.space;
|
result.space = hackspace.space;
|
||||||
result.logo = absoluteURL(origin)(hackspace?.logo.url);
|
}
|
||||||
|
|
||||||
|
/* */
|
||||||
|
|
||||||
|
if (!isEmpty(hackspace.logo)) {
|
||||||
|
result.logo = absoluteURL(origin)(hackspace.logo?.url);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* */
|
||||||
|
|
||||||
|
if (!isEmpty(hackspace.url)) {
|
||||||
result.url = 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([
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue