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

typesafe-array

v1.1.0

Published

Functions to check types of arrays

Downloads

7

Readme

TypeSafe-Array

version: v1.1.0 Changelog

About

JavaScript functions to check types of arrays.

Usage

First of all, import/require the package.

import * as typesafeArray from "typesafe-array";
const typesafeArray = require('typesafe-array');

Then select the type to check for.

typesafeArray.number;
typesafeArray.string;
typesafeArray.object;

The following types can be checked for:

  • custom
  • any
  • unknown - use this instead of any when using TypeScript
  • bigint
  • boolean
  • number
  • string
  • object
  • instance

Then select how many dimensions the array should have.
Currently, dimensions 0 - 3 are supported.

typesafeArray.number[1];
typesafeArray.number[2];

Then pass your object.

typesafeArray.number[1]([1, 2, 3]); // true
typesafeArray.string[1]([1, 2, 3]); // false

By default, empty arrays will always pass the test, the next optional parameter can turn this off.

typesafeArray.number[1]([] /* , true */ ); // true
typesafeArray.number[1]([], false       ); // false

The next two optional parameters are there to allow null and undefined values, both are not allowed by default.

typesafeArray.number[1]([1, null,      3], undefined /* , false */            ); // false
typesafeArray.number[1]([1, undefined, 3], undefined /* , undefined, false */ ); // false
typesafeArray.number[1]([1, null,      3], undefined, true                    ); // true
typesafeArray.number[1]([1, undefined, 3], undefined, undefined, true         ); // true

Custom and Instance Types

The types custom and instance work a little bit different.

The custom type needs an extra parameter after the value that must be a function that returns a boolean value.
This function will be passed the object to check.

// leave out all the type stuff when you're using JavaScript

interface X {
	foobar: string;
}

const checker = (obj: unknown): obj is X => {
	return typeof(obj) === "object" &&
	       typeof((obj as X).foobar) === "string";
};

const obj1 = {
	foobar: "yeehaw"
};
const obj2 = {
	foobar: 42069
};

typesafeArray.custom[1]([obj1], checker); // true
typesafeArray.custom[1]([obj2], checker); // false

The passed object will never be null or undefined.

The instance type also needs an extra parameter, but instead of a function that manually checks the type of the object, it must be a constructor function.
Things like RegExp, Date, Map.

typesafeArray.instance[1]([/^(.*)$/,   /^.$/       ], RegExp); // true
typesafeArray.instance[1]([new Date(), "2001-05-17"], Date);   // false

The optional parameters to allow/disallow empty arrays, null and undefined values are also available for both custom and undefined.

Examples

import * as typesafeArray from "typesafe-array";

typesafeArray.number[1]([1, 2, 3]);   // true
typesafeArray.number[1]([1, "2", 3]); // false

typesafeArray.number[2]([[1, 2], [3, 4], [5, 6]]); // true
typesafeArray.number[2]([[1, 2], 3, [4, 5], 6]);   // false

typesafeArray.number[1]([]);        // true
typesafeArray.number[1]([], false); // false

Installation

Using npm:

npm i typesafe-array

Using Yarn:

yarn add typesafe-array

Contributing

Read through the Contribution Guidelines if you want to contribute to this project.

License

TypeSafe-Array is licensed under both the Mozilla Public License 2.0 AND the Apache License 2.0.
For more information about copying and licensing, see the COPYING.txt file.