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

pyjamas

v0.0.6

Published

JS Utility Functions

Downloads

2

Readme

Pyjamas


This package brings some Python beauty to JavaScript, which mostly includes typing utils that help you get less exceptions and have more control of your codes functionality. As you probably know, JS is dynamically typed which sometimes leads to unwanted behaviours, this package is trying to solve these problems with a new implementation of falsy values and typing utils. Here is a comparision:

These values will make bypass the if blocks in JavaScript

if (false)
if (undefined)
if (0)
if (0n)
if (null)
if (NaN)
if ('')

Now Pyjamas extends these values and also consideres the following values as falsy

// Empty Arrays
[]

// Empty Objects
{}

// Functions that return falsy values or throw an exception
() => null

Installation

use

yarn add pyjamas

or

npm install pyjamas

Example

Pyjamas ships with the isFalsy and bool function which is just an inverter of isFalsy

# ES6

import { isFalsy, bool } from 'pyjamas'

if(isFalsy(undefined)) {
  // this will execute because undefined is Falsy
}

if(bool(undefined)) {
  // this won't execute because bool will convert undefined to false
}

isFalsy(value)

| Function | Returns | | ----------------------------------------- |:-------:| | isFalsy(undefined) | true | | isFalsy(false) | true | | isFalsy(NaN) | true | | isFalsy('') | true | | isFalsy({}) | true | | isFalsy([]) | true | | isFalsy(0) | true | | isFalsy(0.0) | true | | isFalsy([][0]) | true | | isFalsy([].length) | true | | isFalsy(() => [][0].name) | true | | isFalsy(NaN) | true | | isFalsy('hi') | false | | isFalsy(9) | false | | isFalsy(0.0000000001) | false | | isFalsy({ none: 'none' }) | false | | isFalsy(() => [{ name: 'pete' }][0].name) | false |

Especially usefull is that you can check for a value in an array of objects for instance [][0].name howerver just putting it in isFalsy like: isFalsy([][0].name) would throw an exception, you need to write () => in front of the value.

isEmpty(value)

this function checks if a String | Array | Object is empty

| Function | Returns | | ---------------------- |:-------:| | isEmpty('Hi') | false | | isEmpty({ hi: 'hi }) | false | | isEmpty([0,1,2]) | false |

ANYTHING else returns true

isInt(value)

Check if the value is an Integer

| Function | Returns | | --------------------- |:-------:| | isInt(1) | true | | isInt(0) | true | | isInt(2 / 2) | true | | isInt(1.0) | true | | isInt(2 / 1.5) | false | | isInt(1.000000000001) | false | | isInt('1') | false | | isInt(NaN) | false | | isInt(0.999999999999) | false |

isFloat(value)

Check if the value is a Float

| Function | Returns | | --------------------- |:-------:| | isFloat(1.1) | true | | isFloat(0.000000001) | true | | isFloat(2 / 2.1) | true | | isFloat(1) | false | | isFloat(0) | false | | isFloat('1') | false | | isFloat(NaN) | false | | isFloat(undefined) | false |

isNumber(value)

check if value is either an Interger or a Float

isString(value)

check if value is a String

| Function | Returns | | ------------------- |:-------:| | isString('') | true | | isString('String') | true | | isString(undefined) | false | | isString(null) | false |

Converters

Methods that parse the arguments into defined data structures

bool(value)

bool uses isFalsy to parse the incoming value into a boolean

len(value)

get the length (Integer) of an Array | String | Object

| Function | Returns | | ----------------------- |:-------:| | len('Hi') | 2 | | len([0, 1]) | 2 | | len({ a: 'a', b: 'b' }) | 2 | | len('') | 0 | | len([]) | 0 | | len({}) | 0 | | len(NaN) | -1 | | len(undefined) | -1 | | len(false) | -1 | | len(null) | -1 |

sum(value)

add all values inside an Array

| Function | Returns | | ------------------ |:-------:| | sum([1, 1]) | 2 | | sum([0.5, 0.5]) | 1 | | sum([1, null]) | 1 | | sum(undefined) | 0 | | sum([null, NaN]) | 0 | | sum('Hi') | 0 |

int(value)

Trys to parse anything to an Integer, else returns 0

| Function | Returns | | ------------------ |:-------:| | int(1) | 1 | | int(1.0) | 1 | | int('1') | 1 | | int(1.999999999) | 1 | | int(0.999999999) | 0 | | int('NotaNumber') | 0 | | int(undefined) | 0 | | int(NaN) | 0 |

float(value)

Trys to parse anything to a Float, else returns 0.0

| Function | Returns | | --------------------- |:-------:| | float(1) | 1.0 | | float('1.1') | 1.1 | | float('1') | 1.0 | | float(' 1.2 ') | 1.2 | | float('1.2 m') | 1.2 | | float('NotaNumber') | 0.0 | | int(undefined) | 0.0 | | int(NaN) | 0.0 |

range(start: int, stop: int)

generate an Array with start as the Integer to begin with and Stop as the Index to end with

| Function | Returns | | ----------------- |:---------------:| | range(2) | [0, 1] | | range(10).length | 10 | | range(5, 10) | [5, 6, 7, 8, 9] |

Testing

This package is heavily tested and tries to cover every possible scenario with unit test, in case you should find something we didn't thought about please contact us. We are much obliged for your help.