@@ -430,28 +430,63 @@ let printTableRaw = (f, name, table, format) => {
430430
431431/**
432432 * @param {WriteStream } f
433- * @param {CategorizedUnicodeRange[] } breakTable
434- * @param {string[] } breakCats
433+ * @param {CategorizedUnicodeRange[] } ranges
434+ * @param {string[] } cats
435+ * @param {string } catsModule
435436 * @param {string } name
436437 * @returns
437438 */
438- let printBreakModule = ( f , breakTable , breakCats , name ) => {
439- let cats = [ 'Any' , ...breakCats . toSorted ( ) ] ;
440-
439+ let printDataModule = ( f , ranges , cats , catsModule , name ) => {
441440 let capitalName = capitalize ( name ) ;
442441 let typeName = `${ capitalName } Category` ;
443- let keyTypeName = `${ typeName } Key` ;
444442 let numTypeName = `${ typeName } Num` ;
445443 let rangeTypeName = `${ typeName } Range` ;
446444
445+ /** @type {Record<string, number> } */
446+ let inversed = { } ;
447+ cats . forEach ( ( cat , idx ) => {
448+ inversed [ cat ] = idx ;
449+ } ) ;
450+
447451 f . write ( preamble ) ;
448452 f . write ( `
449453import { decodeUnicodeData } from './core.js';
450454
451455/**
456+ * @typedef {import('./${ catsModule } ').${ numTypeName } } ${ numTypeName }
452457 * @typedef {import('./core.js').UnicodeDataEncoding} UnicodeDataEncoding
458+ * @typedef {import('./core.js').CategorizedUnicodeRange<${ numTypeName } >} ${ rangeTypeName }
453459 */
460+ ` ,
461+ ) ;
454462
463+ f . write ( `
464+ /**
465+ * @type {${ rangeTypeName } []}
466+ */
467+ export const ${ name } _ranges = decodeUnicodeData(
468+ /** @type {UnicodeDataEncoding} */
469+ ('${ encodeUnicodeData ( ranges . map ( range => [ range [ 0 ] , range [ 1 ] , 0 ] ) ) } '),
470+ '${ ranges . map ( range => inversed [ range [ 2 ] ] . toString ( 36 ) ) . join ( '' ) } ',
471+ );
472+ ` ,
473+ ) ;
474+ } ;
475+
476+ /**
477+ * @param {WriteStream } f
478+ * @param {string[] } cats
479+ * @param {string } name
480+ * @returns
481+ */
482+ let printCategoryModule = ( f , cats , name ) => {
483+ let capitalName = capitalize ( name ) ;
484+ let typeName = `${ capitalName } Category` ;
485+ let keyTypeName = `${ typeName } Key` ;
486+ let numTypeName = `${ typeName } Num` ;
487+
488+ f . write ( preamble ) ;
489+ f . write ( `
455490/**
456491` ,
457492 ) ;
@@ -471,13 +506,6 @@ import { decodeUnicodeData } from './core.js';
471506
472507 f . write ( `
473508/**
474- * @typedef {import('./core.js').CategorizedUnicodeRange<${ numTypeName } >} ${ rangeTypeName }
475- */
476- ` ,
477- ) ;
478-
479- f . write ( `
480- /**
481509 * @typedef {(
482510` ,
483511 ) ;
@@ -489,33 +517,15 @@ import { decodeUnicodeData } from './core.js';
489517
490518 f . write ( `
491519/**
492- * Grapheme category enum
493- *
494- * Note:
495- * The object isn't actually frozen
496- * because using \`Object.freeze\` increases 800 bytes on Brotli compression.
497- *
498- * @type {Readonly<Record<${ keyTypeName } , ${ numTypeName } >>}
520+ * ${ capitalName } _Break property values
499521 */
500- export const ${ typeName } = {
522+ export const ${ typeName } = /** @type {const} */ ( {
501523` . trimStart ( ) ,
502524 ) ;
503525 for ( let cat of cats ) {
504526 f . write ( ` ${ cat } : ${ inversed [ cat ] } ,\n` ) ;
505527 }
506- f . write ( '};\n' ) ;
507-
508- f . write ( `
509- /**
510- * @type {${ rangeTypeName } []}
511- */
512- export const ${ name } _ranges = decodeUnicodeData(
513- /** @type {UnicodeDataEncoding} */
514- ('${ encodeUnicodeData ( breakTable . map ( row => [ row [ 0 ] , row [ 1 ] , 0 ] ) ) } '),
515- '${ breakTable . map ( row => inversed [ row [ 2 ] ] . toString ( 36 ) ) . join ( '' ) } ',
516- );
517- ` ,
518- ) ;
528+ f . write ( '});\n' ) ;
519529} ;
520530
521531/**
@@ -865,12 +875,27 @@ let graphemeTableOptimized = graphemeTable.filter(([from, to, cat]) => {
865875 return true ;
866876} ) ;
867877
878+ let graphemeCategories =
879+ [ 'Any' , ...Object . keys ( graphemeCats ) . concat ( [ 'Extended_Pictographic' ] ) . toSorted ( ) ] ;
880+
881+ let graphemCatsModule = '_grapheme_categories.js'
882+
883+ await emitSrc (
884+ graphemCatsModule ,
885+ async f => printCategoryModule (
886+ f ,
887+ graphemeCategories ,
888+ 'grapheme' ,
889+ ) ,
890+ ) ;
891+
868892await emitSrc (
869893 '_grapheme_data.js' ,
870- async f => printBreakModule (
894+ async f => printDataModule (
871895 f ,
872896 graphemeTableOptimized ,
873- Object . keys ( graphemeCats ) . concat ( [ 'Extended_Pictographic' ] ) ,
897+ graphemeCategories ,
898+ graphemCatsModule ,
874899 'grapheme' ,
875900 ) ,
876901) ;
0 commit comments