Home Reference Source Test

Function

Static Public Summary
public

TinyTypeOf(): *

this function is experimental.

The TinyTypeOf can be used to define simple single-value TinyTypes on a single line.

public

and(predicates: ...Array<Predicate<T>>): Predicate<T>

Ensures that the value meets all the provided Predicates.

public

deprecated(message: string, log: Logger): *

A decorator to mark a class, method or function as deprecated and make it log a warning whenever it is used.

public

Ensures that the value ends with a given suffix.

public

ensure(name: string, value: T, predicates: ...Array<Predicate<T>>): T

The ensure function verifies if the value meets the specified {Predicate}s.

public

hasLengthOf(expectedLength: number): Predicate

Ensures that the value is of expectedLength.

public

isArray(): Predicate<T[]>

Ensures that the value is an Array.

public

Ensures that the value is a Boolean value.

public

Ensures that the value is defined as anything other than null or undefined.

public

isEqualTo(expectedValue: string | number | symbol | TinyType | object): Predicate<any>

Ensures that the value is equal to expectedValue.

public

Ensures that the value is greater than the lowerBound.

public

Ensures that the value is greater than or equal to the lowerBound.

public

isInRange(lowerBound: number, upperBound: number): Predicate<number>

Ensures that the value is greater than or equal to the lowerBound and less than or equal to the upperBound

public

isInstanceOf(type: Constructor<T>): Predicate<T>

Ensures that the value is an instance of type

public

Ensures that the value is an integer Number.

public

Ensures that the value is less than the upperBound.

public

Ensures that the value is less than or equal to the upperBound.

public

Ensures that the value is not an empty string.

public

Ensures that the value is a Number.

public

isOneOf(allowedValues: ...T[]): Predicate<T>

Ensures that the value is equal to one of the allowedValues

public

Ensures that the value is a plain Object.

public

Ensures that the value is a String.

public

match(value: *): PatternMatcher<any, any, any, any>

this function is experimental.
public

matches(expression: RegExp): Predicate<string>

Ensures that the value matches RegExp.

public

or(predicates: Predicate<T>): Predicate<T>

Ensures that the value meets at least one of the provided Predicates.

public

property(prop: *, predicates: ...*): Predicate<T>

Ensures that the property of the value meets the predicates

public

Ensures that the value starts with a given prefix.

Static Public

public TinyTypeOf(): * source

import {TinyTypeOf} from 'tiny-types/lib/TinyType'
this function is experimental.

The TinyTypeOf can be used to define simple single-value TinyTypes on a single line.

It contains a check preventing the constructor argument from being undefined (see isDefined);

Return:

*

a dynamically created base class your tiny type can extend from

Example:

class Username extends TinyTypeOf<string>() {}
class Age extends TinyTypeOf<number>() {}

Test:

public and(predicates: ...Array<Predicate<T>>): Predicate<T> source

import {and} from 'tiny-types/lib/predicates/and'

Ensures that the value meets all the provided Predicates.

Params:

NameTypeAttributeDescription
predicates ...Array<Predicate<T>>

Return:

Predicate<T>

Example:

import { and, ensure, isDefined, isGreaterThan, isInteger, TinyType } from 'tiny-types';

