Locale.mjs (12889B)
1 // generated by diplomat-tool 2 import { LocaleParseError } from "./LocaleParseError.mjs" 3 import wasm from "./diplomat-wasm.mjs"; 4 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 5 6 7 /** 8 * An ICU4X Locale, capable of representing strings like `"en-US"`. 9 * 10 * See the [Rust documentation for `Locale`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html) for more information. 11 */ 12 const Locale_box_destroy_registry = new FinalizationRegistry((ptr) => { 13 wasm.icu4x_Locale_destroy_mv1(ptr); 14 }); 15 16 export class Locale { 17 // Internal ptr reference: 18 #ptr = null; 19 20 // Lifetimes are only to keep dependencies alive. 21 // Since JS won't garbage collect until there are no incoming edges. 22 #selfEdge = []; 23 24 #internalConstructor(symbol, ptr, selfEdge) { 25 if (symbol !== diplomatRuntime.internalConstructor) { 26 console.error("Locale is an Opaque type. You cannot call its constructor."); 27 return; 28 } 29 this.#ptr = ptr; 30 this.#selfEdge = selfEdge; 31 32 // Are we being borrowed? If not, we can register. 33 if (this.#selfEdge.length === 0) { 34 Locale_box_destroy_registry.register(this, this.#ptr); 35 } 36 37 return this; 38 } 39 get ffiValue() { 40 return this.#ptr; 41 } 42 43 44 /** 45 * Construct an [`Locale`] from an locale identifier. 46 * 47 * This will run the complete locale parsing algorithm. If code size and 48 * performance are critical and the locale is of a known shape (such as 49 * `aa-BB`) use `create_und`, `set_language`, `set_script`, and `set_region`. 50 * 51 * See the [Rust documentation for `try_from_str`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html#method.try_from_str) for more information. 52 */ 53 static fromString(name) { 54 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 55 56 const nameSlice = diplomatRuntime.DiplomatBuf.str8(wasm, name); 57 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 58 59 60 const result = wasm.icu4x_Locale_from_string_mv1(diplomatReceive.buffer, ...nameSlice.splat()); 61 62 try { 63 if (!diplomatReceive.resultFlag) { 64 const cause = new LocaleParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 65 throw new globalThis.Error('LocaleParseError: ' + cause.value, { cause }); 66 } 67 return new Locale(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 68 } 69 70 finally { 71 functionCleanupArena.free(); 72 73 diplomatReceive.free(); 74 } 75 } 76 77 /** 78 * Construct a unknown [`Locale`] "und". 79 * 80 * See the [Rust documentation for `UNKNOWN`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html#associatedconstant.UNKNOWN) for more information. 81 */ 82 static unknown() { 83 84 const result = wasm.icu4x_Locale_unknown_mv1(); 85 86 try { 87 return new Locale(diplomatRuntime.internalConstructor, result, []); 88 } 89 90 finally { 91 } 92 } 93 94 /** 95 * Clones the [`Locale`]. 96 * 97 * See the [Rust documentation for `Locale`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html) for more information. 98 */ 99 clone() { 100 101 const result = wasm.icu4x_Locale_clone_mv1(this.ffiValue); 102 103 try { 104 return new Locale(diplomatRuntime.internalConstructor, result, []); 105 } 106 107 finally { 108 } 109 } 110 111 /** 112 * Returns a string representation of the `LanguageIdentifier` part of 113 * [`Locale`]. 114 * 115 * See the [Rust documentation for `id`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html#structfield.id) for more information. 116 */ 117 get basename() { 118 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 119 120 wasm.icu4x_Locale_basename_mv1(this.ffiValue, write.buffer); 121 122 try { 123 return write.readString8(); 124 } 125 126 finally { 127 write.free(); 128 } 129 } 130 131 /** 132 * Returns a string representation of the unicode extension. 133 * 134 * See the [Rust documentation for `extensions`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html#structfield.extensions) for more information. 135 */ 136 getUnicodeExtension(s) { 137 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 138 139 const sSlice = diplomatRuntime.DiplomatBuf.str8(wasm, s); 140 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 141 142 143 const result = wasm.icu4x_Locale_get_unicode_extension_mv1(this.ffiValue, ...sSlice.splat(), write.buffer); 144 145 try { 146 return result === 0 ? null : write.readString8(); 147 } 148 149 finally { 150 functionCleanupArena.free(); 151 152 write.free(); 153 } 154 } 155 156 /** 157 * Returns a string representation of [`Locale`] language. 158 * 159 * See the [Rust documentation for `id`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html#structfield.id) for more information. 160 */ 161 get language() { 162 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 163 164 wasm.icu4x_Locale_language_mv1(this.ffiValue, write.buffer); 165 166 try { 167 return write.readString8(); 168 } 169 170 finally { 171 write.free(); 172 } 173 } 174 175 /** 176 * Set the language part of the [`Locale`]. 177 * 178 * See the [Rust documentation for `try_from_str`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html#method.try_from_str) for more information. 179 */ 180 set language(s) { 181 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 182 183 const sSlice = diplomatRuntime.DiplomatBuf.str8(wasm, s); 184 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 185 186 187 const result = wasm.icu4x_Locale_set_language_mv1(diplomatReceive.buffer, this.ffiValue, ...sSlice.splat()); 188 189 try { 190 if (!diplomatReceive.resultFlag) { 191 const cause = new LocaleParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 192 throw new globalThis.Error('LocaleParseError: ' + cause.value, { cause }); 193 } 194 } 195 196 finally { 197 functionCleanupArena.free(); 198 199 diplomatReceive.free(); 200 } 201 } 202 203 /** 204 * Returns a string representation of [`Locale`] region. 205 * 206 * See the [Rust documentation for `id`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html#structfield.id) for more information. 207 */ 208 get region() { 209 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 210 211 212 const result = wasm.icu4x_Locale_region_mv1(this.ffiValue, write.buffer); 213 214 try { 215 return result === 0 ? null : write.readString8(); 216 } 217 218 finally { 219 write.free(); 220 } 221 } 222 223 /** 224 * Set the region part of the [`Locale`]. 225 * 226 * See the [Rust documentation for `try_from_str`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html#method.try_from_str) for more information. 227 */ 228 set region(s) { 229 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 230 231 const sSlice = diplomatRuntime.DiplomatBuf.str8(wasm, s); 232 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 233 234 235 const result = wasm.icu4x_Locale_set_region_mv1(diplomatReceive.buffer, this.ffiValue, ...sSlice.splat()); 236 237 try { 238 if (!diplomatReceive.resultFlag) { 239 const cause = new LocaleParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 240 throw new globalThis.Error('LocaleParseError: ' + cause.value, { cause }); 241 } 242 } 243 244 finally { 245 functionCleanupArena.free(); 246 247 diplomatReceive.free(); 248 } 249 } 250 251 /** 252 * Returns a string representation of [`Locale`] script. 253 * 254 * See the [Rust documentation for `id`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html#structfield.id) for more information. 255 */ 256 get script() { 257 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 258 259 260 const result = wasm.icu4x_Locale_script_mv1(this.ffiValue, write.buffer); 261 262 try { 263 return result === 0 ? null : write.readString8(); 264 } 265 266 finally { 267 write.free(); 268 } 269 } 270 271 /** 272 * Set the script part of the [`Locale`]. Pass an empty string to remove the script. 273 * 274 * See the [Rust documentation for `try_from_str`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html#method.try_from_str) for more information. 275 */ 276 set script(s) { 277 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 278 279 const sSlice = diplomatRuntime.DiplomatBuf.str8(wasm, s); 280 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 281 282 283 const result = wasm.icu4x_Locale_set_script_mv1(diplomatReceive.buffer, this.ffiValue, ...sSlice.splat()); 284 285 try { 286 if (!diplomatReceive.resultFlag) { 287 const cause = new LocaleParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 288 throw new globalThis.Error('LocaleParseError: ' + cause.value, { cause }); 289 } 290 } 291 292 finally { 293 functionCleanupArena.free(); 294 295 diplomatReceive.free(); 296 } 297 } 298 299 /** 300 * Normalizes a locale string. 301 * 302 * See the [Rust documentation for `normalize`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html#method.normalize) for more information. 303 */ 304 static normalize(s) { 305 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 306 307 const sSlice = diplomatRuntime.DiplomatBuf.str8(wasm, s); 308 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 309 310 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 311 312 313 const result = wasm.icu4x_Locale_normalize_mv1(diplomatReceive.buffer, ...sSlice.splat(), write.buffer); 314 315 try { 316 if (!diplomatReceive.resultFlag) { 317 const cause = new LocaleParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 318 throw new globalThis.Error('LocaleParseError: ' + cause.value, { cause }); 319 } 320 return write.readString8(); 321 } 322 323 finally { 324 functionCleanupArena.free(); 325 326 diplomatReceive.free(); 327 write.free(); 328 } 329 } 330 331 /** 332 * Returns a string representation of [`Locale`]. 333 * 334 * See the [Rust documentation for `write_to`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html#method.write_to) for more information. 335 */ 336 toString() { 337 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 338 339 wasm.icu4x_Locale_to_string_mv1(this.ffiValue, write.buffer); 340 341 try { 342 return write.readString8(); 343 } 344 345 finally { 346 write.free(); 347 } 348 } 349 350 /** 351 * See the [Rust documentation for `normalizing_eq`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html#method.normalizing_eq) for more information. 352 */ 353 normalizingEq(other) { 354 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 355 356 const otherSlice = diplomatRuntime.DiplomatBuf.str8(wasm, other); 357 358 const result = wasm.icu4x_Locale_normalizing_eq_mv1(this.ffiValue, ...otherSlice.splat()); 359 360 try { 361 return result; 362 } 363 364 finally { 365 functionCleanupArena.free(); 366 367 } 368 } 369 370 /** 371 * See the [Rust documentation for `strict_cmp`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html#method.strict_cmp) for more information. 372 */ 373 compareToString(other) { 374 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 375 376 const otherSlice = diplomatRuntime.DiplomatBuf.str8(wasm, other); 377 378 const result = wasm.icu4x_Locale_compare_to_string_mv1(this.ffiValue, ...otherSlice.splat()); 379 380 try { 381 return result; 382 } 383 384 finally { 385 functionCleanupArena.free(); 386 387 } 388 } 389 390 /** 391 * See the [Rust documentation for `total_cmp`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html#method.total_cmp) for more information. 392 */ 393 compareTo(other) { 394 395 const result = wasm.icu4x_Locale_compare_to_mv1(this.ffiValue, other.ffiValue); 396 397 try { 398 return result; 399 } 400 401 finally { 402 } 403 } 404 405 constructor(symbol, ptr, selfEdge) { 406 return this.#internalConstructor(...arguments) 407 } 408 }