Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@dwtechs/checkard

DWTechs2.1kMIT3.2.3TypeScript support: included

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.

type checking, type checker

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, isString } from "@dwtechs/checkard";

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

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

if (!isString(variable)) {
  // variable is not a string
}

if (isString(variable, "!0")) {
  // variable is a string and is not empty
}

if (isString(variable, ">", 2)) {
  // variable is a string of length greater than 2
}

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(variable)) {
  // variable is not a string
}

if (ch.isString(variable, "!0")) {
  // variable is a string and is not empty
}

if (ch.isString(variable, ">", 2)) {
  // variable is a string of length greater than 2
}

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
}

if (!ch.isString(variable)) {
  // variable is not a string
}

if (ch.isString(variable, "!0")) {
  // variable is a string and is not empty
}

if (ch.isString(variable, ">", 2)) {
  // variable is a string of length greater than 2
}

API Reference

Types


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

type PasswordOptions = {
  lowerCase: boolean,
  upperCase: boolean,
  number: boolean,
  specialCharacter: boolean,
  maxLength: number,
  minLength: number
}

Primitive


primitive methods accept any variable as parameter in order to check its type.


/**
 * Checks if the given value is a boolean.
 *
 * @param {unknown} v - The value to check.
 * @returns {boolean} True if the value is a boolean, otherwise false.
 */
isBoolean(v: unknown): v is boolean {}

/**
 * Checks if the given value is a number and optionally check its length.
 * If type checking  = false, string values like '4', '0', '8e4', '+true', '0x44' return true
 *
 * @param {unknown} v - The value to check.
 * @param {boolean} [type=true] - A boolean indicating whether to perform type checking. Defaults to true.
 * @param {Comparator | null} [comparator=null] - An optional comparator to compare the value. Defaults to null.
 * @param {number | null} [limit=null] - An optional limit to compare the value against. Defaults to null.
 * @returns {boolean} True if the value is a number and passes all checks, otherwise false.
 */
isNumber
    v: unknown, 
    type = true,
    comparator = null, 
    limit = null
): v is number {}

/**
 * Checks if the given value is a string and optionally checks its length.
 *
 * @param {unknown} v - The value to check.
 * @param {Comparator | null} [comparator=null] - An optional comparator to compare the string length.
 * @param {number | null} [limit=null] - An optional limit to compare the string length against.
 * @returns {boolean} True if the value is a string and meets the limit conditions, otherwise false.
 */
isString(
    v: unknown, 
    comparator = null, 
    limit = null
): v is string {}

/**
 * Checks if the provided value is a symbol.
 *
 * @param {unknown} v - The value to check.
 * @returns {boolean} True if the value is a symbol, otherwise false.
 */
isSymbol(v: unknown): v is symbol {}

/**
 * Checks if the given value is null or undefined.
 *
 * @param {unknown} v - The value to check.
 * @returns {boolean} True if the value is null or undefined, otherwise false.
 */
isNil(v: unknown): v is null | undefined {}

/**
 * Checks if the given value is null.
 *
 * @param {unknown} v - The value to check.
 * @returns {boolean} True if the value is null, otherwise false.
 */
isNull(v: unknown): v is null {}

/**
 * Checks if the given value is undefined.
 *
 * @param {unknown} v - The value to check.
 * @returns {boolean} True if the value is undefined, otherwise false.
 */
isUndefined(v: unknown): v is undefined {}

Usage example for isString method:


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

const str = 'dog';

if (isString(str)) {
  // check if str is a string
}

if (isString(str, '!0')) {
  // check if str is not empty
}

if (isString(str, '0')) {
  // check if str is empty
}

if (isString(str, '=', 2)) {
  // check if str is a string of length 2
}

if (isString(str, '>=', 1)) {
  // check if str is a string of length greater than or equal to 1
}

Note

If isString() returns false, Typescript will consider str is not a string.
So if you need to check if str is of length x but needs to be considered as a string even if its length is not x you should do it like this:


import { isString, isStringOfLength } from "@dwtechs/checkard";

