From 5552fdd5adea94c63fb231595519f0c8416944ef Mon Sep 17 00:00:00 2001 From: Eberhard Beilharz Date: Wed, 24 Jul 2024 15:52:50 +0200 Subject: [PATCH] chore(web): expose `util.ts` as separate sub-export The exports of the `util.ts` can now be imported as `@keymanapp/common-types/utils`. Those methods are used by developer. We still export the `Uni_IsSurrogate*` methods in `main.js` because they are needed by the keyboard-processor, but exporting everything would increase the size of the resulting `keymanweb.js`. Doing it this way keeps the size the same. --- .../keyboard-processor/src/text/stringDivergence.ts | 6 +++--- common/web/types/package.json | 4 ++++ common/web/types/src/kmx/element-string.ts | 3 ++- .../web/types/src/ldml-keyboard/pattern-parser.ts | 3 ++- common/web/types/src/main.ts | 2 +- common/web/types/src/util/consts.ts | 12 ++++++++++++ common/web/types/src/util/index.ts | 2 ++ common/web/types/src/util/util.ts | 13 +------------ .../src/kmc-ldml/src/compiler/empty-compiler.ts | 3 ++- developer/src/kmc-ldml/src/compiler/messages.ts | 3 ++- developer/src/kmc-ldml/src/compiler/tran.ts | 3 ++- 11 files changed, 33 insertions(+), 21 deletions(-) create mode 100644 common/web/types/src/util/consts.ts create mode 100644 common/web/types/src/util/index.ts diff --git a/common/web/keyboard-processor/src/text/stringDivergence.ts b/common/web/keyboard-processor/src/text/stringDivergence.ts index 31a94d8bdb..f45ea9f3df 100644 --- a/common/web/keyboard-processor/src/text/stringDivergence.ts +++ b/common/web/keyboard-processor/src/text/stringDivergence.ts @@ -1,4 +1,4 @@ -import { util } from '@keymanapp/common-types'; +import { Uni_IsSurrogate1, Uni_IsSurrogate2 } from '@keymanapp/common-types'; /** * Returns the index for the code point divergence point between two strings, as measured in code @@ -72,8 +72,8 @@ export function findCommonSubstringEndIndex(str1: string, str2: string, commonSu const divergentChar1 = str1.charCodeAt(index); const divergentChar2 = str2.charCodeAt(index + offset); - const commonSurrogateChecker = commonSuffix ? util.Uni_IsSurrogate2 : util.Uni_IsSurrogate1; - const divergentSurrogateChecker = commonSuffix ? util.Uni_IsSurrogate1 : util.Uni_IsSurrogate2; + const commonSurrogateChecker = commonSuffix ? Uni_IsSurrogate2 : Uni_IsSurrogate1; + const divergentSurrogateChecker = commonSuffix ? Uni_IsSurrogate1 : Uni_IsSurrogate2; // If the last common character if of the direction-appropriate surrogate type (for // comprising a potential split surrogate pair representing a non-BMP char)... diff --git a/common/web/types/package.json b/common/web/types/package.json index 9327e48590..4d41b42e6e 100644 --- a/common/web/types/package.json +++ b/common/web/types/package.json @@ -12,6 +12,10 @@ ".": { "es6-bundling": "./src/main.ts", "default": "./build/src/main.js" + }, + "./utils": { + "es6-bundling": "./src/util/index.ts", + "default": "./build/src/util/index.js" } }, "files": [ diff --git a/common/web/types/src/kmx/element-string.ts b/common/web/types/src/kmx/element-string.ts index b26878f459..1b24c3eb45 100644 --- a/common/web/types/src/kmx/element-string.ts +++ b/common/web/types/src/kmx/element-string.ts @@ -1,7 +1,8 @@ import { constants } from '@keymanapp/ldml-keyboard-constants'; import { DependencySections, StrsItem, UsetItem } from './kmx-plus.js'; import { ElementParser, ElementSegment, ElementType } from '../ldml-keyboard/pattern-parser.js'; -import { MATCH_HEX_ESCAPE, unescapeOneQuadString } from '../util/util.js'; +import { MATCH_HEX_ESCAPE } from '../util/consts.js'; +import { unescapeOneQuadString } from '../util/util.js'; export enum ElemElementFlags { none = 0, diff --git a/common/web/types/src/ldml-keyboard/pattern-parser.ts b/common/web/types/src/ldml-keyboard/pattern-parser.ts index 8901de6ddd..3e56096ec6 100644 --- a/common/web/types/src/ldml-keyboard/pattern-parser.ts +++ b/common/web/types/src/ldml-keyboard/pattern-parser.ts @@ -3,7 +3,8 @@ */ import { constants } from "@keymanapp/ldml-keyboard-constants"; -import { MATCH_QUAD_ESCAPE, isOneChar, unescapeOneQuadString, unescapeString, hexQuad } from "../util/util.js"; +import { MATCH_QUAD_ESCAPE } from "../util/consts.js"; +import { isOneChar, unescapeOneQuadString, unescapeString, hexQuad } from "../util/util.js"; /** * Helper function for extracting matched items diff --git a/common/web/types/src/main.ts b/common/web/types/src/main.ts index 3d18812876..92e3397c48 100644 --- a/common/web/types/src/main.ts +++ b/common/web/types/src/main.ts @@ -50,7 +50,7 @@ export { KeymanDeveloperProject, KeymanDeveloperProjectFile, KeymanDeveloperProj export * as KpsFile from './package/kps-file.js'; export * as KmpJsonFile from './package/kmp-json-file.js'; -export * as util from './util/util.js'; +export { Uni_IsSurrogate1, Uni_IsSurrogate2 } from './util/util.js'; export * as KeymanFileTypes from './util/file-types.js'; diff --git a/common/web/types/src/util/consts.ts b/common/web/types/src/util/consts.ts new file mode 100644 index 0000000000..271b3aa6bc --- /dev/null +++ b/common/web/types/src/util/consts.ts @@ -0,0 +1,12 @@ +// TODO-LDML: #7569 the below regex works, but captures more than it should +// (it would include \u{fffffffffffffffff } which +// is overlong and has a space at the end.) The second regex does not work yet. +export const MATCH_HEX_ESCAPE = /\\u{([0-9a-fA-F ]{1,})}/g; +// const MATCH_HEX_ESCAPE = /\\u{((?:(?:[0-9a-fA-F]{1,5})|(?:10[0-9a-fA-F]{4})(?: (?!}))?)+)}/g; + +/** regex for single quad escape such as \u0127 or \U00000000 */ +export const CONTAINS_QUAD_ESCAPE = /(?:\\u([0-9a-fA-F]{4})|\\U([0-9a-fA-F]{8}))/; + +/** regex for single quad escape such as \u0127 */ +export const MATCH_QUAD_ESCAPE = new RegExp(CONTAINS_QUAD_ESCAPE, 'g'); + diff --git a/common/web/types/src/util/index.ts b/common/web/types/src/util/index.ts new file mode 100644 index 0000000000..0833d3dd06 --- /dev/null +++ b/common/web/types/src/util/index.ts @@ -0,0 +1,2 @@ +export * from './consts.js'; +export * from './util.js'; diff --git a/common/web/types/src/util/util.ts b/common/web/types/src/util/util.ts index 7835743e62..c5f082328a 100644 --- a/common/web/types/src/util/util.ts +++ b/common/web/types/src/util/util.ts @@ -1,3 +1,4 @@ +import { MATCH_HEX_ESCAPE, MATCH_QUAD_ESCAPE } from './consts.js'; /** * xml2js will not place single-entry objects into arrays. Easiest way to fix * this is to box them ourselves as needed. Ensures that o.x is an array. @@ -16,18 +17,6 @@ export function boxXmlArray(o: any, x: string): void { } } -// TODO-LDML: #7569 the below regex works, but captures more than it should -// (it would include \u{fffffffffffffffff } which -// is overlong and has a space at the end.) The second regex does not work yet. -export const MATCH_HEX_ESCAPE = /\\u{([0-9a-fA-F ]{1,})}/g; -// const MATCH_HEX_ESCAPE = /\\u{((?:(?:[0-9a-fA-F]{1,5})|(?:10[0-9a-fA-F]{4})(?: (?!}))?)+)}/g; - -/** regex for single quad escape such as \u0127 or \U00000000 */ -export const CONTAINS_QUAD_ESCAPE = /(?:\\u([0-9a-fA-F]{4})|\\U([0-9a-fA-F]{8}))/; - -/** regex for single quad escape such as \u0127 */ -export const MATCH_QUAD_ESCAPE = new RegExp(CONTAINS_QUAD_ESCAPE, 'g'); - export class UnescapeError extends Error { } diff --git a/developer/src/kmc-ldml/src/compiler/empty-compiler.ts b/developer/src/kmc-ldml/src/compiler/empty-compiler.ts index 911e56d896..7d39545ce3 100644 --- a/developer/src/kmc-ldml/src/compiler/empty-compiler.ts +++ b/developer/src/kmc-ldml/src/compiler/empty-compiler.ts @@ -1,6 +1,7 @@ import { SectionIdent, constants } from '@keymanapp/ldml-keyboard-constants'; import { SectionCompiler } from "./section-compiler.js"; -import { LDMLKeyboard, KMXPlus, CompilerCallbacks, util, MarkerParser } from "@keymanapp/common-types"; +import { LDMLKeyboard, KMXPlus, CompilerCallbacks, MarkerParser } from "@keymanapp/common-types"; +import * as util from '@keymanapp/common-types/utils'; import { VarsCompiler } from './vars.js'; import { CompilerMessages } from './messages.js'; diff --git a/developer/src/kmc-ldml/src/compiler/messages.ts b/developer/src/kmc-ldml/src/compiler/messages.ts index aefd106ae4..4e1794dae4 100644 --- a/developer/src/kmc-ldml/src/compiler/messages.ts +++ b/developer/src/kmc-ldml/src/compiler/messages.ts @@ -1,4 +1,5 @@ -import { util, CompilerErrorNamespace, CompilerErrorSeverity, CompilerMessageSpec as m, CompilerMessageDef as def } from "@keymanapp/common-types"; +import { CompilerErrorNamespace, CompilerErrorSeverity, CompilerMessageSpec as m, CompilerMessageDef as def } from "@keymanapp/common-types"; +import * as util from '@keymanapp/common-types/utils'; // const SevInfo = CompilerErrorSeverity.Info | CompilerErrorNamespace.LdmlKeyboardCompiler; const SevHint = CompilerErrorSeverity.Hint | CompilerErrorNamespace.LdmlKeyboardCompiler; const SevWarn = CompilerErrorSeverity.Warn | CompilerErrorNamespace.LdmlKeyboardCompiler; diff --git a/developer/src/kmc-ldml/src/compiler/tran.ts b/developer/src/kmc-ldml/src/compiler/tran.ts index 7ddffd4747..64bc7a7c39 100644 --- a/developer/src/kmc-ldml/src/compiler/tran.ts +++ b/developer/src/kmc-ldml/src/compiler/tran.ts @@ -1,5 +1,6 @@ import { constants, SectionIdent } from "@keymanapp/ldml-keyboard-constants"; -import { KMXPlus, LDMLKeyboard, CompilerCallbacks, VariableParser, MarkerParser, util } from '@keymanapp/common-types'; +import { KMXPlus, LDMLKeyboard, CompilerCallbacks, VariableParser, MarkerParser } from '@keymanapp/common-types'; +import * as util from '@keymanapp/common-types/utils'; import { SectionCompiler } from "./section-compiler.js"; import Bksp = KMXPlus.Bksp;