Public
Edited
Apr 14, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
var bytes = new Uint8Array([0x01, 0, 1, 4, 5, 6, 7, 8, 8, 9, 19, 11]);
return flatMapParser(u1, (_) => {
return tagToParserMap[_.value.value];
})(0, bytes);
}
Insert cell
{
var parseConstantClassInfo = createConstantInfoParser("ConstantClassInfo", [
[u2be, "name_index"]
]);
var bytes = new Uint8Array([0x01, 0x03, 0x0, 4, 5, 6, 7, 8, 8, 9, 19, 11]);
var t = parseConstantClassInfo(0, bytes);
t.value;
return [t, bytes, t.value.value];
}
Insert cell
assert = import("https://esm.sh/uvu/assert")
Insert cell
//Example, this does not handle sizes
/*{
class Struct {
constructor(data) {
this.data = data;
this.keys = Object.keys(data);
}

get(keyOrIndex) {
if (typeof keyOrIndex === "number") {
return this.data[this.keys[keyOrIndex]];
}
return this.data[keyOrIndex];
}

*[Symbol.iterator]() {
for (const value of Object.values(this.data)) {
yield value;
}
}

values() {
return Object.values(this.data);
}

keys() {
return [...this.keys];
}
}

class StructConstructor {
constructor() {
this.bufferSizeMap = new WeakMap();
}

registerBufferSize(parser, size) {
this.bufferSizeMap.set(parser, size);
}

createStruct(parsers, bytes = null) {
if (!Array.isArray(parsers)) {
throw new TypeError(
"parsers must be an array of parsers or [name, parser] pairs"
);
}

const structData = {};
let currentIndex = 0;
let bufferSize = bytes ? bytes.length : 1024;
bytes = bytes || new Uint8Array(bufferSize);

for (let item of parsers) {
let parser, name;
if (Array.isArray(item)) {
[name, parser] = item;
} else {
parser = item;
name = undefined; // No specific name assigned
}

if (typeof parser !== "function") {
throw new TypeError("Each parser must be a function");
}

const result = parser(currentIndex, bytes);
if (result.isErr()) {
throw new Error(result.unwrapErr());
}

const { value, nextIndex } = result.unwrap();
const key = name || `field${Object.keys(structData).length}`;
structData[key] = value.value; // Assuming value property returns the needed result

currentIndex = nextIndex;
}

return {
struct: new Struct(structData),
buffer: bytes.slice(0, currentIndex) // Only use relevant part of the buffer
};
}
}
// Example usage:
const structConstructor = new StructConstructor();
const { struct, buffer } = structConstructor.createStruct([
["intValue", u4le],
u1
]);
return [struct, buffer];
}*/
Insert cell
{
var bytes = new Uint8Array([0x01, 0x02, 0x03, 4, 5, 6, 7, 8, 8, 9, 19, 11]);
var result = applyNTimes(u1, 5)(0, bytes);
result.value.value.value.value[3].value.value = 0;
return [result, bytes];
}
Insert cell


// Define the CpInfo parser using switchParser to select the correct parser
// based on the tag value
var parseCpInfo = mapParser(
concatenateParsers([u1]), // Parse the tag first
resultsArray => { // Transform to a structure containing the tag and info
const tag = resultsArray[0].value.value;
return {
tag: tag,
info: switchParser(tagToParserMap[tag], tagToParserMap)
};
}
);
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more