@dnd-ed/jsshenanigans 0.0.0-stage → 1.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/LICENSE +28 -0
- package/NOTICE.md +89 -0
- package/README.md +97 -2
- package/package.json +112 -4
- package/src/character.d.ts +82 -0
- package/src/character.js +335 -0
- package/src/codec.d.ts +20 -0
- package/src/codec.js +211 -0
- package/src/conditions.d.ts +40 -0
- package/src/conditions.js +79 -0
- package/src/defaults.d.ts +15 -0
- package/src/defaults.js +72 -0
- package/src/errors.d.ts +8 -0
- package/src/errors.js +20 -0
- package/src/index.d.ts +46 -0
- package/src/index.js +49 -0
- package/src/nodes.d.ts +356 -0
- package/src/nodes.js +739 -0
- package/src/tools.d.ts +28 -0
- package/src/tools.js +88 -0
- package/src/types.d.ts +875 -0
- package/src/types.js +3532 -0
- package/src/utils.d.ts +33 -0
- package/src/utils.js +52 -0
package/src/tools.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface SkillInfo {
|
|
2
|
+
kind?: string | null;
|
|
3
|
+
name?: string | null;
|
|
4
|
+
fields?: string[];
|
|
5
|
+
nodes?: Array<string | undefined>;
|
|
6
|
+
conditions?: Array<string | undefined>;
|
|
7
|
+
branches?: string[];
|
|
8
|
+
raw?: unknown;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface DescribeResult {
|
|
12
|
+
skill_count: number;
|
|
13
|
+
skills: SkillInfo[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Return the full payload of `code` as pretty-printed JSON.
|
|
18
|
+
*
|
|
19
|
+
* @param code A character code.
|
|
20
|
+
* @param options `indent` (default 2) and `expand_data` (default true).
|
|
21
|
+
*/
|
|
22
|
+
export declare function dumps(
|
|
23
|
+
code: string,
|
|
24
|
+
options?: { indent?: number; expand_data?: boolean },
|
|
25
|
+
): string;
|
|
26
|
+
|
|
27
|
+
/** Return a compact structural summary of a character code. */
|
|
28
|
+
export declare function describe(code: string): DescribeResult;
|
package/src/tools.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read-only helpers for looking inside a character code you did not build.
|
|
3
|
+
*
|
|
4
|
+
* These exist because the fastest way to learn what the live game supports is
|
|
5
|
+
* to decode a code *it* produced and read the fields straight off it.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as codec from "./codec.js";
|
|
9
|
+
import { InvalidCharacterCodeError } from "./errors.js";
|
|
10
|
+
|
|
11
|
+
/** Recursively parse nested `DATA` JSON strings into objects. */
|
|
12
|
+
function expand(obj) {
|
|
13
|
+
if (Array.isArray(obj)) return obj.map(expand);
|
|
14
|
+
if (obj !== null && typeof obj === "object") {
|
|
15
|
+
const out = {};
|
|
16
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
17
|
+
if (key === "DATA" && typeof value === "string") {
|
|
18
|
+
try {
|
|
19
|
+
out[key] = expand(JSON.parse(value));
|
|
20
|
+
} catch {
|
|
21
|
+
out[key] = value;
|
|
22
|
+
}
|
|
23
|
+
} else {
|
|
24
|
+
out[key] = expand(value);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
return obj;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Return the full payload of `code` as pretty-printed JSON.
|
|
34
|
+
*
|
|
35
|
+
* @param {string} code
|
|
36
|
+
* @param {object} [options]
|
|
37
|
+
* @param {number} [options.indent] JSON indentation.
|
|
38
|
+
* @param {boolean} [options.expand_data] Parse the nested `DATA` strings so the
|
|
39
|
+
* whole node graph is visible instead of one escaped blob.
|
|
40
|
+
* @returns {string}
|
|
41
|
+
*/
|
|
42
|
+
export function dumps(code, { indent = 2, expand_data = true } = {}) {
|
|
43
|
+
const obj = codec.decode(code);
|
|
44
|
+
return JSON.stringify(expand_data ? expand(obj) : obj, null, indent);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Return a compact structural summary of a character code.
|
|
49
|
+
* @param {string} code
|
|
50
|
+
* @returns {{skill_count: number, skills: object[]}}
|
|
51
|
+
*/
|
|
52
|
+
export function describe(code) {
|
|
53
|
+
const obj = codec.decode(code);
|
|
54
|
+
if (!Array.isArray(obj)) {
|
|
55
|
+
throw new InvalidCharacterCodeError("top-level payload is not a list of skills");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const skills = obj.map((entry) => {
|
|
59
|
+
if (entry === null || typeof entry !== "object" || Array.isArray(entry)) {
|
|
60
|
+
return { kind: null, name: null, raw: entry };
|
|
61
|
+
}
|
|
62
|
+
const info = {
|
|
63
|
+
kind: entry.K_NAME ?? null,
|
|
64
|
+
name: entry.NAME ?? null,
|
|
65
|
+
fields: Object.keys(entry).sort(),
|
|
66
|
+
};
|
|
67
|
+
if (typeof entry.DATA === "string") {
|
|
68
|
+
let inner = null;
|
|
69
|
+
try {
|
|
70
|
+
inner = JSON.parse(entry.DATA);
|
|
71
|
+
} catch {
|
|
72
|
+
inner = null;
|
|
73
|
+
}
|
|
74
|
+
if (inner !== null && typeof inner === "object" && !Array.isArray(inner)) {
|
|
75
|
+
info.nodes = (inner.Line ?? [])
|
|
76
|
+
.filter((node) => node !== null && typeof node === "object")
|
|
77
|
+
.map((node) => node.K_NAME);
|
|
78
|
+
info.conditions = (inner.Req ?? [])
|
|
79
|
+
.filter((cond) => cond !== null && typeof cond === "object")
|
|
80
|
+
.map((cond) => cond.K_NAME);
|
|
81
|
+
if (inner.Branch) info.branches = Object.keys(inner.Branch).sort();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return info;
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
return { skill_count: obj.length, skills };
|
|
88
|
+
}
|