const str = 'dog';

if (isString(str) && !isStringOfLength(str, 4, 4)) {
  // string is of type string even if length is not 4
}

Non-primitive


Non-primitive methods accept any variable as parameter in order to check its type.


/**
 * Checks if the given value is an object and optionally if it is not empty.
 *
 * @template T - The expected type of the object.
 * @param {unknown} v - The value to check.
 * @param {boolean} [empty=false] - If true, the function will also check if the object is not empty.
 * @returns {o is object & T} True if the value is an object (and not empty if specified), otherwise false.
 */
isObject<T = unknown>(
  o: unknown, 
  empty = false
): o is object & T {}

/**
 * Checks if the given value is an array and optionally compares its length.
 *
 * @template T - The type of elements in the array.
 * @param {unknown} v - The value to check.
 * @param {Comparator | null} [comparator=null] - An optional comparator function to compare the array length.
 * @param {number | null} [limit=null] - An optional limit to compare the array length against.
 * @returns {boolean} True if the value is an array and meets the limit conditions, otherwise false.
 */
isArray<T = unknown>(
    v: unknown, 
    comparator = null, 
    limit = null
): v is T[] {}

/**
 * Checks if the given input is a valid JSON string.
 *
 * @param {unknown} v - The value to check.
 * @returns {boolean} True if the input is a valid JSON string, otherwise false.
 */
isJson(v: unknown): v is JSON {} 

/**
 * Checks if the given value is a regular expression.
 *
 * @param {unknown} v - The value to check.
 * @param {boolean} [type=true] - A boolean indicating whether to perform type checking. Defaults to true.
 * If false, String values like '/^abc$/' are considered as regular expressions.
 * @returns {boolean} True if v is a RegExp or can be converted to a RegExp, otherwise false.
 */
isRegex(v: unknown, type = true): v is RegExp {} 

/**
 * Checks if the given value is a valid Date object.
 *
 * @param {unknown} v - The value to check.
 * @returns {boolean} True if the value is a Date object, otherwise false.
 *
 */
isDate(v: unknown): v is Date {}

/**
 * Checks if the provided value is a function.
 *
 * @param {unknown} v - The value to check.
 * @returns {boolean} True if the value is a function, otherwise false.
 */
isFunction(v: unknown): v is (...args: unknown[]) => unknown {}

Usage example for isArray method:


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

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

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

if (isArray(arr, '!0')) {
  // arr is not empty
}

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

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

Note

Note that if isArray() returns false Typescript will consider arr is not an array.
So if you need to check arr is of length x but needs to be considered as an array even if its length is not x you can do it like this:


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

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

if (isArray(arr) && !isArrayOfLength(arr, 4, 4)) {
  // array is of type array even if length is not 4
}

Boolean



/**
 * Checks if a value is falsy.
 *
 * A value is considered falsy if it is : 
 * false,
 * 0, 
 * -0,
 * "",
 * null,
 * undefined,
 * NaN,
 * 0n.
 *
 * @param {unknown} v - The value to check.
 * @returns {boolean} True if the value is falsy, otherwise false.
 */
function isFalsy(v: unknown): boolean {}

/**
 * Checks if a value is truthy.
 *
 * A value is considered truthy if it is : 
 * true: The boolean value true.
 * Non-zero numbers: Any number other than 0 or -0.
 * Non-empty strings: Any string with at least one character.
 * Objects: Any object, including empty objects and arrays.
 * Symbols: Any symbol.
 * BigInt values: Any BigInt value other than 0n.
 *
 * @param {unknown} v - The value to check.
 * @returns {boolean} True if the value is truthy, otherwise false.
 */
function isTruthy(v: unknown): boolean {}

Number



/**
 * Checks if a given number is an integer.
 *
 * @param {number | string | undefined | null} n - The number to check.
 * @param {boolean} [type=true] - Optional boolean indicating whether to perform type checking. Defaults to true.
 * @returns {boolean} True if the number is an integer, otherwise false.
 */
