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

@dwtechs/checkard

v2.29.2

Published

Open source dynamic type checking library for Javascript, Typescript and Node.js to test if a given variable is what it is supposed to be.

Downloads

882

Readme

License: MIT npm version last version release date Jest:coverage minified size

Synopsis

Checkard.js is an open source dynamic type checking library for Javascript, Typescript and Node.js to test if a given variable is what it is supposed to be ( Function, object, ascii, boolean, integer, string, json, email...)

  • No dependency
  • Very lightweight
  • Thoroughly tested
  • Works in Javascript, Typescript and Node.js
  • Can be used as CommonJS module, EcmaScrypt module or IIFE
  • Old browsers support

Support

  • android: 2.2,
  • chrome: 34,
  • edge: 12,
  • firefox: 11,
  • ie: 9,
  • ios: 4.2,
  • opera: 28,
  • safari: 5.1,
  • samsung: 4,
  • Node.js: 14

Those are the oldest targeted versions. The library should work properly on older devices but we do not support it officially.

Installation

$ npm i @dwtechs/checkard

Usage

ES6 / TypeScript

import { isFunction, isArray } from "@dwtechs/checkard";

if (isFunction(variable)) {
  //variable is a function
}

if (!isArray(variable, '=', 2)) {
  //variable is not an array of length 2
}

if (!isString(firstName, true))
    return next({ status: 400, msg: "Missing first name" });
if (!isString(lastName, true))
    return next({ status: 400, msg: "Missing last name" });

CommonJS

const ch = require("@dwtechs/checkard");
// you may need to use "require("@dwtechs/checkard/dist/ch"); with Node.js old versions"

if (ch.isFunction(variable)) {
  //variable is a function
}

if (!ch.isArray(variable, '=', 2)) {
  //variable is not an array of length 2
}

if (!ch.isString(firstName, true))
    return next({ status: 400, msg: "Missing first name" });
if (!ch.isString(lastName, true))
    return next({ status: 400, msg: "Missing last name" });

IIFE

<script src="node_modules/@dwtechs/checkard/dist/ch.iife.min.js"></script>
if (ch.isFunction(variable)) {
  //variable is a function
}

if (!ch.isArray(variable, '=', 2)) {
  //variable is not an array of length 2
}

API Reference

Types


type Comparator = '='|'<'|'>'|'<='|'>=';

type PasswordOptions = {
  lowercase: boolean,
  uppercase: boolean,
  number: boolean,
  specialCharacter: boolean
}

Primitive


isBoolean(bool: any): boolean {}

// If typeCheck = false values like '4', '0', '8e4', '+true', '0x44' return true
isNumber(number: any, typeCheck?: boolean = true): boolean {}

isSymbol(sym: any): boolean {}

//Check whether val is null or undefined
isNil(val: any): boolean {}

Number


isInteger(number: any, typeCheck?: boolean = true): boolean {}

isFloat(number: any, typeCheck?: boolean = true): boolean {}

isEven(number: any, typeCheck?: boolean = true): boolean {}

isOdd(number: any, typeCheck?: boolean = true): boolean {}

isOrigin(number: any, typeCheck?: boolean = true): boolean {}

isPositive(number: any, typeCheck?: boolean = true): boolean {}

isNegative(number: any, typeCheck?: boolean = true): boolean {}

isPowerOfTwo(number: any, typeCheck?: boolean = true): boolean {}

isAscii(code: any, extended?: boolean = false): boolean {}

Valid number


isValidNumber(number: any, 
              min?: number = -999999999, 
              max?: number = 999999999, 
              typeCheck?: boolean = true ): boolean {}

isValidInteger(number: any, 
               min?: number = -999999999, 
               max?: number = 999999999, 
               typeCheck?: boolean = true ): boolean {}

isValidFloat( number: any, 
              min?: number = -999999999.9, 
              max?: number = 999999999.9, 
              typeCheck?: boolean = true ): boolean {}

String


isString(string: any, required?: boolean = false): boolean {}

isStringOfLength( string: any, 
                  min?: number = 0, 
                  max?: number = 999999999 ): boolean {}

isJson(string: any): boolean {}

isRegex(regex: any, typeCheck?: boolean = true): boolean {}

isEmail(email: any): boolean {}

isIpAddress(ipAddress: any): boolean {}

isJWT(t: any): boolean {}

isSlug(slug: any): boolean {}

isHexadecimal(string: any): boolean {}

const PwdDefaultOptions = {
  lowerCase: true,
  upperCase: true,
  number: true,
  specialCharacter: true,
  minLength: 12,
  maxLength: 64,
};
isValidPassword(string: any, options: PasswordOptions = PwdDefaultOptions): boolean {}

