Skip to content

Commit 9069bc5

Browse files
Saul-Mironejdecked
authored andcommitted
feat(typescript): improve declarations
1 parent 7c0e481 commit 9069bc5

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

index.d.ts

+48-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
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+
declare type ParseCallback = (icon: string, options: object, variant: string) => string | false;
11+
112
declare interface TwemojiOptions {
213
/**
314
* Default: MaxCDN
@@ -22,19 +33,52 @@ declare interface TwemojiOptions {
2233
/**
2334
* The function to invoke in order to generate image src(s).
2435
*/
25-
callback?(icon: string, options: TwemojiOptions): void;
36+
callback?: ParseCallback
2637
/**
38+
* The function to invoke in order to generate additional, custom attributes for the image tag.
2739
* 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+
*
2843
*/
29-
attributes?(): void;
44+
attributes?(icon: string, variant: string): object;
3045
}
3146

3247
declare type Twemoji = {
3348
convert: {
49+
/**
50+
* Given an HEX codepoint, returns UTF16 surrogate pairs.
51+
*
52+
* @param codepoint string generic codepoint, i.e. '1F4A9'
53+
* @return string codepoint transformed into utf16 surrogates pair,
54+
* i.e. \uD83D\uDCA9
55+
*
56+
* @example
57+
* twemoji.convert.fromCodePoint('1f1e8');
58+
* // "\ud83c\udde8"
59+
*
60+
* '1f1e8-1f1f3'.split('-').map(twemoji.convert.fromCodePoint).join('')
61+
* // "\ud83c\udde8\ud83c\uddf3"
62+
*/
3463
fromCodePoint(hexCodePoint: string): string;
35-
toCodePoint(utf16surrogatePairs: string): string;
64+
65+
/**
66+
* Given UTF16 surrogate pairs, returns the equivalent HEX codepoint.
67+
*
68+
* @param utf16surrogatePairs string generic utf16 surrogates pair, i.e. \uD83D\uDCA9
69+
* @param sep string optional separator for double code points, default='-'
70+
* @return string utf16 transformed into codepoint, i.e. '1F4A9'
71+
*
72+
* @example
73+
* twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3');
74+
* // "1f1e8-1f1f3"
75+
*
76+
* twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3', '~');
77+
* // "1f1e8~1f1f3"
78+
*/
79+
toCodePoint(utf16surrogatePairs: string, sep?: string): string;
3680
};
37-
parse(node: HTMLElement | string, options?: TwemojiOptions): void;
81+
parse<T extends string | HTMLElement>(node: T, options?: TwemojiOptions | ParseCallback): T;
3882
};
3983

4084
declare module 'twemoji' {

0 commit comments

Comments
 (0)