@earthsciml/ast 0.1.1

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/README.md ADDED
@@ -0,0 +1,162 @@
1
+ # EarthSci Toolkit - TypeScript Package
2
+
3
+ [![npm version](https://badge.fury.io/js/@earthsciml/ast.svg)](https://badge.fury.io/js/@earthsciml/ast)
4
+ [![Cross-Language Conformance Testing](https://github.com/EarthSciML/EarthSciAST/actions/workflows/conformance-testing.yml/badge.svg)](https://github.com/EarthSciML/EarthSciAST/actions/workflows/conformance-testing.yml)
5
+
6
+ TypeScript types and utilities for the **EarthSciML Serialization Format**, providing complete type definitions, parsing, validation, and manipulation tools for scientific modeling data structures.
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ npm install @earthsciml/ast
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ### Basic Usage
17
+
18
+ ```typescript
19
+ import { EsmFile, Model, load, save, validate } from '@earthsciml/ast';
20
+
21
+ // Parse ESM file from JSON
22
+ const esmFile = load('{"esm": "0.8.0", "metadata": {"name": "demo"}, "models": {...}}');
23
+
24
+ // Create a new model
25
+ const model: Model = {
26
+ variables: {
27
+ x: { type: 'state', default: 1.0 },
28
+ k: { type: 'parameter', default: 0.1 },
29
+ },
30
+ equations: [{ lhs: { op: 'D', args: ['x'], wrt: 't' }, rhs: { op: '*', args: [-1, 'k', 'x'] } }],
31
+ };
32
+
33
+ // Validate an ESM structure
34
+ const result = validate(esmFile);
35
+ if (result.is_valid) {
36
+ console.log("Valid ESM file!");
37
+ } else {
38
+ console.error("Validation errors:", result.schema_errors, result.structural_errors);
39
+ }
40
+
41
+ // Serialize back to JSON
42
+ const jsonString = save(esmFile);
43
+ ```
44
+
45
+ ### Working with Expressions
46
+
47
+ ```typescript
48
+ import { toUnicode, toLatex, substitute, freeVariables } from '@earthsciml/ast';
49
+
50
+ // Pretty-print mathematical expressions
51
+ const expr = { op: "+", args: ["x", { op: "^", args: ["y", "2"] }] };
52
+ console.log(toUnicode(expr)); // "x + y²"
53
+ console.log(toLatex(expr)); // "x + y^{2}"
54
+
55
+ // Analyze expressions
56
+ const variables = freeVariables(expr); // ["x", "y"]
57
+
58
+ // Substitute values
59
+ const substituted = substitute(expr, { x: "2", y: "t" });
60
+ // Result: { op: "+", args: ["2", { op: "^", args: ["t", "2"] }] }
61
+ ```
62
+
63
+ ### Graph Analysis
64
+
65
+ ```typescript
66
+ import { component_graph, componentExists } from '@earthsciml/ast';
67
+
68
+ // Analyze component dependencies
69
+ const graph = component_graph(esmFile);
70
+ console.log("Components:", graph.nodes.map(n => n.name));
71
+ console.log("Coupling edges:", graph.edges.length);
72
+
73
+ // Check for component existence
74
+ if (componentExists(esmFile, "atmospheric_chemistry")) {
75
+ console.log("Found atmospheric chemistry model");
76
+ }
77
+ ```
78
+
79
+ ## Dual Package Support
80
+
81
+ This package supports both ESM and CommonJS environments:
82
+
83
+ ### ESM (ECMAScript Modules)
84
+ ```typescript
85
+ import { load, save, validate } from '@earthsciml/ast';
86
+ ```
87
+
88
+ ### CommonJS
89
+ ```javascript
90
+ const { load, save, validate } = require('@earthsciml/ast');
91
+ ```
92
+
93
+ ## API Reference
94
+
95
+ ### Core Functions
96
+
97
+ - **`load(input: string | object): EsmFile`** - Parse JSON string or object into ESM structure
98
+ - **`save(esmFile: EsmFile): string`** - Serialize ESM structure to formatted JSON
99
+ - **`validate(esmFile: EsmFile): ValidationResult`** - Validate ESM structure against schema
100
+
101
+ ### Type System
102
+
103
+ The package provides complete TypeScript type definitions for:
104
+
105
+ - `EsmFile` - Root ESM file structure
106
+ - `Model` - Scientific model definition
107
+ - `Expression` - Mathematical expression trees
108
+ - `Reaction` - Chemical reaction specifications
109
+ - `CouplingEntry` - Model coupling definitions
110
+ - And many more...
111
+
112
+ ### Expression Utilities
113
+
114
+ - **`toUnicode(expr: Expression): string`** - Render as Unicode mathematical notation
115
+ - **`toLatex(expr: Expression): string`** - Render as LaTeX mathematical notation
116
+ - **`toAscii(expr: Expression): string`** - Render as plain ASCII text
117
+ - **`substitute(expr: Expression, substitutions: Record<string, string>): Expression`** - Variable substitution
118
+ - **`freeVariables(expr: Expression): string[]`** - Extract variable names
119
+ - **`simplify(expr: Expression): Expression`** - Algebraic simplification
120
+
121
+ ### Graph Analysis
122
+
123
+ - **`component_graph(esmFile: EsmFile): ComponentGraph`** - Build dependency graph
124
+ - **`componentExists(esmFile: EsmFile, name: string): boolean`** - Check component existence
125
+ - **`getComponentType(esmFile: EsmFile, name: string): string`** - Get component type
126
+
127
+ ## Development
128
+
129
+ ### Building from Source
130
+
131
+ ```bash
132
+ npm install
133
+ npm run build
134
+ ```
135
+
136
+ This creates dual ESM/CommonJS builds in the `dist/` directory:
137
+ - `dist/esm/` - ESM build
138
+ - `dist/cjs/` - CommonJS build
139
+ - Type definitions in both directories
140
+
141
+ ### Testing
142
+
143
+ ```bash
144
+ npm test # Run unit tests (watch mode)
145
+ npm run test:ci # Run unit tests once
146
+ npm run typecheck # Strict TypeScript check
147
+ npm run lint # ESLint
148
+ npm run format:check # Prettier check
149
+ ```
150
+
151
+ ## Schema Version
152
+
153
+ This package supports ESM Format schema version **0.8.0** (exported as
154
+ `SCHEMA_VERSION`, derived from the embedded schema's `$id`).
155
+
156
+ ## License
157
+
158
+ [License details to be added]
159
+
160
+ ## Contributing
161
+
162
+ [Contributing guidelines to be added]