worldformat 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +68 -2
- package/dist/index.d.ts +4 -0
- package/dist/index.js +98 -0
- package/package.json +42 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 L. C. Maren
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,69 @@
|
|
|
1
|
-
#
|
|
1
|
+
# World Format
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Reference implementation of the **World** text format.
|
|
4
|
+
|
|
5
|
+
This repository contains a minimal parser and related utilities
|
|
6
|
+
for reading and writing World documents.
|
|
7
|
+
|
|
8
|
+
The World format is a human-editable snapshot format designed for
|
|
9
|
+
small systems and human–AI workflows.
|
|
10
|
+
|
|
11
|
+
## Status
|
|
12
|
+
|
|
13
|
+
⚠️ Experimental.
|
|
14
|
+
|
|
15
|
+
- APIs may change without notice
|
|
16
|
+
- No stability or backward-compatibility guarantees
|
|
17
|
+
- Test coverage is minimal or nonexistent
|
|
18
|
+
|
|
19
|
+
## Specification
|
|
20
|
+
|
|
21
|
+
The format definition lives here:
|
|
22
|
+
|
|
23
|
+
https://worldformat.org
|
|
24
|
+
|
|
25
|
+
This repository follows the specification, but may lead or lag it
|
|
26
|
+
as the format evolves.
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
npm install worldformat
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { parse, validate } from "worldformat";
|
|
36
|
+
|
|
37
|
+
const world = parse(`
|
|
38
|
+
[task]
|
|
39
|
+
name = "Test"
|
|
40
|
+
done = false
|
|
41
|
+
`);
|
|
42
|
+
// → { task: { name: "Test", done: false } }
|
|
43
|
+
|
|
44
|
+
validate("name = 1"); // → false: an attribute needs a node above it
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
`parse` throws on an invalid document; `validate` returns `false` instead.
|
|
48
|
+
|
|
49
|
+
The package is ESM only.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This repository may grow to include:
|
|
54
|
+
|
|
55
|
+
- parsers
|
|
56
|
+
- generators
|
|
57
|
+
- format utilities
|
|
58
|
+
|
|
59
|
+
It intentionally does **not** include:
|
|
60
|
+
|
|
61
|
+
- schema enforcement
|
|
62
|
+
- validation rules
|
|
63
|
+
- storage layers
|
|
64
|
+
|
|
65
|
+
Those concerns belong elsewhere.
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
MIT — see [LICENSE](LICENSE).
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
function parseValue(raw) {
|
|
2
|
+
// string
|
|
3
|
+
if (raw.startsWith('"') && raw.endsWith('"')) {
|
|
4
|
+
return raw.slice(1, -1);
|
|
5
|
+
}
|
|
6
|
+
// boolean
|
|
7
|
+
if (raw === "true")
|
|
8
|
+
return true;
|
|
9
|
+
if (raw === "false")
|
|
10
|
+
return false;
|
|
11
|
+
// number
|
|
12
|
+
return Number(raw);
|
|
13
|
+
}
|
|
14
|
+
export function parse(text) {
|
|
15
|
+
const root = {};
|
|
16
|
+
const lines = text.split(/\r?\n/);
|
|
17
|
+
let currentPath = null;
|
|
18
|
+
let currentNode = null;
|
|
19
|
+
for (const rawLine of lines) {
|
|
20
|
+
const line = stripComment(rawLine);
|
|
21
|
+
if (!line)
|
|
22
|
+
continue;
|
|
23
|
+
// Node declaration
|
|
24
|
+
if (line.startsWith("[") && line.endsWith("]")) {
|
|
25
|
+
const path = line.slice(1, -1).split("/");
|
|
26
|
+
let cursor = root;
|
|
27
|
+
for (let i = 0; i < path.length; i++) {
|
|
28
|
+
const key = path[i];
|
|
29
|
+
const isLeaf = i === path.length - 1;
|
|
30
|
+
if (isLeaf) {
|
|
31
|
+
if (!(key in cursor)) {
|
|
32
|
+
cursor[key] = {};
|
|
33
|
+
currentNode = cursor[key];
|
|
34
|
+
}
|
|
35
|
+
else if (Array.isArray(cursor[key])) {
|
|
36
|
+
const obj = {};
|
|
37
|
+
cursor[key].push(obj);
|
|
38
|
+
currentNode = obj;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
const first = cursor[key];
|
|
42
|
+
const obj = {};
|
|
43
|
+
cursor[key] = [first, obj];
|
|
44
|
+
currentNode = obj;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
if (!(key in cursor)) {
|
|
49
|
+
cursor[key] = {};
|
|
50
|
+
}
|
|
51
|
+
else if (Array.isArray(cursor[key])) {
|
|
52
|
+
throw new Error(`Ambiguous parent path: "${path.slice(0, i + 1).join("/")}"`);
|
|
53
|
+
}
|
|
54
|
+
cursor = cursor[key];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
currentPath = path;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
// Attribute
|
|
61
|
+
if (currentNode) {
|
|
62
|
+
const idx = line.indexOf("=");
|
|
63
|
+
if (idx === -1) {
|
|
64
|
+
throw new Error(`Invalid line: "${line}"`);
|
|
65
|
+
}
|
|
66
|
+
const key = line.slice(0, idx).trim();
|
|
67
|
+
const rawValue = line.slice(idx + 1).trim();
|
|
68
|
+
currentNode[key] = parseValue(rawValue);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
// If we reach here, the line is invalid
|
|
72
|
+
throw new Error(`Invalid line: "${line}"`);
|
|
73
|
+
}
|
|
74
|
+
return root;
|
|
75
|
+
}
|
|
76
|
+
export function validate(text) {
|
|
77
|
+
try {
|
|
78
|
+
parse(text);
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function stripComment(line) {
|
|
86
|
+
let inString = false;
|
|
87
|
+
for (let i = 0; i < line.length; i++) {
|
|
88
|
+
const ch = line[i];
|
|
89
|
+
if (ch === '"') {
|
|
90
|
+
inString = !inString;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (ch === "#" && !inString) {
|
|
94
|
+
return line.slice(0, i).trim();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return line.trim();
|
|
98
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "worldformat",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Reference implementation of the World format (parser and related utilities).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
20
|
+
"check": "tsc --noEmit",
|
|
21
|
+
"test": "tsx tests/parse.test.ts",
|
|
22
|
+
"prepack": "pnpm build"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"worldformat",
|
|
26
|
+
"parser"
|
|
27
|
+
],
|
|
28
|
+
"author": "L. C. Maren",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"packageManager": "pnpm@10.27.0",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/lcmaren/worldformat.git"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/lcmaren/worldformat#readme",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/lcmaren/worldformat/issues"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "^25.0.3",
|
|
41
|
+
"tsx": "^4.21.0",
|
|
42
|
+
"typescript": "^5.9.3"
|
|
43
|
+
}
|
|
44
|
+
}
|