jssm 5.65.2 → 5.65.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -51,13 +51,121 @@ declare function arrow_left_kind(arrow: JssmArrow): JssmArrowKind;
51
51
  declare function arrow_right_kind(arrow: JssmArrow): JssmArrowKind;
52
52
  /*********
53
53
  *
54
- * Internal convenience method for alting out an object as the options call.
55
- * Not generally meant for external use.
54
+ * This method wraps the parser call that comes from the peg grammar,
55
+ * {@link parse}. Generally neither this nor that should be used directly
56
+ * unless you mean to develop plugins or extensions for the machine.
57
+ *
58
+ * Parses the intermediate representation of a compiled string down to a
59
+ * machine configuration object. If you're using this (probably don't,) you're
60
+ * probably also using {@link compile} and {@link Machine.constructor}.
61
+ *
62
+ * ```typescript
63
+ * import { parse, compile, Machine } from './jssm';
64
+ *
65
+ * const intermediate = wrap_parse('a -> b;', {});
66
+ * // [ {key:'transition', from:'a', se:{kind:'->',to:'b'}} ]
67
+ *
68
+ * const cfg = compile(intermediate);
69
+ * // { start_states:['a'], transitions: [{ from:'a', to:'b', kind:'legal', forced_only:false, main_path:false }] }
70
+ *
71
+ * const machine = new Machine(cfg);
72
+ * // Machine { _instance_name: undefined, _state: 'a', ...
73
+ * ```
74
+ *
75
+ * This method is mostly for plugin and intermediate tool authors, or people
76
+ * who need to work with the machine's intermediate representation.
77
+ *
78
+ * # Hey!
79
+ *
80
+ * Most people looking at this want either the `sm` operator or method `from`,
81
+ * which perform all the steps in the chain. The library's author mostly uses
82
+ * operator `sm`, and mostly falls back to `.from` when needing to parse
83
+ * strings dynamically instead of from template literals.
84
+ *
85
+ * Operator {@link sm}:
86
+ *
87
+ * ```typescript
88
+ * import { sm } from './jssm';
89
+ *
90
+ * const switch = sm`on <=> off;`;
91
+ * ```
92
+ *
93
+ * Method {@link from}:
94
+ *
95
+ * ```typescript
96
+ * import * as jssm from './jssm';
97
+ *
98
+ * const toggle = jssm.from('up <=> down;');
99
+ * ```
100
+ *
101
+ * `wrap_parse` itself is an internal convenience method for alting out an
102
+ * object as the options call. Not generally meant for external use.
56
103
  *
57
104
  */
58
105
  declare function wrap_parse(input: string, options?: Object): any;
106
+ /*********
107
+ *
108
+ * Compile a machine's JSON intermediate representation to a config object. If
109
+ * you're using this (probably don't,) you're probably also using
110
+ * {@link parse} to get the IR, and the object constructor
111
+ * {@link Machine.construct} to turn the config object into a workable machine.
112
+ *
113
+ * ```typescript
114
+ * import { parse, compile, Machine } from './jssm';
115
+ *
116
+ * const intermediate = parse('a -> b;');
117
+ * // [ {key:'transition', from:'a', se:{kind:'->',to:'b'}} ]
118
+ *
119
+ * const cfg = compile(intermediate);
120
+ * // { start_states:['a'], transitions: [{ from:'a', to:'b', kind:'legal', forced_only:false, main_path:false }] }
121
+ *
122
+ * const machine = new Machine(cfg);
123
+ * // Machine { _instance_name: undefined, _state: 'a', ...
124
+ * ```
125
+ *
126
+ * This method is mostly for plugin and intermediate tool authors, or people
127
+ * who need to work with the machine's intermediate representation.
128
+ *
129
+ * # Hey!
130
+ *
131
+ * Most people looking at this want either the `sm` operator or method `from`,
132
+ * which perform all the steps in the chain. The library's author mostly uses
133
+ * operator `sm`, and mostly falls back to `.from` when needing to parse
134
+ * strings dynamically instead of from template literals.
135
+ *
136
+ * Operator {@link sm}:
137
+ *
138
+ * ```typescript
139
+ * import { sm } from './jssm';
140
+ *
141
+ * const switch = sm`on <=> off;`;
142
+ * ```
143
+ *
144
+ * Method {@link from}:
145
+ *
146
+ * ```typescript
147
+ * import * as jssm from './jssm';
148
+ *
149
+ * const toggle = jssm.from('up <=> down;');
150
+ * ```
151
+ *
152
+ */
59
153
  declare function compile<mDT>(tree: JssmParseTree): JssmGenericConfig<mDT>;
