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 |
Ensures that the |
|
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 |
|
public |
The |
|
public |
hasLengthOf(expectedLength: number): Predicate Ensures that the |
|
public |
Ensures that the |
|
public |
Ensures that the |
|
public | ||
public |
Ensures that the |
|
public |
Ensures that the |
|
public |
isGreaterThan(lowerBound: number): Predicate<number> Ensures that the |
|
public |
isGreaterThanOrEqualTo(lowerBound: number): Predicate<number> Ensures that the |
|
public |
Ensures that the |
|
public |
isInstanceOf(type: Constructor<T>): Predicate<T> Ensures that the |
|
public |
Ensures that the |
|
public |
isLessThan(upperBound: number): Predicate<number> Ensures that the |
|
public |
isLessThanOrEqualTo(upperBound: number): Predicate<number> Ensures that the |
|
public |
Ensures that the |
|
public |
Ensures that the |
|
public |
Ensures that the |
|
public |
Ensures that the |
|
public |
|
|
public |
Ensures that the |
|
public |
match(value: *): PatternMatcher<any, any, any, any> this function is experimental.
|
|
public |
Ensures that the |
|
public |
Ensures that the |
|
public |
Ensures that the |
|
public |
startsWith(prefix: string): Predicate<string> Ensures that the |
|
public |
Serialises the object to a JSON representation. |
Static Public
public TinyTypeOf(): * source
import {TinyTypeOf} from 'tiny-types/lib/TinyType'
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:
- when working with TinyTypes
- TinyType wrapping a single value definition can be a one-liner for TinyTypes representing a single value
- TinyType wrapping a single value definition prevents null and undefined when the single-line definition style is used
- TinyType wrapping a single value definition needs to extend the TinyType for types with more than one value
- TinyType wrapping a single value definition can be mixed and matched
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:
Name | Type | Attribute | Description |
predicates | ...Array<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:
Name | Type | Attribute | Description |
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:
Name | Type | Attribute | Description |
suffix | 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.
Return:
T | if the original value passes all the predicates, it's returned from the function |
Example:
import { ensure, isDefined } from 'tiny-types'
const username = 'jan-molak'
ensure('Username', username, isDefined());
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:
Name | Type | Attribute | Description |
expectedLength | number |
Example:
import { ensure, hasLengthOf, TinyType } from 'tiny-types';
class Tuple extends TinyType {
constructor(public readonly values: any[]) {
super();
ensure('Tuple', values, hasLengthOf(2));
}
}
import { ensure, hasLengthOf, TinyType } from 'tiny-types';
class Username extends TinyType {
constructor(public readonly value: string) {
super();
ensure('Username', value, hasLengthOf(8));
}
}
public isArray(): Predicate<T[]> source
import {isArray} from 'tiny-types/lib/predicates/isArray'
Ensures that the value
is an Array.
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.
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'
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:
Name | Type | Attribute | Description |
expectedValue | string | number | symbol | TinyType | object |
Example:
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));
}
}
import { ensure, isEqualTo, TinyType } from 'tiny-types';
class Admin extends TinyType {
constructor(public readonly id: number) {
ensure('Admin::id', id, isEqualTo(1));
}
}
public isFunction(): Predicate<Function> source
import {isFunction} from 'tiny-types/lib/predicates/isFunction'
Ensures that the value
is a Function.
Example:
import { ensure, isFunction, TinyType } from 'tiny-types';
function myFunction(callback: (error?: Error) => void): void {
ensure('callback', callback, isFunction());
}
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:
Name | Type | Attribute | Description |
lowerBound | 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));
}
}
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:
Name | Type | Attribute | Description |
lowerBound | 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));
}
}
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
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:
Name | Type | Attribute | Description |
type | Constructor<T> |
Example:
import { ensure, isInstanceOf, TinyType } from 'tiny-types';
class Birthday extends TinyType {
constructor(public readonly value: Date) {
ensure('Date', value, isInstanceOf(Date));
}
}
public isInteger(): Predicate<number> source
import {isInteger} from 'tiny-types/lib/predicates/isInteger'
Ensures that the value
is an integer 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:
Name | Type | Attribute | Description |
upperBound | 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:
Name | Type | Attribute | Description |
upperBound | number |
Example:
import { ensure, isLessThanOrEqualTo, TinyType } from 'tiny-types';
class InvestmentPeriod extends TinyType {
constructor(public readonly value: number) {
ensure('InvestmentPeriod', value, isLessThanOrEqualTo(50));
}
}
public isNotBlank(): Predicate<string> source
import {isNotBlank} from 'tiny-types/lib/predicates/isNotBlank'
Ensures that the value
is not an empty 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.
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:
Name | Type | Attribute | Description |
allowedValues | ...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.
Example:
import { ensure, isPlainObject } from 'tiny-types';
ensure('plain object', {}, isPlainObject());
public isRecord(value: unknown): boolean source
import {isRecord} from 'tiny-types/lib/objects/isRecord'
Params:
Name | Type | Attribute | Description |
value | unknown |
public isString(): Predicate<string> source
import {isString} from 'tiny-types/lib/predicates/isString'
Ensures that the value
is a 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'
Params:
Name | Type | Attribute | Description |
value | * |
Return:
PatternMatcher<any, any, any, any> |
public matches(expression: RegExp): Predicate<string> source
import {matches} from 'tiny-types/lib/predicates/matches'
Ensures that the value
matches RegExp.
Params:
Name | Type | Attribute | Description |
expression | RegExp |
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:
Name | Type | Attribute | Description |
predicates | 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(propertyName: *, predicates: ...*): Predicate<T> source
import {property} from 'tiny-types/lib/predicates/property'
Ensures that the property
of the value
meets the predicates
Params:
Name | Type | Attribute | Description |
propertyName | * | ||
predicates | ...* |
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)));
}
}
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:
Name | Type | Attribute | Description |
prefix | 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:
public toJSON(value: undefined): * source
import {toJSON} from 'tiny-types/lib/objects/toJSON'
Serialises the object to a JSON representation.
Params:
Name | Type | Attribute | Description |
value | undefined |
Return:
* |