class AgeInYears extends TinyType {
    constructor(public readonly value: number) {
        ensure('AgeInYears', value, and(isDefined(), isInteger(), isGreaterThan(18));
    }
}

Test:

public deprecated(message: string, log: Logger): * source

import {deprecated} from 'tiny-types/lib/objects/deprecated'

A decorator to mark a class, method or function as deprecated and make it log a warning whenever it is used. Please see the tests for examples of usage.

Params:

NameTypeAttributeDescription
message string

describes the alternative implementation that should be used instead of the deprecated method/function/class

log Logger

a function that handles the printing of the message, such as console.warn

Return:

*

Test:

public endsWith(suffix: string): Predicate<string> source

import {endsWith} from 'tiny-types/lib/predicates/endsWith'

Ensures that the value ends with a given suffix.

Params:

NameTypeAttributeDescription
suffix string

Return:

Predicate<string>

Example:

import { endsWith, ensure, TinyType } from 'tiny-types';

class TextFileName extends TinyType {
    constructor(public readonly value: string) {
        super();

        ensure('TextFileName', value, endsWith('.txt'));
    }
}

Test:

public ensure(name: string, value: T, predicates: ...Array<Predicate<T>>): T source

import {ensure} from 'tiny-types/lib/ensure'

The ensure function verifies if the value meets the specified {Predicate}s.

Params:

NameTypeAttributeDescription
name string

the name of the value to check. This name will be included in the error message should the check fail

value T

the argument to check

predicates ...Array<Predicate<T>>

a list of predicates to check the value against

Return:

T

if the original value passes all the predicates, it's returned from the function

Example:

Basic usage
import { ensure, isDefined } from 'tiny-types'

const username = 'jan-molak'
ensure('Username', username, isDefined());
Ensuring validity of a domain object upon creation
import { TinyType, ensure, isDefined, isInteger, isInRange } from 'tiny-types'

class Age extends TinyType {
  constructor(public readonly value: number) {
    ensure('Age', value, isDefined(), isInteger(), isInRange(0, 125));
  }
}

public hasLengthOf(expectedLength: number): Predicate source

import {hasLengthOf} from 'tiny-types/lib/predicates/hasLengthOf'

Ensures that the value is of expectedLength. Applies to Strings, Arrays and anything that has a .length property.

This function is an alias for to property('length', isDefined(), isEqualTo(expectedLength))

Params:

NameTypeAttributeDescription
expectedLength number

Return:

Predicate

Example:

Array
import { ensure, hasLengthOf, TinyType } from 'tiny-types';

class Tuple extends TinyType {
  constructor(public readonly values: any[]) {
     super();
     ensure('Tuple', values, hasLengthOf(2));
  }
}
String
import { ensure, hasLengthOf, TinyType } from 'tiny-types';

class Username extends TinyType {
  constructor(public readonly value: string) {
     super();
     ensure('Username', value, hasLengthOf(8));
  }
}

Test:

public isArray(): Predicate<T[]> source

import {isArray} from 'tiny-types/lib/predicates/isArray'

Ensures that the value is an Array.

Return:

Predicate<T[]>

Example:

import { ensure, isArray, TinyType, TinyTypeOf } from 'tiny-types';

class Name extends TinyTypeOf<string>() {}

class Names extends TinyType {
  constructor(public readonly values: Name[]) {
     super();
     ensure('Names', values, isArray());
  }
}

Test:

public isBoolean(): Predicate<boolean> source

import {isBoolean} from 'tiny-types/lib/predicates/isBoolean'

Ensures that the value is a Boolean value.

Return:

Predicate<boolean>

Example:

import { ensure, isBoolean, TinyType } from 'tiny-types';

class MarketingOptIn extends TinyType {
    constructor(public readonly value: boolean) {
        ensure('MarketingOptIn', value, isBoolean());
    }
}

Test:

public isDefined(): Predicate<T> source

import {isDefined} from 'tiny-types/lib/predicates/isDefined'

Ensures that the value is defined as anything other than null or undefined.

Return:

Predicate<T>

Example:

import { ensure, isDefined, TinyType } from 'tiny-types';

class Name extends TinyType {
    constructor(public readonly value: string) {
      ensure('Name', value, isDefined());
    }
}

Test:

public isEqualTo(expectedValue: string | number | symbol | TinyType | object): Predicate<any> source

import {isEqualTo} from 'tiny-types/lib/predicates/isEqualTo'

Ensures that the value is equal to expectedValue. This Predicate is typically used in combination with other Predicates.

Params:

NameTypeAttributeDescription
expectedValue string | number | symbol | TinyType | object

Return:

Predicate<any>

Example:

Comparing Tiny Types
import { ensure, isEqualTo, TinyType, TinyTypeOf } from 'tiny-types';

class AccountId         extends TinyTypeOf<number>() {}
class Command           extends TinyTypeOf<AccountId>() {}
class UpgradeAccount    extends Command {}

class AccountsService {
    constructor(public readonly loggedInUser: AccountId) {}
    handle(command: Command) {
        ensure('AccountId', command.value, isEqualTo(this.loggedInUser));
    }
 }
Comparing primitives
import { ensure, isEqualTo, TinyType } from 'tiny-types';

class Admin extends TinyType {
    constructor(public readonly id: number) {
        ensure('Admin::id', id, isEqualTo(1));
    }
}

Test:

public isGreaterThan(lowerBound: number): Predicate<number> source

import {isGreaterThan} from 'tiny-types/lib/predicates/isGreaterThan'

Ensures that the value is greater than the lowerBound.

Params:

NameTypeAttributeDescription
lowerBound number

Return:

Predicate<number>

Example:

import { ensure, isGreaterThan, TinyType } from 'tiny-types';

class AgeInYears extends TinyType {
    constructor(public readonly value: number) {
        ensure('Age in years', value, isGreaterThan(0));
    }
}

Test:

public isGreaterThanOrEqualTo(lowerBound: number): Predicate<number> source

import {isGreaterThanOrEqualTo} from 'tiny-types/lib/predicates/isGreaterThanOrEqualTo'

Ensures that the value is greater than or equal to the lowerBound.

Params:

NameTypeAttributeDescription
lowerBound number

Return:

Predicate<number>

Example:

import { ensure, isGreaterThanOrEqualTo, TinyType } from 'tiny-types';

class AgeInYears extends TinyType {
    constructor(public readonly value: number) {
        ensure('Age in years', value, isGreaterThanOrEqualTo(18));
    }
}

Test:

public isInRange(lowerBound: number, upperBound: number): Predicate<number> source

import {isInRange} from 'tiny-types/lib/predicates/isInRange'

Ensures that the value is greater than or equal to the lowerBound and less than or equal to the upperBound

Params:

NameTypeAttributeDescription
lowerBound number
upperBound number

Return:

Predicate<number>

Example:

import { ensure, isInRange, TinyType } from 'tiny-types';

class InvestmentLengthInYears extends TinyType {
    constructor(public readonly value: number) {
        super();
        ensure('InvestmentLengthInYears', value, isInRange(1, 5));
    }
}

Test:

public isInstanceOf(type: Constructor<T>): Predicate<T> source

import {isInstanceOf} from 'tiny-types/lib/predicates/isInstanceOf'

Ensures that the value is an instance of type

Params:

NameTypeAttributeDescription
type Constructor<T>

Return:

Predicate<T>

Example:

import { ensure, isInstanceOf, TinyType } from 'tiny-types';

class Birthday extends TinyType {
    constructor(public readonly value: Date) {
        ensure('Date', value, isInstanceOf(Date));
    }
}

Test:

public isInteger(): Predicate<number> source

import {isInteger} from 'tiny-types/lib/predicates/isInteger'

Ensures that the value is an integer Number.

Return:

Predicate<number>

Example:

import { ensure, isInteger, TinyType } from 'tiny-types';

class AgeInYears extends TinyType {
    constructor(public readonly value: number) {
        ensure('Age in years', value, isInteger());
    }
}

Test:

public isLessThan(upperBound: number): Predicate<number> source

import {isLessThan} from 'tiny-types/lib/predicates/isLessThan'

Ensures that the value is less than the upperBound.

Params:

NameTypeAttributeDescription
upperBound number

Return:

Predicate<number>

Example:

import { ensure, isLessThan, TinyType } from 'tiny-types';

class InvestmentPeriodInYears extends TinyType {
    constructor(public readonly value: number) {
        ensure('Investment period in years', value, isLessThan(50));
    }
}

Test:

public isLessThanOrEqualTo(upperBound: number): Predicate<number> source

import {isLessThanOrEqualTo} from 'tiny-types/lib/predicates/isLessThanOrEqualTo'

Ensures that the value is less than or equal to the upperBound.

Params:

NameTypeAttributeDescription
upperBound number

Return:

Predicate<number>

Example:

import { ensure, isLessThanOrEqualTo, TinyType } from 'tiny-types';

class InvestmentPeriod extends TinyType {
    constructor(public readonly value: number) {
        ensure('InvestmentPeriod', value, isLessThanOrEqualTo(50));
    }
}

Test:

public isNotBlank(): Predicate<string> source

import {isNotBlank} from 'tiny-types/lib/predicates/isNotBlank'

Ensures that the value is not an empty string.

Return:

Predicate<string>

Example:

import { ensure, isString, TinyType } from 'tiny-types';

class FirstName extends TinyType {
    constructor(public readonly value: string) {
        ensure('FirstName', value, isNotBlank());
    }
}

public isNumber(): Predicate<number> source

import {isNumber} from 'tiny-types/lib/predicates/isNumber'

Ensures that the value is a Number.

Return:

Predicate<number>

Example:

import { ensure, isNumber, TinyType } from 'tiny-types';

class Percentage extends TinyType {
    constructor(public readonly value: number) {
        ensure('Percentage', value, isNumber());
    }
}

Test:

public isOneOf(allowedValues: ...T[]): Predicate<T> source

import {isOneOf} from 'tiny-types/lib/predicates/isOneOf'

Ensures that the value is equal to one of the allowedValues

Params:

NameTypeAttributeDescription
allowedValues ...T[]

Return:

Predicate<T>

Example:

import { ensure, isOneOf, TinyType } from 'tiny-types';

class StreetLight extends TinyType {
    constructor(public readonly value: string) {
        super();

        ensure('StreetLight', value, isOneOf('red', 'yellow', 'green'));
    }
}

Test:

public isPlainObject(): Predicate<string> source

import {isPlainObject} from 'tiny-types/lib/predicates/isPlainObject'

Ensures that the value is a plain Object. Based on Jon Schlinkert's implementation.

Return:

Predicate<string>

Example:

