@cbortech/cbor 0.25.10 → 0.25.11
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/README.ja.md +11 -6
- package/README.md +13 -6
- package/dist/ast/index.cjs +1 -1
- package/dist/ast/index.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/{mapEntries-B8riGPMD.js → mapEntries-CYvdsAfP.js} +663 -648
- package/dist/mapEntries-CYvdsAfP.js.map +1 -0
- package/dist/mapEntries-vOYb5Q1s.cjs +11 -0
- package/dist/mapEntries-vOYb5Q1s.cjs.map +1 -0
- package/dist/types.d.ts +29 -19
- package/package.json +1 -1
- package/dist/mapEntries-B-INYL6l.cjs +0 -11
- package/dist/mapEntries-B-INYL6l.cjs.map +0 -1
- package/dist/mapEntries-B8riGPMD.js.map +0 -1
package/README.ja.md
CHANGED
|
@@ -212,15 +212,15 @@ console.log(text);
|
|
|
212
212
|
|
|
213
213
|
### テキスト文字列を分割して整形する
|
|
214
214
|
|
|
215
|
-
`
|
|
216
|
-
|
|
215
|
+
`splitNewline` を使うと、長いテキスト文字列を改行文字の位置で CDN の文字列連結として
|
|
216
|
+
分割できます。このオプションは `indent` を指定したときに適用されます。
|
|
217
217
|
|
|
218
218
|
```ts
|
|
219
219
|
import { CBOR } from '@cbortech/cbor';
|
|
220
220
|
|
|
221
221
|
const text = CBOR.format('{"text": "line1\\nline2\\nline3"}', {
|
|
222
222
|
indent: 2,
|
|
223
|
-
|
|
223
|
+
splitNewline: true,
|
|
224
224
|
});
|
|
225
225
|
|
|
226
226
|
console.log(text);
|
|
@@ -231,15 +231,16 @@ console.log(text);
|
|
|
231
231
|
// }
|
|
232
232
|
```
|
|
233
233
|
|
|
234
|
-
文字列の中身が CDN や JSON 風の内容なら、`
|
|
235
|
-
|
|
234
|
+
文字列の中身が CDN や JSON 風の内容なら、`splitCdn` で周囲の CDN と同じように
|
|
235
|
+
構造に応じた改行とインデントを入れて整形できます。両方のオプションは併用でき、
|
|
236
|
+
配列で指定する従来の `textStringFormat` オプションを置き換えます(非推奨)。
|
|
236
237
|
|
|
237
238
|
```ts
|
|
238
239
|
import { CBOR } from '@cbortech/cbor';
|
|
239
240
|
|
|
240
241
|
const text = CBOR.format('{"cdn": "[1,2,3]"}', {
|
|
241
242
|
indent: 2,
|
|
242
|
-
|
|
243
|
+
splitCdn: true,
|
|
243
244
|
});
|
|
244
245
|
|
|
245
246
|
console.log(text);
|
|
@@ -259,6 +260,10 @@ console.log(text);
|
|
|
259
260
|
元の連結の区切りを保持します。`preserveByteString` を併用すると、バイト文字列の
|
|
260
261
|
各パートの元の表記も保持されます。
|
|
261
262
|
|
|
263
|
+
分割オプションとの関係: 文字列の中身が CDN としてパースできる場合は `splitCdn` が
|
|
264
|
+
`preserveConcatenation` より優先されます。`splitNewline` は共存し、保持した各パートに
|
|
265
|
+
改行文字が含まれていればさらにそこで分割します。
|
|
266
|
+
|
|
262
267
|
```ts
|
|
263
268
|
import { CBOR } from '@cbortech/cbor';
|
|
264
269
|
|
package/README.md
CHANGED
|
@@ -214,15 +214,15 @@ console.log(text);
|
|
|
214
214
|
|
|
215
215
|
### Split text strings while formatting
|
|
216
216
|
|
|
217
|
-
`
|
|
218
|
-
It is applied when `indent` is specified.
|
|
217
|
+
`splitNewline` splits long text strings at newline characters using CDN
|
|
218
|
+
string concatenation. It is applied when `indent` is specified.
|
|
219
219
|
|
|
220
220
|
```ts
|
|
221
221
|
import { CBOR } from '@cbortech/cbor';
|
|
222
222
|
|
|
223
223
|
const text = CBOR.format('{"text": "line1\\nline2\\nline3"}', {
|
|
224
224
|
indent: 2,
|
|
225
|
-
|
|
225
|
+
splitNewline: true,
|
|
226
226
|
});
|
|
227
227
|
|
|
228
228
|
console.log(text);
|
|
@@ -233,15 +233,17 @@ console.log(text);
|
|
|
233
233
|
// }
|
|
234
234
|
```
|
|
235
235
|
|
|
236
|
-
For strings that contain CDN or JSON-like content,
|
|
237
|
-
|
|
236
|
+
For strings that contain CDN or JSON-like content, `splitCdn` formats the
|
|
237
|
+
string content with structure-aware line breaks and indentation, the same
|
|
238
|
+
way the surrounding CDN is formatted. Both options can be combined, and they
|
|
239
|
+
replace the deprecated array-valued `textStringFormat` option.
|
|
238
240
|
|
|
239
241
|
```ts
|
|
240
242
|
import { CBOR } from '@cbortech/cbor';
|
|
241
243
|
|
|
242
244
|
const text = CBOR.format('{"cdn": "[1,2,3]"}', {
|
|
243
245
|
indent: 2,
|
|
244
|
-
|
|
246
|
+
splitCdn: true,
|
|
245
247
|
});
|
|
246
248
|
|
|
247
249
|
console.log(text);
|
|
@@ -261,6 +263,11 @@ literal. `preserveConcatenation` keeps the original part boundaries for both
|
|
|
261
263
|
text strings and byte strings; add `preserveByteString` to also keep the
|
|
262
264
|
original spelling of byte string parts.
|
|
263
265
|
|
|
266
|
+
`preserveConcatenation` interacts with the split options: `splitCdn` takes
|
|
267
|
+
precedence when the string content parses as CDN, while `splitNewline`
|
|
268
|
+
combines with it by further splitting the preserved parts at newline
|
|
269
|
+
characters.
|
|
270
|
+
|
|
264
271
|
```ts
|
|
265
272
|
import { CBOR } from '@cbortech/cbor';
|
|
266
273
|
|
package/dist/ast/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../mapEntries-
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../mapEntries-vOYb5Q1s.cjs");exports.CborArray=e.p,exports.CborBigNint=e.c,exports.CborBigUint=e.l,exports.CborByteString=e.g,exports.CborEmbeddedCBOR=e.u,exports.CborFloat=e.v,exports.CborIndefiniteByteString=e.h,exports.CborIndefiniteTextString=e.m,exports.CborItem=e.x,exports.CborMap=e.f,exports.CborNint=e.y,exports.CborSimple=e.d,exports.CborTag=e._,exports.CborTextString=e.o,exports.CborUint=e.b;
|
package/dist/ast/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { _ as e, b as t, c as n, d as r, f as i, g as a, h as o, l as s, m as c, o as l, p as u, u as d, v as f, x as p, y as m } from "../mapEntries-
|
|
1
|
+
import { _ as e, b as t, c as n, d as r, f as i, g as a, h as o, l as s, m as c, o as l, p as u, u as d, v as f, x as p, y as m } from "../mapEntries-CYvdsAfP.js";
|
|
2
2
|
export { u as CborArray, n as CborBigNint, s as CborBigUint, a as CborByteString, d as CborEmbeddedCBOR, f as CborFloat, o as CborIndefiniteByteString, c as CborIndefiniteTextString, p as CborItem, i as CborMap, m as CborNint, r as CborSimple, e as CborTag, l as CborTextString, t as CborUint };
|
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});const e=require("./tokenizer-CVcIyZZa.cjs"),t=require("./mapEntries-
|
|
1
|
+
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});const e=require("./tokenizer-CVcIyZZa.cjs"),t=require("./mapEntries-vOYb5Q1s.cjs");function n(e){let t=``,n=0;for(;n<e.length;){let r=e[n];if(r===` `||r===`
|
|
2
2
|
`||r===`\r`){n++;continue}if(r===`#`){for(;n<e.length&&e[n]!==`
|
|
3
3
|
`;)n++;continue}if(r===`/`){let t=e[n+1]??``;if(t===`/`){for(;n<e.length&&e[n]!==`
|
|
4
4
|
`;)n++;continue}if(t===`*`){for(n+=2;n<e.length&&!(e[n]===`*`&&(e[n+1]??``)===`/`);)n++;if(n>=e.length)throw SyntaxError(`unterminated block comment`);n+=2;continue}for(n++;n<e.length&&e[n]!==`/`;)n++;if(n>=e.length)throw SyntaxError(`unterminated block comment`);n++;continue}t+=r,n++}return t}var r=`ABCDEFGHIJKLMNOPQRSTUVWXYZ234567`,i=`0123456789ABCDEFGHIJKLMNOPQRSTUV`;function a(e){let t=e.length;for(;t>0&&e.charCodeAt(t-1)===61;)t--;return e.slice(0,t)}function o(e,t,n){let r=a(e).toUpperCase(),i=r.length%8;if(i===1||i===3||i===6)throw SyntaxError(`invalid base32 length: ${r.length} characters`);let o=new Uint8Array(128).fill(255);for(let e=0;e<t.length;e++)o[t.charCodeAt(e)]=e;let s=new Uint8Array(Math.floor(r.length*5/8)),c=0,l=0,u=0;for(let e of r){let t=e.charCodeAt(0),n=t<128?o[t]:255;if(n===255)throw SyntaxError(`invalid character in byte string: ${JSON.stringify(e)}`);c=c<<5|n,l+=5,l>=8&&(l-=8,s[u++]=c>>l&255)}if(l>0&&c&(1<<l)-1){let e=`non-zero trailing bits in base32 input`;if(n)n(e);else throw SyntaxError(e)}return s}var s={appStringPrefixes:[`b32`],parseAppString(e,i,a){return new t.g(o(n(i),r,a),{ednEncoding:`base32`})}},c={appStringPrefixes:[`h32`],parseAppString(e,r,a){return new t.g(o(n(r),i,a),{ednEncoding:`base32hex`})}},l=class extends t.v{_bits;constructor(e){super(t.S(e),{precision:`half`}),this._bits=e&65535}_toCBOR(){return new Uint8Array([249,this._bits>>8&255,this._bits&255])}},u=class extends t.v{_raw;constructor(e){super(new DataView(e.buffer,e.byteOffset).getFloat32(0,!1),{precision:`single`}),this._raw=e.slice()}_toCBOR(){let e=new Uint8Array(5);return e[0]=250,e.set(this._raw,1),e}},d=class extends t.v{_raw;constructor(e){super(new DataView(e.buffer,e.byteOffset).getFloat64(0,!1),{precision:`double`}),this._raw=e.slice()}_toCBOR(){let e=new Uint8Array(9);return e[0]=251,e.set(this._raw,1),e}};function f(e){if(e.length===2)return new l(e[0]<<8|e[1]);if(e.length===4)return new u(e);if(e.length===8)return new d(e);throw SyntaxError(`float'...' requires 4, 8, or 16 hex digits (2, 4, or 8 bytes); got ${e.length} bytes`)}function p(e){let t=e>>>15&1,n=e>>>10&31,r=e&1023,i;if(n===31)i=t<<31|2139095040|r<<13;else if(n===0&&r===0)i=t<<31;else if(n===0){let e=r,n=0;for(;!(e&512);)e<<=1,n++;i=t<<31|112-n<<23|(e&511)<<14}else i=t<<31|n+112<<23|r<<13;let a=new Uint8Array(4);return new DataView(a.buffer).setUint32(0,i>>>0,!1),a}function m(e){let t=e>>>15&1,n=e>>>10&31,r=e&1023,i=new Uint8Array(8),a=new DataView(i.buffer);if(n===31)a.setUint32(0,(t<<31|2146435072|r<<10)>>>0,!1),a.setUint32(4,0,!1);else if(n===0&&r===0)a.setUint32(0,t<<31>>>0,!1),a.setUint32(4,0,!1);else if(n===0){let e=r,n=0;for(;!(e&512);)e<<=1,n++;a.setUint32(0,(t<<31|1008-n<<20|(e&511)<<11)>>>0,!1),a.setUint32(4,0,!1)}else a.setUint32(0,(t<<31|n+1008<<20|r<<10)>>>0,!1),a.setUint32(4,0,!1);return i}function h(e){let t=new DataView(e.buffer,e.byteOffset).getUint32(0,!1),n=t>>>31&1,r=t>>>23&255,i=t&8388607,a=new Uint8Array(8),o=new DataView(a.buffer);if(r===255)o.setUint32(0,(n<<31|2146435072|i>>>3)>>>0,!1),o.setUint32(4,(i&7)<<29,!1);else if(r===0&&i===0)o.setUint32(0,n<<31>>>0,!1),o.setUint32(4,0,!1);else{let t=new DataView(e.buffer,e.byteOffset).getFloat32(0,!1);o.setFloat64(0,t,!1)}return a}function g(e,n,r){let i=e.length===2?1:e.length===4?2:3;if(i===1){let t=e[0]<<8|e[1];if(n===2)return new u(p(t));if(n===3)return new d(m(t))}if(i===2){if(n===3)return new d(h(e));if(n===1){let n=new DataView(e.buffer,e.byteOffset).getFloat32(0,!1),i=t.C(n);return!Object.is(t.S(i),n)&&!isNaN(n)&&r(`float'...' value cannot be exactly represented as float16 (_1)`),new l(i)}}if(i===3){let i=new DataView(e.buffer,e.byteOffset).getFloat64(0,!1);if(n===1){let e=t.C(i);return!Object.is(t.S(e),i)&&!isNaN(i)&&r(`float'...' value cannot be exactly represented as float16 (_1)`),new l(e)}if(n===2){let e=Math.fround(i);!Object.is(e,i)&&!isNaN(i)&&r(`float'...' value cannot be exactly represented as float32 (_2)`);let t=new Uint8Array(4);return new DataView(t.buffer).setFloat32(0,e,!1),new u(t)}}return f(e)}var _={appStringPrefixes:[`float`],parseAppString(t,r,i,a){let o=n(r);if(!/^[0-9a-fA-F]*$/.test(o))throw SyntaxError(`float'...' contains non-hex characters`);if(o.length%2!=0)throw SyntaxError(`float'...' hex content has odd length (${o.length} digits)`);let s=e.a(o),c=a?.encodingWidth;if(c===void 0)return f(s);if(c!==1&&c!==2&&c!==3){let e=`float'...' encoding indicator _${c} is not valid; use _1, _2, or _3`;if(i)return i(e),f(s);throw SyntaxError(e)}return c===(s.length===2?1:s.length===4?2:3)?f(s):g(s,c,i??(e=>{throw SyntaxError(e)}))},parseAppSequence(e,n,r){if(n.length===0)throw SyntaxError(`float<<...>> requires exactly one byte-string item`);if(n.length>1){let e=`float<<...>> expects 1 item; got ${n.length} — using first`;if(r)r(e);else throw SyntaxError(e)}if(!(n[0]instanceof t.g))throw SyntaxError(`float<<...>> item must be a byte string`);return f(n[0].value)}};function v(e,t){if(e.length!==t.length)return!1;for(let n=0;n<e.length;n++)if(e[n]!==t[n])return!1;return!0}var y={appStringPrefixes:[`same`],preserveAppSeqSource:!0,parseAppSequence(e,t,n){if(t.length===0)throw SyntaxError(`same<<...>> requires at least one item`);let r=t[0],i=r.toCBOR();for(let e=1;e<t.length;e++)if(!v(i,t[e].toCBOR())){let t=`same<<...>>: item ${e} produces different CBOR bytes than item 0`;if(n)n(t);else throw SyntaxError(t)}return r}},b=class n{static OMIT=t.T;static TAG=t.E;static Tag=t.O;static Simple=t.w;static MapEntries=t.t;static dt_as_Date=t.a;#e;constructor(e){this.#e=e??{}}#t(e){return{...this.#e,...e??{}}}fromCBOR(e,t){let r=n.fromCBOR(e,this.#t(t));return r._defaults=this.#e,r}fromCDN(e,t){let r=n.fromCDN(e,this.#t(t));return r._defaults=this.#e,r}fromEDN(e,t){return this.fromCDN(e,t)}fromJS(e,t){let r=n.fromJS(e,this.#t(t));return r._defaults=this.#e,r}fromHexDump(e,t){let r=n.fromHexDump(e,this.#t(t));return r._defaults=this.#e,r}*fromCBORSeq(e,t){for(let r of n.fromCBORSeq(e,this.#t(t)))r._defaults=this.#e,yield r}*fromCDNSeq(e,t){for(let r of n.fromCDNSeq(e,this.#t(t)))r._defaults=this.#e,yield r}*fromHexDumpSeq(e,t){for(let r of n.fromHexDumpSeq(e,this.#t(t)))r._defaults=this.#e,yield r}decode(e,t){return n.decode(e,this.#t(t))}*decodeSeq(e,t){yield*n.decodeSeq(e,this.#t(t))}*parseSeq(e,t){yield*n.parseSeq(e,this.#t(t))}encode(e,t){return n.encode(e,this.#t(t))}compile(e,t){return n.compile(e,this.#t(t))}decompile(e,t){return n.decompile(e,this.#t(t))}toHex(e,t){return n.toHex(e,this.#t(t))}fromHex(e,t){return n.fromHex(e,this.#t(t))}cborToCborEdn(e,t){return this.cborToCdn(e,t)}cborToCdn(e,t){let r=this.#t(t),i=n.fromCBOR(e,r);return i._defaults=this.#e,i.toCDN(r)}cborEdnToCbor(e,t){return this.cdnToCbor(e,t)}cdnToCbor(e,t){let r=this.#t(t);return n.fromCDN(e,r).toCBOR(r)}parse(e,t){if(typeof t==`function`){let r=this.#t({reviver:t});return n.fromCDN(e,r).toJS(r)}let r=this.#t(t);return n.fromCDN(e,r).toJS(r)}stringify(e,t,r){if(typeof t==`function`||Array.isArray(t)||t===null||t===void 0&&r!==void 0){let i={...this.#e};return t===null?i.replacer=void 0:(typeof t==`function`||Array.isArray(t))&&(i.replacer=t),r!==void 0&&(i.indent=D(r)),n.stringify(e,i)}return n.stringify(e,this.#t(t??void 0))}format(e,t){return n.format(e,this.#t(t))}static fromCBOR(e,n){return t.i(e,n)}static fromCDN(e,n){return t.s(e,n)}static fromEDN(e,t){return n.fromCDN(e,t)}static*fromHexDumpSeq(e,t){let r=[],i=x(e).trim().split(/\s+/).filter(Boolean);for(let e of i)if(/^[0-9A-Fa-f]{2}$/.test(e))r.push(parseInt(e,16));else if(/^[0-9A-Fa-f]+$/.test(e)&&e.length%2==0)for(let t=0;t<e.length;t+=2)r.push(parseInt(e.slice(t,t+2),16));else throw SyntaxError(`Invalid hex token in dump: ${JSON.stringify(e)}`);yield*n.fromCBORSeq(new Uint8Array(r),t)}static*fromCBORSeq(e,n){let r=e instanceof ArrayBuffer||typeof SharedArrayBuffer<`u`&&e instanceof SharedArrayBuffer?new Uint8Array(e):new Uint8Array(e.buffer,e.byteOffset,e.byteLength),i=0;for(;i<r.byteLength;){let e=t.i(r,{...n,offset:i,allowTrailing:!0});yield e,i=e.end}}static*fromCDNSeq(n,r){let i=!!r?.preserveComments,a=0,o=!0;for(;;){let{offset:s,hadSeparator:c,commaOffset:l}=E(n,a,r,i?o?`all`:`after-newline`:`none`);if(o&&l>=0){let e=`leading comma in CDN sequence`;if(r?.strict!==!1)throw SyntaxError(e);w(e,l,r)}if(s>=n.length||i&&T(n,s)&&E(n,s,r).offset>=n.length)break;if(!o&&!c){let e=`CDN sequence items must be separated by whitespace, comma, or comment`;if(r?.strict!==!1)throw SyntaxError(e);w(e,s,r)}a=s;let u;try{u=t.s(n,{...r,offset:a,allowTrailing:!0,_skipRS:!0})}catch(t){if(r?.strict!==!1)throw t;w(t instanceof Error?t.message:String(t),a,r,!0,t instanceof e.o?t:void 0);break}yield u,a=u.end,o=!1}}static fromJS(e,n){return t.r(e,n)}static fromHexDump(e,n){let r=[],i=x(e).trim().split(/\s+/).filter(Boolean);for(let e of i)if(/^[0-9A-Fa-f]{2}$/.test(e))r.push(parseInt(e,16));else if(/^[0-9A-Fa-f]+$/.test(e)&&e.length%2==0)for(let t=0;t<e.length;t+=2)r.push(parseInt(e.slice(t,t+2),16));else throw SyntaxError(`Invalid hex token in dump: ${JSON.stringify(e)}`);return t.i(new Uint8Array(r),n)}static decode(e,t){return n.fromCBOR(e,t).toJS(t)}static*decodeSeq(e,t){for(let r of n.fromCBORSeq(e,t))yield r.toJS(t)}static*parseSeq(e,t){for(let r of n.fromCDNSeq(e,t))yield r.toJS(t)}static encode(e,t){return n.fromJS(e,t).toCBOR(t)}static compile(e,t){let r=[...n.fromCDNSeq(e,t)].map(e=>e.toCBOR(t)),i=r.reduce((e,t)=>e+t.length,0),a=new Uint8Array(i),o=0;for(let e of r)a.set(e,o),o+=e.length;return a}static decompile(e,t){return[...n.fromCBORSeq(e,t)].map(e=>e.toCDN(t)).join(`
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { CBOROptions, CborExtension, DecodeWarning, ParseWarning, FromCBOROptions, FromCBORSeqOptions, ToCBOROptions, FromCDNOptions, FromCDNSeqOptions, ToCDNOptions,
|
|
1
|
+
export type { CBOROptions, CborExtension, DecodeWarning, ParseWarning, FromCBOROptions, FromCBORSeqOptions, ToCBOROptions, FromCDNOptions, FromCDNSeqOptions, ToCDNOptions, FromEDNOptions, ToEDNOptions, FromJSOptions, ToJSOptions, FromHexDumpOptions, ToHexDumpOptions, } from './types';
|
|
2
2
|
export { CdnSyntaxError } from './cdn/errors';
|
|
3
3
|
export { CBOR_TAG, Null, Tag, Undefined } from './tag';
|
|
4
4
|
export { CBOR_OMIT } from './types';
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { a as e, o as t } from "./tokenizer-DkLlZ1gc.js";
|
|
2
|
-
import { C as n, D as r, E as i, O as a, S as o, T as s, a as c, g as l, i as u, k as d, n as f, r as p, s as m, t as h, v as g, w as _ } from "./mapEntries-
|
|
2
|
+
import { C as n, D as r, E as i, O as a, S as o, T as s, a as c, g as l, i as u, k as d, n as f, r as p, s as m, t as h, v as g, w as _ } from "./mapEntries-CYvdsAfP.js";
|
|
3
3
|
//#region src/utils/strip-comments.ts
|
|
4
4
|
function v(e) {
|
|
5
5
|
let t = "", n = 0;
|