Options
All
  • Public
  • Public/Protected
  • All
Menu

Index

Variables

histograph: Function = ...

Returns the histograph of an array as a Map. Makes no attempt to cope with deep equality; will fail for complex contents, as such.

import { histograph } from './jssm';

histograph( [0, 0, 1, 1, 2, 2, 1] ); // Map()
weighted_histo_key: Function = ...

Draws n weighted random samples, extracts a named key from each, and returns a histograph (Map) of how often each key value appeared. Useful for validating that a probabilistic transition distribution is roughly correct over many trials.

const opts = [
{ to: 'a', probability: 0.7 },
{ to: 'b', probability: 0.3 }
];

weighted_histo_key(1000, opts, 'probability', 'to');
// Map { 'a' => ~700, 'b' => ~300 }
param n

Number of samples to draw.

param opts

Non-empty array of weighted objects.

param prob_prop

Name of the numeric weight property.

param extract

Name of the property to extract from each sample for histogramming.

param rng

Optional random number generator.

returns

A Map from extracted key values to their occurrence counts.

weighted_rand_select: Function = ...

Selects a single item from a weighted array of objects using cumulative probability. Each object in the array should have a numeric property indicating its relative weight (defaults to 'probability'). Objects missing the property are treated as weight 1.

const opts = [
{ value: 'common', probability: 0.8 },
{ value: 'rare', probability: 0.2 }
];

weighted_rand_select(opts); // most often { value: 'common', ... }
param options

Non-empty array of objects to choose from.

param probability_property

Name of the numeric weight property on each object. Defaults to 'probability'.

param rng

Optional random number generator () => number in [0, 1). Defaults to Math.random.

returns

One element from options, chosen by weighted random selection.

throws

{TypeError} If options is not a non-empty array of objects.

weighted_sample_select: Function = ...

Draws n weighted random samples from an array of objects. Each draw is independent (with replacement), delegating to weighted_rand_select.

const opts = [
{ value: 'a', probability: 0.9 },
{ value: 'b', probability: 0.1 }
];

weighted_sample_select(3, opts, 'probability');
// e.g. [ { value: 'a', ... }, { value: 'a', ... }, { value: 'b', ... } ]
param n

Number of samples to draw.

param options

Non-empty array of weighted objects.

param probability_property

Name of the numeric weight property.

param rng

Optional random number generator.

returns

An array of n independently selected items.

Functions

  • arr_uniq_p<T>(el: T, i: number, source: T[]): boolean
  • Predicate for validating an array for uniqueness. Returns true when el is the first occurrence in source, false otherwise. Intended for use as an Array.filter callback. Not generally meant for external use.

    [1, 2, 2, 3].filter(arr_uniq_p);  // [1, 2, 3]
    

    Type Parameters

    • T

    Parameters

    • el: T

      The current element being tested.

    • i: number

      The index of the current element.

    • source: T[]

      The full array being filtered.

    Returns boolean

    true if el is the first occurrence in source.

  • array_box_if_string(n: any): any
  • Wraps a string in an array, or passes through if already non-string. Used to normalize arguments that accept either a single state name or an array of state names.

    array_box_if_string('hello');    // ['hello']
    array_box_if_string(['a','b']); // ['a','b']

    Parameters

    • n: any

      A string to box, or a value to pass through unchanged.

    Returns any

    The input wrapped in an array if it was a string, otherwise the input unchanged.

  • find_repeated<T>(arr: T[]): [T, number][]
  • Lists all repeated items in an array along with their counts. Subject to matching rules of Map. NaN is manually removed because of conflict rules around unique. Because these are compared with === and because arrays and objects never match that way unless they're the same object, arrays and objects are never considered repeats.

    find_repeated<string>([ ]);                     // []
    find_repeated<string>([ "one" ]); // []
    find_repeated<string>([ "one", "two" ]); // []
    find_repeated<string>([ "one", "one" ]); // [ ["one", 2] ]
    find_repeated<string>([ "one", "two", "one" ]); // [ ["one", 2] ]
    find_repeated<number>([ 0, NaN, 0, NaN ]); // [ [0, 2] ]

    Type Parameters

    • T

    Parameters

    • arr: T[]

    Returns [T, number][]

  • gen_splitmix32(a?: number): (() => number)
  • hook_name(from: string, to: string): string
  • Internal method generating composite keys for transition hooks by JSON-serializing a [from, to] state pair. Used to look up hooks registered on a specific edge. Not meant for external use.

    hook_name('Red', 'Green');  // '["Red","Green"]'
    

    Parameters

    • from: string

      The source state name.

    • to: string

      The target state name.

    Returns string

    A deterministic JSON string key for the [from, to] pair.

  • name_bind_prop_and_state(prop: string, state: string): string
  • Internal method generating composite keys for the hook lookup map by JSON-serializing a [property, state] pair. Not meant for external use.

    name_bind_prop_and_state('color', 'Red');  // '["color","Red"]'
    
    throws

    {JssmError} If either argument is not a string.

    Parameters

    • prop: string

      The property name (e.g. a data key or hook category).

    • state: string

      The state name to bind to.

    Returns string

    A deterministic JSON string key for the [prop, state] pair.

  • named_hook_name(from: string, to: string, action: string): string
  • Internal method generating composite keys for named-action hooks by JSON-serializing a [from, to, action] triple. Used to look up hooks registered on a specific action-labeled edge. Not meant for external use.

    named_hook_name('Red', 'Green', 'next');  // '["Red","Green","next"]'
    

    Parameters

    • from: string

      The source state name.

    • to: string

      The target state name.

    • action: string

      The action label on the edge.

    Returns string

    A deterministic JSON string key for the [from, to, action] triple.

  • seq(n: number): number[]
  • Returns, for a non-negative integer argument n, the series [0 .. n].

    import { seq } from './jssm';

    seq(5); // [0, 1, 2, 3, 4]
    seq(0); // []

    Parameters

    • n: number

    Returns number[]

  • sleep(ms: number): Promise<unknown>
  • Returns a Promise that resolves after ms milliseconds. Useful for inserting delays in async test flows or demos.

    await sleep(100);  // pauses execution for 100ms
    

    Parameters

    • ms: number

      Number of milliseconds to wait before resolving.

    Returns Promise<unknown>

    A Promise<void> that resolves after the timeout.

  • unique<T>(arr: T[]): T[]
  • Reduces an array to its unique contents. Compares with === and makes no effort to deep-compare contents; two matching arrays or objects contained will be treated as distinct, according to javascript rules. This also means that NaNs will be dropped, because they do not self-compare.

    unique( [] );                     // []
    unique( [0,0] ); // [0]
    unique( [0,1,2, 0,1,2, 0,1,2] ); // [0,1,2]
    unique( [ [1], [1] ] ); // [ [1], [1] ] because arrays don't match
    unique( [0,NaN,2] ); // [0,2]

    Type Parameters

    • T

    Parameters

    • arr: T[]

    Returns T[]

Generated using TypeDoc