Skip to content

Commit

Permalink
chore: refactor for unicode property of strings (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Sep 27, 2023
1 parent 8c2965e commit 9f383cd
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
28 changes: 28 additions & 0 deletions scripts/update-unicode-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const DATA_SOURCES = [
binProperties: "#table-binary-unicode-properties",
gcValues: getLatestUnicodeGeneralCategoryValues,
scValues: getLatestUnicodeScriptValues,
binPropertiesOfStrings: "#table-binary-unicode-properties-of-strings",
},
]
const FILE_PATH = "src/unicode/properties.ts"
Expand All @@ -63,6 +64,7 @@ type Datum = {
binProperties: string[]
gcValues: string[]
scValues: string[]
binPropertiesOfStrings: string[]
}

// Main
Expand All @@ -72,12 +74,14 @@ type Datum = {
binProperties: new Set<string>(),
gcValues: new Set<string>(),
scValues: new Set<string>(),
binPropertiesOfStrings: new Set<string>(),
}

for (const {
binProperties,
gcValues,
scValues,
binPropertiesOfStrings,
url,
version,
} of DATA_SOURCES) {
Expand All @@ -86,6 +90,7 @@ type Datum = {
binProperties: [],
gcValues: [],
scValues: [],
binPropertiesOfStrings: [],
}
data[version] = datum

Expand Down Expand Up @@ -120,6 +125,13 @@ type Datum = {
scValues,
existing.scValues,
)
if (binPropertiesOfStrings) {
datum.binPropertiesOfStrings = await collectValues(
window,
binPropertiesOfStrings,
existing.binPropertiesOfStrings,
)
}

logger.log("Done")
}
Expand All @@ -140,6 +152,9 @@ const scValueSets = new DataSet(${Object.values(data)
const binPropertySets = new DataSet(${Object.values(data)
.map((d) => makeDataCode(d.binProperties))
.join(",")})
const binPropertyOfStringsSets = new DataSet(${Object.values(data)
.map((d) => makeDataCode(d.binPropertiesOfStrings))
.join(",")})
export function isValidUnicodeProperty(version: number, name: string, value: string): boolean {
if (gcNameSet.has(name)) {
Expand Down Expand Up @@ -169,6 +184,19 @@ export function isValidLoneUnicodeProperty(version: number, value: string): bool
.filter(Boolean)
.join(" || ")}
}
export function isValidLoneUnicodePropertyOfString(version: number, value: string): boolean {
return ${Object.entries(data)
.map(([version, { binPropertiesOfStrings }]) =>
makeVerificationCode(
version,
"binPropertyOfStringsSets",
binPropertiesOfStrings,
),
)
.filter(Boolean)
.join(" || ")}
}
`

logger.log("Formatting code...")
Expand Down
1 change: 1 addition & 0 deletions src/unicode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { isIdContinue, isIdStart } from "./ids"
export {
isValidLoneUnicodeProperty,
isValidUnicodeProperty,
isValidLoneUnicodePropertyOfString,
} from "./properties"

export const NULL = 0x00
Expand Down
16 changes: 0 additions & 16 deletions src/unicode/properties-of-strings.ts

This file was deleted.

16 changes: 16 additions & 0 deletions src/unicode/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ const binPropertySets = new DataSet(
"",
"",
)
const binPropertyOfStringsSets = new DataSet(
"",
"",
"",
"",
"",
"",
"Basic_Emoji Emoji_Keycap_Sequence RGI_Emoji RGI_Emoji_Flag_Sequence RGI_Emoji_Modifier_Sequence RGI_Emoji_Tag_Sequence RGI_Emoji_ZWJ_Sequence",
)

export function isValidUnicodeProperty(
version: number,
Expand Down Expand Up @@ -151,3 +160,10 @@ export function isValidLoneUnicodeProperty(
(version >= 2021 && binPropertySets.es2021.has(value))
)
}

export function isValidLoneUnicodePropertyOfString(
version: number,
value: string,
): boolean {
return version >= 2024 && binPropertyOfStringsSets.es2024.has(value)
}
2 changes: 1 addition & 1 deletion src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import {
isTrailSurrogate,
isValidLoneUnicodeProperty,
isValidUnicodeProperty,
isValidLoneUnicodePropertyOfString,
isValidUnicode,
AMPERSAND,
NUMBER_SIGN,
Expand All @@ -83,7 +84,6 @@ import {
TILDE,
LATIN_SMALL_LETTER_Q,
} from "./unicode"
import { isValidLoneUnicodePropertyOfString } from "./unicode/properties-of-strings"

// ^ $ \ . * + ? ( ) [ ] { } |
const SYNTAX_CHARACTER = new Set([
Expand Down

0 comments on commit 9f383cd

Please sign in to comment.