state-machine-cat 13.0.2 → 13.0.3

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.
@@ -4,13 +4,7 @@ import { parse as parseSmCat } from "./smcat/parse.mjs";
4
4
  import { parse as parseSCXML } from "./scxml/index.mjs";
5
5
  import $schema from "./smcat-ast.schema.mjs";
6
6
  const ajv = new Ajv();
7
- function validateAgainstSchema(pSchema, pObject) {
8
- if (!ajv.validate(pSchema, pObject)) {
9
- throw new Error(
10
- `The provided JSON is not a valid state-machine-cat AST: ${ajv.errorsText()}.\n`,
11
- );
12
- }
13
- }
7
+ const validate = ajv.compile($schema);
14
8
  export default {
15
9
  getAST(pScript, pOptions) {
16
10
  let lReturnValue = pScript;
@@ -21,7 +15,11 @@ export default {
21
15
  } else if (typeof pScript === "string") {
22
16
  lReturnValue = JSON.parse(pScript);
23
17
  }
24
- validateAgainstSchema($schema, lReturnValue);
18
+ if (!validate(lReturnValue)) {
19
+ throw new Error(
20
+ `The provided JSON is not a valid state-machine-cat AST: ${ajv.errorsText()}.\n`,
21
+ );
22
+ }
25
23
  return lReturnValue;
26
24
  },
27
25
  };
