npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-bool

v4.1.0

Published

A simple set of functions that return a boolean.

Downloads

447

Readme

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();
// or
const { isDefined, isNumber, ... } = SimpleBool;

Usage

import * from 'simple-bool';
// or
import { /* function names here */ } 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); // -> true
isClassDeclaration(RegExp);  // -> false

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.