isInteger(n: number | string | undefined | null, type = true): boolean {}

/**
 * Checks if a given number is a floating-point number.
 *
 * @param {number | string | undefined | null} n - The number to check.
 * @param {boolean} [type=true] - Optional boolean indicating whether to perform type checking. Defaults to true.
 * @returns {boolean} True if the number is a floating-point number, otherwise false.
 */
isFloat(n: number | string | undefined | null, type = true): boolean {}

/**
 * Checks if a given number is even.
 *
 * @param {number | string | undefined | null} n - The number to check.
 * @param {boolean} [type=true] - Optional boolean indicating whether to perform type checking. Defaults to true.
 * @returns {boolean} True if the number is an even integer, otherwise false.
 */
isEven(n: number | string | undefined | null, type = true): boolean {}

/**
 * Determines if a given number is odd.
 *
 * @param {number | string | undefined | null} n - The number to check.
 * @param {boolean} [type=true] - Optional boolean indicating whether to perform type checking. Defaults to true.
 * @returns {boolean} True if the number is an odd integer, otherwise false.
 */
isOdd(n: number | string | undefined | null, type = true): boolean {}

/**
 * Checks if a given number is zero.
 *
 * @param {number | string | undefined | null} n - The number to check.
 * @param {boolean} [type=true] - Optional boolean indicating whether to perform type checking. Defaults to true.
 * @returns {boolean} True if the number is zero, otherwise false.
 */
isOrigin(n: number | string | undefined | null, type = true): boolean {}

/**
 * Checks if a given number is positive.
 *
 * @param {number | string | undefined | null} n - The number to check.
 * @param {boolean} [type=true] - Optional boolean indicating whether to perform type checking. Defaults to true.
 * @returns {boolean} True if the number is positive, otherwise false.
 * 
 * @remarks
 * This function also check if the value is a number.
 */
isPositive(n: number | string | undefined | null, type = true): boolean {}

/**
 * Checks if a given number is negative.
 *
 * @param {number | string | undefined | null} n - The number to check.
 * @param {boolean} [type=true] - Optional boolean indicating whether to perform type checking. Defaults to true.
 * @returns {boolean} True if the number is negative, otherwise false.
 *  
 * @remarks
 * This function also check if the value is a number.
 */
isNegative(n: number | string | undefined | null, type = true): boolean {}

/**
 * Checks if a given number is a power of two.
 *
 * @param {number | string | undefined | null} n - The number to check.
 * @param {boolean} [type=true] - Optional boolean indicating whether to perform type checking. Defaults to true.
 * @returns {boolean} True if the number is a power of two, otherwise false.
 */
isPowerOfTwo(n: number | string | undefined | null, type = true): boolean {}

/**
 * Checks if a given number is an ASCII code.
 *
 * @param {number | undefined | null} n - The number to check.
 * @param {boolean} [ext=true] - Optional boolean to include extended ASCII range (0-255). Defaults to true.
 * @returns {boolean} True if the number is a valid ASCII code, otherwise false.
 */
isAscii(n: number | undefined | null, ext = true): boolean {}

Valid number


Valid number methods take a number as parameter and check of the number lies in the right interval


/**
 * Checks if a given value is a valid number within given range.
 *
 * @param {number | string | undefined | null} n - The number to check
 * @param {number} [min=-999999999] - minimal value of the range
 * @param {number} [max=999999999] - maximal value of the range
 * @param {boolean} [type=true] - whether to perform type checking
 * @returns {boolean} True if the value is a valid number, otherwise false.
 *  
 * @remarks
 * This function also check if the value is a number.
 */
isValidNumber(
    n: number | string | undefined | null, 
    min = -999999999, 
    max = 999999999, 
    type = true 
): boolean {}

/**
 * Checks if a given value is a valid integer within given range.
 *
 * @param {number | string | undefined | null} n - The number to check
 * @param {number} [min=-999999999] - minimal value of the range
 * @param {number} [max=999999999] - maximal value of the range
 * @param {boolean} [type=true] - whether to perform type checking
 * @returns {boolean} True if the value is a valid integer, otherwise false.
 */
