Skip to content

Commit aa5be84

Browse files
alex-kinokonmathiasbynens
authored andcommitted
Generate TypeScript types (#88)
1 parent 832bcd2 commit aa5be84

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ const generateData = function(version) {
187187
path.resolve(__dirname, `output/unicode-${version}/index.js`),
188188
compileIndex({ 'version': version, 'data': jsesc(dirMap) })
189189
);
190+
fs.writeFileSync(
191+
path.resolve(__dirname, `output/unicode-${version}/index.d.ts`),
192+
Object.keys(dirMap)
193+
.map(key => `export const ${key}: string[];`)
194+
.join('\n')
195+
);
190196
fs.writeFileSync(
191197
path.resolve(__dirname, `output/unicode-${version}/package.json`),
192198
compilePackage({ 'version': version })
@@ -203,7 +209,9 @@ const generateData = function(version) {
203209
'.gitignore',
204210
'.npmignore',
205211
'decode-property-map.js',
212+
'decode-property-map.d.ts',
206213
'decode-ranges.js',
214+
'decode-ranges.d.ts',
207215
].forEach(function(file) {
208216
fs.copyFileSync(
209217
path.resolve(staticPath, file),

scripts/utils.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ const writeFiles = function(options) {
8484
return;
8585
}
8686
const dirMap = {};
87+
88+
const rootDir = path.resolve(
89+
__dirname, '..',
90+
'output', 'unicode-' + version
91+
);
92+
8793
/**
8894
* A list of flatten (x, y) pairs,
8995
* where x is a codepoint, y := codepoint(z) - x,
@@ -101,10 +107,7 @@ const writeFiles = function(options) {
101107
const isNamesCanon = type == 'Names' && !subType;
102108
const isNameAliases = type == 'Names' && subType == 'name-aliases';
103109
const subdir = isNameAliases ? item.charAt(0).toUpperCase() + item.slice(1) : item;
104-
const dir = path.resolve(
105-
__dirname, '..',
106-
'output', 'unicode-' + version, type, subdir
107-
);
110+
const dir = path.resolve(rootDir, type, subdir);
108111
if (
109112
type == 'Bidi_Class' ||
110113
type == 'Bidi_Mirroring_Glyph' ||
@@ -147,6 +150,12 @@ const writeFiles = function(options) {
147150
path.resolve(dir, 'index.js'),
148151
output
149152
);
153+
fs.writeFileSync(
154+
path.resolve(dir, 'index.d.ts'),
155+
type === 'Sequence_Property'
156+
? `const data: string[];\nexport default data;`
157+
: `const aliasMap: Record<number, string[]>;\nexport default aliasMap;`
158+
);
150159
return;
151160
}
152161

@@ -159,10 +168,18 @@ const writeFiles = function(options) {
159168
path.resolve(dir, 'ranges.js'),
160169
`module.exports=require('../../decode-ranges.js')('${encodedRanges}')`
161170
);
171+
fs.writeFileSync(
172+
path.resolve(dir, 'ranges.d.ts'),
173+
'import type { UnicodeRange } from "../../decode-ranges.js";\n\nconst ranges: UnicodeRange[];\nexport default ranges;\n'
174+
);
162175
fs.writeFileSync(
163176
path.resolve(dir, 'regex.js'),
164177
'module.exports=/' + regenerate(codePoints).toString() + '/'
165178
);
179+
fs.writeFileSync(
180+
path.resolve(dir, 'regex.d.ts'),
181+
'declare const regex: RegExp;\nexport default regex;'
182+
);
166183
if (codePointsSizeLt(codePoints, 10)) {
167184
const codePointsAsArray = codePoints instanceof regenerate ? codePoints.toArray() : codePoints;
168185
codePointsExports = jsesc(codePointsAsArray);
@@ -186,10 +203,18 @@ const writeFiles = function(options) {
186203
path.resolve(dir, 'code-points.js'),
187204
`module.exports=${ codePointsExports }`
188205
);
206+
fs.writeFileSync(
207+
path.resolve(dir, 'code-points.d.ts'),
208+
`declare const codePoints: number[];\nexport default codePoints;`
209+
);
189210
fs.writeFileSync(
190211
path.resolve(dir, 'symbols.js'),
191212
`module.exports=${ symbolsExports }`
192213
);
214+
fs.writeFileSync(
215+
path.resolve(dir, 'symbols.d.ts'),
216+
`declare const symbols: string[];\nexport default symbols;`
217+
);
193218
});
194219
if (options.type == 'Bidi_Mirroring_Glyph') {
195220
const type = options.type;
@@ -214,6 +239,10 @@ const writeFiles = function(options) {
214239
path.resolve(dir, 'index.js'),
215240
output
216241
);
242+
fs.writeFileSync(
243+
path.resolve(dir, 'index.d.ts'),
244+
`const data: Map<number, string>;\nexport default data;`
245+
);
217246
} else {
218247
Object.keys(auxMap).forEach(function(type) {
219248
const dir = path.resolve(
@@ -233,6 +262,7 @@ const writeFiles = function(options) {
233262
flatRuns
234263
)})`;
235264
fs.writeFileSync(path.resolve(dir, "index.js"), output);
265+
fs.writeFileSync(path.resolve(dir, "index.d.ts"), `declare const map: Map<number, string>;\nexport default map;`);
236266
});
237267
}
238268
return dirMap;

static/decode-property-map.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default function decodePropertyMap(runs: Array<number | string>): Map<number, string>;

static/decode-ranges.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class UnicodeRange {
2+
readonly begin: number;
3+
readonly end: number;
4+
readonly length: number;
5+
6+
private constructor(begin: number, end: number);
7+
8+
keys(): Generator<number, void, unknown>;
9+
values(): Generator<string, void, unknown>;
10+
}
11+
12+
/**
13+
* RLE + base64 decode code point ranges.
14+
*/
15+
export default function decodeRanges(input: string): UnicodeRange[];
16+
17+
export type { UnicodeRange };

static/decode-ranges.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ const base64dec = Object.freeze(Object.fromEntries(
1111
));
1212

1313
class UnicodeRange {
14+
/**
15+
* @param {number} begin
16+
* @param {number} end
17+
*/
1418
constructor(begin, end) {
1519
this.begin = begin;
1620
this.end = end;
@@ -32,8 +36,10 @@ class UnicodeRange {
3236

3337
/**
3438
* Base64 decode variable-length deltas (5/10/15/21-bit).
39+
* @param {string} input
3540
*/
3641
function decodeDeltas(input) {
42+
/** @type {number[]} */
3743
const output = [];
3844
for (let i = 0; i < input.length; ) {
3945
let x = base64dec[input[i++]];
@@ -63,6 +69,7 @@ function decodeDeltas(input) {
6369

6470
/**
6571
* RLE + base64 decode code point ranges.
72+
* @param {string} input
6673
*/
6774
function decodeRanges(input) {
6875
const deltas = decodeDeltas(input);

0 commit comments

Comments
 (0)