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

@akanshgulati/are

v1.1.0

Published

A utility for type checking elements on an array. The library is inspired from chai framework supporting chaining to combine multiple operations.

Downloads

4

Readme

🚀 Installation

NPM

$ npm install @akanshgulati/are  

YARN

$ yarn add @akanshgulati/are  

🎉 Usage

const Are = require('@akanshgulati/are');  
  
Are([1,2,3,4]).of.number();  
//=> true  
  
Are(["🎉","🎁","🔆" ]).of.type('string');  
//=> true'  
  
Are(["a", "b", "c"]).of.not.type('string');  
//=> false  
  
Are([[[[[[[[[1]]]]]]]]]).flat.of.number();  
//=> true  

📙 API

Helpers

Helps in selected a particular set of an array and negative of the current type-check

.all - checks for all elements (default)  
.any - checks for all elements of array  
.first - check for first element  
.last - check for last element  
.middle - check for middle element(s)
.flat - check for type in flatten array
.not - checks for opposite, e.g. (.not.any => .all)  

Types

.type(<value>)

The value needs to be in Pascal case (e.g. NaturalNumbers) or Underscore separated (e.g. natural_number)

Primitive types

String
Number
Object
Array
Boolean
Date
ArrayBuffer

Array types

Int8Array
Uint8Array  
Uint8ClampedArray  
Int16Array  
Uint16Array  
Int32Array  
Uint32Array  
Float32Array  
Float64Array  
BigInt64Array  
BigUint64Array  
ArrayBuffer  
SharedArrayBuffer  
DataView

Number types

Integer  
SafeInteger  
Decimal  
Float  
Finite  
Infinite  
NaturalNumber  
WholeNumber  
PositiveNumber  
NegativeNumber  
Zero  

Boolean types

Falsy  
Truthy

Built-in methods

.string()

.number()

.array()

.boolean()

.null()

.undefined()

.date()

.int8Array()

.uint8Array()

.uint8ClampedArray()

.int16Array()

.uint16Array()

.int32Array()

.uint32Array()

.float32Array()

.float64Array()

.bigInt64Array()

.bigUint64Array()

.dateView()

.arrayBuffer()

.sharedArrayBuffer()

.integer()

.safeInteger()

.finite()

.infinite()

.wholeNumber()

.positiveNumber()

.negativeNumber()

.zero()

.decimal()

.float()

.truthy()

.falsy()

Check all elements (default)

  • Are(ARRAY).of.type(value) // custom('string', 'number', 'object', 'boolean', 'array')
  • Are(ARRAY).of.number() or Are(ARRAY).of.number() or Are(ARRAY).all.of.number()
  • Are(ARRAY).of.object() or - Are(ARRAY).of.object() or - Are(ARRAY).all.of.object()
  • Are(ARRAY).of.string() or - Are(ARRAY).of.string() or - Are(ARRAY).all.of.string()
  • Are(ARRAY).of.boolean() or - Are(ARRAY).of.boolean() or - Are(ARRAY).all.of.boolean()

Check one element

  • Are(ARRAY).any.of.type(value) // custom('string', 'number', 'object', 'boolean', 'array')
  • Are(ARRAY).any.of.number()
  • Are(ARRAY).any.of.object()
  • Are(ARRAY).any.of.string()
  • Are(ARRAY).any.of.boolean()

Check at particular index

  • Are(ARRAY).middle.of.type(value) // checks for (n-1)/2 index in case of odd and (n-1)/2 and n/2 indexes in case even

  • Are(ARRAY).first.of.type(value) // checks for 0th index for type value

  • Are(ARRAY).last.of.type(value) // checks last element for type value

  • Are(ARRAY).middle.of.not.type(value) // returns true if neither of middle index(s) is of a type value

  • Are(ARRAY).first.of.not.type(value) // checks only for 0th index for not to be of type value

Check in nested array

  • Are(ARRAY).flat.of.type(value) // flatten array to same root and then checks for all elements for type value

Negation

  • Are(ARRAY).of.not.type(value) // checks if all the elements are of not type value

🍺 Inspiration

No package available online which checks for an array of elements

  1. It checks not only for a single element, instead, checks for an array of data
  2. Chaining technique to add multiple operations in one go
  3. Performant while type checking an array of elements.
  4. Provides selection helpers for type checking a particular element of an array
  5. Supports negation for checking opposite to current type checking.

🗜️ Examples

You can refer to test file for various examples on above types, see examples.

🔗 Related

Currently there are multiple libraries which check for a single element but not for an array of elements.

  • is - Package which checks single element
  • expect - Chaining of operations for testing assertions
  • @sindresorhus/is - Advanced type checking of over 100 types.

✔️ To-do

  • [ ] Check for an empty array isEmpty()
  • [ ] Add support for TypeScript type gaurds
  • [x] More type checking like .int16Array(), .float32Array()
  • [x] Check for truthy elements if no element is undefined or null, .isTruthy()
  • [x] Check for integers, float values under type number, 'integers(), decimals()

👨‍💻 Maintainers