isValidInteger(
    n: number | string | undefined | null, 
    min = -999999999, 
    max = 999999999, 
    type = true
): boolean {}

/**
 * Checks if a given value is a valid float within given range.
 *
 * @param {number | string | undefined | null} n - The number to check
 * @param {number} [min=-999999999.9] - minimal value of the range
 * @param {number} [max=999999999.9] - maximal value of the range
 * @param {boolean} [type=true] - whether to perform type checking
 * @returns {boolean} True if the value is a valid float, otherwise false.
 */
isValidFloat( 
    n: number | string | undefined | null, 
    min = -999999999.9, 
    max = 999999999.9, 
    type = true 
): boolean {}

String



/**
 * Checks if the length of a given string is within the specified range.
 *
 * @param {string | undefined | null} s - The string to check.
 * @param {number} [min=0] - The minimum length of the string (inclusive). Default is 0.
 * @param {number} [max=999999999] - The maximum length of the string (inclusive). Default is 999999999.
 * @returns {boolean} True if the string length is within the specified range, otherwise false.
 */
isStringOfLength( 
    s: string | undefined | null, 
    min = 0, 
    max = 999999999 
): boolean {}

/**
 * Checks if the given string is a valid email address.
 *
 * @param {string | undefined | null} s - The string to be checked.
 * @returns {boolean} True if the string is a valid email address, otherwise false.
 */
isEmail(s: string | undefined | null): boolean {}

/**
 * Checks if the given string is a valid IP address.
 *
 * @param {string | undefined | null} s - The string to be checked.
 * @returns {boolean} True if the string is a valid IP address, otherwise false.
 */
isIpAddress(s: string | undefined | null): boolean {}

/**
 * Checks if a given string is a valid Base64 encoded string.
 *
 * @param {string | undefined | null} s - The string to check.
 * @param {boolean} [urlEncoded=false] - Optional. If true, checks for URL-safe Base64 encoded strings. Defaults to false.
 * @returns {boolean} True if the string is a valid Base64 encoded string, otherwise false.
 */
isBase64(s: string | undefined | null, urlEncoded = false): boolean {}

/**
 * Checks if a given string is a valid JSON Web Token (JWT).
 *
 * A valid JWT consists of three parts separated by dots ('.'):
 * - Header
 * - Payload
 * - Signature
 *
 * Each part must be a valid Base64 encoded string. Additionally, the header and payload
 * must be valid JSON objects when decoded.
 *
 * @param {string | undefined | null} s - The string to check.
 * @returns {boolean} True if the string is a valid JWT, otherwise false.
 */
isJWT(s: string | undefined | null): boolean {}

/**
 * Checks if the given string is a valid slug.
 * 
 * A slug is typically a URL-friendly string that contains only lowercase letters, numbers, and hyphens.
 * 
 * @param {string | undefined | null} s - The string to check.
 * @returns {boolean} True if the string is a valid slug, otherwise false.
 */
isSlug(s: string | undefined | null): boolean {}

/**
 * Checks if the given string is a valid hexadecimal number.
 *
 * @param {string | undefined | null} s - The string to check.
 * @returns {boolean} True if the string is a valid hexadecimal number, otherwise false.
 */
isHexadecimal(s: string | undefined | null): boolean {}


const PwdDefaultOptions = {
  lowerCase: true,
  upperCase: true,
  number: true,
  specialCharacter: true,
  minLength: 12,
  maxLength: 64,
};

/**
 * Checks if a given password string meets the specified validation criteria.
 *
 * @param {string} s - The password string to validate.
 * @param {PasswordOptions} [options=PwdDefaultOptions] - Optional configuration object to specify password validation criteria.
 * @returns {boolean} True if the password meets all the specified criteria, otherwise false.
 *
 * @example
 * const options = {
 *   minLength: 8,
 *   maxLength: 20,
 *   lowerCase: true,
 *   upperCase: true,
 *   number: true,
 *   specialCharacter: true
 * };
 * const isValid = isValidPassword('Password123!', options);
 */
