Options
All
  • Public
  • Public/Protected
  • All
Menu

perusal-immutable

Variables

Const defs

defs: object
member

{[key: string]: Spec} defs holds all user-made definitions.

Type declaration

  • [key: string]: Spec

Const even

even: Pred = pred('is even?', (number: number) => {return number % 2 === 0 && typeof number === 'number';})

Predicate checking if value is even.

Const invalid

invalid: unique symbol = Symbol('invalid')

Symbol used to express that a value did not satisfy a certain spec.

Const isBool

isBool: Pred = pred('is a boolean?', (maybeBool: any) => typeof maybeBool === 'boolean')

Predicate checking if value is a number.

Const isFn

isFn: Pred = pred('is a function?', (maybeFn: any) => typeof maybeFn === 'function')

Predicate checking if value is a function.

Const isNumber

isNumber: Pred = pred('is a number?', (maybeNumber: any) => typeof maybeNumber === 'number')

Predicate checking if value is a number.

Const isObject

isObject: Pred = pred('is an object?',(maybeObject: any) => typeof maybeObject === 'object')

Predicate checking if value is a number.

Const isString

isString: Pred = pred('is a string?', (maybeString: any) => typeof maybeString === 'string')

Predicate checking if value is a number.

Const isSymbol

isSymbol: Pred = pred('is a symbol?', (maybeSymbol: any) => typeof maybeSymbol === 'symbol')

Predicate checking if value is a symbol.

Const negative

negative: Pred = pred('is negative?', (number: number) => {return typeof number === 'number' && number < 0;})

Predicate checking if value is negative.

Const odd

odd: Pred = pred('is odd?', (number: number) => {return number % 2 === 1 && typeof number === 'number';})

Predicate checking if value is odd.

Const positive

positive: Pred = pred('is positive?', (number: number) => {return typeof number === 'number' && number > 0;})

Predicate checking if value is positive.

Const zero

zero: Pred = pred('is zero?', (number: number) => {return typeof number === 'number' && number === 0;})

Predicate checking if value is zero.

Functions

Private addToSpecs

  • addToSpecs(object: any, key: string, value: true | Symbol): void
  • Adds a certain value to a certain key object's specs, initializing specs if necessary.

    Parameters

    • object: any

      The object to be added to.

    • key: string

      The key for the spec to be added to the object's spec (typically the spec's name).

    • value: true | Symbol

      Value representing if the object satisfied the keys.

    Returns void

and

  • and(name: string, ...specs: Spec[]): And
  • Factory function for And specs.

    Parameters

    • name: string

      Name of the and spec. Important to set this to a human readable (meaningful) string, since this is what will get printed out in explain.

    • Rest ...specs: Spec[]

      Specs that have all to be satisfied to fulfill this spec.

    Returns And

    Returns an And spec representing the conjunction of the given specs.

assert

  • assert(value: any, spec: Spec | string): any
  • Asserts a spec on a given value. Returns the value if value passes specification, returns perusal-immutable.invalid otherwise.

    Parameters

    • value: any

      The value to be asserted.

    • spec: Spec | string

      The spec to be used.

    Returns any

    Returns the value if value passes specification, returns perusal-immutable.invalid otherwise.

define

  • define(name: string, spec: Spec): void
  • Defines a new spec, allowing it to be referenced as such in the future.

    Parameters

    • name: string

      The name that will be used to refer to the spec in the future.

    • spec: Spec

      The spec to be defined.

    Returns void

every

  • Factory function for Every specs.

    Parameters

    • name: string

      Name of the every spec. Important to set this to a human readable (meaningful) string, since this is what will get printed out in explain.

    • spec: Spec

      Specs that have all to be satisfied to fulfill this spec.

    Returns Every

    Returns an Every spec representing that all elements in the collection have to satisfy the provided spec..

explain

  • explain(value: any, spec: Spec | string): void
  • Explains why a value passes/fails a specification.

    Parameters

    • value: any

      The value to be checked.

    • spec: Spec | string

      The speficiation to be used.

    Returns void

explainIfInvalid

  • explainIfInvalid(value: any, spec: Spec | string): void
  • Explains why a value fails a specification. Like explain, but only produces output if invalid.

    Parameters

    • value: any

      The value to be checked.

    • spec: Spec | string

      The specificiation to be used.

    Returns void

getSpec

  • getSpec(maybeSpec: string | Spec): Spec
  • Defines a new spec, allowing it to be referenced as such in the future.

    Parameters

    • maybeSpec: string | Spec

      The name of the spec as previously defined in define or the spec itself.

    Returns Spec

isValid

  • isValid(value: any, spec: Spec | string): boolean
  • Checks if a value is valid given a specificiation.

    Parameters

    • value: any

      The value to be checked.

    • spec: Spec | string

      The speficiation to be used.

    Returns boolean

    Returns boolean representing if the value if value passes spec.

keys

  • keys(name: string, specs: object): Keys
  • Factory function for Keys specs.

    Parameters

    • name: string

      Name of the key spec. Important to set this to a human readable (meaningful) string, since this is what will get printed out in explain.

    • specs: object

      Specs that have all to be satisfied to fulfill this spec.

      • [key: string]: Spec

    Returns Keys

    Returns a Keys spec requiring the values of the input to satisfy the keys initialized in this spec.

nullable

  • Factory function for Nullable specs.

    Parameters

    • spec: Spec

      Spec that has to be fulfiled if input value is not undefined.

    Returns Nullable

    Returns a Nullable spec requiring the input value to satisfy the provided spec if it is not null.

oneOf

  • oneOf(name: string, ...values: any[]): OneOf
  • Factory function for OneOf specs. Consider using the spread operator if you start with a collection!

    Parameters

    • name: string

      Name of the and spec. Important to set this to a human readable (meaningful) string, since this is what will get printed out in explain.

    • Rest ...values: any[]

      user-defined values used to assert future values.

    Returns OneOf

    Returns a OneOf spec requiring the input value to be within one of the user pre-defined values.

optional

  • Factory function for Optional specs.

    Parameters

    • spec: Spec

      Spec that has to be fulfiled if input value is not undefined.

    Returns Optional

    Returns a Optional spec requiring the input value to satisfy the provided spec if it is defined.

or

  • or(name: string, ...specs: Spec[]): Or
  • Factory function for Or specs.

    Parameters

    • name: string

      Name of the and spec. Important to set this to a human readable (meaningful) string, since this is what will get printed out in explain.

    • Rest ...specs: Spec[]

      Specs, where one of them has to be satisfied to fulfill this spec.

    Returns Or

    Returns an Or spec representing the disjunction of the given specs.

pred

  • pred(name: string, fn: function): Pred
  • Factory function for Pred specs. Note that this does not check if input function is a valid boolean function (since program is unaware of user input domain), thus the responsibility is on the user to verify that the input fn is a function. However, during runtime, should a non-boolean output be detected, assert throws an error, which may help in debugging.

    throws

    Throws an error if name is not a valid string or fn is not a valid function.

    throws

    Throws an error if name is not a valid string or fn is not a valid function.

    Parameters

    • name: string

      Name of the key spec. Important to set this to a human readable (meaningful) string, since this is what will get printed out in explain.

    • fn: function

      Predicate function that returns a boolean value on input within user-desired domain.

        • (value: any): boolean
        • Parameters

          • value: any

          Returns boolean

    Returns Pred

    Returns a Pred spec requiring the value to return true when fed to fn.

Generated using TypeDoc