index.d.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * If given to parse, this callback will be invoked per each found emoji.
  3. *
  4. * If this callback returns a falsy value instead of a valid `src` to use for the image, nothing will actually change for that specific emoji.
  5. *
  6. * @param icon the lower case HEX code point i.e. "1f4a9"
  7. * @param options all info for this parsing operation
  8. * @param variant the optional \uFE0F ("as image") variant, in case this info is anyhow meaningful. By default this is ignored.
  9. */
  10. export type ParseCallback = (icon: string, options: object, variant: string) => string | false;
  11. export type ReplacerFunction = (substring: string, ...args: any[]) => string;
  12. export type TwemojiOptions = {
  13. /**
  14. * Default: Cloudflare
  15. */
  16. base?: string;
  17. /**
  18. * Default: .png
  19. */
  20. ext?: string;
  21. /**
  22. * Default: emoji
  23. */
  24. className?: string;
  25. /**
  26. * Default: 72x72
  27. */
  28. size?: string | number;
  29. /**
  30. * To render with SVG use `folder: svg, ext: .svg`
  31. */
  32. folder?: string;
  33. /**
  34. * The function to invoke in order to generate image src(s).
  35. */
  36. callback?: ParseCallback
  37. /**
  38. * The function to invoke in order to generate additional, custom attributes for the image tag.
  39. * Default () => ({})
  40. * @param icon the lower case HEX code point i.e. "1f4a9"
  41. * @param variant variant the optional \uFE0F ("as image") variant, in case this info is anyhow meaningful. By default this is ignored.
  42. *
  43. */
  44. attributes?(icon: string, variant: string): object;
  45. }
  46. export type Twemoji = {
  47. base: string;
  48. ext: string;
  49. className: string;
  50. size: string;
  51. convert: {
  52. /**
  53. * Given an HEX codepoint, returns UTF16 surrogate pairs.
  54. *
  55. * @param codepoint string generic codepoint, i.e. '1F4A9'
  56. * @return string codepoint transformed into utf16 surrogates pair,
  57. * i.e. \uD83D\uDCA9
  58. *
  59. * @example
  60. * twemoji.convert.fromCodePoint('1f1e8');
  61. * // "\ud83c\udde8"
  62. *
  63. * '1f1e8-1f1f3'.split('-').map(twemoji.convert.fromCodePoint).join('')
  64. * // "\ud83c\udde8\ud83c\uddf3"
  65. */
  66. fromCodePoint(hexCodePoint: string): string;
  67. /**
  68. * Given UTF16 surrogate pairs, returns the equivalent HEX codepoint.
  69. *
  70. * @param utf16surrogatePairs string generic utf16 surrogates pair, i.e. \uD83D\uDCA9
  71. * @param sep string optional separator for double code points, default='-'
  72. * @return string utf16 transformed into codepoint, i.e. '1F4A9'
  73. *
  74. * @example
  75. * twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3');
  76. * // "1f1e8-1f1f3"
  77. *
  78. * twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3', '~');
  79. * // "1f1e8~1f1f3"
  80. */
  81. toCodePoint(utf16surrogatePairs: string, sep?: string): string;
  82. };
  83. parse<T extends string | HTMLElement>(node: T, options?: TwemojiOptions | ParseCallback): T;
  84. replace(text: string, replacer: string | ReplacerFunction): string;
  85. test(text: string): boolean;
  86. onerror(): void;
  87. };
  88. declare module 'twemoji' {
  89. const twemoji: Twemoji;
  90. export default twemoji;
  91. }