@@ -64,22 +64,22 @@ function extractUndeclaredStates(pStateMachine, pKnownStateNames) {
64
64
  pKnownStateNames ?? getAlreadyDeclaredStates(pStateMachine);
65
65
  pStateMachine.states = pStateMachine?.states ?? [];
66
66
  const lTransitions = pStateMachine?.transitions ?? [];
67
- pStateMachine.states.filter(isComposite).forEach((pState) => {
68
- pState.statemachine.states = extractUndeclaredStates(
69
- pState.statemachine,
67
+ for (const lState of pStateMachine.states.filter(isComposite)) {
68
+ lState.statemachine.states = extractUndeclaredStates(
69
+ lState.statemachine,
70
70
  pKnownStateNames,
71
71
  );
72
- });
73
- lTransitions.forEach((pTransition) => {
74
- if (!stateExists(pKnownStateNames, pTransition.from)) {
75
- pKnownStateNames.push(pTransition.from);
76
- pStateMachine.states.push(initState(pTransition.from));
72
+ }
73
+ for (const lTransition of lTransitions) {
74
+ if (!stateExists(pKnownStateNames, lTransition.from)) {
75
+ pKnownStateNames.push(lTransition.from);
76
+ pStateMachine.states.push(initState(lTransition.from));
77
77
  }
78
- if (!stateExists(pKnownStateNames, pTransition.to)) {
79
- pKnownStateNames.push(pTransition.to);
80
- pStateMachine.states.push(initState(pTransition.to));
78
+ if (!stateExists(pKnownStateNames, lTransition.to)) {
79
+ pKnownStateNames.push(lTransition.to);
80
+ pStateMachine.states.push(initState(lTransition.to));
81
81
  }
82
- });
82
+ }
83
83
  return pStateMachine.states;
84
84
  }
85
85
  function classifyForkJoin(pInComingCount, pOutGoingCount) {
@@ -345,13 +345,13 @@ function peg$parse(input, options) {
345
345
  }
346
346
  function peg$f8(notes, id, extended_state_attributes, actions, statemachine) {
347
347
  let lState = parserHelpers.initState(id);
348
- (extended_state_attributes || []).forEach((pExtendedAttribute) =>
348
+ for (const lExtendedAttribute of extended_state_attributes || []) {
349
349
  parserHelpers.setIf(
350
350
  lState,
351
- pExtendedAttribute.name,
352
- pExtendedAttribute.value,
353
- ),
354
- );
351
+ lExtendedAttribute.name,
352
+ lExtendedAttribute.value,
353
+ );
354
+ }
355
355
  parserHelpers.setIf(
356
356
  lState,
357
357
  "typeExplicitlySet",
@@ -408,13 +408,13 @@ function peg$parse(input, options) {
408
408
  parserHelpers.parseTransitionExpression(label),
409
409
  );
410
410
  }
411
- (extended_attributes || []).forEach((pExtendedAttribute) =>
411
+ for (const lExtendedAttribute of extended_attributes || []) {
412
412
  parserHelpers.setIf(
413
413
  trans,
414
- pExtendedAttribute.name,
415
- pExtendedAttribute.value,
416
- ),
417
- );
414
+ lExtendedAttribute.name,
415
+ lExtendedAttribute.value,
416
+ );
417
+ }
418
418
  parserHelpers.setIfNotEmpty(trans, "note", notes);
419
419
  trans.id = options.counter.next();
420
420
  return trans;
@@ -37,11 +37,20 @@ function toNameValueString(pAttribute) {
37
37
  return `${pAttribute.name}=${pAttribute.value}`;
38
38
  }
39
39
  export function buildGraphAttributes(pEngine, pDirection, pDotGraphAttributes) {
40
- return GENERIC_GRAPH_ATTRIBUTES.concat(GRAPH_ATTRIBUTES[pEngine] || [])
41
- .concat(DIRECTION_ATTRIBUTES[pDirection] || [])
42
- .concat(pDotGraphAttributes || [])
43
- .map(toNameValueString)
44
- .join(" ");
40
+ const lParts = [];
41
+ for (const lAttribute of GENERIC_GRAPH_ATTRIBUTES) {
42
+ lParts.push(lAttribute);
43
+ }
44
+ for (const lAttribute of GRAPH_ATTRIBUTES[pEngine] || []) {
45
+ lParts.push(lAttribute);
46
+ }
47
+ for (const lAttribute of DIRECTION_ATTRIBUTES[pDirection] || []) {
48
+ lParts.push(lAttribute);
49
+ }
50
+ for (const lAttribute of pDotGraphAttributes || []) {
51
+ lParts.push(lAttribute);
52
+ }
53
+ return lParts.map(toNameValueString).join(" ");
45
54
  }
46
55
  export function buildNodeAttributes(pDotNodeAttributes) {
47
56
  return NODE_ATTRIBUTES.concat(pDotNodeAttributes || [])
@@ -180,9 +180,9 @@ function state(pState, pIndent, pOptions, pModel, pRenderedTransitions) {
180
180
  pState.name,
181
181
  pRenderedTransitions,
182
182
  );
183
- lCandidateTransitions.forEach((pTransition) => {
184
- pRenderedTransitions.add(pTransition.id);
185
- });
183
+ for (const lTransition of lCandidateTransitions) {
184
+ pRenderedTransitions.add(lTransition.id);
185
+ }
186
186
  const lTransitions = transitions(
187
187
  lCandidateTransitions,
188
188
  pIndent,
@@ -1,19 +1,17 @@
1
1
  function flattenStatesToMap(pStates, pMap, pParent = "") {
2
- pStates
3
- .filter((pState) => Boolean(pState.statemachine))
4
- .forEach((pState) => {
5
- if (Object.hasOwn(pState.statemachine, "states")) {
6
- flattenStatesToMap(pState.statemachine.states, pMap, pState.name);
7
- }
8
- });
9
- pStates.forEach((pState) =>
10
- pMap.set(pState.name, {
11
- name: pState.name,
12
- type: pState.type,
13
- statemachine: Boolean(pState.statemachine),
2
+ for (const lState of pStates) {
3
+ if (lState?.statemachine?.states) {
4
+ flattenStatesToMap(lState.statemachine.states, pMap, lState.name);
5
+ }
6
+ }
7
+ for (const lState of pStates) {
8
+ pMap.set(lState.name, {
9
+ name: lState.name,
10
+ type: lState.type,
11
+ statemachine: Boolean(lState.statemachine),
14
12
  parent: pParent,
15
- }),
16
- );
13
+ });
14
+ }
17
15
  }
18
16
  function flattenTransitions(pStateMachine) {
19
17
  let lTransitions = [];
@@ -21,13 +19,13 @@ function flattenTransitions(pStateMachine) {
21
19
  lTransitions = structuredClone(pStateMachine.transitions);
22
20
  }
23
21
  if (Object.hasOwn(pStateMachine, "states")) {
24
- pStateMachine.states
25
- .filter((pState) => Boolean(pState.statemachine))
26
- .forEach((pState) => {
22
+ for (const lState of pStateMachine.states) {
23
+ if (lState.statemachine) {
27
24
  lTransitions = lTransitions.concat(
28
- flattenTransitions(pState.statemachine),
25
+ flattenTransitions(lState.statemachine),
29
26
  );
30
- });
27
+ }
28
+ }
31
29
  }
32
30
  return lTransitions;
33
31
  }
@@ -40,20 +40,24 @@ function fuseTransitions(
40
40
  pOutgoingTransitionMap,
41
41
  pCounter,
42
42
  ) {
43
- return pTransitions.reduce((pAll, pTransition) => {
44
- pPseudoStateNames.forEach((pStateName, pIndex) => {
45
- if (pStateName === pTransition.to && pOutgoingTransitionMap[pStateName]) {
46
- pAll = pAll.concat(
47
- pOutgoingTransitionMap[pStateName].map((pOutgoingTransition) =>
48
- fuseIncomingToOutgoing(pTransition, pOutgoingTransition, pCounter),
49
- ),
50
- );
51
- } else {
52
- pAll = pIndex === 0 ? pAll.concat(pTransition) : pAll;
43
+ const lResult = [];
44
+ for (const lTransition of pTransitions) {
45
+ let lAdded = false;
46
+ for (const lStateName of pPseudoStateNames) {
47
+ if (lStateName === lTransition.to && pOutgoingTransitionMap[lStateName]) {
48
+ for (const lOutgoing of pOutgoingTransitionMap[lStateName]) {
49
+ lResult.push(
50
+ fuseIncomingToOutgoing(lTransition, lOutgoing, pCounter),
51
+ );
52
+ }
53
+ lAdded = true;
53
54
  }
54
- });
55
- return pAll;
56
- }, []);
55
+ }
56
+ if (!lAdded) {
57
+ lResult.push(lTransition);
58
+ }
59
+ }
60
+ return lResult;
57
61
  }
58
62
  function deSugarPseudoStates(
59
63
  pMachine,
package/dist/version.mjs CHANGED
@@ -1 +1 @@
1
- export const version = "13.0.2";
1
+ export const version = "13.0.3";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "state-machine-cat",
3
- "version": "13.0.2",
3
+ "version": "13.0.3",
4
4
  "description": "write beautiful state charts",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",