Skip to content

Instantly share code, notes, and snippets.

@terrysahaidak
Created February 13, 2020 10:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terrysahaidak/fdde7f22250033bec34a51a9a0b7b798 to your computer and use it in GitHub Desktop.
Save terrysahaidak/fdde7f22250033bec34a51a9a0b7b798 to your computer and use it in GitHub Desktop.
import T from 'prop-types';
/* eslint-disable no-extra-boolean-cast */
const existsCheck = (value) =>
typeof value !== 'undefined' && value !== null;
export default function Ternary({
children,
exp,
exists,
any,
some,
}) {
let value = exp;
if (Array.isArray(exists)) {
value = exists.every(existsCheck);
} else if (existsCheck(exists)) {
value = existsCheck(exists);
} else if (Array.isArray(any)) {
value = any.some((item) => {
if (typeof item === 'string') {
return item.length > 0;
}
return existsCheck(item);
});
} else if (Array.isArray(some)) {
value = some.some((i) => Boolean(i));
}
if (Array.isArray(children)) {
return children[!!value ? 0 : 1];
}
return !!value ? children : null;
}
Ternary.propTypes = {
children: T.any,
exp: T.bool,
exists: T.oneOfType([T.arrayOf(T.any), T.any]),
any: T.arrayOf(T.any),
some: T.arrayOf(T.any),
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment