UNPKG

tooner

Version:

Token-efficient serialization for LLMs - Convert JSON/YAML/TOML to TOON format

14 lines 16.2 kB
import {parse,stringify}from'yaml';var P=class extends Error{constructor(n,o,r){super(n);this.line=o;this.column=r;this.name="ToonError";}},C=class extends P{constructor(t){super(t),this.name="ToonEncodeError";}},p=class extends P{constructor(t,n,o){super(t,n,o),this.name="ToonDecodeError";}};function U(e,t=","){return e.length===0||e==="true"||e==="false"||e==="null"||/^-?\d+(\.\d+)?(e[+-]?\d+)?$/i.test(e)||/^0\d/.test(e)||/^[[\]{}]/.test(e)||e.includes("[")||e.includes("{")||e==="-"||/^-\s/.test(e)||/[\n\r\t\\"]/.test(e)||/^\s+$/.test(e)||e!==e.trim()?true:!(t===","?/^[\w\s\u0080-\uFFFF]+$/:/^[\w\s,\u0080-\uFFFF]+$/).test(e)}function re(e,t=","){return !!(U(e,t)||e.includes(t)||e.includes(":"))}function D(e){return !!(e.length===0||/^\d+$/.test(e)||e.startsWith("-")||e.includes("-")||e.includes(":")||e.includes(",")||e.includes(" ")||e.includes("[")||e.includes("]")||e.includes("{")||e.includes("}")||e.includes(` `)||e.includes(" ")||e.includes("\r")||e.includes('"'))}function N(e){return e.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t")}function _(e,t){let n="",o=0;for(;o<e.length;)if(e[o]==="\\"&&o+1<e.length){let r=e[o+1];if(r==="n")n+=` `,o+=2;else if(r==="r")n+="\r",o+=2;else if(r==="t")n+=" ",o+=2;else if(r==='"')n+='"',o+=2;else if(r==="\\")n+="\\",o+=2;else {if(t!==void 0)throw new Error(`Invalid escape sequence: \\${r} at line ${t+1}`);n+=e[o],o++;}}else n+=e[o],o++;return n}function z(e,t=false,n=","){return (t?re(e,n):U(e,n))?`"${N(e)}"`:e}function T(e){return D(e)?`"${N(e)}"`:e}function K(e,t){let n=e.trim();if(n.startsWith('"')&&n.endsWith('"')){let o=n.slice(1,-1);return _(o,t)}return n}function $(e){let t=e.trim();if(t.startsWith('"')){let o=1,r=false,i="";for(;o<t.length;){let c=t[o];if(r){i+="\\"+c,r=false,o++;continue}if(c==="\\"){r=true,o++;continue}if(c==='"')return {key:_(i,0),rest:t.slice(o+1),wasQuoted:true};i+=c,o++;}return null}let n=t.match(/^([\w.-]+?)(\[.*|:\s*.*|$)/);return n&&n[1]?{key:n[1],rest:n[2],wasQuoted:false}:null}function Z(e,t={}){let n=typeof t.indent=="number"?" ".repeat(t.indent):t.indent??" ",o=t.delimiter??",",r=t.keyFolding??false,i=t.flattenDepth??(r==="safe"?1/0:0);return Array.isArray(e)?oe(e,n,o):X(e,0,n,o,r,i)}function A(e,t=false,n=","){if(e===null)return "null";if(typeof e=="boolean")return e.toString();if(typeof e=="number")return Object.is(e,-0)?"0":e.toString();if(typeof e=="string")return z(e,t,n);throw new C(`Unsupported primitive type: ${typeof e}`)}function O(e){return e===","?"":e}function I(e){return e.every(t=>t===null||typeof t=="boolean"||typeof t=="number"||typeof t=="string")}function G(e){if(e.length===0||!e.every(n=>n!==null&&typeof n=="object"&&!Array.isArray(n)))return false;let t=Object.keys(e[0]).sort();return e.every(n=>{let o=Object.keys(n).sort();return o.length===t.length&&o.every((r,i)=>r===t[i])})}function H(e){return Object.values(e).every(t=>t===null||typeof t=="boolean"||typeof t=="number"||typeof t=="string")}function J(e,t,n,o){if(!n||n!=="safe"||o<=1||D(e))return null;let r=e,i=t,c=1;for(;i!==null&&typeof i=="object"&&!Array.isArray(i);){let s=i,a=Object.keys(s);if(a.length!==1)break;let g=a[0];if(D(g)||c>=o)break;r=`${r}.${g}`,i=s[g],c++;}return r===e?null:[r,i]}function oe(e,t,n){let o=O(n);if(e.length===0)return `[0${o}]:`;if(I(e)){let i=e.map(c=>A(c,true,n));return `[${e.length}${o}]: ${i.join(n)}`}if(G(e)){let i=e;if(i.every(c=>H(c))){let c=Object.keys(i[0]),s=n===","?",":n,a=c.map(h=>T(h)).join(s),g=[`[${e.length}${o}]{${a}}:`];for(let h of i){let f=c.map(l=>A(h[l],true,n));g.push(`${t}${f.join(n)}`);}return g.join(` `)}}let r=[`[${e.length}${o}]:`];for(let i of e){let c=Q(i,1,t,n);r.push(c);}return r.join(` `)}function se(e,t,n,o,r){let i=o.repeat(n),c=O(r),s=T(e);if(t.length===0)return `${i}${s}[0${c}]:`;let a=t.map(g=>A(g,true,r));return `${i}${s}[${t.length}${c}]: ${a.join(r)}`}function ie(e,t,n,o,r){let i=o.repeat(n),c=O(r),s=T(e);if(t.length===0)return `${i}${s}[0${c}]:`;let a=Object.keys(t[0]),g=r===","?",":r,h=a.map(l=>T(l)).join(g),f=[`${i}${s}[${t.length}${c}]{${h}}:`];for(let l of t){let y=a.map(d=>A(l[d],true,r)).join(r);f.push(`${o.repeat(n+1)}${y}`);}return f.join(` `)}function ce(e,t,n,o,r){let i=o.repeat(n),c=O(r),s=T(e),a=[`${i}${s}[${t.length}${c}]:`];for(let g of t){let h=Q(g,n+1,o,r);a.push(h);}return a.join(` `)}function Q(e,t,n,o){let i=`${n.repeat(t)}- `;if(e===null||typeof e=="boolean"||typeof e=="number"||typeof e=="string")return `${i}${A(e,false,o)}`;if(Array.isArray(e)){let c=O(o);if(e.length===0)return `${i}[0${c}]:`;if(I(e)){let a=e.map(g=>A(g,true,o));return `${i}[${e.length}${c}]: ${a.join(o)}`}let s=[`${i}[${e.length}${c}]:`];for(let a of e){let g=Q(a,t+1,n,o);s.push(g);}return s.join(` `)}if(typeof e=="object"&&e!==null){let c=e,s=Object.keys(c);if(s.length===0)return `${i}{}`;let a=[],g=true;for(let h of s){let f=c[h];if(g){let u=L(h,f,t+1,n,o,false,0).split(` `),y=u[0].trimStart();a.push(`${i}${y}`);for(let d=1;d<u.length;d++){let m=u[d],b=m.startsWith(n)?m.slice(n.length):m;a.push(b);}g=false;}else {let l=L(h,f,t+1,n,o,false,0);a.push(l);}}return a.join(` `)}throw new C(`Unsupported list item type: ${typeof e}`)}function L(e,t,n,o,r,i,c){let s=o.repeat(n);if(i){let a=J(e,t,i,c);if(a){let[g,h]=a;return L(g,h,n,o,r,false,0)}}if(t===null||typeof t=="boolean"||typeof t=="number"||typeof t=="string")return `${s}${T(e)}: ${A(t,false,r)}`;if(Array.isArray(t)){if(t.length===0){let a=O(r);return `${s}${T(e)}[0${a}]:`}if(I(t))return se(e,t,n,o,r);if(G(t)){let a=t;if(a.every(g=>H(g)))return ie(e,a,n,o,r)}return ce(e,t,n,o,r)}if(typeof t=="object"&&t!==null){let a=X(t,n+1,o,r,i,c);return a===""?`${s}${T(e)}:`:[`${s}${T(e)}:`,a].join(` `)}throw new C(`Unsupported field type: ${typeof t}`)}function X(e,t,n,o,r,i){if(e===null||typeof e=="boolean"||typeof e=="number"||typeof e=="string")return A(e,false,o);if(typeof e=="object"&&e!==null&&!Array.isArray(e)){let c=e,s=Object.keys(c);if(s.length===0)return "";let a=new Set(s),g=new Set;if(r&&r==="safe")for(let f of s){let l=c[f],u=J(f,l,r,i);if(u){let[y]=u;a.has(y)&&y!==f&&g.add(f);}}let h=[];for(let f of s){let l=c[f],u=g.has(f)?false:r,y=L(f,l,t,n,o,u,i);h.push(y);}return h.join(` `)}throw new C(`Unsupported value type: ${typeof e}`)}function ue(e,t){for(let n=0;n<e.length;n++){let o=e[n];if(o.trim()==="")continue;let r=o.match(/^(\s*)/)?.[1]||"";if(r.includes(" "))throw new p("Tab characters not allowed in indentation in strict mode",n+1);let i=r.length;if(i>0&&i%t!==0)throw new p(`Invalid indentation: expected multiple of ${t}, got ${i}`,n+1)}}function le(e,t,n){return n.expandPaths!=="safe"||t||!e.includes(".")?false:e.split(".").every(r=>/^[a-zA-Z_]\w*$/.test(r))}function ae(e,t,n){let o={};for(let[r,i]of Object.entries(e)){let c=t.get(r)??false;if(le(r,c,n)){let s=r.split(".");B(o,s,i,n);}else B(o,[r],i,n);}return o}function B(e,t,n,o){if(t.length===0)return;if(t.length===1){let c=t[0],s=e[c];if(s!==void 0){let a=Array.isArray(s)?"array":typeof s=="object"&&s!==null?"object":"primitive",g=Array.isArray(n)?"array":typeof n=="object"&&n!==null?"object":"primitive";if(a!==g){if(o.strict)throw new p(`Path expansion conflict: cannot merge ${g} with ${a}`,0);e[c]=n;return}if(a==="object"&&g==="object"&&!Array.isArray(s)&&!Array.isArray(n)){e[c]={...s,...n};return}}e[c]=n;return}let r=t[0],i=t.slice(1);if(e[r]===void 0)e[r]={};else if(typeof e[r]!="object"||Array.isArray(e[r])){if(o.strict)throw new p("Path expansion conflict: cannot create nested object",0);e[r]={};}B(e[r],i,n,o);}function Y(e,t={}){let n={strict:t.strict??false,indent:t.indent??2,expandPaths:t.expandPaths??"off"};if(e.trim()==="")return {};let o=e.split(` `);n.strict&&ue(o,n.indent);let r=o.filter(s=>s.trim()!=="");if(r.length===1){let s=r[0].trim();if(s.startsWith('"')&&s.endsWith('"')&&s.length>1){let a=$(s);if(!a||!a.rest.trim().startsWith(":"))return v(r[0],0)}if(!s.includes(":"))return v(r[0],0)}if(n.strict&&r.length>1&&r.every(a=>!a.includes(":")&&!a.startsWith("[")))throw new p("Multiple primitives at root not allowed in strict mode",1);let i=r[0];if(i.trim().startsWith("["))return ne(i.trim(),o,0,n,0).value;let c=E(o,0,n);if(n.expandPaths==="safe"&&typeof c.value=="object"&&c.value!==null&&!Array.isArray(c.value)){let s=c.keyMetadata||new Map;return ae(c.value,s,n)}return c.value}function v(e,t){let n=e.trim();if(n.startsWith('"')){if(!n.endsWith('"')||n.length<2)throw new p("Unterminated string",t+1);return K(n,t)}if(n==="true")return true;if(n==="false")return false;if(n==="null")return null;if(/^-?\d+(\.\d+)?([eE][+-]?\d+)?$/.test(n)){if(/^-?0\d/.test(n))return n;let o=Number(n);return Object.is(o,-0)?0:o}return n}function j(e){let t=e.match(/^(\s*)/);return t?t[1].length:0}function M(e){return e.includes(" ")?" ":e.includes("|")?"|":","}function F(e,t){let n=$(e);if(!n)throw new p("Invalid array key",t);let{key:o,rest:r}=n,i=r.match(/^\[([^\]]+)\]:(.*)$/);if(!i)throw new p("Invalid inline array format",t);let c=i[1],s=i[2].trimStart(),a=M(c),g=c.replace(/[\t|]/g,""),h=parseInt(g,10);if(h===0)return {key:o,values:[],delimiter:a};let f=V(s,a);if(f.length!==h)throw new p(`Array count mismatch: expected ${h}, got ${f.length}`,t);let l=f.map(u=>v(u,t));return {key:o,values:l,delimiter:a}}function V(e,t){let n=[],o="",r=false,i=false;for(let c=0;c<e.length;c++){let s=e[c];if(i){o+=s,i=false;continue}if(s==="\\"){o+=s,i=true;continue}if(s==='"'){o+=s,r=!r;continue}if(s===t&&!r){n.push(o.trim()),o="";continue}o+=s;}return n.push(o.trim()),n}function W(e,t,n,o,r=0){let i=$(e);if(!i)throw new p("Invalid tabular array key",n);let{rest:c}=i,s=c.match(/^\[([^\]]+)\]\{([^}]+)\}:\s*$/);if(!s)throw new p("Invalid tabular array header",n);let a=s[1],g=s[2],h=M(a),f=a.replace(/[\t|]/g,""),l=parseInt(f,10),u=V(g,h).map(m=>K(m)),y=[],d=n+1;for(let m=0;m<l;m++){for(;d<t.length&&t[d].trim()==="";){if(o.strict&&m>0&&m<l)throw new p("Blank lines not allowed inside arrays in strict mode",d+1);d++;}if(d>=t.length)throw new p(`Expected ${l} rows, got ${m}`,d);let b=t[d],w=ee(b,u.length,h);if(w.length!==u.length)throw new p(`Row has ${w.length} values, expected ${u.length}`,d);let x={};for(let k=0;k<u.length;k++)x[u[k]]=v(w[k],d);y.push(x),d++;}if(y.length!==l)throw new p(`Tabular array length mismatch: expected ${l} rows, got ${y.length}`,n);return {value:y,linesConsumed:d-n}}function ee(e,t,n=","){let o=e.trim();return V(o,n)}function ne(e,t,n,o,r=0){let i=e.match(/^\[([^\]]+)\]:(.+)$/);if(i){let a=i[1],g=i[2].trimStart(),h=M(a),f=a.replace(/[\t|]/g,""),l=parseInt(f,10);if(l===0)return {value:[],linesConsumed:1};let u=V(g,h);if(u.length!==l)throw new p(`Array count mismatch: expected ${l}, got ${u.length}`,n);return {value:u.map(y=>v(y,n)),linesConsumed:1}}let c=e.match(/^\[([^\]]+)\]\{([^}]+)\}:\s*$/);if(c){let a=c[1],g=c[2],h=M(a),f=a.replace(/[\t|]/g,""),l=parseInt(f,10),u=V(g,h).map(m=>K(m)),y=[],d=n+1;for(let m=0;m<l;m++){if(d>=t.length)throw new p(`Expected ${l} rows, got ${m}`,d);let b=t[d],w=ee(b,u.length,h);if(w.length!==u.length)throw new p(`Row has ${w.length} values, expected ${u.length}`,d);let x={};for(let k=0;k<u.length;k++)x[u[k]]=v(w[k],d);y.push(x),d++;}if(d<t.length){let m=t[d];if(m.trim()!==""&&j(m)>0)throw new p(`Tabular array has more rows than declared count ${l}`,d)}return {value:y,linesConsumed:d-n}}let s=e.match(/^\[([^\]]+)\]:\s*$/);if(s){let g=s[1].replace(/[\t|]/g,""),h=parseInt(g,10);if(h===0)return {value:[],linesConsumed:1};let f=n+1;if(f<t.length){let d=t[f],m=j(d),b=d.trim();if(b.startsWith("- ")||b==="-")return te(t,f,h,m,o,r)}let l=[],u=n+1,y=j(t[u]);for(;l.length<h&&u<t.length;){let d=t[u];if(d.trim()===""){if(o.strict&&l.length>0&&l.length<h)throw new p("Blank lines not allowed inside arrays in strict mode",u+1);u++;continue}if(j(d)<y)break;let b=v(d,u);l.push(b),u++;}return {value:l,linesConsumed:u-n}}throw new p("Invalid root array format",n)}function te(e,t,n,o,r,i=0){let c=[],s=t;for(;c.length<n&&s<e.length;){let a=e[s];if(a.trim()===""){if(r.strict&&c.length>0&&c.length<n)throw new p("Blank lines not allowed inside arrays in strict mode",s+1);s++;continue}let g=j(a),h=a.trim();if(g===o&&h.startsWith("- ")){let f=h.slice(2).trim();if(f===""){c.push({}),s++;continue}if(f.match(/^\[[^\]]+\]:/)){let l=ne(f,e,s,r,i);c.push(l.value),s+=l.linesConsumed;continue}if(f.includes(":")){let l={};if(f.match(/^("([^"\\]|\\.)*"|\w+)\[[^\]]+\]:\s*\S+/)){let u=F(f,s);l[u.key]=u.values,s++;}else if(f.match(/^("([^"\\]|\\.)*"|\w+)\[[^\]]+\]\{[^}]+\}:\s*$/)){let u=$(f);if(u){let y=W(f,e,s,r,i);l[u.key]=y.value,s+=y.linesConsumed;}else s++;}else if(f.match(/^("([^"\\]|\\.)*"|\w+)\[[^\]]+\]:\s*$/)){let u=$(f);if(u){let y=q(f,e,s,r,i);l[u.key]=y.value,s+=y.linesConsumed;}else s++;}else {let u=$(f);if(u){let{key:y,rest:d}=u,m=d.match(/^:(.*)$/);if(m){let b=m[1].trim();if(b===""){let w=s+1,x=E(e,w,r,i+1);l[y]=x.value,s=w+x.linesConsumed;}else l[y]=v(b,s),s++;}else s++;}else s++;}for(;s<e.length;){let u=e[s];if(u.trim()===""){s++;continue}if(j(u)<=o)break;let d=u.trim();if(d.match(/^("([^"\\]|\\.)*"|\w+)\[[^\]]+\]:\s*\S+/)){let R=F(d,s);l[R.key]=R.values,s++;continue}if(d.match(/^("([^"\\]|\\.)*"|\w+)\[[^\]]+\]\{[^}]+\}:\s*$/)){let R=$(d);if(R){let S=W(d,e,s,r,i);l[R.key]=S.value,s+=S.linesConsumed;}else break;continue}if(d.match(/^("([^"\\]|\\.)*"|\w+)\[[^\]]+\]:\s*$/)){let R=$(d);if(R){let S=q(d,e,s,r,i);l[R.key]=S.value,s+=S.linesConsumed;}else break;continue}let m=$(d);if(!m)break;let{key:b,rest:w}=m,x=w.match(/^:(.*)$/);if(!x)break;let k=x[1].trim();if(k===""){let R=s+1,S=E(e,R,r,i+1);l[b]=S.value,s=R+S.linesConsumed;}else l[b]=v(k,s),s++;}c.push(l);continue}c.push(v(f,s)),s++;continue}if(g===o&&h==="-"){c.push({}),s++;continue}s++;}if(c.length!==n)throw new p(`Array length mismatch: expected ${n}, got ${c.length}`,t);if(s<e.length){let a=e[s],g=j(a),h=a.trim();if(g===o&&(h.startsWith("- ")||h==="-"))throw new p(`Array length mismatch: expected ${n}, but found more items`,s+1)}return {value:c,linesConsumed:s-t}}function q(e,t,n,o,r=0){if(e.includes("{"))return W(e,t,n,o,r);let i=$(e);if(!i)throw new p("Invalid array header",n);let{rest:c}=i,s=c.match(/^\[([^\]]+)\]:\s*$/);if(!s)throw new p("Invalid array header format",n);let g=s[1].replace(/[\t|]/g,""),h=parseInt(g,10);if(h===0)return {value:[],linesConsumed:1};let f=n+1;if(f<t.length){let d=t[f],m=j(d),b=d.trim();if(b.startsWith("- ")||b==="-"){let w=te(t,f,h,m,o,r);return {value:w.value,linesConsumed:w.linesConsumed+1}}}let l=[],u=n+1,y=j(t[u]);for(;l.length<h&&u<t.length;){let d=t[u];if(d.trim()===""){if(o.strict&&l.length>0&&l.length<h)throw new p("Blank lines not allowed inside arrays in strict mode",u+1);u++;continue}if(j(d)<y)break;let b=v(d,u);l.push(b),u++;}return {value:l,linesConsumed:u-n}}function E(e,t,n,o=0){let r=t,i={},c=new Map,s=-1;for(let g=t;g<e.length;g++)if(e[g].trim()!==""){s=j(e[g]);break}if(s===-1)return {value:null,linesConsumed:0};for(;r<e.length;){let g=e[r];if(g.trim()===""){r++;continue}let h=j(g);if(h<s&&r>t)break;if(h!==s){r++;continue}let f=g.trim();if(f.match(/^("([^"\\]|\\.)*"|[\w.-]+)\[[^\]]+\]:\s*\S+/)){let l=F(f,r),u=$(f);u&&c.set(l.key,u.wasQuoted??false),i[l.key]=l.values,r++;continue}if(f.match(/^("([^"\\]|\\.)*"|[\w.-]+)\[[^\]]+\]\{[^}]+\}:\s*$/)){let l=W(f,e,r,n,o),u=$(f);u&&(c.set(u.key,u.wasQuoted??false),i[u.key]=l.value),r+=l.linesConsumed;continue}if(f.match(/^("([^"\\]|\\.)*"|[\w.-]+)\[[^\]]+\]:\s*$/)){let l=q(f,e,r,n,o),u=$(f);u&&(c.set(u.key,u.wasQuoted??false),i[u.key]=l.value),r+=l.linesConsumed;continue}if(f.includes(":")){let l=$(f);if(!l){r++;continue}let{key:u,rest:y}=l,d=y.match(/^:(.*)$/);if(!d)throw new p("Missing colon in key-value context",r+1);let m=d[1].trim();if(c.set(u,l.wasQuoted??false),m===""){let b=r+1,w=E(e,b,n,o+1);i[u]=w.value===null?{}:w.value,r=b+w.linesConsumed;}else i[u]=v(m,r),r++;continue}if(o>0)throw new p("Missing colon in key-value context",r+1);r++;}return Object.keys(i).length===0?{value:null,linesConsumed:r-t}:{value:i,linesConsumed:r-t,keyMetadata:c}}function xe(e,t){let n=parse(e);return Z(n,t)}function Re(e,t){let n=Y(e,t);return stringify(n)}export{Re as decode,xe as encode};//# sourceMappingURL=yaml.js.map //# sourceMappingURL=yaml.js.map