From 50000f783dc4cc1226c12da23781cf3300db12e4 Mon Sep 17 00:00:00 2001 From: Alex <8125011+alex-kinokon@users.noreply.github.com> Date: Tue, 6 May 2025 01:00:39 +0000 Subject: [PATCH] Generate types --- index.js | 8 +++++++ scripts/utils.js | 38 +++++++++++++++++++++++++++++---- static/decode-property-map.d.ts | 1 + static/decode-ranges.d.ts | 17 +++++++++++++++ static/decode-ranges.js | 7 ++++++ 5 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 static/decode-property-map.d.ts create mode 100644 static/decode-ranges.d.ts diff --git a/index.js b/index.js index 3bd4bf2..ba4e81f 100644 --- a/index.js +++ b/index.js @@ -187,6 +187,12 @@ const generateData = function(version) { path.resolve(__dirname, `output/unicode-${version}/index.js`), compileIndex({ 'version': version, 'data': jsesc(dirMap) }) ); + fs.writeFileSync( + path.resolve(__dirname, `output/unicode-${version}/index.d.ts`), + Object.keys(dirMap) + .map(key => `export const ${key}: string[];`) + .join('\n') + ); fs.writeFileSync( path.resolve(__dirname, `output/unicode-${version}/package.json`), compilePackage({ 'version': version }) @@ -203,7 +209,9 @@ const generateData = function(version) { '.gitignore', '.npmignore', 'decode-property-map.js', + 'decode-property-map.d.ts', 'decode-ranges.js', + 'decode-ranges.d.ts', ].forEach(function(file) { fs.copyFileSync( path.resolve(staticPath, file), diff --git a/scripts/utils.js b/scripts/utils.js index c9dddaa..c2b7230 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -84,6 +84,12 @@ const writeFiles = function(options) { return; } const dirMap = {}; + + const rootDir = path.resolve( + __dirname, '..', + 'output', 'unicode-' + version + ); + /** * A list of flatten (x, y) pairs, * where x is a codepoint, y := codepoint(z) - x, @@ -101,10 +107,7 @@ const writeFiles = function(options) { const isNamesCanon = type == 'Names' && !subType; const isNameAliases = type == 'Names' && subType == 'name-aliases'; const subdir = isNameAliases ? item.charAt(0).toUpperCase() + item.slice(1) : item; - const dir = path.resolve( - __dirname, '..', - 'output', 'unicode-' + version, type, subdir - ); + const dir = path.resolve(rootDir, type, subdir); if ( type == 'Bidi_Class' || type == 'Bidi_Mirroring_Glyph' || @@ -147,6 +150,12 @@ const writeFiles = function(options) { path.resolve(dir, 'index.js'), output ); + fs.writeFileSync( + path.resolve(dir, 'index.d.ts'), + type === 'Sequence_Property' + ? `const data: string[];\nexport default data;` + : `const aliasMap: Record;\nexport default aliasMap;` + ); return; } @@ -159,10 +168,18 @@ const writeFiles = function(options) { path.resolve(dir, 'ranges.js'), `module.exports=require('../../decode-ranges.js')('${encodedRanges}')` ); + fs.writeFileSync( + path.resolve(dir, 'ranges.d.ts'), + 'import type { UnicodeRange } from "../../decode-ranges.js";\n\nconst ranges: UnicodeRange[];\nexport default ranges;\n' + ); fs.writeFileSync( path.resolve(dir, 'regex.js'), 'module.exports=/' + regenerate(codePoints).toString() + '/' ); + fs.writeFileSync( + path.resolve(dir, 'regex.d.ts'), + 'declare const regex: RegExp;\nexport default regex;' + ); if (codePointsSizeLt(codePoints, 10)) { const codePointsAsArray = codePoints instanceof regenerate ? codePoints.toArray() : codePoints; codePointsExports = jsesc(codePointsAsArray); @@ -186,10 +203,18 @@ const writeFiles = function(options) { path.resolve(dir, 'code-points.js'), `module.exports=${ codePointsExports }` ); + fs.writeFileSync( + path.resolve(dir, 'code-points.d.ts'), + `declare const codePoints: number[];\nexport default codePoints;` + ); fs.writeFileSync( path.resolve(dir, 'symbols.js'), `module.exports=${ symbolsExports }` ); + fs.writeFileSync( + path.resolve(dir, 'symbols.d.ts'), + `declare const symbols: string[];\nexport default symbols;` + ); }); if (options.type == 'Bidi_Mirroring_Glyph') { const type = options.type; @@ -214,6 +239,10 @@ const writeFiles = function(options) { path.resolve(dir, 'index.js'), output ); + fs.writeFileSync( + path.resolve(dir, 'index.d.ts'), + `const data: Map;\nexport default data;` + ); } else { Object.keys(auxMap).forEach(function(type) { const dir = path.resolve( @@ -233,6 +262,7 @@ const writeFiles = function(options) { flatRuns )})`; fs.writeFileSync(path.resolve(dir, "index.js"), output); + fs.writeFileSync(path.resolve(dir, "index.d.ts"), `declare const map: Map;\nexport default map;`); }); } return dirMap; diff --git a/static/decode-property-map.d.ts b/static/decode-property-map.d.ts new file mode 100644 index 0000000..48f46d6 --- /dev/null +++ b/static/decode-property-map.d.ts @@ -0,0 +1 @@ +export default function decodePropertyMap(runs: Array): Map; diff --git a/static/decode-ranges.d.ts b/static/decode-ranges.d.ts new file mode 100644 index 0000000..38c1d9a --- /dev/null +++ b/static/decode-ranges.d.ts @@ -0,0 +1,17 @@ +class UnicodeRange { + readonly begin: number; + readonly end: number; + readonly length: number; + + private constructor(begin: number, end: number); + + keys(): Generator; + values(): Generator; +} + +/** + * RLE + base64 decode code point ranges. + */ +export default function decodeRanges(input: string): UnicodeRange[]; + +export type { UnicodeRange }; diff --git a/static/decode-ranges.js b/static/decode-ranges.js index d04dbb1..38b412d 100644 --- a/static/decode-ranges.js +++ b/static/decode-ranges.js @@ -11,6 +11,10 @@ const base64dec = Object.freeze(Object.fromEntries( )); class UnicodeRange { + /** + * @param {number} begin + * @param {number} end + */ constructor(begin, end) { this.begin = begin; this.end = end; @@ -32,8 +36,10 @@ class UnicodeRange { /** * Base64 decode variable-length deltas (5/10/15/21-bit). + * @param {string} input */ function decodeDeltas(input) { + /** @type {number[]} */ const output = []; for (let i = 0; i < input.length; ) { let x = base64dec[input[i++]]; @@ -63,6 +69,7 @@ function decodeDeltas(input) { /** * RLE + base64 decode code point ranges. + * @param {string} input */ function decodeRanges(input) { const deltas = decodeDeltas(input);