@drawcall/charta 0.1.16 → 0.1.17
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/dist/generator.d.ts +8 -0
- package/dist/generator.d.ts.map +1 -0
- package/dist/generator.js +51 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Call } from './parser.js';
|
|
2
|
+
/**
|
|
3
|
+
* Generates a charta text from a parsed AST.
|
|
4
|
+
* This is the inverse of parse() - it takes the Call[][][] structure
|
|
5
|
+
* and produces the original charta text format.
|
|
6
|
+
*/
|
|
7
|
+
export declare function generate(ast: Call[][][]): string;
|
|
8
|
+
//# sourceMappingURL=generator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../src/generator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAY,MAAM,aAAa,CAAA;AAEjD;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,GAAG,MAAM,CAsBhD"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a charta text from a parsed AST.
|
|
3
|
+
* This is the inverse of parse() - it takes the Call[][][] structure
|
|
4
|
+
* and produces the original charta text format.
|
|
5
|
+
*/
|
|
6
|
+
export function generate(ast) {
|
|
7
|
+
if (ast.length === 0)
|
|
8
|
+
return '';
|
|
9
|
+
const rows = [];
|
|
10
|
+
for (const row of ast) {
|
|
11
|
+
const cells = [];
|
|
12
|
+
for (const cell of row) {
|
|
13
|
+
const calls = [];
|
|
14
|
+
for (const call of cell) {
|
|
15
|
+
calls.push(generateCall(call));
|
|
16
|
+
}
|
|
17
|
+
cells.push(calls.join(' '));
|
|
18
|
+
}
|
|
19
|
+
rows.push(cells.join(' | '));
|
|
20
|
+
}
|
|
21
|
+
return rows.join('\n');
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Generates a single call string from a Call object.
|
|
25
|
+
* Format: name(param1, param2, key=value, ...)
|
|
26
|
+
*/
|
|
27
|
+
function generateCall(call) {
|
|
28
|
+
const args = [];
|
|
29
|
+
// Add positional parameters
|
|
30
|
+
for (const param of call.locationParams) {
|
|
31
|
+
args.push(formatValue(param));
|
|
32
|
+
}
|
|
33
|
+
// Add key-value parameters
|
|
34
|
+
for (const [key, value] of call.keyParams) {
|
|
35
|
+
args.push(`${key}=${formatValue(value)}`);
|
|
36
|
+
}
|
|
37
|
+
return `${call.name}(${args.join(', ')})`;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Formats a value, quoting it if necessary.
|
|
41
|
+
* A value needs to be quoted if it contains special characters
|
|
42
|
+
* that would interfere with parsing: space, tab, comma, parentheses, equals, pipe, newline, or quotes.
|
|
43
|
+
*/
|
|
44
|
+
function formatValue(value) {
|
|
45
|
+
// Check if value contains characters that require quoting
|
|
46
|
+
if (/[ \t,()=|\n"]/.test(value)) {
|
|
47
|
+
// Escape any quotes within the value and wrap in quotes
|
|
48
|
+
return `"${value.replace(/"/g, '\\"')}"`;
|
|
49
|
+
}
|
|
50
|
+
return value;
|
|
51
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,aAAa,CAAA;AAC3B,cAAc,oBAAoB,CAAA;AAClC,cAAc,kBAAkB,CAAA;AAChC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,iCAAiC,CAAA;AAC/C,cAAc,oBAAoB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,aAAa,CAAA;AAC3B,cAAc,oBAAoB,CAAA;AAClC,cAAc,kBAAkB,CAAA;AAChC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,iCAAiC,CAAA;AAC/C,cAAc,oBAAoB,CAAA"}
|
package/dist/index.js
CHANGED