@zenoaihq/tson 1.0.1 → 1.1.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/dist/index.d.ts CHANGED
@@ -21,17 +21,27 @@ type TSONObject = {
21
21
  * Any valid TSON value
22
22
  */
23
23
  type TSONValue = TSONPrimitive | TSONArray | TSONObject;
24
+ /**
25
+ * Schema info for nested object/array structures
26
+ * - schema: nested schema keys (or null if no schema)
27
+ * - count: array count (or null if single object)
28
+ */
29
+ interface SchemaInfo {
30
+ schema: string[] | null;
31
+ count: number | null;
32
+ }
24
33
  /**
25
34
  * Schema map for nested object structures
26
- * Maps field names to their nested schemas (or null if no schema)
35
+ * Maps field names to their schema info
27
36
  */
28
- type SchemaMap = Record<string, string[] | null>;
37
+ type SchemaMap = Record<string, SchemaInfo>;
29
38
  /**
30
39
  * Result of parsing a key with optional schema notation
31
40
  */
32
41
  interface KeySchema {
33
42
  keyName: string;
34
43
  schema: string[] | null;
44
+ count: number | null;
35
45
  }
36
46
  /**
37
47
  * Result of parsing keys with optional row count
@@ -93,6 +103,154 @@ declare function loads(s: string): TSONValue;
93
103
  */
94
104
  declare function load(filePath: string): Promise<TSONValue>;
95
105
 
106
+ /**
107
+ * TSON Prettify
108
+ *
109
+ * Format TSON strings for human readability.
110
+ * CSV-like format: schema on top, one row/value per line.
111
+ */
112
+ /**
113
+ * Format a compact TSON string for human readability.
114
+ *
115
+ * Uses CSV-like formatting:
116
+ * - Schema declaration on first line
117
+ * - Each row/value on its own line with leading delimiter
118
+ *
119
+ * @example
120
+ * prettify('{@id,name#2|1,Alice|2,Bob}')
121
+ * // Returns: '{@id,name#2\n|1,Alice\n|2,Bob}'
122
+ *
123
+ * @param tsonStr - Compact TSON string
124
+ * @param indent - Indentation string (default: 2 spaces)
125
+ * @returns Pretty-formatted TSON string
126
+ */
127
+ declare function prettify(tsonStr: string, indent?: string): string;
128
+ /**
129
+ * Remove all formatting from a TSON string.
130
+ *
131
+ * @param tsonStr - Pretty-formatted TSON string
132
+ * @returns Compact single-line TSON string
133
+ */
134
+ declare function minify(tsonStr: string): string;
135
+
136
+ /**
137
+ * TSON File Utilities
138
+ *
139
+ * Convenience functions for working with TSON and JSON files.
140
+ * Includes support for:
141
+ * - Loading JSON/TSON files
142
+ * - Saving to TSON with formatting options
143
+ *
144
+ * Note: These functions are Node.js only (use dynamic imports for fs).
145
+ */
146
+ type FormatOption = 'compact' | 'pretty';
147
+ /**
148
+ * Load a JSON file and return JavaScript data structure.
149
+ *
150
+ * @example
151
+ * const data = await loadJson('data.json');
152
+ *
153
+ * @param filepath - Path to JSON file
154
+ * @returns Parsed JavaScript object
155
+ */
156
+ declare function loadJson(filepath: string): Promise<unknown>;
157
+ /**
158
+ * Load a TSON file and return JavaScript data structure.
159
+ *
160
+ * @example
161
+ * const data = await loadTson('data.tson');
162
+ *
163
+ * @param filepath - Path to TSON file
164
+ * @returns Parsed JavaScript object
165
+ */
166
+ declare function loadTson(filepath: string): Promise<unknown>;
167
+ /**
168
+ * Save JavaScript data to a TSON file.
169
+ *
170
+ * @example
171
+ * await saveTson(data, 'output.tson');
172
+ * await saveTson(data, 'output.tson', { format: 'pretty' });
173
+ *
174
+ * @param data - JavaScript object to serialize
175
+ * @param filepath - Output file path
176
+ * @param options - Formatting options
177
+ */
178
+ declare function saveTson(data: unknown, filepath: string, options?: {
179
+ format?: FormatOption;
180
+ indent?: string;
181
+ }): Promise<void>;
182
+ /**
183
+ * Save a TSON string directly to file.
184
+ *
185
+ * @example
186
+ * await saveTsonString(tsonData, 'output.tson');
187
+ * await saveTsonString(tsonData, 'output.tson', { format: 'pretty' });
188
+ *
189
+ * @param tsonStr - TSON formatted string
190
+ * @param filepath - Output file path
191
+ * @param options - Formatting options
192
+ */
193
+ declare function saveTsonString(tsonStr: string, filepath: string, options?: {
194
+ format?: FormatOption | null;
195
+ indent?: string;
196
+ }): Promise<void>;
197
+ /**
198
+ * Convert a JSON file to TSON format.
199
+ *
200
+ * @example
201
+ * const tsonStr = await jsonToTson('data.json');
202
+ * await jsonToTson('data.json', 'data.tson');
203
+ *
204
+ * @param inputPath - Path to input JSON file
205
+ * @param outputPath - Path to output TSON file (optional)
206
+ * @param options - Formatting options
207
+ * @returns TSON string representation
208
+ */
209
+ declare function jsonToTson(inputPath: string, outputPath?: string, options?: {
210
+ format?: FormatOption;
211
+ }): Promise<string>;
212
+ /**
213
+ * Convert a TSON file to JSON format.
214
+ *
215
+ * @example
216
+ * const jsonStr = await tsonToJson('data.tson');
217
+ * await tsonToJson('data.tson', 'data.json');
218
+ *
219
+ * @param inputPath - Path to input TSON file
220
+ * @param outputPath - Path to output JSON file (optional)
221
+ * @param options - Formatting options
222
+ * @returns JSON string representation
223
+ */
224
+ declare function tsonToJson(inputPath: string, outputPath?: string, options?: {
225
+ indent?: number;
226
+ }): Promise<string>;
227
+ /**
228
+ * Read a TSON file as raw string (without parsing).
229
+ *
230
+ * Useful when you want to prettify/minify without deserializing.
231
+ *
232
+ * @param filepath - Path to TSON file
233
+ * @returns Raw TSON string
234
+ */
235
+ declare function readTsonString(filepath: string): Promise<string>;
236
+ /**
237
+ * Prettify a TSON file in place or to a new file.
238
+ *
239
+ * @param inputPath - Path to input TSON file
240
+ * @param outputPath - Path to output file (default: overwrite input)
241
+ * @param indent - Indentation string (default: 2 spaces)
242
+ * @returns Prettified TSON string
243
+ */
244
+ declare function prettifyFile(inputPath: string, outputPath?: string, indent?: string): Promise<string>;
245
+ /**
246
+ * Minify a TSON file in place or to a new file.
247
+ *
248
+ * @param inputPath - Path to input TSON file
249
+ * @param outputPath - Path to output file (default: overwrite input)
250
+ * @returns Minified TSON string
251
+ */
252
+ declare function minifyFile(inputPath: string, outputPath?: string): Promise<string>;
253
+
96
254
  /**
97
255
  * TSON Utility Functions
98
256
  *
@@ -127,6 +285,7 @@ declare function escapeString(value: string): string;
127
285
  * Unescape a quoted string back to its original form.
128
286
  *
129
287
  * Reverses the escaping done by escapeString().
288
+ * Must process character by character to handle sequences like \\n correctly.
130
289
  */
131
290
  declare function unescapeString(value: string): string;
132
291
  /**
@@ -153,20 +312,28 @@ declare function isUniformObjectArray(data: unknown): data is TSONObject[];
153
312
  */
154
313
  declare function splitByDelimiter(text: string, delimiter: string): string[];
155
314
  /**
156
- * Parse a key which may include nested schema notation.
315
+ * Parse a key which may include nested schema notation and optional array count.
316
+ *
317
+ * The array count can be specified INSIDE the parentheses to avoid ambiguity:
318
+ * - `key(@schema#N)` means key is an array of N objects with the given schema
319
+ * - `key(@schema)` means key is a single object with the given schema
157
320
  *
158
321
  * Examples:
159
- * "name" -> { keyName: "name", schema: null }
160
- * "address(@city,zip)" -> { keyName: "address", schema: ["city", "zip"] }
161
- * "location(@coords(@lat,lng))" -> { keyName: "location", schema: ["coords(@lat,lng)"] }
322
+ * "name" -> { keyName: "name", schema: null, count: null }
323
+ * "address(@city,zip)" -> { keyName: "address", schema: ["city", "zip"], count: null }
324
+ * "characters(@name,role#2)" -> { keyName: "characters", schema: ["name", "role"], count: 2 }
162
325
  */
163
326
  declare function parseKeySchema(keyString: string): KeySchema;
164
327
  /**
165
- * Build a mapping of field names to their nested schemas.
328
+ * Build a mapping of field names to their nested schemas and array counts.
166
329
  *
167
330
  * Example:
168
- * ["id", "address(@city,zip)"]
169
- * -> { id: null, address: ["city", "zip"] }
331
+ * ["id", "address(@city,zip)", "items(@x,y#2)"]
332
+ * -> {
333
+ * id: { schema: null, count: null },
334
+ * address: { schema: ["city", "zip"], count: null },
335
+ * items: { schema: ["x", "y"], count: 2 }
336
+ * }
170
337
  */
171
338
  declare function buildSchemaMap(keys: string[]): SchemaMap;
172
339
  /**
@@ -176,4 +343,4 @@ declare function buildSchemaMap(keys: string[]): SchemaMap;
176
343
  */
177
344
  declare function parseKeys(keysStr: string): ParsedKeys;
178
345
 
179
- export { type KeySchema, type ParsedKeys, type SchemaMap, type TSONArray, type TSONObject, type TSONPrimitive, type TSONValue, buildSchemaMap, dump, dumps, escapeString, formatPrimitive, isUniformObjectArray, load, loads, looksLikeNumber, needsQuoting, parseKeySchema, parseKeys, parsePrimitive, splitByDelimiter, unescapeString };
346
+ export { type KeySchema, type ParsedKeys, type SchemaInfo, type SchemaMap, type TSONArray, type TSONObject, type TSONPrimitive, type TSONValue, buildSchemaMap, dump, dumps, escapeString, formatPrimitive, isUniformObjectArray, jsonToTson, load, loadJson, loadTson, loads, looksLikeNumber, minify, minifyFile, needsQuoting, parseKeySchema, parseKeys, parsePrimitive, prettify, prettifyFile, readTsonString, saveTson, saveTsonString, splitByDelimiter, tsonToJson, unescapeString };