isValidPassword(
    s: string, 
    options = PwdDefaultOptions
): boolean {}

/**
 * Checks if the given string contains any uppercase letters.
 *
 * @param {string} s - The string to check.
 * @returns {boolean} True if the string contains at least one uppercase letter, otherwise false.
 */
containsUpperCase(s: string): boolean {}

/**
 * Checks if the given string contains at least one lowercase letter.
 *
 * @param {string} s - The string to check.
 * @returns {boolean} True if the string contains at least one lowercase letter, otherwise false.
 */
containsLowerCase(s: string): boolean {}

/**
 * Checks if the given string contains any special characters.
 *
 * @param {string} s - The string to be checked.
 * @returns {boolean} True if the string contains special characters, otherwise false.
 */
containsSpecialCharacter(s: string): boolean {}

/**
 * Checks if a given string contains a specified number of digits.
 *
 * @param {string} s - The string to check.
 * @param {number} [min=1] - The minimum number of digits required in the string. Defaults to 1.
 * @param {number|null} [max=null] - The maximum number of digits allowed in the string. If not provided, there is no upper limit.
 * @returns {boolean} True if the string contains the required number of digits, otherwise false.
 */
containsNumber(
    s: string, 
    min = 1, 
    max = null
): boolean {}

Note

String methods take a string as parameter.
Use isString() method before any string method if you are not sure about the variable type you will receive.

Example :


import { isString, isEmail } from "@dwtechs/checkard";

if (isString(value) && isEmail(value)) {
  // value is an email
}

Usage example for isValidPassword:


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



const minDate = new Date('1/1/1900');
const maxDate = new Date('1/1/2200');

/**
 * Checks if a given date is valid within a specified range.
 *
 * @param {Date} d - The date to be validated.
 * @param {Date} [min=minDate] - The minimum allowable date. Defaults to `minDate`.
 * @param {Date} [max=maxDate] - The maximum allowable date. Defaults to `maxDate`.
 * @returns {boolean} True if the date is valid and within the specified range, otherwise false.
 *  
 * @remarks
 * This function also check if the value is an instance of the Date object.
 */
isValidDate(
    d: date, 
    min = minDate, 
    max = maxDate
): boolean {}

/**
 * Checks if the given number is a valid timestamp.
 *
 * @param {number} t - The number to check.
 * @param {boolean} [type=true] - An optional boolean parameter to verify the type of t. Defaults to true.
 * @returns {boolean} True if the value is a timestamp, otherwise false.
 */
isTimestamp(t: number, type = true): boolean {}

/**
 * Checks if a given timestamp is valid within a specified range.
 *
 * @param {number} t - The timestamp to validate.
 * @param {number} [min=-2208989361000] - The minimum allowed timestamp. Default is -2208989361000,eg 1/1/1900.
 * @param {number} [max=7258114800000] - The maximum allowed timestamp. Default is 7258114800000, eg 1/1/2200).
 * @param {boolean} [type=true] - A boolean indicating the type of timestamp. Default to true.
 * @returns {boolean} True if the timestamp is valid and within the specified range, otherwise false.
 * 
 * @remarks
 * This function also check if the value is timestamp.
 */
isValidTimestamp(
  t: number, 
  min = -2208989361000, 
  max = 7258114800000, 
  type = true
): boolean {} 

Array



/**
 * Checks if the length of an array is within the specified range.
 *
 * @param {unknown[]} a - The array to check.
 * @param {number} [min=0] - The minimum length of the array (inclusive).
 * @param {number} [max=999999999] - The maximum length of the array (inclusive).
 * @returns {boolean} True if the array length is within the specified range, otherwise false.
 */
isArrayOfLength(
    a: unknown[], 
    min = 0, 
    max = 999999999
): boolean {}

