![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
check-is-truthy
Advanced tools
Utilities and types to help with simple Boolean checks
This is intended to be used with TypeScript, but it should work with JavaScript as well.
This package is side-effect free and can be fully tree-shaken.
npm install check-is-truthy
or
yarn add check-is-truthy
By and large, the primary useful thing about this package is the isTruthy
function which can be used within standard JavaScript functions like
Array.prototype.filter
or within other utilities which take a predicate function with a type guard,
such as Lodash's _.find.
Though the same behavior can be achieved at runtime by calling the Boolean
constructor as a function, TypeScript makes no special handling of this with
regards to type guards as mentioned above.
isTruthy
isTruthy
is a function that takes a value and returns a boolean indicating
whether it is
"truthy" within
JavaScript.
It is particularly useful for filtering collections, and can be used in most cases
where predicates are used, such as
Array.prototype.filter
.
import { isTruthy } from "check-is-truthy";
doSomethingComplex(["foo", "bar", doSomething && expensive()].filter(isTruthy));
isFalsy
isFalsy
is a function that takes a value and returns a boolean indicating
whether it is "falsy"
within JavaScript.
toggle
toggle
is a function that takes a value and returns its logical opposite. This has the effect of cycling between true
and false
.
function MyComponent() {
const [isOpen, setIsOpen] = useState(false);
const toggleIsOpen = useCallback(() => setIsOpen(toggle), []);
return (
<Modal isOpen={isOpen}>
<button onClick={toggleIsOpen} />
</Modal>
);
}
FalsyValue
FalsyValue
is a type that represents all values that are falsy in JavaScript.
One can use TypeScript's built-in
Extract
or
Exclude
types to extract values from a type that are falsy (or truthy).
import type { FalsyValue } from "check-is-truthy";
function doSomething<T>(
value: T,
whenTruthy: (value: Exclude<T, FalsyValue>) => void,
whenFalsy: (value: Extract<T, FalsyValue>) => void
) {
if (value) {
// in this branch, we know `value` is truthy
whenTruthy(value);
} else {
// in this branch, we know `value` is falsy
whenFalsy(value);
}
}
NaN
The reserved value NaN
. This is equivalent to the global NaN
value.
This has a special type of NaN
, which is an opaque value that can represent
NaN
.
It is not fully safe to use, as NaN
values can be easily created through other
means that are not typed as NaN
.
isNaN
Equivalent to
Number.isNaN
.
This is provided as a convenience as a check for the NaN
value as the NaN
type.
FAQs
Utilities and types to help with simple Boolean checks
We found that check-is-truthy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.