Decimal.mjs (17003B)
1 // generated by diplomat-tool 2 import { DecimalLimitError } from "./DecimalLimitError.mjs" 3 import { DecimalParseError } from "./DecimalParseError.mjs" 4 import { DecimalRoundingIncrement } from "./DecimalRoundingIncrement.mjs" 5 import { DecimalSign } from "./DecimalSign.mjs" 6 import { DecimalSignDisplay } from "./DecimalSignDisplay.mjs" 7 import { DecimalSignedRoundingMode } from "./DecimalSignedRoundingMode.mjs" 8 import wasm from "./diplomat-wasm.mjs"; 9 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 10 11 12 /** 13 * See the [Rust documentation for `Decimal`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html) for more information. 14 */ 15 const Decimal_box_destroy_registry = new FinalizationRegistry((ptr) => { 16 wasm.icu4x_Decimal_destroy_mv1(ptr); 17 }); 18 19 export class Decimal { 20 // Internal ptr reference: 21 #ptr = null; 22 23 // Lifetimes are only to keep dependencies alive. 24 // Since JS won't garbage collect until there are no incoming edges. 25 #selfEdge = []; 26 27 #internalConstructor(symbol, ptr, selfEdge) { 28 if (symbol !== diplomatRuntime.internalConstructor) { 29 console.error("Decimal is an Opaque type. You cannot call its constructor."); 30 return; 31 } 32 this.#ptr = ptr; 33 this.#selfEdge = selfEdge; 34 35 // Are we being borrowed? If not, we can register. 36 if (this.#selfEdge.length === 0) { 37 Decimal_box_destroy_registry.register(this, this.#ptr); 38 } 39 40 return this; 41 } 42 get ffiValue() { 43 return this.#ptr; 44 } 45 46 47 /** 48 * Construct an [`Decimal`] from an integer. 49 * 50 * See the [Rust documentation for `Decimal`](https://docs.rs/fixed_decimal/latest/fixed_decimal/struct.Decimal.html) for more information. 51 */ 52 static fromNumber(v) { 53 54 const result = wasm.icu4x_Decimal_from_int32_mv1(v); 55 56 try { 57 return new Decimal(diplomatRuntime.internalConstructor, result, []); 58 } 59 60 finally { 61 } 62 } 63 64 /** 65 * Construct an [`Decimal`] from an integer. 66 * 67 * See the [Rust documentation for `Decimal`](https://docs.rs/fixed_decimal/latest/fixed_decimal/struct.Decimal.html) for more information. 68 */ 69 static fromBigInt(v) { 70 71 const result = wasm.icu4x_Decimal_from_int64_mv1(v); 72 73 try { 74 return new Decimal(diplomatRuntime.internalConstructor, result, []); 75 } 76 77 finally { 78 } 79 } 80 81 /** 82 * Construct an [`Decimal`] from an float, with a given power of 10 for the lower magnitude 83 * 84 * See the [Rust documentation for `try_from_f64`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.try_from_f64) for more information. 85 * 86 * See the [Rust documentation for `FloatPrecision`](https://docs.rs/fixed_decimal/latest/fixed_decimal/enum.FloatPrecision.html) for more information. 87 */ 88 static fromNumberWithLowerMagnitude(f, magnitude) { 89 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 90 91 92 const result = wasm.icu4x_Decimal_from_double_with_lower_magnitude_mv1(diplomatReceive.buffer, f, magnitude); 93 94 try { 95 if (!diplomatReceive.resultFlag) { 96 const cause = DecimalLimitError.fromFields({}, diplomatRuntime.internalConstructor); 97 throw new globalThis.Error('DecimalLimitError', { cause }); 98 } 99 return new Decimal(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 100 } 101 102 finally { 103 diplomatReceive.free(); 104 } 105 } 106 107 /** 108 * Construct an [`Decimal`] from an float, for a given number of significant digits 109 * 110 * See the [Rust documentation for `try_from_f64`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.try_from_f64) for more information. 111 * 112 * See the [Rust documentation for `FloatPrecision`](https://docs.rs/fixed_decimal/latest/fixed_decimal/enum.FloatPrecision.html) for more information. 113 */ 114 static fromNumberWithSignificantDigits(f, digits) { 115 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 116 117 118 const result = wasm.icu4x_Decimal_from_double_with_significant_digits_mv1(diplomatReceive.buffer, f, digits); 119 120 try { 121 if (!diplomatReceive.resultFlag) { 122 const cause = DecimalLimitError.fromFields({}, diplomatRuntime.internalConstructor); 123 throw new globalThis.Error('DecimalLimitError', { cause }); 124 } 125 return new Decimal(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 126 } 127 128 finally { 129 diplomatReceive.free(); 130 } 131 } 132 133 /** 134 * Construct an [`Decimal`] from an float, with enough digits to recover 135 * the original floating point in IEEE 754 without needing trailing zeros 136 * 137 * See the [Rust documentation for `try_from_f64`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.try_from_f64) for more information. 138 * 139 * See the [Rust documentation for `FloatPrecision`](https://docs.rs/fixed_decimal/latest/fixed_decimal/enum.FloatPrecision.html) for more information. 140 */ 141 static fromNumberWithRoundTripPrecision(f) { 142 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 143 144 145 const result = wasm.icu4x_Decimal_from_double_with_round_trip_precision_mv1(diplomatReceive.buffer, f); 146 147 try { 148 if (!diplomatReceive.resultFlag) { 149 const cause = DecimalLimitError.fromFields({}, diplomatRuntime.internalConstructor); 150 throw new globalThis.Error('DecimalLimitError', { cause }); 151 } 152 return new Decimal(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 153 } 154 155 finally { 156 diplomatReceive.free(); 157 } 158 } 159 160 /** 161 * Construct an [`Decimal`] from a string. 162 * 163 * See the [Rust documentation for `try_from_str`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.try_from_str) for more information. 164 */ 165 static fromString(v) { 166 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 167 168 const vSlice = diplomatRuntime.DiplomatBuf.str8(wasm, v); 169 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 170 171 172 const result = wasm.icu4x_Decimal_from_string_mv1(diplomatReceive.buffer, ...vSlice.splat()); 173 174 try { 175 if (!diplomatReceive.resultFlag) { 176 const cause = new DecimalParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 177 throw new globalThis.Error('DecimalParseError: ' + cause.value, { cause }); 178 } 179 return new Decimal(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 180 } 181 182 finally { 183 functionCleanupArena.free(); 184 185 diplomatReceive.free(); 186 } 187 } 188 189 /** 190 * See the [Rust documentation for `digit_at`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.digit_at) for more information. 191 */ 192 digitAt(magnitude) { 193 194 const result = wasm.icu4x_Decimal_digit_at_mv1(this.ffiValue, magnitude); 195 196 try { 197 return result; 198 } 199 200 finally { 201 } 202 } 203 204 /** 205 * See the [Rust documentation for `magnitude_range`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.magnitude_range) for more information. 206 */ 207 get magnitudeStart() { 208 209 const result = wasm.icu4x_Decimal_magnitude_start_mv1(this.ffiValue); 210 211 try { 212 return result; 213 } 214 215 finally { 216 } 217 } 218 219 /** 220 * See the [Rust documentation for `magnitude_range`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.magnitude_range) for more information. 221 */ 222 get magnitudeEnd() { 223 224 const result = wasm.icu4x_Decimal_magnitude_end_mv1(this.ffiValue); 225 226 try { 227 return result; 228 } 229 230 finally { 231 } 232 } 233 234 /** 235 * See the [Rust documentation for `nonzero_magnitude_start`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.nonzero_magnitude_start) for more information. 236 */ 237 get nonzeroMagnitudeStart() { 238 239 const result = wasm.icu4x_Decimal_nonzero_magnitude_start_mv1(this.ffiValue); 240 241 try { 242 return result; 243 } 244 245 finally { 246 } 247 } 248 249 /** 250 * See the [Rust documentation for `nonzero_magnitude_end`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.nonzero_magnitude_end) for more information. 251 */ 252 get nonzeroMagnitudeEnd() { 253 254 const result = wasm.icu4x_Decimal_nonzero_magnitude_end_mv1(this.ffiValue); 255 256 try { 257 return result; 258 } 259 260 finally { 261 } 262 } 263 264 /** 265 * See the [Rust documentation for `is_zero`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.is_zero) for more information. 266 */ 267 get isZero() { 268 269 const result = wasm.icu4x_Decimal_is_zero_mv1(this.ffiValue); 270 271 try { 272 return result; 273 } 274 275 finally { 276 } 277 } 278 279 /** 280 * Multiply the [`Decimal`] by a given power of ten. 281 * 282 * See the [Rust documentation for `multiply_pow10`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.multiply_pow10) for more information. 283 */ 284 multiplyPow10(power) { 285 wasm.icu4x_Decimal_multiply_pow10_mv1(this.ffiValue, power); 286 287 try {} 288 289 finally { 290 } 291 } 292 293 /** 294 * See the [Rust documentation for `sign`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.sign) for more information. 295 */ 296 get sign() { 297 298 const result = wasm.icu4x_Decimal_sign_mv1(this.ffiValue); 299 300 try { 301 return new DecimalSign(diplomatRuntime.internalConstructor, result); 302 } 303 304 finally { 305 } 306 } 307 308 /** 309 * Set the sign of the [`Decimal`]. 310 * 311 * See the [Rust documentation for `set_sign`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.set_sign) for more information. 312 */ 313 set sign(sign) { 314 wasm.icu4x_Decimal_set_sign_mv1(this.ffiValue, sign.ffiValue); 315 316 try {} 317 318 finally { 319 } 320 } 321 322 /** 323 * See the [Rust documentation for `apply_sign_display`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.apply_sign_display) for more information. 324 */ 325 applySignDisplay(signDisplay) { 326 wasm.icu4x_Decimal_apply_sign_display_mv1(this.ffiValue, signDisplay.ffiValue); 327 328 try {} 329 330 finally { 331 } 332 } 333 334 /** 335 * See the [Rust documentation for `trim_start`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.trim_start) for more information. 336 */ 337 trimStart() { 338 wasm.icu4x_Decimal_trim_start_mv1(this.ffiValue); 339 340 try {} 341 342 finally { 343 } 344 } 345 346 /** 347 * See the [Rust documentation for `trim_end`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.trim_end) for more information. 348 */ 349 trimEnd() { 350 wasm.icu4x_Decimal_trim_end_mv1(this.ffiValue); 351 352 try {} 353 354 finally { 355 } 356 } 357 358 /** 359 * See the [Rust documentation for `trim_end_if_integer`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.trim_end_if_integer) for more information. 360 */ 361 trimEndIfInteger() { 362 wasm.icu4x_Decimal_trim_end_if_integer_mv1(this.ffiValue); 363 364 try {} 365 366 finally { 367 } 368 } 369 370 /** 371 * Zero-pad the [`Decimal`] on the left to a particular position 372 * 373 * See the [Rust documentation for `pad_start`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.pad_start) for more information. 374 */ 375 padStart(position) { 376 wasm.icu4x_Decimal_pad_start_mv1(this.ffiValue, position); 377 378 try {} 379 380 finally { 381 } 382 } 383 384 /** 385 * Zero-pad the [`Decimal`] on the right to a particular position 386 * 387 * See the [Rust documentation for `pad_end`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.pad_end) for more information. 388 */ 389 padEnd(position) { 390 wasm.icu4x_Decimal_pad_end_mv1(this.ffiValue, position); 391 392 try {} 393 394 finally { 395 } 396 } 397 398 /** 399 * Truncate the [`Decimal`] on the left to a particular position, deleting digits if necessary. This is useful for, e.g. abbreviating years 400 * ("2022" -> "22") 401 * 402 * See the [Rust documentation for `set_max_position`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.set_max_position) for more information. 403 */ 404 setMaxPosition(position) { 405 wasm.icu4x_Decimal_set_max_position_mv1(this.ffiValue, position); 406 407 try {} 408 409 finally { 410 } 411 } 412 413 /** 414 * Round the number at a particular digit position. 415 * 416 * This uses half to even rounding, which resolves ties by selecting the nearest 417 * even integer to the original value. 418 * 419 * See the [Rust documentation for `round`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.round) for more information. 420 */ 421 round(position) { 422 wasm.icu4x_Decimal_round_mv1(this.ffiValue, position); 423 424 try {} 425 426 finally { 427 } 428 } 429 430 /** 431 * See the [Rust documentation for `ceil`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.ceil) for more information. 432 */ 433 ceil(position) { 434 wasm.icu4x_Decimal_ceil_mv1(this.ffiValue, position); 435 436 try {} 437 438 finally { 439 } 440 } 441 442 /** 443 * See the [Rust documentation for `expand`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.expand) for more information. 444 */ 445 expand(position) { 446 wasm.icu4x_Decimal_expand_mv1(this.ffiValue, position); 447 448 try {} 449 450 finally { 451 } 452 } 453 454 /** 455 * See the [Rust documentation for `floor`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.floor) for more information. 456 */ 457 floor(position) { 458 wasm.icu4x_Decimal_floor_mv1(this.ffiValue, position); 459 460 try {} 461 462 finally { 463 } 464 } 465 466 /** 467 * See the [Rust documentation for `trunc`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.trunc) for more information. 468 */ 469 trunc(position) { 470 wasm.icu4x_Decimal_trunc_mv1(this.ffiValue, position); 471 472 try {} 473 474 finally { 475 } 476 } 477 478 /** 479 * See the [Rust documentation for `round_with_mode`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.round_with_mode) for more information. 480 */ 481 roundWithMode(position, mode) { 482 wasm.icu4x_Decimal_round_with_mode_mv1(this.ffiValue, position, mode.ffiValue); 483 484 try {} 485 486 finally { 487 } 488 } 489 490 /** 491 * See the [Rust documentation for `round_with_mode_and_increment`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.round_with_mode_and_increment) for more information. 492 */ 493 roundWithModeAndIncrement(position, mode, increment) { 494 wasm.icu4x_Decimal_round_with_mode_and_increment_mv1(this.ffiValue, position, mode.ffiValue, increment.ffiValue); 495 496 try {} 497 498 finally { 499 } 500 } 501 502 /** 503 * Concatenates `other` to the end of `self`. 504 * 505 * If successful, `other` will be set to 0 and a successful status is returned. 506 * 507 * If not successful, `other` will be unchanged and an error is returned. 508 * 509 * See the [Rust documentation for `concatenate_end`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.concatenate_end) for more information. 510 */ 511 concatenateEnd(other) { 512 513 const result = wasm.icu4x_Decimal_concatenate_end_mv1(this.ffiValue, other.ffiValue); 514 515 try { 516 return result === 1; 517 } 518 519 finally { 520 } 521 } 522 523 /** 524 * Format the [`Decimal`] as a string. 525 * 526 * See the [Rust documentation for `write_to`](https://docs.rs/fixed_decimal/latest/fixed_decimal/type.Decimal.html#method.write_to) for more information. 527 */ 528 toString() { 529 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 530 531 wasm.icu4x_Decimal_to_string_mv1(this.ffiValue, write.buffer); 532 533 try { 534 return write.readString8(); 535 } 536 537 finally { 538 write.free(); 539 } 540 } 541 542 constructor(symbol, ptr, selfEdge) { 543 return this.#internalConstructor(...arguments) 544 } 545 }