/**
 * Checks if a value is present in an array starting from a specified index.
 *
 * @param {unknown[]} a - The array to search within.
 * @param {unknown} v - The value to search for.
 * @param {number} [from=0] - The index to start the search from. Defaults to 0.
 * @returns {boolean} True if the value is found in the array, otherwise false.
 */
isIn(
    a: unknown[], 
    v: unknown, 
    from = 0
): boolean {}

Usage example :


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

// 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 = "warn"

Object



/**
 * Checks if a given property exists on an object.
 * enumerable: boolean - whether to check enumerable properties only
 *
 * @template K - The type of the property key.
 * @param {object} obj - The object to check the property on.
 * @param {K} k - The property key to check for.
 * @param {boolean} [own=true] - If true, checks if the property is an own property of the object. Defaults to true.
 * @param {boolean} [enumerable=true] - If true, checks if the property is enumerable. Defaults to true.
 * @returns {boolean} True if the property exists on the object based on the specified conditions, otherwise false.
 */
isProperty<K extends PropertyKey>(
    obj: object,
    k: K,
    own = true,
    enumerable = true
): boolean // obj is Record<K, { [key: PropertyKey]: unknown }>

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

Html



/**
 * Checks if the given value is an HTML element.
 *
 * @param {unknown} h - The value to check.
 * @returns {boolean} True if the value is an HTML element, otherwise false.
 */
isHtmlElement(h: unknown): h is HTMLElement {}

/**
 * Checks if a given string is a valid HTML event attribute.
 *
 * @param {sgtring} h - The string to check.
 * @returns {boolean} True if the string is a valid HTML event attribute, otherwise false.
 *
 * @remarks
 * This function checks against a predefined list of HTML event attributes such as `onclick`, `onload`, `onerror`, etc.
 *
 * @example
 * ```typescript
 * isHtmlEventAttribute("onclick"); // returns true
 * isHtmlEventAttribute("onunknown"); // returns false
 *

*/ isHtmlEventAttribute(h: string): boolean {}

/**

  • Checks if the given value is a DOM Node. *
  • @param {unknown} n - The value to check.
  • @returns {boolean} True if the value is a Node, otherwise false. */ isNode(n: unknown): n is Node {}


### Normalize
---

