jssm 5.77.0 → 5.79.0

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.
package/jssm.d.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  declare type StateType = string;
2
- import { circular_buffer } from 'circular_buffer_js';
3
2
  import { JssmGenericState, JssmGenericConfig, JssmTransition, JssmTransitionList, // JssmTransitionRule,
4
- JssmMachineInternalState, JssmParseTree, JssmStateDeclaration, JssmArrow, JssmArrowDirection, JssmArrowKind, JssmLayout, FslDirection, FslTheme, HookDescription, HookHandler, HookContext, HookResult, HookComplexResult } from './jssm_types';
5
- import { seq, weighted_rand_select, weighted_sample_select, histograph, weighted_histo_key } from './jssm_util';
3
+ JssmMachineInternalState, JssmParseTree, JssmStateDeclaration, JssmArrow, JssmArrowDirection, JssmArrowKind, JssmLayout, JssmHistory, JssmSerialization, FslDirection, FslTheme, HookDescription, HookHandler, HookContext, HookResult, HookComplexResult } from './jssm_types';
4
+ import { seq, unique, find_repeated, weighted_rand_select, weighted_sample_select, histograph, weighted_histo_key } from './jssm_util';
6
5
  import { shapes, gviz_shapes, named_colors } from './jssm_constants';
7
6
  import { version } from './version';
8
7
  /*********
@@ -251,9 +250,12 @@ declare class Machine<mDT> {
251
250
  _post_main_transition_hook: HookHandler<mDT> | undefined;
252
251
  _post_forced_transition_hook: HookHandler<mDT> | undefined;
253
252
  _post_any_transition_hook: HookHandler<mDT> | undefined;
254
- _history: circular_buffer<[StateType, mDT]>;
253
+ _property_keys: Set<string>;
254
+ _default_properties: Map<string, any>;
255
+ _state_properties: Map<string, any>;
256
+ _history: JssmHistory<mDT>;
255
257
  _history_length: number;
256
- constructor({ start_states, complete, transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, state_declaration, fsl_version, dot_preamble, arrange_declaration, arrange_start_declaration, arrange_end_declaration, theme, flow, graph_layout, instance_name, history, data }: JssmGenericConfig<mDT>);
258
+ constructor({ start_states, complete, transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, state_declaration, property_definition, state_property, fsl_version, dot_preamble, arrange_declaration, arrange_start_declaration, arrange_end_declaration, theme, flow, graph_layout, instance_name, history, data }: JssmGenericConfig<mDT>);
257
259
  /********
258
260
  *
259
261
  * Internal method for fabricating states. Not meant for external use.
@@ -295,6 +297,105 @@ declare class Machine<mDT> {
295
297
  *
296
298
  */
297
299
  data(): mDT;
