// generated by diplomat-tool import { DataError } from "./DataError.mjs" import { DataProvider } from "./DataProvider.mjs" import { FixedDecimal } from "./FixedDecimal.mjs" import { FixedDecimalGroupingStrategy } from "./FixedDecimalGroupingStrategy.mjs" import { Locale } from "./Locale.mjs" import wasm from "./diplomat-wasm.mjs"; import * as diplomatRuntime from "./diplomat-runtime.mjs"; /** An ICU4X Fixed Decimal Format object, capable of formatting a [`FixedDecimal`] as a string. * *See the [Rust documentation for `FixedDecimalFormatter`](https://docs.rs/icu/latest/icu/decimal/struct.FixedDecimalFormatter.html) for more information. */ const FixedDecimalFormatter_box_destroy_registry = new FinalizationRegistry((ptr) => { wasm.icu4x_FixedDecimalFormatter_destroy_mv1(ptr); }); export class FixedDecimalFormatter { // Internal ptr reference: #ptr = null; // Lifetimes are only to keep dependencies alive. // Since JS won't garbage collect until there are no incoming edges. #selfEdge = []; constructor(symbol, ptr, selfEdge) { if (symbol !== diplomatRuntime.internalConstructor) { console.error("FixedDecimalFormatter is an Opaque type. You cannot call its constructor."); return; } this.#ptr = ptr; this.#selfEdge = selfEdge; // Are we being borrowed? If not, we can register. if (this.#selfEdge.length === 0) { FixedDecimalFormatter_box_destroy_registry.register(this, this.#ptr); } } get ffiValue() { return this.#ptr; } static createWithGroupingStrategy(provider, locale, groupingStrategy) { const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); const result = wasm.icu4x_FixedDecimalFormatter_create_with_grouping_strategy_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue, ...diplomatRuntime.optionToArgsForCalling(groupingStrategy, 4, 4, false, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)])); try { if (!diplomatReceive.resultFlag) { const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); throw new globalThis.Error('DataError: ' + cause.value, { cause }); } return new FixedDecimalFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); } finally { diplomatReceive.free(); } } static createWithManualData(plusSignPrefix, plusSignSuffix, minusSignPrefix, minusSignSuffix, decimalSeparator, groupingSeparator, primaryGroupSize, secondaryGroupSize, minGroupSize, digits, groupingStrategy) { let functionCleanupArena = new diplomatRuntime.CleanupArena(); const plusSignPrefixSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, plusSignPrefix)); const plusSignSuffixSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, plusSignSuffix)); const minusSignPrefixSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, minusSignPrefix)); const minusSignSuffixSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, minusSignSuffix)); const decimalSeparatorSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, decimalSeparator)); const groupingSeparatorSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, groupingSeparator)); const digitsSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.slice(wasm, digits, "u16")); const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); const result = wasm.icu4x_FixedDecimalFormatter_create_with_manual_data_mv1(diplomatReceive.buffer, ...plusSignPrefixSlice.splat(), ...plusSignSuffixSlice.splat(), ...minusSignPrefixSlice.splat(), ...minusSignSuffixSlice.splat(), ...decimalSeparatorSlice.splat(), ...groupingSeparatorSlice.splat(), primaryGroupSize, secondaryGroupSize, minGroupSize, ...digitsSlice.splat(), ...diplomatRuntime.optionToArgsForCalling(groupingStrategy, 4, 4, false, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)])); try { if (!diplomatReceive.resultFlag) { const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); throw new globalThis.Error('DataError: ' + cause.value, { cause }); } return new FixedDecimalFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); } finally { functionCleanupArena.free(); diplomatReceive.free(); } } format(value) { const write = new diplomatRuntime.DiplomatWriteBuf(wasm); wasm.icu4x_FixedDecimalFormatter_format_mv1(this.ffiValue, value.ffiValue, write.buffer); try { return write.readString8(); } finally { write.free(); } } }