 import { ensure, isPlainObject } from 'tiny-types';

 ensure('plain object', {}, isPlainObject());

Test:

See:

public isString(): Predicate<string> source

import {isString} from 'tiny-types/lib/predicates/isString'

Ensures that the value is a String.

Return:

Predicate<string>

Example:

import { ensure, isString, TinyType } from 'tiny-types';

class FirstName extends TinyType {
    constructor(public readonly value: string) {
        ensure('FirstName', value, isString());
    }
}

Test:

public match(value: *): PatternMatcher<any, any, any, any> source

import {match} from 'tiny-types/lib/match'
this function is experimental.

Params:

NameTypeAttributeDescription
value *

Return:

PatternMatcher<any, any, any, any>

Test:

public matches(expression: RegExp): Predicate<string> source

import {matches} from 'tiny-types/lib/predicates/matches'

Ensures that the value matches RegExp.

Params:

NameTypeAttributeDescription
expression RegExp

Return:

Predicate<string>

Example:

import { ensure, matches, TinyType } from 'tiny-types';


class CompanyEmailAddress extends TinyType {
    constructor(public readonly value: string) {
        super();
        ensure('EmailAddress', value, matches(/[a-z]+\.[a-z]+@example\.org/));
    }
}

Test:

public or(predicates: Predicate<T>): Predicate<T> source

import {or} from 'tiny-types/lib/predicates/or'

Ensures that the value meets at least one of the provided Predicates.

Params:

NameTypeAttributeDescription
predicates Predicate<T>

Return:

Predicate<T>

Example:

import { ensure, isEqualTo, isGreaterThan, isLessThan, or } from 'tiny-type'l

class Percentage extends TinyType {
    constructor(public readonly value: number) {
        ensure('Percentage', value, or(isEqualTo(0), isGreaterThan(0)), or(isLessThan(100), isEqualTo(100));
    }
}

Test:

public property(prop: *, predicates: ...*): Predicate<T> source

import {property} from 'tiny-types/lib/predicates/property'

Ensures that the property of the value meets the predicates

Params:

NameTypeAttributeDescription
prop *
predicates ...*

Return:

Predicate<T>

Example:

import { ensure, isGreaterThan, property, TinyType } from 'tiny-types';

class Name extends TinyType {
    constructor(public readonly value: string) {
        super();
        ensure('Name', value, property('length', isGreaterThan(3)));
    }
}

Test:

public startsWith(prefix: string): Predicate<string> source

import {startsWith} from 'tiny-types/lib/predicates/startsWith'

Ensures that the value starts with a given prefix.

Params:

NameTypeAttributeDescription
prefix string

Return:

Predicate<string>

Example:

import { ensure, startsWith, TinyType } from 'tiny-types';

class Username extends TinyType {
    constructor(public readonly value: string) {
        super();

        ensure('Username', value, startsWith('usr'));
    }
}

Test: