Files
discord-decomp/_bundle/Compiler.js
T
2026-11-01 12:12:55 +02:00

171 lines
4.8 KiB
JavaScript

// Metro module 16058 (f16059).
let StringFormat, PluralFormat, PluralOffsetString, SelectFormat;
const Compiler = function Compiler(a0, a1, a2) {
this.locales = a0;
this.formats = a1;
this.pluralFn = a2;
};
StringFormat = function StringFormat(a0) {
this.id = a0;
};
PluralFormat = function PluralFormat(a0, a1, a2, a3, a4) {
this.id = a0;
this.useOrdinal = a1;
this.offset = a2;
this.options = a3;
this.pluralFn = a4;
};
PluralOffsetString = function PluralOffsetString(a0, a1, a2, a3) {
this.id = a0;
this.offset = a1;
this.numberFormat = a2;
this.string = a3;
};
SelectFormat = function SelectFormat(a0, a1) {
this.id = a0;
this.options = a1;
};
export default Compiler;
Compiler.prototype.compile = function(a0) {
this.pluralStack = [];
this.currentPlural = null;
this.pluralNumberFormat = null;
return this.compileMessage(a0);
};
Compiler.prototype.compileMessage = function(a0) {
if (a0 && a0.type === "messageFormatPattern") {
const elements = a0.elements;
const r16 = [];
const length = elements.length;
let r0 = 0;
while (r0 < length) {
const r12 = elements[r0];
const type = r12.type;
const r1 = r0;
if (type === "messageTextElement") {
r16.push(this.compileMessageText(r12));
} else {
if (type !== "argumentElement") {
throw new Error("Message element does not have a valid type");
}
r16.push(this.compileArgument(r12));
}
r0 = r1 + 1;
}
return r16;
}
throw new Error("Message AST is not of type: \"messageFormatPattern\"");
};
Compiler.prototype.compileMessageText = function(a0) {
let r15;
if (this.currentPlural && /(^|[^\\])#/g.test(a0.value)) {
if (!this.pluralNumberFormat) {
this.pluralNumberFormat = new Intl.NumberFormat(this.locales);
}
const id = this.currentPlural.id;
const offset = this.currentPlural.format.offset;
const pluralNumberFormat = this.pluralNumberFormat;
const value = a0.value;
const r32 = {
__proto__: PluralOffsetString.prototype
};
r32.id = id;
r32.offset = offset;
r32.numberFormat = pluralNumberFormat;
r32.string = value;
r15 = r32;
} else {
r15 = a0.value.replace(/\\#/g, "#");
}
return r15;
};
Compiler.prototype.compileArgument = function(a0) {
const format = a0.format;
if (format) {
const formats = this.formats;
const locales = this.locales;
const pluralFn = this.pluralFn;
const type = format.type;
if (type === "numberFormat") {
return {
id: a0.id,
format: new Intl.NumberFormat(locales, formats.number[format.style]).format
};
}
if (type === "dateFormat") {
return {
id: a0.id,
format: new Intl.DateTimeFormat(locales, formats.date[format.style]).format
};
}
if (type === "timeFormat") {
return {
id: a0.id,
format: new Intl.DateTimeFormat(locales, formats.time[format.style]).format
};
}
if (type === "pluralFormat") {
const options2 = this.compileOptions(a0);
const id3 = a0.id;
const { ordinal, offset } = format;
const r38 = {
__proto__: PluralFormat.prototype
};
r38.id = id3;
r38.useOrdinal = ordinal;
r38.offset = offset;
r38.options = options2;
r38.pluralFn = pluralFn;
return r38;
}
if (type === "selectFormat") {
const options = this.compileOptions(a0);
const id2 = a0.id;
const r30 = {
__proto__: SelectFormat.prototype
};
r30.id = id2;
r30.options = options;
return r30;
}
throw new Error("Message element does not have a valid format type");
}
const id = a0.id;
const r15 = {
__proto__: StringFormat.prototype
};
r15.id = id;
return r15;
};
Compiler.prototype.compileOptions = function(a0) {
const format = a0.format;
const options = format.options;
this.pluralStack.push(this.currentPlural);
const currentPlural = format.type === "pluralFormat" ? a0 : null;
const r15 = {};
this.currentPlural = currentPlural;
const length = options.length;
let r0 = 0;
while (r0 < length) {
const r17 = options[r0];
r15[r17.selector] = this.compileMessage(r17.value);
r0 = r0 + 1;
}
this.currentPlural = this.pluralStack.pop();
return r15;
};
StringFormat.prototype.format = function(a0) {
return a0 ? typeof a0 !== "string" ? String(a0) : a0 : "";
};
PluralFormat.prototype.getOption = function(a0) {
const options = this.options;
return options["=" + a0] || options[this.pluralFn(a0 - this.offset, this.useOrdinal)] || options.other;
};
PluralOffsetString.prototype.format = function(a0) {
return this.string.replace(/(^|[^\\])#/g, "$1" + this.numberFormat.format(a0 - this.offset)).replace(/\\#/g, "#");
};
SelectFormat.prototype.getOption = function(a0) {
const options = this.options;
return options[a0] || options.other;
};