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 }
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', ... }
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', ... } ]
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]
The current element being tested.
The index of the current element.
The full array being filtered.
true if el is the first occurrence in source.
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']
A string to box, or a value to pass through unchanged.
The input wrapped in an array if it was a string, otherwise the input unchanged.
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] ]
Creates a SplitMix32 random generator. Used by the randomness test suite.
Sourced from bryc: https://github.com/bryc/code/blob/master/jshash/PRNGs.md#splitmix32
Replaces the Mulberry generator, which was found to have problems
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"]'
The source state name.
The target state name.
A deterministic JSON string key for the [from, to] pair.
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"]'
The property name (e.g. a data key or hook category).
The state name to bind to.
A deterministic JSON string key for the [prop, state] pair.
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"]'
The source state name.
The target state name.
The action label on the edge.
A deterministic JSON string key for the [from, to, action] triple.
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); // []
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
Number of milliseconds to wait before resolving.
A Promise<void> that resolves after the timeout.
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]
Generated using TypeDoc
Returns the histograph of an array as a
Map. Makes no attempt to cope with deep equality; will fail for complex contents, as such.