# istruthy-js

> A simple function to check if a piece of data is truthy. JavaScript version.

Latest version **1.3.1** (published 2023-01-22) · ISC license · 0 weekly downloads

## Install

```sh
npm install istruthy-js
pnpm add istruthy-js
yarn add istruthy-js
bun add istruthy-js
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.1 |
| Published | 2023-01-22 |
| First published | 2023-01-21 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | AroenvR |
| Maintainers | aroenvr |
| Keywords | true, false, truthy, falsy, javascript, validation, checking, data validation, object validation, array validation, set validation, map validation |

## Links

- npm: https://www.npmjs.com/package/istruthy-js
- npm.io page: https://npm.io/package/istruthy-js

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.3.1 (latest) — 2023-01-22
- 1.3.0 — 2023-01-21

## README

# isTruthy

### TL;DR:
```  
npm i istruthy-js  
```  
This function checks if the given data is truthy.  
By default: if an object, array, set, or map contains ANY truthy values, it is truthy.  
By default: the value 0 is considered as truthy.

Package at: https://www.npmjs.com/package/istruthy-js  
GitHub repo at: https://github.com/AroenvR/istruthy  
(main branch contains as few files as possible, clone the working_tests branch if you want to experiment with it)


## Supports the following data types:
 - boolean
 - number
 - string
 - objects
 - arrays
 - nested arrays
 - sets
 - nested sets
 - maps
 - nested maps
 - null
 - undefined
 - NaN

<br>

### Disclaimer
This function is primarily designed for use in frontend applications, where it can be utilized to ensure that rendered elements are truthy and prevent potential crashes in the user interface.  
While it may have additional potential use cases, this is the main purpose of the function.

<br>
<hr>
<br>

## Parameters
- `data` (required): The data, object or array to check.
- `zero` (optional): If this parameter === false, then 0 is also considered falsy.
- `obj` (optional): If this parameter === true, then the function will only allow objects where all values are truthy.

### Returns
- `true` if the data is truthy.
- `false` if the data is falsy.

### Usage
import { isTruthy } from 'isTruthy';

console.log(isTruthy(true)); // true  
console.log(isTruthy(false)); // false   

console.log(isTruthy(0)); // true  
console.log(isTruthy(0, false)); // false

Check the example test script (using Jest) for detailed examples.

## Notes

- The function uses `Object.getOwnPropertyNames(data)` and `Object.getPrototypeOf(data)` to check if the given data is an object and if it is empty or not.
- The function uses `Array.isArray(data)` to check if the given data is an array.
- The function uses `typeof(data)` to check the type of data.
- The function uses `for...of` loop to iterate over sets and maps.
- If `zero` parameter is set to false, the function considers 0 as falsy.
- If `obj` parameter is set to true, the function will only allow objects where all values are truthy.

<hr>

## Currently known constraints
Things the isTruthy function cannot (currently) handle:  
Classes  
Funnctions  
Generators  
Enums  
.. Maybe others I missed?

---
_Source: https://npm.io/package/istruthy-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