154
+ /*********
155
+ *
156
+ * An internal convenience wrapper for parsing then compiling a machine string.
157
+ * Not generally meant for external use. Please see {@link compile} or
158
+ * {@link sm}.
159
+ *
160
+ */
60
161
  declare function make<mDT>(plan: string): JssmGenericConfig<mDT>;
162
+ /*********
163
+ *
164
+ * An internal method meant to take a series of declarations and fold them into
165
+ * a single multi-faceted declaration, in the process of building a state. Not
166
+ * generally meant for external use.
167
+ *
168
+ */
61
169
  declare function transfer_state_properties(state_decl: JssmStateDeclaration): JssmStateDeclaration;
62
170
  declare class Machine<mDT> {
63
171
  _state: StateType;
@@ -177,6 +285,39 @@ declare class Machine<mDT> {
177
285
  instance_name(): string | undefined;
178
286
  sm(template_strings: TemplateStringsArray, ...remainder: any[]): Machine<mDT>;
179
287
  }
288
+ /*********
289
+ *
290
+ * Create a state machine from a template string. This is one of the two main
291
+ * paths for working with JSSM, alongside {@link from}.
292
+ *
293
+ * Use this method when you want to work directly and conveniently with a
294
+ * constant template expression. Use `.from` when you want to pull from
295
+ * dynamic strings.
296
+ *
297
+ *
298
+ * ```typescript
299
+ * import * as jssm from './jssm';
300
+ *
301
+ * const switch = jssm.from('on <=> off;');
302
+ * ```
303
+ *
304
+ */
180
305
  declare function sm<mDT>(template_strings: TemplateStringsArray, ...remainder: any[]): Machine<mDT>;
306
+ /*********
307
+ *
308
+ * Create a state machine from an implementation string. This is one of the
309
+ * two main paths for working with JSSM, alongside {@link sm}.
310
+ *
311
+ * Use this method when you want to conveniently pull a state machine from a
312
+ * string dynamically. Use operator `sm` when you just want to work with a
313
+ * template expression.
314
+ *
315
+ * ```typescript
316
+ * import * as jssm from './jssm';
317
+ *
318
+ * const switch = jssm.from('on <=> off;');
319
+ * ```
320
+ *
321
+ */
181
322
  declare function from<mDT>(MachineAsString: string, ExtraConstructorFields?: Partial<JssmGenericConfig<mDT>> | undefined): Machine<mDT>;
182
323
  export { version, transfer_state_properties, Machine, make, wrap_parse as parse, compile, sm, from, arrow_direction, arrow_left_kind, arrow_right_kind, seq, weighted_rand_select, histograph, weighted_sample_select, weighted_histo_key, shapes, gviz_shapes, named_colors };
package/dist/es6/jssm.js CHANGED
@@ -222,13 +222,66 @@ function makeTransition(this_se, from, to, isRight, _wasList, _wasIndex) {
222
222
  }
223
223
  /*********
224
224
  *
225
- * Internal convenience method for alting out an object as the options call.
226
- * Not generally meant for external use.
225
+ * This method wraps the parser call that comes from the peg grammar,
226
+ * {@link parse}. Generally neither this nor that should be used directly
227
+ * unless you mean to develop plugins or extensions for the machine.
228
+ *
229
+ * Parses the intermediate representation of a compiled string down to a
230
+ * machine configuration object. If you're using this (probably don't,) you're
231
+ * probably also using {@link compile} and {@link Machine.constructor}.
232
+ *
233
+ * ```typescript
234
+ * import { parse, compile, Machine } from './jssm';
235
+ *
236
+ * const intermediate = wrap_parse('a -> b;', {});
237
+ * // [ {key:'transition', from:'a', se:{kind:'->',to:'b'}} ]
238
+ *
239
+ * const cfg = compile(intermediate);
240
+ * // { start_states:['a'], transitions: [{ from:'a', to:'b', kind:'legal', forced_only:false, main_path:false }] }
241
+ *
242
+ * const machine = new Machine(cfg);
243
+ * // Machine { _instance_name: undefined, _state: 'a', ...
244
+ * ```
245
+ *
246
+ * This method is mostly for plugin and intermediate tool authors, or people
247
+ * who need to work with the machine's intermediate representation.
248
+ *
249
+ * # Hey!
250
+ *
251
+ * Most people looking at this want either the `sm` operator or method `from`,
252
+ * which perform all the steps in the chain. The library's author mostly uses
253
+ * operator `sm`, and mostly falls back to `.from` when needing to parse
254
+ * strings dynamically instead of from template literals.
255
+ *
256
+ * Operator {@link sm}:
257
+ *
258
+ * ```typescript
259
+ * import { sm } from './jssm';
260
+ *
261
+ * const switch = sm`on <=> off;`;
262
+ * ```
263
+ *
264
+ * Method {@link from}:
265
+ *
266
+ * ```typescript
267
+ * import * as jssm from './jssm';
268
+ *
269
+ * const toggle = jssm.from('up <=> down;');
270
+ * ```
271
+ *
272
+ * `wrap_parse` itself is an internal convenience method for alting out an
273
+ * object as the options call. Not generally meant for external use.
227
274
  *
228
275
  */
229
276
  function wrap_parse(input, options) {
230
277
  return parse(input, options || {});
231
278
  }
279
+ /*********
280
+ *
281
+ * Internal method performing one step in compiling rules for transitions. Not
282
+ * generally meant for external use.
283
+ *
284
+ */
232
285
  function compile_rule_transition_step(acc, from, to, this_se, next_se) {
233
286
  const edges = [];
234
287
  const uFrom = (Array.isArray(from) ? from : [from]), uTo = (Array.isArray(to) ? to : [to]);
@@ -252,9 +305,21 @@ function compile_rule_transition_step(acc, from, to, this_se, next_se) {
252
305
  return new_acc;
253
306
  }
254
307
  }
308
+ /*********
309
+ *
310
+ * Internal method performing one step in compiling rules for transitions. Not
311
+ * generally meant for external use.
312
+ *
313
+ */
255
314
  function compile_rule_handle_transition(rule) {
256
315
  return compile_rule_transition_step([], rule.from, rule.se.to, rule.se, rule.se.se);
257
316
  }
317
+ /*********
318
+ *
319
+ * Internal method performing one step in compiling rules for transitions. Not
320
+ * generally meant for external use.
321
+ *
322
+ */
258
323
  function compile_rule_handler(rule) {
259
324
  if (rule.key === 'transition') {
260
325
  return { agg_as: 'transition', val: compile_rule_handle_transition(rule) };
@@ -283,6 +348,53 @@ function compile_rule_handler(rule) {
283
348
  }
284
349
  throw new JssmError(undefined, `compile_rule_handler: Unknown rule: ${JSON.stringify(rule)}`);
285
350
  }
351
+ /*********
352
+ *
353
+ * Compile a machine's JSON intermediate representation to a config object. If
354
+ * you're using this (probably don't,) you're probably also using
355
+ * {@link parse} to get the IR, and the object constructor
356
+ * {@link Machine.construct} to turn the config object into a workable machine.
357
+ *
358
+ * ```typescript
359
+ * import { parse, compile, Machine } from './jssm';
360
+ *
361
+ * const intermediate = parse('a -> b;');
362
+ * // [ {key:'transition', from:'a', se:{kind:'->',to:'b'}} ]
363
+ *
364
+ * const cfg = compile(intermediate);
365
+ * // { start_states:['a'], transitions: [{ from:'a', to:'b', kind:'legal', forced_only:false, main_path:false }] }
366
+ *
367
+ * const machine = new Machine(cfg);
368
+ * // Machine { _instance_name: undefined, _state: 'a', ...
369
+ * ```
370
+ *
371
+ * This method is mostly for plugin and intermediate tool authors, or people
372
+ * who need to work with the machine's intermediate representation.
373
+ *
374
+ * # Hey!
375
+ *
376
+ * Most people looking at this want either the `sm` operator or method `from`,
377
+ * which perform all the steps in the chain. The library's author mostly uses
378
+ * operator `sm`, and mostly falls back to `.from` when needing to parse
379
+ * strings dynamically instead of from template literals.
380
+ *
381
+ * Operator {@link sm}:
382
+ *
383
+ * ```typescript
384
+ * import { sm } from './jssm';
385
+ *
386
+ * const switch = sm`on <=> off;`;
387
+ * ```
388
+ *
389
+ * Method {@link from}:
390
+ *
391
+ * ```typescript
392
+ * import * as jssm from './jssm';
393
+ *
394
+ * const toggle = jssm.from('up <=> down;');
395
+ * ```
396
+ *
397
+ */
286
398
  function compile(tree) {
287
399
  const results = {
288
400
  graph_layout: [],
@@ -340,9 +452,23 @@ function compile(tree) {
340
452
  });
341
453
  return result_cfg;
342
454
  }
455
+ /*********
456
+ *
457
+ * An internal convenience wrapper for parsing then compiling a machine string.
458
+ * Not generally meant for external use. Please see {@link compile} or
459
+ * {@link sm}.
460
+ *
461
+ */
343
462
  function make(plan) {
344
463
  return compile(wrap_parse(plan));
345
464
  }
465
+ /*********
466
+ *
467
+ * An internal method meant to take a series of declarations and fold them into
468
+ * a single multi-faceted declaration, in the process of building a state. Not
469
+ * generally meant for external use.
470
+ *
471
+ */
346
472
  function transfer_state_properties(state_decl) {
347
473
  state_decl.declarations.map((d) => {
348
474
  switch (d.key) {
@@ -1073,6 +1199,23 @@ class Machine {
1073
1199
  return sm(template_strings, ...remainder);
1074
1200
  }
1075
1201
  }
1202
+ /*********
1203
+ *
1204
+ * Create a state machine from a template string. This is one of the two main
1205
+ * paths for working with JSSM, alongside {@link from}.
1206
+ *
1207
+ * Use this method when you want to work directly and conveniently with a
1208
+ * constant template expression. Use `.from` when you want to pull from
1209
+ * dynamic strings.
1210
+ *
1211
+ *
1212
+ * ```typescript
1213
+ * import * as jssm from './jssm';
1214
+ *
1215
+ * const switch = jssm.from('on <=> off;');
1216
+ * ```
1217
+ *
1218
+ */
1076
1219
  function sm(template_strings, ...remainder /* , arguments */) {
1077
1220
  // foo`a${1}b${2}c` will come in as (['a','b','c'],1,2)
1078
1221
  // this includes when a and c are empty strings
@@ -1086,6 +1229,22 @@ function sm(template_strings, ...remainder /* , arguments */) {
1086
1229
  /* eslint-enable prefer-rest-params */
1087
1230
  )));
1088
1231
  }
1232
+ /*********
1233
+ *
1234
+ * Create a state machine from an implementation string. This is one of the
1235
+ * two main paths for working with JSSM, alongside {@link sm}.
1236
+ *
1237
+ * Use this method when you want to conveniently pull a state machine from a
1238
+ * string dynamically. Use operator `sm` when you just want to work with a
1239
+ * template expression.
1240
+ *
1241
+ * ```typescript
1242
+ * import * as jssm from './jssm';
1243
+ *
1244
+ * const switch = jssm.from('on <=> off;');
1245
+ * ```
1246
+ *
1247
+ */
1089
1248
  function from(MachineAsString, ExtraConstructorFields) {
1090
1249
  const to_decorate = make(MachineAsString);
1091
1250
  if (ExtraConstructorFields !== undefined) {
@@ -1,10 +1,52 @@
1
+ /*******
2
+ *
3
+ * Predicate for validating an array for uniqueness. Not generally meant for
4
+ * external use.
5
+ *
6
+ */
1
7
  declare function arr_uniq_p<T>(el: T, i: number, source: T[]): boolean;
2
8
  declare const array_box_if_string: (n: any) => any;
3
9
  declare const weighted_rand_select: Function;
4
- declare const seq: Function;
10
+ /*******
11
+ *
12
+ * Returns, for a non-negative integer argument `n`, the series `[0 .. n]`.
13
+ *
14
+ * ```typescript
15
+ * import { seq } from './jssm';
16
+ *
17
+ * seq(5); // [0, 1, 2, 3, 4]
18
+ * seq(0); // []
19
+ * ```
20
+ *
21
+ */
22
+ declare function seq(n: number): number[];
23
+ /*******
24
+ *
25
+ * Returns the histograph of an array as a `Map`. Makes no attempt to cope
26
+ * with deep equality; will fail for complex contents, as such.
27
+ *
28
+ * ```typescript
29
+ * import { histograph } from './jssm';
30
+ *
31
+ * histograph( [0, 0, 1, 1, 2, 2, 1] ); // Map()
32
+ * ```
33
+ *
34
+ */
5
35
  declare const histograph: Function;
6
36
  declare const weighted_sample_select: Function;
7
37
  declare const weighted_histo_key: Function;
38
+ /*******
39
+ *
40
+ * Internal method generating names for edges for the hook lookup map. Not
41
+ * meant for external use.
42
+ *
43
+ */
8
44
  declare const hook_name: (from: string, to: string) => string;
45
+ /*******
46
+ *
47
+ * Internal method generating names for actions for the hook lookup map. Not
48
+ * meant for external use.
49
+ *
50
+ */
9
51
  declare const named_hook_name: (from: string, to: string, action: string) => string;
10
52
  export { seq, arr_uniq_p, histograph, weighted_histo_key, weighted_rand_select, weighted_sample_select, array_box_if_string, hook_name, named_hook_name };
@@ -1,3 +1,9 @@
1
+ /*******
2
+ *
3
+ * Predicate for validating an array for uniqueness. Not generally meant for
4
+ * external use.
5
+ *
6
+ */
1
7
  function arr_uniq_p(el, i, source) {
2
8
  return source.indexOf(el) === i;
3
9
  }
@@ -17,8 +23,41 @@ const weighted_rand_select = (options, probability_property = 'probability') =>
17
23
  return options[cursor - 1];
18
24
  };
19
25
  /* eslint-enable flowtype/no-weak-types */
20
- const seq = (n) => (new Array(n)).fill(true)
21
- .map((_, i) => i);
26
+ /*******
27
+ *
28
+ * Returns, for a non-negative integer argument `n`, the series `[0 .. n]`.
29
+ *
30
+ * ```typescript
31
+ * import { seq } from './jssm';
32
+ *
33
+ * seq(5); // [0, 1, 2, 3, 4]
34
+ * seq(0); // []
35
+ * ```
36
+ *
37
+ */
38
+ function seq(n) {
39
+ if (!(Number.isInteger(n))) {
40
+ throw new TypeError('seq/1 takes a non-negative integer n as an argument');
41
+ }
42
+ if (n < 0) {
43
+ throw new TypeError('seq/1 takes a non-negative integer n as an argument');
44
+ }
45
+ return (new Array(n))
46
+ .fill(true)
47
+ .map((_, i) => i);
48
+ }
49
+ /*******
50
+ *
51
+ * Returns the histograph of an array as a `Map`. Makes no attempt to cope
52
+ * with deep equality; will fail for complex contents, as such.
53
+ *
54
+ * ```typescript
55
+ * import { histograph } from './jssm';
56
+ *
57
+ * histograph( [0, 0, 1, 1, 2, 2, 1] ); // Map()
58
+ * ```
59
+ *
60
+ */
22
61
  const histograph = (ar) => // eslint-disable-line flowtype/no-weak-types
23
62
  ar.sort()
24
63
  .reduce((m, v) => // TODO FIXME eslint-disable-line flowtype/no-weak-types,no-sequences
@@ -31,6 +70,18 @@ const weighted_histo_key = (n, opts, prob_prop, extract) => // TODO FIXME no any
31
70
  histograph(weighted_sample_select(n, opts, prob_prop)
32
71
  .map((s) => s[extract] // TODO FIXME eslint-disable-line flowtype/no-weak-types
33
72
  ));
73
+ /*******
74
+ *
75
+ * Internal method generating names for edges for the hook lookup map. Not
76
+ * meant for external use.
77
+ *
78
+ */
34
79
  const hook_name = (from, to) => JSON.stringify([from, to]);
80
+ /*******
81
+ *
82
+ * Internal method generating names for actions for the hook lookup map. Not
83
+ * meant for external use.
84
+ *
85
+ */
35
86
  const named_hook_name = (from, to, action) => JSON.stringify([from, to, action]);
36
87
  export { seq, arr_uniq_p, histograph, weighted_histo_key, weighted_rand_select, weighted_sample_select, array_box_if_string, hook_name, named_hook_name };
@@ -1,2 +1,2 @@
1
- const version = "5.65.2";
1
+ const version = "5.65.5";
2
2
  export { version };