containsUpperCase(string: any): boolean {}

containsLowerCase(string: any): boolean {}

containsSpecialCharacter(string: any): boolean {}

containsNumber(string: any, min?: number|null, max?: number|null): boolean {}

Usage example :


import { isValidPassword } from "@dwtechs/checkard";

const PwdOptions = {
  lowerCase: true,
  upperCase: true,
  number: true,
  specialCharacter: false,
  minLength: 12,
  maxLength: 16,
};
const password = 'teSt1234';

if (isValidPassword(password, PwdOptions)) {
  // check if password is valid compared to PwdOptions
}

Date


isDate(date: any): boolean {}

isValidDate(
    date: any, 
    min?: Date = new Date('1/1/1900'), 
    max?: Date = new Date('1/1/2200')
  ): boolean {}

isTimestamp(number: any, typeCheck?: boolean = true): boolean {}

// default min = 1/1/1900 (month/day/year)
// default max = 1/1/2200 (month/day/year)
isValidTimestamp(
    number: any, 
    min?: number = -2208989361000, 
    max?: number = 7258114800000, 
    typeCheck?: boolean = true
  ): boolean {}

Array


// Check if 'array' is an array and optionally if it is of length =, <, >, <= or >= than 'length'
isArray(
    array: any, 
    comparator?: Comparator|null, 
    length?: number|null
  ): boolean {}

isArrayOfLength(
    array: any, 
    min = 0, 
    max = 999999999): boolean {}

// This method lets you check if a value is included in an array.
isIn(value: any, arr: any[]): boolean {}

Usage example :


import { isArray, isIn } from "@dwtechs/checkard";

let ar = ['dog','cat','bird'];

if (isArray(ar)) {
  // check if ar is an array
}

if (isArray(ar, '=', 2)) {
  // check if ar is an array of length 2
}

if (isArray(ar, '>=', 1)) {
  // check if ar is an array of length greater than or equal to 1
}


// an array of restricted values
const levels = [ "error", "warn", "info", "debug" ];

// Basic usage : 
console.log(isIn("debug", levels)); // true
console.log(isIn("debag", levels)); // false

// Typical usage : 
const defaultLvl = "warn";
function setLevel(level: Levels): Levels {
  return isIn(level, levels) ? level : defaultLvl;
}
let lvl = setLevel("error"); // lvl = "error"
let lvl = setLevel("infos"); // lvl = "error"

Object


isObject(obj: any, emptyCheck?: boolean = false): boolean {}

// This method lets you check if a value is included in an object properties.
isProperty(val: any, obj: { [key: string]: any }): boolean {}

Usage example :


import { isProperty } from "@dwtechs/checkard";

// an object to describe the custom type.
const levels = {
  error: 0,
  warn: 1,
  info: 2,
  debug: 3,
};

// Basic usage : 
console.log(isProperty("debug", levels)); // true
console.log(isProperty("debag", levels)); // false

Function


isFunction(func: any): boolean {}

Html


isHtmlElement(htmlElement: any): boolean {}

isHtmlEventAttribute(htmlEventAttribute: any): boolean {}

isNode(node: any): boolean {}

Normalize


ucfirst(string: string, everyWords?: boolean = true): string | false {}

// accept a-z - and _ characters
normalizeNickname(
  nickname: string, 
  firstName: string, 
  lastName: string
): string | false {}

normalizeName(string: string): string | false {}

normalizeEmail(string: string): string | false {}

Example :


const ch = require("@dwtechs/checkard");

function normalizeInputs(req, res, next) {
  const users = req.body.rows;
  log.debug(`Normalize values for ${users.length} users`);

  for (const u of users) {
    const { firstName, lastName, nickname, email } = u;
    u.firstname = ch.normalizeName(firstName);
    u.lastname = ch.normalizeName(lastName);
    u.nickname = ch.normalizeNickname(nickname, firstName, lastName);
    u.email = ch.normalizeName(email);
  }
  next();
}

Contributors

Checkard.js is under continuous development and we would be glad to get all the help you can provide. To contribute please read contributor.md for detailed installation guide.

Stack

| Purpose | Choice | Motivation | | :-------------- | :------------------------------------------: | -------------------------------------------------------------: | | repository | Github | hosting for software development version control using Git | | package manager | npm | default node.js package manager | | language | TypeScript | static type checking along with the latest ECMAScript features | | module bundler | Rollup | advanced module bundler for ES6 modules | | unit testing | Jest | delightful testing with a focus on simplicity |