300
+ /*********
301
+ *
302
+ * Get the current value of a given property name.
303
+ *
304
+ * ```typescript
305
+ *
306
+ * ```
307
+ *
308
+ * @param name The relevant property name to look up
309
+ *
310
+ * @returns The value behind the prop name. Because functional props are
311
+ * evaluated as getters, this can be anything.
312
+ *
313
+ */
314
+ prop(name: string): any;
315
+ /*********
316
+ *
317
+ * Get the current value of every prop, as an object. If no current definition
318
+ * exists for a prop - that is, if the prop was defined without a default and
319
+ * the current state also doesn't define the prop - then that prop will be listed
320
+ * in the returned object with a value of `undefined`.
321
+ *
322
+ * ```typescript
323
+ * const traffic_light = sm`
324
+ *
325
+ * property can_go default true;
326
+ * property hesitate default true;
327
+ * property stop_first default false;
328
+ *
329
+ * Off -> Red => Green => Yellow => Red;
330
+ * [Red Yellow Green] ~> [Off FlashingRed];
331
+ * FlashingRed -> Red;
332
+ *
333
+ * state Red: { property stop_first true; property can_go false; };
334
+ * state Off: { property stop_first true; };
335
+ * state FlashingRed: { property stop_first true; };
336
+ * state Green: { property hesitate false; };
337
+ *
338
+ * `;
339
+ *
340
+ * traffic_light.state(); // Off
341
+ * traffic_light.props(); // { can_go: true, hesitate: true, stop_first: true; }
342
+ *
343
+ * traffic_light.go('Red');
344
+ * traffic_light.props(); // { can_go: false, hesitate: true, stop_first: true; }
345
+ *
346
+ * traffic_light.go('Green');
347
+ * traffic_light.props(); // { can_go: true, hesitate: false, stop_first: false; }
348
+ * ```
349
+ *
350
+ */
351
+ props(): object;
352
+ /*********
353
+ *
354
+ * Get the current value of every prop, as an object. Compare
355
+ * {@link prop_map}, which returns a `Map`.
356
+ *
357
+ * ```typescript
358
+ *
359
+ * ```
360
+ *
361
+ */
362
+ /*********
363
+ *
364
+ * Get the current value of every prop, as an object. Compare
365
+ * {@link prop_map}, which returns a `Map`. Akin to {@link strict_prop},
366
+ * this throws if a required prop is missing.
367
+ *
368
+ * ```typescript
369
+ *
370
+ * ```
371
+ *
372
+ */
373
+ /*********
374
+ *
375
+ * Check whether a given string is a known property's name.
376
+ *
377
+ * ```typescript
378
+ * const example = sm`property foo default 1; a->b;`;
379
+ *
380
+ * example.known_prop('foo'); // true
381
+ * example.known_prop('bar'); // false
382
+ * ```
383
+ *
384
+ * @param prop_name The relevant property name to look up
385
+ *
386
+ */
387
+ known_prop(prop_name: string): boolean;
388
+ /*********
389
+ *
390
+ * List all known property names. If you'd also like values, use
391
+ * {@link props} instead. The order of the properties is not defined, and
392
+ * the properties generally will not be sorted.
393
+ *
394
+ * ```typescript
395
+ * ```
396
+ *
397
+ */
398
+ known_props(): string[];
298
399
  /********
299
400
  *
300
401
  * Check whether a given state is final (either has no exits or is marked
@@ -330,10 +431,19 @@ declare class Machine<mDT> {
330
431
  * console.log( final_test.is_final() ); // true
331
432
  * ```
332
433
  *
434
+ */
435
+ is_final(): boolean;
436
+ /********
437
+ *
438
+ * Serialize the current machine, including all defining state but not the
439
+ * machine string, to a structure. This means you will need the machine
440
+ * string to recreate (to not waste repeated space;) if you want the machine
441
+ * string embedded, call {@link serialize_with_string} instead.
442
+ *
333
443
  * @typeparam mDT The type of the machine data member; usually omitted
334
444
  *
335
445
  */
336
- is_final(): boolean;
446
+ serialize(comment?: string | undefined): JssmSerialization<mDT>;
337
447
  graph_layout(): string;
338
448
  dot_preamble(): string;
339
449
  machine_author(): Array<string>;
