Type predicate functions for checking if a value is of a specific type or asserting that it is.

Overview

As-Is

Description

As-Is contains two modules.

  • Is - Type predicates for checking values are of certain types.
  • As - Asserting values are of a certain type using the above predicates.

Usage

Using npm:

$ npm i -g npm
$ npm i as-is

In Javascript:

// Load the As and Is
var { As, Is } = require('@dec-land/as-is');

// Load them seperately
var As = require('@dec-land/as-is/as');
var Is = require('@dec-land/as-is/is');

In Typescript

// Load the As and Is
import { Is, As } from '@dec-land/as-is';

// Load them seperately
import As from '@dec-land/as-is/As';
import Is from '@dec-land/as-is/Is';

Examples

In Examples

import Is from '@dec-land/as-is/Is';

Is.number(5) // true
Is.number('test'); // false

Is.arrayOf([5, 10], Is.integer) // true
Is.arrayOf(['a', 'b'], Is.integer); // false

Is.date('date') // false
Is.date(new Date()); // true

// etc...

As Examples

import { As, Is } from '@dec-land/as-is';

let a: unknown = 'test'; // Here we have an unknown string

// console.log(a.split(',')); Split does not exist on type unknown

As.string(a); // Assert the a as a string, if it isn't an error will be thrown.

console.log(a.split(',')); // Now works as flow wouldn't reach here unless a is a string

let b: unknown = [new Date(), new Date()]; // Here we have an unknown array of dates.

// b.map((date) => console.log(date.toLocaleString())); // Property 'map' does not exist on type 'unknown'.

As.arrayOf(b, Is.date); // Assert that b is an array of dates. If not an error will be thrown

b.map((date) => console.log(date.toLocaleString())); // Works fine

// etc...

Why As-Is?

As-Is offers the following predicates that can be used to check or assert whether a value is of a specific type.

  • number
  • integer
  • string
  • boolean
  • primitive
  • nonPrimitive
  • date
  • json
  • array
  • readonlyArray
  • readonlyArrayOf
  • object
  • objectWithProperties
  • function

Tests

Tests are included for As and Is. These can be ran using:

$ npm test
You might also like...

Very tiny function that checks if an object/array/value is shaped like another, with TypeScript type refining.

@suchipi/has-shape Very tiny (~200B before minification/compression) function that checks if an object/array/value is shaped like another, with TypeSc

Aug 13, 2022

Keep the type of storage value unchanged and change array and object directly. Supports listening to the changes and setting expires.

proxy-web-storage A more convenient way to use storage through proxy. try it on codesandbox. Install npm i proxy-web-storage Features Base Keep the ty

Dec 25, 2022

Functions Recipes is a library of examples to help you getting started with Salesforce Functions and get used to their main features.

Functions Recipes is a library of examples to help you getting started with Salesforce Functions and get used to their main features.

Functions Recipes Introduction Salesforce Functions lets you use the Salesforce Platform for building event-driven, elastically scalable apps and expe

Dec 29, 2022

🔥 A Powerful JavaScript Module for Generating and Checking Discord Nitro 🌹

🔥 A Powerful JavaScript Module for Generating and Checking Discord Nitro 🌹

DANG: Dreamy's Awesome Nitro Generator Join Our Discord Getting Started Before, We start please follow these Steps: Required* ⭐ Give a Star to this dr

Jan 5, 2023

Babel-plugin-amd-checker - Module format checking plugin for Babel usable in both Node.js the web browser environments.

babel-plugin-amd-checker A Babel plugin to check the format of your modules when compiling your code using Babel. This plugin allows you to abort the

Jan 6, 2022

ToDo list app is a simple web app that helps you organize your day, by adding, checking and deleting daily tasks

ToDo list app is a simple web app that helps you organize your day, by adding, checking and deleting daily tasks

TODO List App "To-do list" is a WebApp tool that helps to organize your day. It simply lists the tasks that you need to do and allows you to mark them

Oct 18, 2022

This package is for developers to be able to easily integrate bad word checking into their projects.\r This package can return bad words in array or regular expression (regex) form.

Vietnamese Bad Words This package is for developers to be able to easily integrate bad word checking into their projects. This package can return bad

Nov 3, 2022

The ultimate parity checking as-a-service platform.

Astro Starter Kit: Minimal npm create astro@latest -- --template minimal 🧑‍🚀 Seasoned astronaut? Delete this file. Have fun! 🚀 Project Structure I

Nov 28, 2022

Snipes Test Flight apps. Configurable & has the ability to use a burner account for checking the status to avoid bans.

Snipes Test Flight apps. Configurable & has the ability to use a burner account for checking the status to avoid bans.

TestFlight Sniper Snipes TestFlight beta apps. Configurable & has the ability to use a burner account for checking the status to avoid bans. Features

Dec 20, 2022
Comments
  • Is.number missing union with null

    Is.number missing union with null

    Hey :) I saw your lib on reddit. I was just reading the source and saw this:

    https://github.com/dec-land/as-is/blob/c35e237991f38cd2d086f01f0c81f42d38d7fb11/is/index.ts#L18

    Shouldn't you return number | null if it is allowed to be null?

    opened by Honga1 2
Owner
Declan Fitzpatrick
rabbits 🐰
Declan Fitzpatrick
A complete media query framework for CSS, to apply specific properties in specific screen

A complete media query framework for CSS, to apply specific properties in specific screen Note: Size of every media query is `50px, 100px, 150px, 200p

Rohit Chouhan 6 Aug 23, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Combine type and value imports using Typescript 4.5 type modifier syntax

type-import-codemod Combines your type and value imports together into a single statement, using Typescript 4.5's type modifier syntax. Before: import

Ian VanSchooten 4 Sep 29, 2022
A type speed checking website which lets you check your typing speed and shows the real-tme leaderboards with mongodb as DB and express as backend

This is a Next.js project bootstrapped with create-next-app. Getting Started First, run the development server: npm run dev # or yarn dev Open http://

Sreehari jayaraj 8 Mar 27, 2022
A library to create pipelines with contexts and strong type checking.

TypePipe A library to create pipelines with contexts and strong type checking. Installation With Node.js and npm installed in your computer run: npm i

Alvaro Fresquet 16 Jun 11, 2022
Runtime type checking in pure javascript.

Install npm install function-schema Usage import { signature } from 'function-schema'; const myFunction = signature(...ParamTypeChecks)(ReturnValueCh

Jeysson Guevara 3 May 30, 2022
Resolve parallel promises in key-value pairs whilst maintaining type information

async-kv Resolves promises in key-value pairs maintaining type information. Prerequisites NodeJS 12 or later Installation npm i async-kv yarn add asyn

Tony Tamplin 4 Feb 17, 2022
🧩 TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)

?? TypeScript Primitives type TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types

CodelyTV 82 Dec 7, 2022