UNPKG

zon-format

Version:

ZON: The most token-efficient serialization format for LLMs - beats CSV, TOON, JSON, and all competitors

137 lines (136 loc) 3.68 kB
"use strict"; /** * Format Conversion Utilities * * Convert between ZON, JSON, and Binary formats */ Object.defineProperty(exports, "__esModule", { value: true }); exports.BatchConverter = void 0; exports.jsonToZon = jsonToZon; exports.zonToJson = zonToJson; exports.zonToBinary = zonToBinary; exports.binaryToZon = binaryToZon; exports.jsonToBinary = jsonToBinary; exports.binaryToJson = binaryToJson; exports.autoConvert = autoConvert; const encoder_1 = require("../core/encoder"); const decoder_1 = require("../core/decoder"); const binary_1 = require("../binary"); /** * Convert JSON to ZON text format */ function jsonToZon(json, options) { const data = typeof json === 'string' ? JSON.parse(json) : json; return (0, encoder_1.encode)(data); } /** * Convert ZON text to JSON */ function zonToJson(zon, options) { const data = (0, decoder_1.decode)(zon); return (options === null || options === void 0 ? void 0 : options.pretty) ? JSON.stringify(data, null, 2) : JSON.stringify(data); } /** * Convert ZON text to Binary ZON */ function zonToBinary(zon) { const data = (0, decoder_1.decode)(zon); return (0, binary_1.encodeBinary)(data); } /** * Convert Binary ZON to ZON text */ function binaryToZon(binary) { const data = (0, binary_1.decodeBinary)(binary); return (0, encoder_1.encode)(data); } /** * Convert JSON to Binary ZON */ function jsonToBinary(json) { const data = typeof json === 'string' ? JSON.parse(json) : json; return (0, binary_1.encodeBinary)(data); } /** * Convert Binary ZON to JSON */ function binaryToJson(binary, options) { const data = (0, binary_1.decodeBinary)(binary); return (options === null || options === void 0 ? void 0 : options.pretty) ? JSON.stringify(data, null, 2) : JSON.stringify(data); } /** * Batch converter for multiple files */ class BatchConverter { constructor() { this.conversions = []; } /** * Queue a conversion */ add(data, from, to) { this.conversions.push({ from, to, data }); } /** * Execute all queued conversions */ convert() { return this.conversions.map(({ from, to, data }) => { return this.convertSingle(data, from, to); }); } /** * Convert a single item */ convertSingle(data, from, to) { let normalized; if (from === 'json') { normalized = typeof data === 'string' ? JSON.parse(data) : data; } else if (from === 'zon') { normalized = (0, decoder_1.decode)(data); } else if (from === 'binary') { normalized = (0, binary_1.decodeBinary)(data); } if (to === 'json') { return JSON.stringify(normalized, null, 2); } else if (to === 'zon') { return (0, encoder_1.encode)(normalized); } else if (to === 'binary') { return (0, binary_1.encodeBinary)(normalized); } return normalized; } /** * Clear all queued conversions */ clear() { this.conversions = []; } } exports.BatchConverter = BatchConverter; /** * Auto-detect format and convert */ function autoConvert(input, targetFormat, options) { let sourceFormat; if (input instanceof Uint8Array) { sourceFormat = 'binary'; } else if (input.trim().startsWith('{') || input.trim().startsWith('[')) { sourceFormat = 'json'; } else { sourceFormat = 'zon'; } const converter = new BatchConverter(); converter.add(input, sourceFormat, targetFormat); return converter.convert()[0]; }