@@ -665,7 +775,7 @@ declare class Machine<mDT> {
665
775
  set history_length(to: number);
666
776
  /********
667
777
  *
668
- * Instruct the machine to complete an action.
778
+ * Instruct the machine to complete an action. Synonym for {@link do}.
669
779
  *
670
780
  * ```typescript
671
781
  * const light = sm`red 'next' -> green 'next' -> yellow 'next' -> red; [red yellow green] 'shutdown' ~> off 'start' -> red;`;
@@ -683,16 +793,58 @@ declare class Machine<mDT> {
683
793
  *
684
794
  */
685
795
  action(actionName: StateType, newData?: mDT): boolean;
796
+ /********
797
+ *
798
+ * Instruct the machine to complete an action. Synonym for {@link action}.
799
+ *
800
+ * ```typescript
801
+ * const light = sm`
802
+ * off 'start' -> red;
803
+ * red 'next' -> green 'next' -> yellow 'next' -> red;
804
+ * [red yellow green] 'shutdown' ~> off;
805
+ * `;
806
+ *
807
+ * light.state(); // 'off'
808
+ * light.do('start'); // true
809
+ * light.state(); // 'red'
810
+ * light.do('next'); // true
811
+ * light.state(); // 'green'
812
+ * light.do('next'); // true
813
+ * light.state(); // 'yellow'
814
+ * light.do('dance'); // !! false - no such action
815
+ * light.state(); // 'yellow'
816
+ * light.do('start'); // !! false - yellow does not have the action start
817
+ * light.state(); // 'yellow'
818
+ * ```
819
+ *
820
+ * @typeparam mDT The type of the machine data member; usually omitted
821
+ b *
822
+ * @param actionName The action to engage
823
+ *
824
+ * @param newData The data change to insert during the action
825
+ *
826
+ */
827
+ do(actionName: StateType, newData?: mDT): boolean;
686
828
  /********
687
829
  *
688
830
  * Instruct the machine to complete a transition. Synonym for {@link go}.
689
831
  *
690
832
  * ```typescript
691
- * const light = sm`red -> green -> yellow -> red; [red yellow green] 'shutdown' ~> off 'start' -> red;`;
833
+ * const light = sm`
834
+ * off 'start' -> red;
835
+ * red 'next' -> green 'next' -> yellow 'next' -> red;
836
+ * [red yellow green] 'shutdown' ~> off;
837
+ * `;
692
838
  *
693
- * light.state(); // 'red'
694
- * light.transition('green'); // true
695
- * light.state(); // 'green'
839
+ * light.state(); // 'off'
840
+ * light.go('red'); // true
841
+ * light.state(); // 'red'
842
+ * light.go('green'); // true
843
+ * light.state(); // 'green'
844
+ * light.go('blue'); // !! false - no such state
845
+ * light.state(); // 'green'
846
+ * light.go('red'); // !! false - green may not go directly to red, only to yellow
847
+ * light.state(); // 'green'
696
848
  * ```
697
849
  *
698
850
  * @typeparam mDT The type of the machine data member; usually omitted
@@ -804,4 +956,5 @@ declare function from<mDT>(MachineAsString: string, ExtraConstructorFields?: Par
804
956
  declare function is_hook_complex_result<mDT>(hr: unknown): hr is HookComplexResult<mDT>;
805
957
  declare function is_hook_rejection<mDT>(hr: HookResult<mDT>): boolean;
806
958
  declare function abstract_hook_step<mDT>(maybe_hook: HookHandler<mDT> | undefined, hook_args: HookContext<mDT>): HookComplexResult<mDT>;
807
- 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, is_hook_rejection, is_hook_complex_result, abstract_hook_step };
959
+ declare function deserialize<mDT>(machine_string: string, ser: JssmSerialization<mDT>): Machine<mDT>;
960
+ export { version, transfer_state_properties, Machine, deserialize, make, wrap_parse as parse, compile, sm, from, arrow_direction, arrow_left_kind, arrow_right_kind, seq, unique, find_repeated, weighted_rand_select, histograph, weighted_sample_select, weighted_histo_key, shapes, gviz_shapes, named_colors, is_hook_rejection, is_hook_complex_result, abstract_hook_step };
package/jssm_types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { circular_buffer } from 'circular_buffer_js';
1
2
  declare type StateType = string;
2
3
  declare type JssmSuccess = {
3
4
  success: true;
@@ -25,6 +26,19 @@ declare type JssmCorner = 'regular' | 'rounded' | 'lined';
25
26
  declare type JssmLineStyle = 'solid' | 'dashed' | 'dotted';
26
27
  declare type FslDirection = 'up' | 'right' | 'down' | 'left';
27
28
  declare type FslTheme = 'default' | 'ocean' | 'modern' | 'none';
29
+ declare type JssmSerialization<DataType> = {
30
+ jssm_version: string;
31
+ timestamp: number;
32
+ comment?: string | undefined;
33
+ state: StateType;
34
+ history: [string, DataType][];
35
+ history_capacity: number;
36
+ data: DataType;
37
+ };
38
+ declare type JssmPropertyDefinition = {
39
+ name: string;
40
+ default_value?: any;
41
+ };
28
42
  declare type JssmTransitionPermitter<DataType> = (OldState: StateType, NewState: StateType, OldData: DataType, NewData: DataType) => boolean;
29
43
  declare type JssmTransitionPermitterMaybeArray<DataType> = JssmTransitionPermitter<DataType> | Array<JssmTransitionPermitter<DataType>>;
30
44
  declare type JssmTransition<DataType> = {
@@ -38,7 +52,7 @@ declare type JssmTransition<DataType> = {
38
52
  forced_only: boolean;
39
53
  main_path: boolean;
40
54
  };
41
- declare type JssmTransitions<DataType> = Array<JssmTransition<DataType>>;
55
+ declare type JssmTransitions<DataType> = JssmTransition<DataType>[];
42
56
  declare type JssmTransitionList = {
43
57
  entrances: Array<StateType>;
44
58
  exits: Array<StateType>;
@@ -83,6 +97,7 @@ declare type JssmGenericMachine<DataType> = {
83
97
  declare type JssmStateDeclarationRule = {
84
98
  key: string;
85
99
  value: any;
100
+ name?: string;
86
101
  };
87
102
  declare type JssmStateDeclaration = {
88
103
  declarations: Array<JssmStateDeclarationRule>;
@@ -94,6 +109,10 @@ declare type JssmStateDeclaration = {
94
109
  backgroundColor?: JssmColor;
95
110
  borderColor?: JssmColor;
96
111
  state: StateType;
112
+ property?: {
113
+ name: string;
114
+ value: unknown;
115
+ };
97
116
  };
98
117
  declare type JssmGenericConfig<DataType> = {
99
118
  graph_layout?: JssmLayout;
@@ -115,7 +134,9 @@ declare type JssmGenericConfig<DataType> = {
115
134
  dot_preamble?: string;
116
135
  start_states: Array<StateType>;
117
136
  end_states?: Array<StateType>;
118
- state_declaration?: Array<Object>;
137
+ state_declaration?: Object[];
138
+ property_definition?: JssmPropertyDefinition[];
139
+ state_property?: JssmPropertyDefinition[];
119
140
  arrange_declaration?: Array<Array<StateType>>;
120
141
  arrange_start_declaration?: Array<Array<StateType>>;
121
142
  arrange_end_declaration?: Array<Array<StateType>>;
@@ -150,6 +171,8 @@ declare type JssmCompileSeStart<DataType> = {
150
171
  key: string;
151
172
  value?: string | number;
152
173
  name?: string;
174
+ state?: string;
175
+ default_value?: any;
153
176
  };
154
177
  declare type JssmParseTree = Array<JssmCompileSeStart<StateType>>;
155
178
  declare type JssmParseFunctionType = (string: any) => JssmParseTree;
@@ -264,4 +287,5 @@ declare type PostHookHandler<mDT> = (hook_context: HookContext<mDT>) => void;
264
287
  declare type JssmErrorExtendedInfo = {
265
288
  requested_state?: StateType | undefined;
266
289
  };
267
- export { JssmColor, JssmShape, JssmTransition, JssmTransitions, JssmTransitionList, JssmTransitionRule, JssmArrow, JssmArrowKind, JssmArrowDirection, JssmGenericConfig, JssmGenericState, JssmGenericMachine, JssmParseTree, JssmCompileSe, JssmCompileSeStart, JssmCompileRule, JssmPermitted, JssmPermittedOpt, JssmResult, JssmStateDeclaration, JssmStateDeclarationRule, JssmLayout, JssmParseFunctionType, JssmMachineInternalState, JssmErrorExtendedInfo, FslDirection, FslTheme, HookDescription, HookHandler, HookContext, HookResult, HookComplexResult };
290
+ declare type JssmHistory<mDT> = circular_buffer<[StateType, mDT]>;
291
+ export { JssmColor, JssmShape, JssmTransition, JssmTransitions, JssmTransitionList, JssmTransitionRule, JssmArrow, JssmArrowKind, JssmArrowDirection, JssmGenericConfig, JssmGenericState, JssmGenericMachine, JssmParseTree, JssmCompileSe, JssmCompileSeStart, JssmCompileRule, JssmPermitted, JssmPermittedOpt, JssmResult, JssmStateDeclaration, JssmStateDeclarationRule, JssmLayout, JssmHistory, JssmSerialization, JssmPropertyDefinition, JssmParseFunctionType, JssmMachineInternalState, JssmErrorExtendedInfo, FslDirection, FslTheme, HookDescription, HookHandler, HookContext, HookResult, HookComplexResult };
package/jssm_util.d.ts CHANGED
@@ -35,6 +35,13 @@ declare function seq(n: number): number[];
35
35
  declare const histograph: Function;
36
36
  declare const weighted_sample_select: Function;
37
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
+ */
44
+ declare function name_bind_prop_and_state(prop: string, state: string): string;
38
45
  /*******
39
46
  *
40
47
  * Internal method generating names for edges for the hook lookup map. Not
@@ -57,4 +64,40 @@ declare const named_hook_name: (from: string, to: string, action: string) => str
57
64
  *
58
65
  */
59
66
  declare const make_mulberry_rand: (a?: number | undefined) => () => number;
60
- export { seq, arr_uniq_p, histograph, weighted_histo_key, weighted_rand_select, weighted_sample_select, array_box_if_string, hook_name, named_hook_name, make_mulberry_rand };
67
+ /*******
68
+ *
69
+ * Reduces an array to its unique contents. Compares with `===` and makes no
70
+ * effort to deep-compare contents; two matching arrays or objects contained
71
+ * will be treated as distinct, according to javascript rules. This also means
72
+ * that `NaNs` will be ***dropped***, because they do not self-compare.
73
+ *
74
+ * ```typescript
75
+ * unique( [] ); // []
76
+ * unique( [0,0] ); // [0]
77
+ * unique( [0,1,2, 0,1,2, 0,1,2] ); // [0,1,2]
78
+ * unique( [ [1], [1] ] ); // [ [1], [1] ] because arrays don't match
79
+ * unique( [0,NaN,2] ); // [0,2]
80
+ * ```
81
+ *
82
+ */
83
+ declare const unique: <T>(arr?: T[]) => T[];
84
+ /*******
85
+ *
86
+ * Lists all repeated items in an array along with their counts. Subject to
87
+ * matching rules of Map. `NaN` is manually removed because of conflict rules
88
+ * around {@link unique}. Because these are compared with `===` and because
89
+ * arrays and objects never match that way unless they're the same object,
90
+ * arrays and objects are never considered repeats.
91
+ *
92
+ * ```typescript
93
+ * find_repeated<string>([ ]); // []
94
+ * find_repeated<string>([ "one" ]); // []
95
+ * find_repeated<string>([ "one", "two" ]); // []
96
+ * find_repeated<string>([ "one", "one" ]); // [ ["one", 2] ]
97
+ * find_repeated<string>([ "one", "two", "one" ]); // [ ["one", 2] ]
98
+ * find_repeated<number>([ 0, NaN, 0, NaN ]); // [ [0, 2] ]
99
+ * ```
100
+ *
101
+ */
102
+ declare function find_repeated<T>(arr: T[]): [T, number][];
103
+ export { seq, unique, find_repeated, arr_uniq_p, histograph, weighted_histo_key, weighted_rand_select, weighted_sample_select, array_box_if_string, name_bind_prop_and_state, hook_name, named_hook_name, make_mulberry_rand };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jssm",
3
- "version": "5.77.0",
3
+ "version": "5.79.0",
4
4
  "engines": {
5
5
  "node": ">=10.0.0"
6
6
  },