@planetarium/bencodex
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -24,4 +24,30 @@ /** | ||
import { decode, encode, } from "../deps/deno.land/std@0.177.0/encoding/base64.js"; | ||
import { isDictionary, } from "./types.js"; | ||
import { compareKeys, isDictionary, } from "./types.js"; | ||
import { areUint8ArraysEqual } from "./utils.js"; | ||
function inspectDictionary(dictionary, typeName, inspect, options) { | ||
let s = `${typeName}(${dictionary.size})`; | ||
if (options.depth < 1) | ||
return `[${s}]`; | ||
const nextOptions = { ...options, depth: options.depth - 1 }; | ||
const entries = [...dictionary]; | ||
if (options.sorted) | ||
entries.sort(([a], [b]) => compareKeys(a, b)); | ||
const entryStrings = entries.map(([k, v]) => [inspect(k, nextOptions), inspect(v, nextOptions)]); | ||
const compact = options.compact && | ||
!entryStrings.some(([k, v]) => k.includes("\n") || v.includes("\n")); | ||
s += compact ? " { " : " {\n"; | ||
let first = true; | ||
for (const [key, value] of entryStrings) { | ||
if (!first) | ||
s += compact ? ", " : ",\n"; | ||
if (!compact) | ||
s += " "; | ||
s += `${key.replaceAll("\n", "\n ")} => ${value.replaceAll("\n", "\n ")}`; | ||
first = false; | ||
} | ||
if (options.trailingComma && !compact) | ||
s += ","; | ||
s += compact ? " }" : "\n}"; | ||
return s; | ||
} | ||
const SHORT_BINARY_THRESHOLD = 32; | ||
@@ -190,14 +216,12 @@ /** | ||
} | ||
[Symbol.for("Deno.customInspect")](inspect) { | ||
let s = "BencodexDictionary { "; | ||
let first = true; | ||
for (const [key, value] of this) { | ||
if (!first) | ||
s += ", "; | ||
s += `${inspect(key)}: ${inspect(value)}`; | ||
first = false; | ||
} | ||
s += " }"; | ||
return s; | ||
[Symbol.for("Deno.customInspect")](inspect, options) { | ||
return inspectDictionary(this, "BencodexDictionary", inspect, options); | ||
} | ||
[Symbol.for("nodejs.util.inspect.custom")](_, options, inspect) { | ||
return inspectDictionary(this, "BencodexDictionary", inspect, { | ||
...options, | ||
compact: options.compact !== false, | ||
trailingComma: false, | ||
}); | ||
} | ||
} | ||
@@ -374,5 +398,12 @@ /** | ||
} | ||
[Symbol.for("Deno.customInspect")](inspect) { | ||
return `RecordView { ${inspect(__classPrivateFieldGet(this, _RecordView_record, "f"))}`; | ||
[Symbol.for("Deno.customInspect")](inspect, options) { | ||
return inspectDictionary(this, "BencodexDictionary", inspect, options); | ||
} | ||
[Symbol.for("nodejs.util.inspect.custom")](_, options, inspect) { | ||
return inspectDictionary(this, "BencodexDictionary", inspect, { | ||
...options, | ||
compact: options.compact !== false, | ||
trailingComma: false, | ||
}); | ||
} | ||
} |
@@ -6,3 +6,3 @@ { | ||
"name": "@planetarium/bencodex", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "An alternative take on implementing Bencodex in TypeScript/JavaScript", | ||
@@ -9,0 +9,0 @@ "keywords": [ |
@@ -17,6 +17,6 @@ <!-- deno-fmt-ignore-file --> | ||
- *No Node.js-only APIs*: It does not depend on Node.js-only APIs such as | ||
`Buffer`. Usable with web browsers, Deno, and Node.js. | ||
[`Buffer`]. Usable with web browsers, Deno, and Node.js. | ||
- *Static-time and runtime type safety*: It is fully written in TypeScript, | ||
and promise us, we use `any` nowhere, and it never trusts any data from | ||
and promise us, we use [`any`] nowhere, and it never trusts any data from | ||
external sources thoughtlessly. Instead, it tries to parse data and | ||
@@ -32,4 +32,4 @@ validate them whenever it read data from external sources. It's not only | ||
are streamed or given in a single big chunk, etc). It has its own unit | ||
tests besides spec test suite, and we've done the best to reach near-100% | ||
test coverage. | ||
tests besides spec test suite, and we've done the best to reach | ||
[near-100% test coverage][Coveralls]. | ||
@@ -40,11 +40,11 @@ - *Comprehensive docs*: Every non-trivial library needs the three types of | ||
- *Reducing unnecessary memcpy*: Instead of creating chunks of `Uint8Array`s | ||
and then allocating a large buffer to concatenate the whole data once again, | ||
it allocates only the necessary buffer only once. It is not just | ||
memory-efficient, also time-efficient as memcpy quite takes time. | ||
- *Reducing unnecessary memcpy*: Instead of creating chunks of | ||
[`Uint8Array`]s and then allocating a large buffer to concatenate the whole | ||
data once again, it allocates only the necessary buffer only once. | ||
It is not just memory-efficient, also time-efficient as memcpy quite | ||
takes time. | ||
- *Proper binary keys*: It does not simply depend on | ||
`Map<Uint8Array | string, ...>`, which compares its `Uint8Array` keys by | ||
reference equality. Instead, it implements its own proper dictionary which | ||
compares `Uint8Array` by content equality. | ||
- *Proper binary keys*: It does not simply depend on [`Map`], which compares | ||
its [`Uint8Array`] keys by reference equality. Instead, it implements its | ||
own proper dictionary which compares [`Uint8Array`] by content equality. | ||
@@ -62,2 +62,6 @@ Distributed under LGPL 2.1 or later. | ||
[Bencodex]: https://bencodex.org/ | ||
[`Buffer`]: https://nodejs.org/api/buffer.html | ||
[`any`]: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any | ||
[`Uint8Array`]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array | ||
[`Map`]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map | ||
@@ -119,3 +123,3 @@ | ||
| Bencodex | TypeScript ([`Value`][Value]) | | ||
| Bencodex | TypeScript ([`Value`]) | | ||
|------------|-------------------------------| | ||
@@ -125,10 +129,9 @@ | Null | `null` | | ||
| Integer | `bigint` (not `number`) | | ||
| Binary | [`Uint8Array`][Uint8Array] | | ||
| Binary | [`Uint8Array`] | | ||
| Text | `string` | | ||
| List | `Value[]` | | ||
| Dictionary | [`Dictionary`][Dictionary] | | ||
| Dictionary | [`Dictionary`] | | ||
[Value]: https://deno.land/x/bencodex/mod.ts?s=Value | ||
[Uint8Array]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array | ||
[Dictionary]: https://deno.land/x/bencodex/mod.ts?s=Dictionary | ||
[`Value`]: https://deno.land/x/bencodex/mod.ts?s=Value | ||
[`Dictionary`]: https://deno.land/x/bencodex/mod.ts?s=Dictionary | ||
@@ -135,0 +138,0 @@ |
@@ -29,2 +29,28 @@ "use strict"; | ||
const utils_js_1 = require("./utils.js"); | ||
function inspectDictionary(dictionary, typeName, inspect, options) { | ||
let s = `${typeName}(${dictionary.size})`; | ||
if (options.depth < 1) | ||
return `[${s}]`; | ||
const nextOptions = { ...options, depth: options.depth - 1 }; | ||
const entries = [...dictionary]; | ||
if (options.sorted) | ||
entries.sort(([a], [b]) => (0, types_js_1.compareKeys)(a, b)); | ||
const entryStrings = entries.map(([k, v]) => [inspect(k, nextOptions), inspect(v, nextOptions)]); | ||
const compact = options.compact && | ||
!entryStrings.some(([k, v]) => k.includes("\n") || v.includes("\n")); | ||
s += compact ? " { " : " {\n"; | ||
let first = true; | ||
for (const [key, value] of entryStrings) { | ||
if (!first) | ||
s += compact ? ", " : ",\n"; | ||
if (!compact) | ||
s += " "; | ||
s += `${key.replaceAll("\n", "\n ")} => ${value.replaceAll("\n", "\n ")}`; | ||
first = false; | ||
} | ||
if (options.trailingComma && !compact) | ||
s += ","; | ||
s += compact ? " }" : "\n}"; | ||
return s; | ||
} | ||
const SHORT_BINARY_THRESHOLD = 32; | ||
@@ -193,14 +219,12 @@ /** | ||
} | ||
[Symbol.for("Deno.customInspect")](inspect) { | ||
let s = "BencodexDictionary { "; | ||
let first = true; | ||
for (const [key, value] of this) { | ||
if (!first) | ||
s += ", "; | ||
s += `${inspect(key)}: ${inspect(value)}`; | ||
first = false; | ||
} | ||
s += " }"; | ||
return s; | ||
[Symbol.for("Deno.customInspect")](inspect, options) { | ||
return inspectDictionary(this, "BencodexDictionary", inspect, options); | ||
} | ||
[Symbol.for("nodejs.util.inspect.custom")](_, options, inspect) { | ||
return inspectDictionary(this, "BencodexDictionary", inspect, { | ||
...options, | ||
compact: options.compact !== false, | ||
trailingComma: false, | ||
}); | ||
} | ||
} | ||
@@ -379,6 +403,13 @@ exports.BencodexDictionary = BencodexDictionary; | ||
} | ||
[Symbol.for("Deno.customInspect")](inspect) { | ||
return `RecordView { ${inspect(__classPrivateFieldGet(this, _RecordView_record, "f"))}`; | ||
[Symbol.for("Deno.customInspect")](inspect, options) { | ||
return inspectDictionary(this, "BencodexDictionary", inspect, options); | ||
} | ||
[Symbol.for("nodejs.util.inspect.custom")](_, options, inspect) { | ||
return inspectDictionary(this, "BencodexDictionary", inspect, { | ||
...options, | ||
compact: options.compact !== false, | ||
trailingComma: false, | ||
}); | ||
} | ||
} | ||
exports.RecordView = RecordView; |
3370
160
166713
29