Created
February 13, 2020 10:04
-
-
Save terrysahaidak/fdde7f22250033bec34a51a9a0b7b798 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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