2019-08-25 09:39:20 +03:00
|
|
|
"use strict";
|
|
|
|
import { Coder } from "./abstract-coder";
|
|
|
|
import { pack, unpack } from "./array";
|
|
|
|
export class TupleCoder extends Coder {
|
|
|
|
constructor(coders, localName) {
|
|
|
|
let dynamic = false;
|
2020-04-25 10:54:54 +03:00
|
|
|
const types = [];
|
2019-08-25 09:39:20 +03:00
|
|
|
coders.forEach((coder) => {
|
|
|
|
if (coder.dynamic) {
|
|
|
|
dynamic = true;
|
|
|
|
}
|
|
|
|
types.push(coder.type);
|
|
|
|
});
|
2020-04-25 10:54:54 +03:00
|
|
|
const type = ("tuple(" + types.join(",") + ")");
|
2019-08-25 09:39:20 +03:00
|
|
|
super("tuple", type, localName, dynamic);
|
|
|
|
this.coders = coders;
|
|
|
|
}
|
2020-11-23 11:43:28 +03:00
|
|
|
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);
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
encode(writer, value) {
|
|
|
|
return pack(writer, this.coders, value);
|
|
|
|
}
|
|
|
|
decode(reader) {
|
|
|
|
return reader.coerce(this.name, unpack(reader, this.coders));
|
|
|
|
}
|
|
|
|
}
|
2020-07-13 15:03:56 +03:00
|
|
|
//# sourceMappingURL=tuple.js.map
|