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

typeof-items

v1.0.1

Published

Validate the type of array's items.

Downloads

4

Readme

Description

typeof-items validates the array's items' type.

  • Also see of-type package to check if the given value|object is of expected type
  • Also see typeof-arguments to validate the arguments' types passed through the enclosing function
  • Also see typeof-properties to validate the object's properties' types

Implementation

with NodeJS

npm install typeof-items

const type = require('typeof-items');

with Browser

Add typeof-items.js library to the HTML file.

The library is located in ./dist/typeof-items.js directory.
It is a webpack&babel bundled cross-browser library version.
The library is accessible as typeofItems variable in the global (window) scope.

<head>
  <script src='typeof-items.js'></script>
  <script>
    var inventors = ['Nikola', 'Albert', 'Thomas']
    typeofItems(inventors, String);
  </script>
</head>

Tests

> git clone https://github.com/devrafalko/typeof-items.git
> cd typeof-items
> npm install
> npm test        //run tests in node
> npm test deep   //run tests in node with errors shown

Usage

type(actual, expected[, callback])

actual [Array]

  • It should indicate the [Array] object
  • The item of actual array will be validated

expected

  • The expected should indicate the expected type that all items of actual array should be of

The expected Types

There are four ways to check the type of items:

  • by string expression values
  • by regular expression values
  • by constructor functions, null or undefined values
  • by some of supported custom types

Mind, that the typeof-items library uses the of-type library as the dependency, to validate the types. If you feel confused how to use the types, see more samples here.

[String]

  • Possible values:
    • 'null', 'undefined'
    • any value that equals to property's constructor.name, eg:
      'string', 'number', 'regexp', 'array', 'object', 'boolean','buffer', etc.
  • The [String] type is case insensitive:
    • 'String', 'string', 'StRiNg' checks if the property is of [String] type
    • 'RegExp', 'REGEXP', 'regexp' checks if the property is of [RegExp] type
  • The [String] type can contain multiple types, separated with |:
    • 'array|object' checks if the property is of [Array] OR [Object] type
    • 'undefined|null' checks if the property is of undefined OR null type
type(['Nikola', 'Albert', 'Thomas'], 'String|Number|null');

[RegExp]

  • Possible values:
    • /null/, /undefined/
    • any value matching the property's constructor.name, eg: /String/, /Number/, /RegExp/, /Array/, /Object/, /Boolean/,/Buffer/, /Promise/, etc.
  • Use all regular expression's features to match the type in a desired way:
    • /Str/, /Err/, /Reg/, /B/
    • /.+Error$/, /^RegExp$/,
    • /^[A-Z][a-z]+$/
  • For the case insensitivity use i flag:
    • /string/i, /regexp/i, /TYPEERROR/i
  • For multiple values use regexp (x|y) expression:
    • /String|Number/, /TypeError|Error/, /(obj|str)/i
type(['Nikola', 'Albert', 'Thomas'], /number|string|undefined/i);

[Function|Array|null|undefined]

  • Possible values:
    • null, undefined
    • any [Function] constructor, eg: String, TypeError, Promise, Array, etc.
  • For multiple values use array:
    • [String, Object, Array, null]
    • [null, undefined, Boolean]
type(['Nikola', 'Albert', 'Thomas'], [Number, String, undefined]);

When you use bundlers or minifiers, use [String|RegExp] type wisely as bundlers may change the names of functions|constructors|classes in the output file.

Extra types:

[String] 'arguments' | [RegExp] /arguments/

  • The type 'arguments' or /arguments/ expects the item to be the function's arguments object

[String] 'instance' | [RegExp] /instance/

  • The type 'instance' or /instance/ expects the item to be the instance of the user's class|constructor
  • It fails when the item is an instance of built-in (native) constructor
    • [], 'hello world', {}
  • It fails for instances that are the global|window's properties

[String] 'objectable' | [RegExp] /objectable/

  • The type 'objectable' or /objectable/ expects the item to be the object that is the instance of the Object constructor
    • {}, [], new String('hello world'), new Boolean(1)
  • It fails when the item is a primitive value or a simple value
    • 'hello world', true, 10, null, undefined

[String] 'truthy' | [RegExp] /truthy/

  • The type 'truthy' or /truthy/ expects the item to be like:
    • 'abc', true, 1, -1, {}, [], function(){}

[String] 'falsy' | [RegExp] /falsy/

  • The type 'falsy' or /falsy/ expects the item to be like:
    • '', false, 0, null, undefined, NaN

[String] 'any' | [RegExp] /any/ | [Array] [] | [String] ""

  • The type 'any' or /any/ or empty array [] or empty string "" expects the item to be of any type

callback [Function] (optional)

  • if not passed, the TypeError with default message will be thrown to the console, if any item's type is invalid.
  • The TypeError default message is eg.:
    • Invalid [0] item. The [undefined] value has been assigned, while the value of type matching string expression "string|null" is expected.
    • Invalid [1] item. The [undefined] <<falsy>> value has been assigned, while the value of type matching string expression "truthy|null" is expected.
    • Invalid [5] item. The [undefined] value has been assigned, while the value of type matching regular expression /String|null/ is expected.
    • Invalid [3] item. The [undefined] value has been assigned, while the value of type [String|null] is expected.
  • if passed, the default TypeError will not be thrown to the console and the user can decide what to do inside the callback function.
  • Use callback function if you don't want to stop your code execution by default (no callback) throw statement!
  • the callback function is executed only if at least one item value is of invalid type.
  • The one [Object] argument is passed through callback function with the following properties:
    • index
      indicates the [Number] index of the incorrect item of the array, eg. 0, 5
    • actual
      indicates the [String] actual type of the incorrect items's value, eg. "Boolean", "null"
    • expected
      indicates the type(s) expected by the user, eg. "Array", "Boolean|Number", "/array|object/i"
    • message
      is the default error [String] message, that you can use eg. to log in the console
    • textActual
      indicates the [String] textual actual type, eg. "[undefined] <<falsy>> value"
    • textExpected
      indicates the [String] textual expected type, eg. "value of type matching regular expression /String|null/"
const type = require('typeof-items');

const inventors = ['Nikola', 'Albert', 'Thomas'];

type(inventors, [Object, null], ({ message, index, expected, actual })=>{
  console.error(message);
  //throw new Error(`Invalid [${index}] [${actual}] item. Each inventor on the list should be pushed as the [${expected}] item.`);
});

Return value

The function type() returns true when all items of the actual array are of valid types.
The function type() returns false when at least one of the checked items is of invalid type.

const valid = type(['Nikola', 'Albert'], [Object, null], () => {
  console.log('Aborted.');
});
if(!valid) return;