scjson 0.1.5 → 0.1.7

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/LEGAL.md ADDED
@@ -0,0 +1,7 @@
1
+ # Legal Information
2
+
3
+ This package is distributed as part of the **scjson** project. All original code in this directory is released under the BSD\u00A01-Clause license.
4
+
5
+ The package references assets from the W3C SCXML specification and includes examples derived from Alex Zhornyak's *SCXML Editor Tutorial*. Those third-party materials remain under their respective licenses as documented in the repository's top-level `LEGAL.md` file.
6
+
7
+ For complete license terms please consult that document.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ BSD 1-Clause License
2
+
3
+ Copyright (c) 2025, Softoboros Technology Inc.
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice,
10
+ this list of conditions and the following disclaimer.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
13
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
16
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # scjson JavaScript Package
2
+
3
+ This directory contains the JavaScript implementation of **scjson**, a format for representing SCXML state machines in JSON. The package provides a command line interface to convert between `.scxml` and `.scjson` files and to validate documents against the project's schema.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install scjson
9
+ ```
10
+
11
+ You can also install from a checkout of this repository:
12
+
13
+ ```bash
14
+ cd js && npm install
15
+ ```
16
+
17
+ ## Command Line Usage
18
+
19
+ After installation the `scjson` command is available:
20
+
21
+ ```bash
22
+ # Convert a single file
23
+ scjson json path/to/machine.scxml
24
+
25
+ # Convert back to SCXML
26
+ scjson xml path/to/machine.scjson
27
+
28
+ # Validate recursively
29
+ scjson validate path/to/dir -r
30
+ ```
31
+
32
+ ### Other Resources
33
+ github: [https://github.com/SoftOboros/scjson]
34
+ ```bash
35
+ git clone https://github.com/SoftOboros/scjson.git
36
+
37
+ git clone git@github.com:SoftOboros/scjson.git
38
+
39
+ gh repo clone SoftOboros/scjson
40
+ ```
41
+
42
+ pypi: [https://www.npmjs.com/package/scjson]
43
+ ```bash
44
+ npm install scjson
45
+ ```
46
+
47
+ dockerhub: [https://hub.docker.com/r/iraa/scjson]
48
+ (Full development environment for all supported languages)
49
+ ```bash
50
+ docker pull iraa/scjson:latest
51
+ ```
52
+
53
+
54
+ All source code in this directory is released under the BSD\u00A01-Clause license. See `LICENSE` and `LEGAL.md` for details.
package/browser.mjs ADDED
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Agent Name: js-browser
3
+ *
4
+ * Part of the scjson project.
5
+ * Developed by Softoboros Technology Inc.
6
+ * Licensed under the BSD 1-Clause License.
7
+ */
8
+
9
+ /**
10
+ * @file Browser friendly utilities for converting SCXML to scjson and back.
11
+ */
12
+
13
+ import { XMLParser, XMLBuilder } from 'fast-xml-parser';
14
+ import Ajv from 'ajv';
15
+ import schema from '../scjson.schema.json' assert { type: 'json' };
16
+
17
+ const ajv = new Ajv({ useDefaults: true, strict: false });
18
+ const validate = ajv.compile(schema);
19
+
20
+ /**
21
+ * Remove nulls and empty containers from values recursively.
22
+ *
23
+ * @param {*} value - Candidate value.
24
+ * @returns {*} Sanitised value.
25
+ */
26
+ export function removeEmpty(value) {
27
+ if (Array.isArray(value)) {
28
+ const arr = value.map(removeEmpty).filter(v => v !== undefined);
29
+ return arr.length > 0 ? arr : undefined;
30
+ }
31
+ if (value && typeof value === 'object') {
32
+ const obj = {};
33
+ for (const [k, v] of Object.entries(value)) {
34
+ const r = removeEmpty(v);
35
+ if (r !== undefined) obj[k] = r;
36
+ }
37
+ return Object.keys(obj).length > 0 ? obj : undefined;
38
+ }
39
+ if (value === null || value === '') {
40
+ return undefined;
41
+ }
42
+ return value;
43
+ }
44
+
45
+ /**
46
+ * Convert an SCXML string to scjson.
47
+ *
48
+ * @param {string} xmlStr - XML input.
49
+ * @param {boolean} [omitEmpty=true] - Remove empty values when true.
50
+ * @returns {string} JSON representation.
51
+ */
52
+ export function xmlToJson(xmlStr, omitEmpty = true) {
53
+ const parser = new XMLParser({ ignoreAttributes: false });
54
+ let obj = parser.parse(xmlStr);
55
+ if (obj.scxml) {
56
+ obj = obj.scxml;
57
+ }
58
+ if (omitEmpty) {
59
+ obj = removeEmpty(obj) || {};
60
+ }
61
+ if (obj['@_xmlns']) {
62
+ delete obj['@_xmlns'];
63
+ }
64
+ if (obj.version === undefined) {
65
+ obj.version = 1.0;
66
+ }
67
+ if (obj.datamodel_attribute === undefined) {
68
+ obj.datamodel_attribute = 'null';
69
+ }
70
+ if (!validate(obj)) {
71
+ throw new Error('Invalid scjson');
72
+ }
73
+ if (omitEmpty) {
74
+ obj = removeEmpty(obj) || {};
75
+ }
76
+ return JSON.stringify(obj, null, 2);
77
+ }
78
+
79
+ /**
80
+ * Convert a scjson string to SCXML.
81
+ *
82
+ * @param {string} jsonStr - JSON input.
83
+ * @returns {string} XML output.
84
+ */
85
+ export function jsonToXml(jsonStr) {
86
+ const builder = new XMLBuilder({ ignoreAttributes: false, format: true });
87
+ const obj = JSON.parse(jsonStr);
88
+ if (!validate(obj)) {
89
+ throw new Error('Invalid scjson');
90
+ }
91
+ return builder.build({ scxml: obj });
92
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "scjson",
3
- "version": "0.1.5",
4
- "description": "Convert SCXML to/from scjson (JSON-based representation of state machines)",
3
+ "version": "0.1.7",
4
+ "description": "A JSON-based serialization of SCXML (State Chart XML) for modern tooling, interoperability, and education.",
5
5
  "keywords": [
6
6
  "scjson",
7
7
  "scxml",
@@ -27,13 +27,31 @@
27
27
  "engines": {
28
28
  "node": ">=18"
29
29
  },
30
+ "main": "index.js",
31
+ "type": "commonjs",
32
+ "exports": {
33
+ ".": "./index.js",
34
+ "./browser": "./browser.mjs"
35
+ },
36
+ "typesVersions": {
37
+ "*": {
38
+ "browser": [
39
+ "/types/scjson-browser.d.ts"
40
+ ]
41
+ }
42
+ },
30
43
  "files": [
31
44
  "bin/",
32
45
  "index.js",
33
- "tests/"
46
+ "browser.mjs",
47
+ "types/scjson-browser.d.ts",
48
+ "tests/",
49
+ "README.md",
50
+ "LEGAL.md",
51
+ "LICENSE"
34
52
  ],
35
- "main": "index.js",
36
- "type": "module",
53
+ "module": "./browser.mjs",
54
+ "browser": "./browser.mjs",
37
55
  "publishConfig": {
38
56
  "access": "public"
39
57
  },
@@ -48,6 +66,7 @@
48
66
  "fast-xml-parser": "^5.2.5"
49
67
  },
50
68
  "devDependencies": {
51
- "jest": "^30.0.4"
69
+ "jest": "^30.0.4",
70
+ "typescript": "^5.8.3"
52
71
  }
53
72
  }
@@ -0,0 +1,25 @@
1
+ declare module 'scjson/browser' {
2
+ /**
3
+ * Recursively removes nulls, empty arrays/objects, and empty strings from a value.
4
+ * @param value Any input value (array, object, scalar).
5
+ * @returns Sanitized value or `undefined`.
6
+ */
7
+ export function removeEmpty(value: any): any;
8
+
9
+ /**
10
+ * Converts SCXML (as XML string) to SCJSON (as JSON string).
11
+ * @param xmlStr SCXML input as string.
12
+ * @param omitEmpty Optional: if true, removes empty/null fields (default: true).
13
+ * @returns Validated SCJSON as pretty-printed JSON string.
14
+ * @throws Error if validation fails.
15
+ */
16
+ export function xmlToJson(xmlStr: string, omitEmpty?: boolean): string;
17
+
18
+ /**
19
+ * Converts SCJSON (as JSON string) to SCXML (as XML string).
20
+ * @param jsonStr SCJSON input as string.
21
+ * @returns Valid SCXML string.
22
+ * @throws Error if validation fails.
23
+ */
24
+ export function jsonToXml(jsonStr: string): string;
25
+ }