1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
# wmap-parser
A high-performance JavaScript parser for wmap formatted Wardley Map files.
## Features
- **Zero dependencies** (dev dependencies only for testing and benchmarking)
- **High performance** state machine parser
- **TypeScript-friendly** with comprehensive JSDoc type definitions
- **Cross-platform** line ending support (LF, CRLF, CR)
- **Robust** error handling - invalid lines are gracefully ignored
## Installation
```bash
npm install wmap-parser
```
Or with pnpm:
```bash
pnpm add wmap-parser
```
## Usage
```javascript
import { parse } from "wmap-parser";
const wmapSource = `
[I] 0.25
[II] 0.5
[III] 0.75
[IV] 1.0
Tea (0.9, 0.5) [Circle]
Cup (0.8, 0.4)
Hot Water (0.6, 0.2)
Tea -> Cup
Cup -> Hot Water
[Note] (0.5, 0.5) Supply chain for tea
[Group] Tea, Cup
[Inertia] Cup
[Evolution] Tea + 0.1
`;
const map = await parse(wmapSource);
console.log(map.entities);
// [
// { type: 'stage', number: 'i', value: 0.25 },
// { type: 'stage', number: 'ii', value: 0.5 },
// { type: 'component', label: 'Tea', coordinates: [0.9, 0.5], shape: 'circle' },
// { type: 'dependency', from: 'Tea', to: 'Cup', isDirected: true },
// ...
// ]
```
## API
### `parse(source: string): Promise<Map>`
Parses a wmap formatted string and returns a Map object.
**Parameters:**
- `source` (string): The wmap source code to parse
**Returns:**
- `Promise<Map>`: A promise that resolves to a Map object
### Type Definitions
#### Map
```typescript
{
entities: Entity[]
}
```
#### Entity
Union type of: `Component | Dependency | Note | Stage | Group | Inertia | Evolution`
#### Component
```typescript
{
type: "component";
label: string;
coordinates: [number, number];
shape: string | null; // 'x' | 'square' | 'triangle' | 'circle' | null
}
```
#### Dependency
```typescript
{
type: "dependency";
from: string;
to: string;
isDirected: boolean; // true for ->, false for --
}
```
#### Note
```typescript
{
type: "note";
coordinates: [number, number];
text: string;
}
```
#### Stage
```typescript
{
type: "stage";
number: string; // 'i' | 'ii' | 'iii' | 'iv'
value: number;
}
```
#### Group
```typescript
{
type: 'group'
vertices: string[]
}
```
#### Inertia
```typescript
{
type: "inertia";
vertex: string;
}
```
#### Evolution
```typescript
{
type: "evolution";
vertex: string;
isPositive: boolean;
value: number;
}
```
## Format Specification
The wmap format supports the following entity types:
### Components (Vertices)
```
ComponentName (x, y)
ComponentName (x, y) [Shape]
```
- Coordinates are decimal numbers (0-100 range typical)
- Optional shape: `x`, `Square`, `Triangle`, or `Circle` (case-insensitive)
### Dependencies (Edges)
```
Source -> Target # Directed dependency
Source -- Target # Undirected dependency
```
### Notes
```
[Note] (x, y) Note text content
```
### Stages
```
[I] 0.25
[II] 0.5
[III] 0.75
[IV] 1.0
```
- Stage numbers: `I`, `II`, `III`, or `IV` (case-insensitive)
### Groups
```
[Group] Component1, Component2, Component3
```
### Inertia
```
[Inertia] ComponentName
```
### Evolution
```
[Evolution] ComponentName + 0.5
[Evolution] ComponentName - 0.3
```
See the [formal EBNF specification](doc/wmap-spec.ebnf) for complete grammar details.
## Performance
The parser uses a character-by-character state machine approach for optimal performance:
- **No regex overhead** - direct character comparisons
- **Minimal allocations** - efficient string operations
- **Single-pass parsing** - each line processed once
- **Cache-friendly** - sequential memory access
Benchmark on a typical map with ~120 entities:
- **~50,000+ operations/second**
- **~0.02ms average parse time**
Run benchmarks yourself:
```bash
npm run benchmark
```
## Error Handling
The parser is designed to be fault-tolerant:
- Invalid lines are silently ignored
- Partial entities are not added to the result
- Empty lines and whitespace are handled gracefully
- Different line endings (LF, CRLF, CR) are all supported
## Development
### Running Tests
```bash
npm test
```
Tests use Node's built-in test runner (`node:test`) with no external dependencies.
### Running Benchmarks
```bash
npm run benchmark
```
## License
AGPL-v3
## Contributing
This is a focused, performance-oriented parser. Contributions should maintain:
- Zero runtime dependencies
- High performance characteristics
- Simple, readable code
- Comprehensive test coverage
## See Also
- [wmap specification](doc/wmap-spec.ebnf) - Formal grammar
- [Wardley Maps](https://wardleymaps.com/) - Learn about Wardley Mapping
|