simple-bool
A simple set of functions that return a boolean.
Highlights
- Supports TypeScript!
- Supports Node and browser
- Includes full JSDoc documentation
- Very lightweight!
Installation
NodeJS
npm install simple-bool --save
Browser
Import the script:
<script src="https://joker876.github.io/simple-bool/simple-bool.min.js">
And import the functions from a global object:
SimpleBool.isDefined();
const { isDefined, isNumber, ... } = SimpleBool;
Usage
import * from 'simple-bool';
import { } from 'simple-bool';
Available functions
isDefined
isDefined(value: any): boolean
Returns false
if the value is undefined
or null
. Otherwise returns true
.
isNull
isNull(value: any): boolean
Returns true
if the value is null
. Otherwise returns false
.
isBoolean
isBoolean(value: any): boolean
Returns true
if the value is true
or false
. Otherwise returns false
.
isAnyString
isAnyString(value: any): boolean
Returns true
if the value is of type string
. Otherwise returns false
.
isString
isString(value: any): boolean
Returns true
if the value is of type string
, and is not an empty string. Otherwise returns false
.
isNumber
isNumber(value: any): boolean
Returns true
if the value is of type number
, and is not a NaN
. Otherwise returns false
.
isInt
isInt(value: any): boolean
Returns true
if the value is a number, and it doesn't have any decimal places. Otherwise returns false
.
isFloat
isFloat(value: any): boolean
Returns true
if the value is a number, and it does have some decimal places. Otherwise returns false
.
isObject
isObject(value: any): boolean
Returns true
if the value is of type object
, and is defined. Otherwise returns false
.
isArray
isArray(value: any): boolean
Returns true
if the value is an array. Otherwise returns false
.
isEmpty
isEmpty(value: object | string): boolean
Returns true
if:
- the value is a string, and its length is greater than 0,
- the value is an array, and it has at least 1 item,
- the value is an object, and it has at least 1 key.
Otherwise returns false
.
isClassDeclaration
isClassDeclaration(value: any): boolean
Returns true
if the value is a class declaration. Otherwise returns false
. All native classes will return false
.
Example:
class Example {
constructor() {}
}
isClassDeclaration(Example);
isClassDeclaration(RegExp);
isInstanceOf
isInstanceOf(value: any, cls: Function): boolean
Returns true
if the value is an instance of the class cls. Otherwise returns false
.
isPromise
isPromise(value: any): boolean
Returns true
if the value is a Promise. Otherwise returns false
.
isFunction
isFunction(value: any): boolean
Returns true
if the value is an instance of Function. Otherwise returns false
.
All standard functions, arrow functions, classes, constructors, etc. count towards being a Function.
isRegExp
isRegExp(value: any): boolean
Returns true
if the value is a regular expression. Otherwise returns false
.
isDate
isDate(value: any): boolean
Returns true
if the value is is an instance of Date, or can be parsed into a valid Date. Otherwise returns false
.
All numbers return true
when passed into isDate
.
hasProp
hasProp(value: any, property: PropertyKey): boolean
Returns true
if the value is an object which has a certain property property. Otherwise returns false
.
evaluate
evaluate(value: any): boolean
Returns true
if:
- the value is equal to
true
, - the value is a number, and is not 0,
- the value is an array, and it has at least 1 item,
- the value is an object, and it has at least 1 key,
- the value is any of those strings: (case insensitive)
'yes', 'y', '1', 't', 'true', 'on', 'sure'
Otherwise returns Boolean(value)
.
all
all<T>(array: T[], fn: (value: T) => boolean = Boolean): boolean
Firstly, it flattens the given array.
For each item in array, it calls fn and passes the item.
It counts the times fn returns either true
or false
.
At the end, it returns true
only if fn returned true
for all items. Otherwise returns false
.
most
most<T>(array: T[], fn: (value: T) => boolean = Boolean): boolean
Firstly, it flattens the given array.
For each item in array, it calls fn and passes the item.
It counts the times fn returns either true
or false
.
At the end, it returns true
only if fn returned true
for at least 50% of all items. Otherwise returns false
.
any
any<T>(array: T[], fn: (value: T) => boolean = Boolean): boolean
Firstly, it flattens the given array.
For each item in array, it calls fn and passes the item.
It counts the times fn returns either true
or false
.
At the end, it returns true
if fn returned true
for at least 1 item. Otherwise returns false
.
none
none<T>(array: T[], fn: (value: T) => boolean = Boolean): boolean
Firstly, it flattens the given array.
For each item in array, it calls fn and passes the item.
It counts the times fn returns either true
or false
.
At the end, it returns true
only if fn returned false
for all items. Otherwise returns false
.
some
some<T>(array: T[], threshold: number, fn: (value: T) => boolean = Boolean): boolean
Firstly, it flattens the given array.
For each item in array, it calls fn and passes the item.
It counts the times fn returns either true
or false
.
At the end, it compares these amounts to the threshold:
- if the threshold is less than or equal to 0, it always returns
true
, - if the threshold is between 0 and 1 (non-inclusive), it is treated as a percetage, and it returns
true
only if fn returned true
at least that many percent of all executions, - if the threshold is greater than or equal to 1, it returns
true
only if fn returned true
at least that many times.
In all other cases, it returns false
.