```typescript

/**
 * A function to capitalize the first letter of each word in a string.
 *
 * @param {string} s - The input string to capitalize.
 * @param {boolean} everyWords - A flag to indicate whether to capitalize the first letter of every word or just the first letter of the whole string.
 * @return {string} The string with the first letter of each word capitalized.
 */
ucfirst(s: string, everyWords = true): string {}

/**
 * Returns a normalized nickname for a user.
 *
 * If the nickname is not given, the function will create a nickname
 * based on the first letter of the first name and the last name.
 *
 * nickname accepts a-z - and _ characters
 * 
 * @param {string} nickname - The nickname of the user.
 * @param {string} firstName - The first name of the user.
 * @param {string} lastName - The last name of the user.
 * @return {string | false} The normalized nickname.
 */
normalizeNickname(
    nickname: string, 
    firstName: string, 
    lastName: string
): string | false {}

/**
 * Normalizes a name by capitalizing the first letter of each word.
 *
 * @param {string} s - The name to normalize.
 * @return {string | false} The normalized name.
 */
normalizeName(s: string): string | false {}

/**
 * A function to normalize an email address.
 *
 * If the string is not a valid email address, the function will return false.
 *
 * @param {string} s - The email address to normalize.
 * @return {string | false} The normalized email address or false if the
 * string is not a valid email address.
 */
normalizeEmail(s: 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();
}

Base64



/**
 * Decodes a base64 encoded string.
 *
 * @param {string} str - The base64 encoded string to decode.
 * @param {boolean} urlSafe - A boolean indicating if the input string is URL safe. Defaults to true.
 * @returns {string} The decoded string in UTF-8 format.
 */
function b64Decode(str: string, urlSafe = true): string;

/**
 * Encodes a given string into Base64 format.
 * 
 * @param {string} str - The string to be encoded.
 * @param {boolean} urlSafe - Optional boolean to determine if the output should be URL safe. Defaults to true.
 * @returns {string} The Base64 encoded string. If `urlSafe` is true, the output will be modified to be URL safe.
 */
function b64Encode(str: string, urlSafe = true): string;

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

changelog

3.2.3 (Mar 04th 2025)

  • String, number and validNumber functions support undefined and null values in Typescript

3.2.2 (Feb 25th 2025)

  • Update isProperty() function inputs typings

3.2.1 (Feb 24th 2025)

  • Update number validation functions type for string as input

3.2.0 (Feb 23th 2025)

  • Update string validation functions for empty string cases

3.1.0 (Feb 22th 2025)

  • Add b64Encode() and b64Decode() functions to transform strings accordingly.

3.0.0 (Feb 14th 2025)

  • isString() function now also checks if value is of length =, <, >, <=, >=, !=, !0, 0 than limit.
  • isNumber() function now also checks if value is of length =, <, >, <=, >=, !=, !0, 0 than limit
  • Add isNull() and isUndefined() function
  • Add isTruthy() and isFalsy() functions
  • Add "from" parameter to function isIn() to start from index other then zero
  • Add "!=" to comparisons to check if a is different than b
  • Add "!0" to comparisons to check if a is not empty
  • Add "0" to comparisons to check if a empty
  • Update typings of most functions for easier use with Typescript
  • Typings are more strict. Resulting in possible breaking changes from Checkard version 2.x.x.
  • Other functions than primitives and non-primitives functions may not check for input type anymore. Resulting in possible breaking changes from Checkard version 2.x.x.
  • Improve Documentation

2.30.0 (Jan 29th 2025)

  • Add isBase64() function to check if a value is a valid base64 string
  • Add "own" property to isProperty() function to check inherited properties only
  • Add "enumerable" property to isProperty() function to check enumerable properties only
  • Improve the return type of isProperty() function
  • Change "any" types to "unknown" for every functions

2.29.2 (Dec 21th 2024)

  • Improve declaration file for isArrayOfLength() function

2.29.1 (Dec 20th 2024)

  • Improve isArrayOfLength() function and add more tests

2.29.0 (Dec 19th 2024)

  • Add isArrayOfLength() function to check array length with min and max

2.28.0 (Nov 30th 2024)

  • Add isIn() function to check if a value is included in an array
  • Add isProperty() function to check if a value is included in an object properties

2.27.0 (Nov 13th 2024)

  • Add isValidPassword function
  • Update normalizeNickname function to exclude special characters

2.26.0 (Oct 26th 2024)

  • Add normalization functionalities
  • Add ucfirst function
  • Add normalizeNickname function
  • Add normalizeName function
  • Add normalizeEmail function

2.25.0 (Oct 22th 2024)

  • CheckHard.js becomes Checkard.js
  • Add isValidInteger() function
  • Add isValidFloat() function
  • Fix isArray() function when len param is equal to zero
  • Add more unit tests for isArray() function
  • Improve isInteger() function

2.24.0 (Feb 18th 2024)

  • Add emptyCheck parameter to isObject() function. Which default to false

2.23.0 (Jan 14th 2024)

  • Add isStringOfLength() function to check min and max length

2.22.0 (Dec 29th 2023)

  • Add emptyCheck parameter to isString() function. Which default to false

2.21.0 (Oct 18th 2023)

  • Add isJWT() function
  • Improve isArray() function
  • Update build tools
  • Code cleanup for lighter library
  • Add more unit tests

2.20.0 (Oct 11th 2023)

  • Add typeCheck parameter to isTimestamp() and isValidTimestamp() functions
  • Improve types using "type predicates" for ease of use with Typescript

2.19.2 (May 8th 2023)

  • Fix isNumber() & isTimestamp() issue with array of length one
  • Add more unit tests with arrays

2.19.1 (Oct 23th 2022)

  • Fix isTimestamp() issue with string values

2.19.0 (Aug 16th 2022)

  • isArray() function now checks if value is an array and optionally if it is of length =, <, >, <= or >= than limit

2.18.0 (May 9th 2022)

  • ContainsNumber() function now can take min and max parameters

2.17.2 (Jan 16th 2022)

  • Fix isValidTimestamp() function
  • Fix containsUpperCase() function
  • Fix containsLowerCase() function

2.17.1 (Nov 21th 2021)

  • Fix isValidDate() function
  • Fix declaration file for Typescript

2.17.0 (Nov 20th 2021)

  • Add isDate() function
  • Add isValidDate() function
  • Add isSlug() function
  • Add isHexadecimal() function
  • Add isTimestamp() function
  • Add isValidTimestamp() function
  • Add isValidNumber() function
  • Add containsUpperCase() function
  • Add containsLowerCase() function
  • Add containsSpecialCharacter() function
  • Add containsNumber() function

2.16.0 (Mar 28th 2021)

  • Add 'length' parameter to isArray() function to check the length of the array

2.15.0 (Jan 11th 2021)

  • Add isNil() function to test null and undefined values
  • Add isSymbol() to primitive checkers

2.14.0 (Jan 10th 2021)

  • Ch.js becomes CheckHard.js
  • Project transfered from LCluber to DWTechs

2.13.0 (Dec 6th 2020)

  • Add typeCheck parameter to isNumber(), isOdd(), isOrigin(), isPositive(), isNegative(), isPowerOfTwo() functions
  • Improved isNumber() performance

2.12.0 (Sep 13th 2020)

  • Add IsPowerOfTwo() function

2.11.0 (Sep 11th 2020)

  • IE9 compatibility for iife version
  • Update Typescript to version 4

2.10.1 (2020-04-05)

  • Add polyfill for typeof

2.10.0 (2019-12-25)

  • Add isemail and isipaddress functions

2.9.0 (2019-10-16)

  • Add typecheck to isregex function

2.8.0 (2019-10-15)

  • Add isregex function

2.7.1 (2019-10-06)

  • Update readme.md documentation

2.7.0 (2019-09-19)

  • Add commonjs version of Ch.js into dist folder

2.6.2 (2019-08-16)

  • ch.js is now in ES6 format

2.6.1 (2019-08-10)

  • Update description of the library

2.6.0 (2019-08-09)

  • isascii() function now accepts integers with string type

2.5.0 (2019-08-09)

  • isfloat() function now with type checking parameter

2.4.0 (2019-08-08)

  • isinteger() now with type checking parameter

2.3.1 (2019-07-31)

  • Update documentation api reference

2.3.0 (2019-07-31)

  • Add isHTMLEventAttribute() function

2.2.0 (2019-07-21)

  • Add isEven(), isOdd(), isOrigin(), isPositive(), isNegative() functions

2.1.1 (2019-07-16)

  • isfunction() function returns false if parameter is null or 0

2.1.0 (2019-07-16)

  • Adde isnumber() function

2.0.1 (2019-07-15)

  • isHtmlElement() & isNode() functions return false with null parameter
  • Fix isAscii() function
  • isObject() function now returns false with array as input

2.0.0 (2019-07-14)

  • Fix husky pre-push command
  • Library exports functions instead of a static class
  • Is.string function replaced by isString function, Is.array function replaced by isArray... please see README.md for exhaustive documentation

1.2.0 (2019-07-08)

  • Add Is.boolean() function

1.1.0 (Jun 19th 2019)

  • Added Is.htmlElement() function
  • Added Is.node() function

1.0.2 (Jun 03rd 2019)

  • Improved Is.json() function

1.0.1 (Jun 02nd 2019)

  • Updated README.md

1.0.0 (Jun 01st 2019)

  • Deleted Mouette.js dependency
  • Is.json() function now returns a boolean

0.1.2 (Apr 07th 2019)

  • Updated README.md with documentation and Yarn install command

0.1.1 (Feb 17th 2019)

  • Added Is.array() function
  • Added Is.float() function

0.1.0 (Dec 23th 2018)

  • Initial version from Wee.js library