Skip to content

Commit 1e4e3e8

Browse files
authored
🤖 Merge PR DefinitelyTyped#74022 feat(unicode-name) Add support for unicode-name by @Gamrix
1 parent f5729d7 commit 1e4e3e8

File tree

5 files changed

+224
-0
lines changed

5 files changed

+224
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/**
2+
* Aliases assigned to a Unicode codepoint, organized by category.
3+
*/
4+
export interface UnicodeAliases {
5+
/** Correction aliases */
6+
correction?: string[];
7+
/** Control character aliases */
8+
control?: string[];
9+
/** Figment aliases */
10+
figment?: string[];
11+
/** Alternate name aliases */
12+
alternate?: string[];
13+
/** Abbreviation aliases */
14+
abbreviation?: string[];
15+
}
16+
17+
/**
18+
* Returns the name that has been assigned to a Unicode codepoint.
19+
*
20+
* Please note:
21+
* Some common codepoints do not have a name (e.g. C0 control characters like \n)
22+
*
23+
* Also see:
24+
* - unicodeCorrectName(char) - Checks if there is a corrected name first, if not,
25+
* fallbacks to this method
26+
* - unicodeReadableName(char) - Displays correct name or an applicable alias
27+
*
28+
* @param char Single character string or codepoint
29+
* @returns Name of character or undefined
30+
*/
31+
export function unicodeBaseName(char: string | number): string | undefined;
32+
33+
/**
34+
* Returns the name that has been assigned to a Unicode codepoint, but if the codepoint
35+
* has a correction alias, use this instead.
36+
*
37+
* Please note:
38+
* Some common codepoints do not have a name (e.g. C0 control characters like \n)
39+
*
40+
* Also see:
41+
* - unicodeReadableName(char) - Displays correct name or an applicable alias
42+
*
43+
* @param char Single character string or codepoint
44+
* @returns Corrected name of character or undefined
45+
*/
46+
export function unicodeCorrectName(char: string | number): string | undefined;
47+
48+
/**
49+
* Returns the aliases that have been assigned to a Unicode codepoint.
50+
*
51+
* Aliases can be of these categories (multiple aliases possible):
52+
*
53+
* - correction
54+
* - control
55+
* - figment
56+
* - alternate
57+
* - abbreviation
58+
*
59+
* @param char Single character string or codepoint
60+
* @returns Object containing aliases for this Unicode codepoint
61+
*/
62+
export function unicodeAliases(char: string | number): UnicodeAliases | undefined;
63+
64+
/**
65+
* Determine the basic type of codepoints. Required to be able to get the
66+
* Unicode label of a codepoint. This can be one of:
67+
*
68+
* - Graphic
69+
* - Format
70+
* - Control
71+
* - Private-use
72+
* - Surrogate
73+
* - Noncharacter
74+
* - Reserved
75+
*
76+
* @param char Single character string or codepoint
77+
* @returns Codepoint type
78+
*/
79+
export function unicodeType(char: string | number): string | undefined;
80+
81+
/**
82+
* Returns a label of a codepoint in the following format:
83+
* <type-hex>, e.g. <control-0009> for the tab character or
84+
* <noncharacter-FFFFF> for U+FFFFF
85+
*
86+
* It is only assigned to codepoints of a type other than
87+
* "Graphic" or "Format"
88+
*
89+
* @param char Single character string or codepoint
90+
* @returns A generic label for this codepoint
91+
*/
92+
export function unicodeLabel(char: string | number): string | undefined;
93+
94+
/**
95+
* Returns the best readable representation of a codepoint.
96+
*
97+
* 1) It is the corrected name of a the codepoint (if one exists)
98+
* 2) or it is an appropriate aliase (if one exists)
99+
* 3) or it is the codepoint label
100+
*
101+
* @param char Single character string or codepoint
102+
* @returns Unicode name, alias, or label for this character
103+
*/
104+
export function unicodeReadableName(char: string | number): string | undefined;
105+
106+
/**
107+
* Returns the name of a character that is made of a codepoint sequence (= more than
108+
* one codepoint involved), if one exists.
109+
*
110+
* @param char Single character string made of multiple codepoints
111+
* @returns Unicode sequence name
112+
*/
113+
export function unicodeSequenceName(char: string): string | undefined;
114+
115+
/**
116+
* Returns the name of a character that is made of a codepoint sequence (= more than
117+
* one codepoint involved), if one exists.
118+
*
119+
* Differently from unicodeSequenceName(char), it will only consider Emoji ZWJ sequences
120+
* that are fully qualified, meaning they all required variation selectors (VS16) in place
121+
*
122+
* @param char Single character string made of multiple codepoints
123+
* @returns Unicode sequence name
124+
*/
125+
export function unicodeQualifiedSequenceName(char: string): string | undefined;
126+
127+
/**
128+
* Returns the best name for the Unicode character (codepoint or codepoint sequence).
129+
*
130+
* At first, it will check if the codepoint sequence has a name, e.g. for
131+
* Emoji that are build up using multiple codepoints using unicodeSequenceName(char)
132+
*
133+
* If none is found, will use the unicodeReadableName(char) function to retrieve
134+
* the best name for that codepoint.
135+
*
136+
* @param char Single character string or codepoint
137+
* @returns Name of character
138+
*/
139+
export function unicodeName(char: string | number): string | undefined;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"private": true,
3+
"name": "@types/unicode-name",
4+
"version": "1.1.9999",
5+
"projects": [
6+
"https://github.com/janlelis/unicode-name.js"
7+
],
8+
"type": "module",
9+
"exports": {
10+
".": {
11+
"types": {
12+
"default": "./index.d.ts"
13+
}
14+
}
15+
},
16+
"devDependencies": {
17+
"@types/unicode-name": "workspace:."
18+
},
19+
"owners": [
20+
{
21+
"name": "John Clow",
22+
"githubUsername": "gamrix"
23+
}
24+
]
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictFunctionTypes": true,
10+
"strictNullChecks": true,
11+
"types": [],
12+
"noEmit": true,
13+
"forceConsistentCasingInFileNames": true
14+
},
15+
"files": [
16+
"index.d.ts",
17+
"unicode-name-tests.ts"
18+
]
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {
2+
unicodeAliases,
3+
unicodeBaseName,
4+
unicodeCorrectName,
5+
unicodeLabel,
6+
unicodeName,
7+
unicodeQualifiedSequenceName,
8+
unicodeReadableName,
9+
unicodeSequenceName,
10+
unicodeType,
11+
} from "unicode-name";
12+
13+
// Test string parameter
14+
unicodeName("A"); // $ExpectType string | undefined
15+
unicodeBaseName("🚡"); // $ExpectType string | undefined
16+
unicodeCorrectName("Æ¢"); // $ExpectType string | undefined
17+
18+
// Test number parameter
19+
unicodeName(65); // $ExpectType string | undefined
20+
unicodeBaseName(48); // $ExpectType string | undefined
21+
22+
// Test aliases interface
23+
const aliases = unicodeAliases("\0");
24+
if (aliases) {
25+
aliases.control; // $ExpectType string[] | undefined
26+
aliases.abbreviation; // $ExpectType string[] | undefined
27+
}
28+
29+
// Test other functions
30+
unicodeType("A"); // $ExpectType string | undefined
31+
unicodeLabel("\0"); // $ExpectType string | undefined
32+
unicodeReadableName("A"); // $ExpectType string | undefined
33+
34+
// Test sequence functions (string only)
35+
unicodeSequenceName("🇺🇳"); // $ExpectType string | undefined
36+
unicodeQualifiedSequenceName("‼︎"); // $ExpectType string | undefined

0 commit comments

Comments
 (0)