utf8.d.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * UTF-8 encoding/decoding
  3. *
  4. * ```js
  5. * import { utf8fromString, utf8toString } from '@exodus/bytes/utf8.js'
  6. *
  7. * // loose
  8. * import { utf8fromStringLoose, utf8toStringLoose } from '@exodus/bytes/utf8.js'
  9. * ```
  10. *
  11. * _These methods by design encode/decode BOM (codepoint `U+FEFF` Byte Order Mark) as-is._\
  12. * _If you need BOM handling or detection, use `@exodus/bytes/encoding.js`_
  13. *
  14. * @module @exodus/bytes/utf8.js
  15. */
  16. /// <reference types="node" />
  17. import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
  18. /**
  19. * Encode a string to UTF-8 bytes (strict mode)
  20. *
  21. * Throws on invalid Unicode (unpaired surrogates)
  22. *
  23. * This is similar to the following snippet (but works on all engines):
  24. * ```js
  25. * // Strict encode, requiring Unicode codepoints to be valid
  26. * if (typeof string !== 'string' || !string.isWellFormed()) throw new TypeError()
  27. * return new TextEncoder().encode(string)
  28. * ```
  29. *
  30. * @param string - The string to encode
  31. * @param format - Output format (default: 'uint8')
  32. * @returns The encoded bytes
  33. */
  34. export function utf8fromString(string: string, format?: 'uint8'): Uint8ArrayBuffer;
  35. export function utf8fromString(string: string, format: 'arraybuffer'): ArrayBuffer;
  36. export function utf8fromString(string: string, format: 'buffer'): Buffer;
  37. export function utf8fromString(string: string, format?: OutputFormat): Uint8ArrayBuffer | ArrayBuffer | Buffer;
  38. /**
  39. * Encode a string to UTF-8 bytes (loose mode)
  40. *
  41. * Replaces invalid Unicode (unpaired surrogates) with replacement codepoints `U+FFFD`
  42. * per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
  43. *
  44. * _Such replacement is a non-injective function, is irreversable and causes collisions.\
  45. * Prefer using strict throwing methods for cryptography applications._
  46. *
  47. * This is similar to the following snippet (but works on all engines):
  48. * ```js
  49. * // Loose encode, replacing invalid Unicode codepoints with U+FFFD
  50. * if (typeof string !== 'string') throw new TypeError()
  51. * return new TextEncoder().encode(string)
  52. * ```
  53. *
  54. * @param string - The string to encode
  55. * @param format - Output format (default: 'uint8')
  56. * @returns The encoded bytes
  57. */
  58. export function utf8fromStringLoose(string: string, format?: 'uint8'): Uint8ArrayBuffer;
  59. export function utf8fromStringLoose(string: string, format: 'arraybuffer'): ArrayBuffer;
  60. export function utf8fromStringLoose(string: string, format: 'buffer'): Buffer;
  61. export function utf8fromStringLoose(
  62. string: string,
  63. format?: OutputFormat
  64. ): Uint8ArrayBuffer | ArrayBuffer | Buffer;
  65. /**
  66. * Decode UTF-8 bytes to a string (strict mode)
  67. *
  68. * Throws on invalid UTF-8 byte sequences
  69. *
  70. * This is similar to `new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(arr)`,
  71. * but works on all engines.
  72. *
  73. * @param arr - The bytes to decode
  74. * @returns The decoded string
  75. */
  76. export function utf8toString(arr: Uint8Array): string;
  77. /**
  78. * Decode UTF-8 bytes to a string (loose mode)
  79. *
  80. * Replaces invalid UTF-8 byte sequences with replacement codepoints `U+FFFD`
  81. * per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
  82. *
  83. * _Such replacement is a non-injective function, is irreversable and causes collisions.\
  84. * Prefer using strict throwing methods for cryptography applications._
  85. *
  86. * This is similar to `new TextDecoder('utf-8', { ignoreBOM: true }).decode(arr)`,
  87. * but works on all engines.
  88. *
  89. * @param arr - The bytes to decode
  90. * @returns The decoded string
  91. */
  92. export function utf8toStringLoose(arr: Uint8Array): string;