init
This commit is contained in:
+63
@@ -0,0 +1,63 @@
|
||||
import { defineProperties } from "../../utils/properties.js";
|
||||
import { Typed } from "../typed.js";
|
||||
import { Coder } from "./abstract-coder.js";
|
||||
import { pack, unpack } from "./array.js";
|
||||
/**
|
||||
* @_ignore
|
||||
*/
|
||||
export class TupleCoder extends Coder {
|
||||
coders;
|
||||
constructor(coders, localName) {
|
||||
let dynamic = false;
|
||||
const types = [];
|
||||
coders.forEach((coder) => {
|
||||
if (coder.dynamic) {
|
||||
dynamic = true;
|
||||
}
|
||||
types.push(coder.type);
|
||||
});
|
||||
const type = ("tuple(" + types.join(",") + ")");
|
||||
super("tuple", type, localName, dynamic);
|
||||
defineProperties(this, { coders: Object.freeze(coders.slice()) });
|
||||
}
|
||||
defaultValue() {
|
||||
const values = [];
|
||||
this.coders.forEach((coder) => {
|
||||
values.push(coder.defaultValue());
|
||||
});
|
||||
// We only output named properties for uniquely named coders
|
||||
const uniqueNames = this.coders.reduce((accum, coder) => {
|
||||
const name = coder.localName;
|
||||
if (name) {
|
||||
if (!accum[name]) {
|
||||
accum[name] = 0;
|
||||
}
|
||||
accum[name]++;
|
||||
}
|
||||
return accum;
|
||||
}, {});
|
||||
// Add named values
|
||||
this.coders.forEach((coder, index) => {
|
||||
let name = coder.localName;
|
||||
if (!name || uniqueNames[name] !== 1) {
|
||||
return;
|
||||
}
|
||||
if (name === "length") {
|
||||
name = "_length";
|
||||
}
|
||||
if (values[name] != null) {
|
||||
return;
|
||||
}
|
||||
values[name] = values[index];
|
||||
});
|
||||
return Object.freeze(values);
|
||||
}
|
||||
encode(writer, _value) {
|
||||
const value = Typed.dereference(_value, "tuple");
|
||||
return pack(writer, this.coders, value);
|
||||
}
|
||||
decode(reader) {
|
||||
return unpack(reader, this.coders);
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=tuple.js.map
|
||||
Reference in New Issue
Block a user