@mailwoman/registry 11.0.0 → 11.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -6
- package/data/manifests/artifacts.json +35 -0
- package/data/manifests/coverage.json +63 -0
- package/data/manifests/sources.lock.json +12 -0
- package/data/normalized/language-subtags.json +18518 -0
- package/docs/data-policy.md +5 -5
- package/docs/performance.md +1 -1
- package/docs/rgy1.md +5 -5
- package/lib/artifacts/bcp47/extlang.fixture.json +260 -0
- package/lib/artifacts/bcp47/extlang.ts +32 -0
- package/lib/artifacts/bcp47/grandfathered.fixture.json +28 -0
- package/lib/artifacts/bcp47/grandfathered.ts +32 -0
- package/lib/artifacts/bcp47/language.fixture.json +8278 -0
- package/lib/artifacts/bcp47/language.ts +32 -0
- package/lib/artifacts/bcp47/redundant.fixture.json +69 -0
- package/lib/artifacts/bcp47/redundant.ts +32 -0
- package/lib/artifacts/bcp47/region.fixture.json +307 -0
- package/lib/artifacts/bcp47/region.ts +32 -0
- package/lib/artifacts/bcp47/script.fixture.json +227 -0
- package/lib/artifacts/bcp47/script.ts +32 -0
- package/lib/artifacts/bcp47/variant.fixture.json +141 -0
- package/lib/artifacts/bcp47/variant.ts +32 -0
- package/lib/artifacts/country-airport-collision.ts +7 -5
- package/lib/artifacts/iata-airport/membership.ts +7 -5
- package/lib/artifacts/iata-airport/records.ts +7 -5
- package/lib/artifacts/iso3166-1/alpha2-membership.ts +7 -5
- package/lib/artifacts/iso3166-1/alpha3-membership.ts +7 -5
- package/lib/artifacts/iso3166-1/records.ts +7 -5
- package/lib/bcp47/subtag-index.ts +65 -0
- package/lib/bcp47/tag.ts +301 -0
- package/lib/errors.ts +22 -9
- package/lib/iata/airports/records.ts +3 -4
- package/lib/iata/airports/validate.ts +6 -5
- package/lib/index.ts +18 -8
- package/lib/inspect.ts +5 -5
- package/lib/iso15924/codes.ts +169 -0
- package/lib/iso15924/unicode-script.ts +124 -0
- package/lib/iso3166/alpha2.ts +5 -5
- package/lib/iso3166/alpha3.ts +6 -6
- package/lib/iso3166/records.ts +9 -8
- package/lib/iso3166/relations.ts +5 -5
- package/lib/iso639/languages.ts +88 -0
- package/lib/key.ts +9 -7
- package/lib/normalize.ts +12 -8
- package/lib/overlap/country-airport.ts +14 -12
- package/lib/rgy1/base64.ts +3 -4
- package/lib/rgy1/reader.ts +8 -7
- package/lib/rgy1/sections.generated.ts +6 -5
- package/lib/rgy1/views.ts +5 -6
- package/lib/types.ts +66 -7
- package/lib/wasm.ts +11 -9
- package/out/artifacts/bcp47/extlang.d.ts +21 -0
- package/out/artifacts/bcp47/extlang.d.ts.map +1 -0
- package/out/artifacts/bcp47/extlang.fixture.json +260 -0
- package/out/artifacts/bcp47/extlang.js +17 -0
- package/out/artifacts/bcp47/extlang.js.map +1 -0
- package/out/artifacts/bcp47/grandfathered.d.ts +21 -0
- package/out/artifacts/bcp47/grandfathered.d.ts.map +1 -0
- package/out/artifacts/bcp47/grandfathered.fixture.json +28 -0
- package/out/artifacts/bcp47/grandfathered.js +17 -0
- package/out/artifacts/bcp47/grandfathered.js.map +1 -0
- package/out/artifacts/bcp47/language.d.ts +21 -0
- package/out/artifacts/bcp47/language.d.ts.map +1 -0
- package/out/artifacts/bcp47/language.fixture.json +8278 -0
- package/out/artifacts/bcp47/language.js +17 -0
- package/out/artifacts/bcp47/language.js.map +1 -0
- package/out/artifacts/bcp47/redundant.d.ts +21 -0
- package/out/artifacts/bcp47/redundant.d.ts.map +1 -0
- package/out/artifacts/bcp47/redundant.fixture.json +69 -0
- package/out/artifacts/bcp47/redundant.js +17 -0
- package/out/artifacts/bcp47/redundant.js.map +1 -0
- package/out/artifacts/bcp47/region.d.ts +21 -0
- package/out/artifacts/bcp47/region.d.ts.map +1 -0
- package/out/artifacts/bcp47/region.fixture.json +307 -0
- package/out/artifacts/bcp47/region.js +17 -0
- package/out/artifacts/bcp47/region.js.map +1 -0
- package/out/artifacts/bcp47/script.d.ts +21 -0
- package/out/artifacts/bcp47/script.d.ts.map +1 -0
- package/out/artifacts/bcp47/script.fixture.json +227 -0
- package/out/artifacts/bcp47/script.js +17 -0
- package/out/artifacts/bcp47/script.js.map +1 -0
- package/out/artifacts/bcp47/variant.d.ts +21 -0
- package/out/artifacts/bcp47/variant.d.ts.map +1 -0
- package/out/artifacts/bcp47/variant.fixture.json +141 -0
- package/out/artifacts/bcp47/variant.js +17 -0
- package/out/artifacts/bcp47/variant.js.map +1 -0
- package/out/artifacts/country-airport-collision.d.ts +0 -8
- package/out/artifacts/country-airport-collision.d.ts.map +1 -1
- package/out/artifacts/country-airport-collision.js +6 -5
- package/out/artifacts/country-airport-collision.js.map +1 -1
- package/out/artifacts/iata-airport/membership.d.ts +0 -8
- package/out/artifacts/iata-airport/membership.d.ts.map +1 -1
- package/out/artifacts/iata-airport/membership.js +6 -5
- package/out/artifacts/iata-airport/membership.js.map +1 -1
- package/out/artifacts/iata-airport/records.d.ts +0 -8
- package/out/artifacts/iata-airport/records.d.ts.map +1 -1
- package/out/artifacts/iata-airport/records.js +6 -5
- package/out/artifacts/iata-airport/records.js.map +1 -1
- package/out/artifacts/iso3166-1/alpha2-membership.d.ts +0 -8
- package/out/artifacts/iso3166-1/alpha2-membership.d.ts.map +1 -1
- package/out/artifacts/iso3166-1/alpha2-membership.js +6 -5
- package/out/artifacts/iso3166-1/alpha2-membership.js.map +1 -1
- package/out/artifacts/iso3166-1/alpha3-membership.d.ts +0 -8
- package/out/artifacts/iso3166-1/alpha3-membership.d.ts.map +1 -1
- package/out/artifacts/iso3166-1/alpha3-membership.js +6 -5
- package/out/artifacts/iso3166-1/alpha3-membership.js.map +1 -1
- package/out/artifacts/iso3166-1/records.d.ts +0 -8
- package/out/artifacts/iso3166-1/records.d.ts.map +1 -1
- package/out/artifacts/iso3166-1/records.js +6 -5
- package/out/artifacts/iso3166-1/records.js.map +1 -1
- package/out/bcp47/subtag-index.d.ts +22 -0
- package/out/bcp47/subtag-index.d.ts.map +1 -0
- package/out/bcp47/subtag-index.js +43 -0
- package/out/bcp47/subtag-index.js.map +1 -0
- package/out/bcp47/tag.d.ts +47 -0
- package/out/bcp47/tag.d.ts.map +1 -0
- package/out/bcp47/tag.js +218 -0
- package/out/bcp47/tag.js.map +1 -0
- package/out/errors.d.ts +12 -8
- package/out/errors.d.ts.map +1 -1
- package/out/errors.js +10 -8
- package/out/errors.js.map +1 -1
- package/out/iata/airports/records.d.ts +0 -7
- package/out/iata/airports/records.d.ts.map +1 -1
- package/out/iata/airports/records.js +2 -4
- package/out/iata/airports/records.js.map +1 -1
- package/out/iata/airports/validate.d.ts +0 -8
- package/out/iata/airports/validate.d.ts.map +1 -1
- package/out/iata/airports/validate.js +5 -5
- package/out/iata/airports/validate.js.map +1 -1
- package/out/index.d.ts +13 -8
- package/out/index.d.ts.map +1 -1
- package/out/index.js +6 -12
- package/out/index.js.map +1 -1
- package/out/inspect.d.ts +2 -5
- package/out/inspect.d.ts.map +1 -1
- package/out/inspect.js +4 -5
- package/out/inspect.js.map +1 -1
- package/out/iso15924/codes.d.ts +57 -0
- package/out/iso15924/codes.d.ts.map +1 -0
- package/out/iso15924/codes.js +133 -0
- package/out/iso15924/codes.js.map +1 -0
- package/out/iso15924/unicode-script.d.ts +10 -0
- package/out/iso15924/unicode-script.d.ts.map +1 -0
- package/out/iso15924/unicode-script.js +103 -0
- package/out/iso15924/unicode-script.js.map +1 -0
- package/out/iso3166/alpha2.d.ts +0 -8
- package/out/iso3166/alpha2.d.ts.map +1 -1
- package/out/iso3166/alpha2.js +4 -5
- package/out/iso3166/alpha2.js.map +1 -1
- package/out/iso3166/alpha3.d.ts +1 -9
- package/out/iso3166/alpha3.d.ts.map +1 -1
- package/out/iso3166/alpha3.js +5 -6
- package/out/iso3166/alpha3.js.map +1 -1
- package/out/iso3166/records.d.ts +0 -11
- package/out/iso3166/records.d.ts.map +1 -1
- package/out/iso3166/records.js +8 -8
- package/out/iso3166/records.js.map +1 -1
- package/out/iso3166/relations.d.ts +0 -7
- package/out/iso3166/relations.d.ts.map +1 -1
- package/out/iso3166/relations.js +4 -5
- package/out/iso3166/relations.js.map +1 -1
- package/out/iso639/languages.d.ts +40 -0
- package/out/iso639/languages.d.ts.map +1 -0
- package/out/iso639/languages.js +54 -0
- package/out/iso639/languages.js.map +1 -0
- package/out/key.d.ts +6 -7
- package/out/key.d.ts.map +1 -1
- package/out/key.js +8 -7
- package/out/key.js.map +1 -1
- package/out/normalize.d.ts +9 -8
- package/out/normalize.d.ts.map +1 -1
- package/out/normalize.js +2 -12
- package/out/normalize.js.map +1 -1
- package/out/overlap/country-airport.d.ts +2 -16
- package/out/overlap/country-airport.d.ts.map +1 -1
- package/out/overlap/country-airport.js +13 -12
- package/out/overlap/country-airport.js.map +1 -1
- package/out/rgy1/base64.d.ts +0 -4
- package/out/rgy1/base64.d.ts.map +1 -1
- package/out/rgy1/base64.js +2 -4
- package/out/rgy1/base64.js.map +1 -1
- package/out/rgy1/reader.d.ts +0 -11
- package/out/rgy1/reader.d.ts.map +1 -1
- package/out/rgy1/reader.js +7 -7
- package/out/rgy1/reader.js.map +1 -1
- package/out/rgy1/sections.generated.d.ts +3 -5
- package/out/rgy1/sections.generated.d.ts.map +1 -1
- package/out/rgy1/sections.generated.js +5 -5
- package/out/rgy1/sections.generated.js.map +1 -1
- package/out/rgy1/views.d.ts +2 -9
- package/out/rgy1/views.d.ts.map +1 -1
- package/out/rgy1/views.js +4 -6
- package/out/rgy1/views.js.map +1 -1
- package/out/sdk/fetch.d.ts +6 -6
- package/out/sdk/fetch.d.ts.map +1 -1
- package/out/sdk/fetch.js +23 -11
- package/out/sdk/fetch.js.map +1 -1
- package/out/sdk/fetch.run.d.ts +0 -12
- package/out/sdk/fetch.run.d.ts.map +1 -1
- package/out/sdk/fetch.run.js +22 -9
- package/out/sdk/fetch.run.js.map +1 -1
- package/out/sdk/sources.d.ts +7 -16
- package/out/sdk/sources.d.ts.map +1 -1
- package/out/sdk/sources.js +34 -14
- package/out/sdk/sources.js.map +1 -1
- package/out/tools/artifact-path.d.ts +0 -5
- package/out/tools/artifact-path.d.ts.map +1 -1
- package/out/tools/artifact-path.js +2 -5
- package/out/tools/artifact-path.js.map +1 -1
- package/out/tools/bench.run.d.ts +0 -17
- package/out/tools/bench.run.d.ts.map +1 -1
- package/out/tools/bench.run.js +13 -10
- package/out/tools/bench.run.js.map +1 -1
- package/out/tools/build-wasm.run.d.ts +0 -16
- package/out/tools/build-wasm.run.d.ts.map +1 -1
- package/out/tools/build-wasm.run.js +8 -10
- package/out/tools/build-wasm.run.js.map +1 -1
- package/out/tools/generate-sections.run.d.ts +0 -11
- package/out/tools/generate-sections.run.d.ts.map +1 -1
- package/out/tools/generate-sections.run.js +5 -14
- package/out/tools/generate-sections.run.js.map +1 -1
- package/out/tools/generate.run.d.ts +0 -16
- package/out/tools/generate.run.d.ts.map +1 -1
- package/out/tools/generate.run.js +61 -17
- package/out/tools/generate.run.js.map +1 -1
- package/out/tools/language-subtags.d.ts +59 -0
- package/out/tools/language-subtags.d.ts.map +1 -0
- package/out/tools/language-subtags.js +145 -0
- package/out/tools/language-subtags.js.map +1 -0
- package/out/tools/normalize.d.ts +4 -14
- package/out/tools/normalize.d.ts.map +1 -1
- package/out/tools/normalize.js +14 -14
- package/out/tools/normalize.js.map +1 -1
- package/out/tools/normalize.run.d.ts +0 -12
- package/out/tools/normalize.run.d.ts.map +1 -1
- package/out/tools/normalize.run.js +24 -7
- package/out/tools/normalize.run.js.map +1 -1
- package/out/types.d.ts +62 -7
- package/out/types.d.ts.map +1 -1
- package/out/types.js +12 -9
- package/out/types.js.map +1 -1
- package/out/wasm.d.ts +0 -13
- package/out/wasm.d.ts.map +1 -1
- package/out/wasm.js +10 -9
- package/out/wasm.js.map +1 -1
- package/package.json +12 -4
- package/sdk/fetch.run.ts +25 -9
- package/sdk/fetch.ts +32 -12
- package/sdk/sources.ts +36 -14
- package/tools/artifact-path.ts +2 -5
- package/tools/bench.run.ts +19 -10
- package/tools/build-wasm.run.ts +9 -10
- package/tools/generate-sections.run.ts +6 -14
- package/tools/generate.run.ts +68 -17
- package/tools/language-subtags.ts +195 -0
- package/tools/normalize.run.ts +28 -7
- package/tools/normalize.ts +16 -15
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A case-insensitive lookup over one subtag table, including its private-use ranges.
|
|
6
|
+
*
|
|
7
|
+
* The registry states a private-use block as one range record, such as `qaa..qtz` or `QM..QZ`.
|
|
8
|
+
* A subtag inside a range answers that range's record.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { SubtagRow } from "#types"
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A lookup from a subtag in any case to its registry row.
|
|
15
|
+
*/
|
|
16
|
+
export interface SubtagIndex {
|
|
17
|
+
get(subtag: string): SubtagRow | null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Builds the lookup for one table's rows.
|
|
22
|
+
*/
|
|
23
|
+
export function indexSubtagRows(rows: readonly SubtagRow[]): SubtagIndex {
|
|
24
|
+
const exact = new Map<string, SubtagRow>()
|
|
25
|
+
const ranges: Array<{ low: string; high: string; row: SubtagRow }> = []
|
|
26
|
+
|
|
27
|
+
for (const row of rows) {
|
|
28
|
+
const key = row.subtag.toLowerCase()
|
|
29
|
+
const dots = key.indexOf("..")
|
|
30
|
+
|
|
31
|
+
if (dots === -1) {
|
|
32
|
+
exact.set(key, row)
|
|
33
|
+
} else {
|
|
34
|
+
ranges.push({ low: key.slice(0, dots), high: key.slice(dots + 2), row })
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
get(subtag) {
|
|
40
|
+
const key = subtag.toLowerCase()
|
|
41
|
+
const found = exact.get(key)
|
|
42
|
+
|
|
43
|
+
if (found) return found
|
|
44
|
+
|
|
45
|
+
for (const range of ranges) {
|
|
46
|
+
if (key.length === range.low.length && key >= range.low && key <= range.high) return range.row
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return null
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Returns a lookup built on its first call from `rows()`, then shared.
|
|
56
|
+
*/
|
|
57
|
+
export function lazySubtagIndex(rows: () => readonly SubtagRow[]): () => SubtagIndex {
|
|
58
|
+
let index: SubtagIndex | null = null
|
|
59
|
+
|
|
60
|
+
return () => {
|
|
61
|
+
index ??= indexSubtagRows(rows())
|
|
62
|
+
|
|
63
|
+
return index
|
|
64
|
+
}
|
|
65
|
+
}
|
package/lib/bcp47/tag.ts
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* BCP 47 language tags (RFC 5646): parsing, validation against the IANA Language
|
|
6
|
+
* Subtag Registry, and canonicalization.
|
|
7
|
+
*
|
|
8
|
+
* A tag is accepted when it is well-formed (section 2.1) and valid (section 2.2.9):
|
|
9
|
+
* every language, extlang, script, region and variant subtag is registered, an extlang's
|
|
10
|
+
* prefix matches the language, and no variant or extension singleton repeats.
|
|
11
|
+
*
|
|
12
|
+
* The canonical form (section 4.5) replaces a deprecated subtag or tag with its preferred value,
|
|
13
|
+
* replaces a language-extlang pair with the extlang's preferred value (`zh-yue` becomes `yue`),
|
|
14
|
+
* orders extensions by singleton, and applies the case conventions: `ja-Jpan-JP`.
|
|
15
|
+
* It keeps a script the language would suppress, so `ja-Jpan` stays `ja-Jpan`.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { extlangSubtagRows } from "#artifacts/bcp47/extlang"
|
|
19
|
+
import { grandfatheredSubtagRows } from "#artifacts/bcp47/grandfathered"
|
|
20
|
+
import { languageSubtagRows } from "#artifacts/bcp47/language"
|
|
21
|
+
import { redundantSubtagRows } from "#artifacts/bcp47/redundant"
|
|
22
|
+
import { regionSubtagRows } from "#artifacts/bcp47/region"
|
|
23
|
+
import { scriptSubtagRows } from "#artifacts/bcp47/script"
|
|
24
|
+
import { variantSubtagRows } from "#artifacts/bcp47/variant"
|
|
25
|
+
import { lazySubtagIndex } from "#bcp47/subtag-index"
|
|
26
|
+
import { type Parsed, ParseFailure } from "#errors"
|
|
27
|
+
import type { BCP47 } from "#types"
|
|
28
|
+
|
|
29
|
+
const languages = lazySubtagIndex(languageSubtagRows)
|
|
30
|
+
const extlangs = lazySubtagIndex(extlangSubtagRows)
|
|
31
|
+
const scripts = lazySubtagIndex(scriptSubtagRows)
|
|
32
|
+
const regions = lazySubtagIndex(regionSubtagRows)
|
|
33
|
+
const variants = lazySubtagIndex(variantSubtagRows)
|
|
34
|
+
const grandfatheredTags = lazySubtagIndex(grandfatheredSubtagRows)
|
|
35
|
+
const redundantTags = lazySubtagIndex(redundantSubtagRows)
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A parsed tag in canonical form.
|
|
39
|
+
*/
|
|
40
|
+
export interface LanguageTag {
|
|
41
|
+
tag: BCP47.LanguageTag
|
|
42
|
+
/**
|
|
43
|
+
* The primary language subtag, such as `ja`.
|
|
44
|
+
*
|
|
45
|
+
* An irregular grandfathered tag without a preferred value, such as `i-default`,
|
|
46
|
+
* holds the whole tag here.
|
|
47
|
+
* A private-use tag such as `x-whatever` holds an empty string.
|
|
48
|
+
*/
|
|
49
|
+
language: string
|
|
50
|
+
script: string | null
|
|
51
|
+
region: string | null
|
|
52
|
+
variants: readonly string[]
|
|
53
|
+
/**
|
|
54
|
+
* Each extension with its singleton, such as `u-ca-gregory`, ordered by singleton.
|
|
55
|
+
*/
|
|
56
|
+
extensions: readonly string[]
|
|
57
|
+
/**
|
|
58
|
+
* The private-use sequence with its `x-` prefix, or `null`.
|
|
59
|
+
*/
|
|
60
|
+
privateUse: string | null
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// The subtag grammars of RFC 5646, section 2.1, over a lowercased tag.
|
|
64
|
+
const SUBTAG = /^[a-z0-9]{1,8}$/
|
|
65
|
+
const LANGUAGE = /^(?:[a-z]{2,3}|[a-z]{5,8})$/
|
|
66
|
+
const SHORT_LANGUAGE = /^[a-z]{2,3}$/
|
|
67
|
+
const EXTLANG = /^[a-z]{3}$/
|
|
68
|
+
const SCRIPT = /^[a-z]{4}$/
|
|
69
|
+
const REGION = /^(?:[a-z]{2}|[0-9]{3})$/
|
|
70
|
+
const VARIANT = /^(?:[a-z0-9]{5,8}|[0-9][a-z0-9]{3})$/
|
|
71
|
+
const SINGLETON = /^[0-9a-z]$/
|
|
72
|
+
const EXTENSION_BODY = /^[a-z0-9]{2,8}$/
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The grammar admits up to three extlang subtags after a two- or three-letter language.
|
|
76
|
+
*/
|
|
77
|
+
const MAX_EXTLANGS = 3
|
|
78
|
+
|
|
79
|
+
function refuse(input: string, reason: ParseFailure, part?: string): Parsed<LanguageTag> {
|
|
80
|
+
return part ? { ok: false, reason, input, part } : { ok: false, reason, input }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function titleCase(subtag: string): string {
|
|
84
|
+
return subtag.charAt(0).toUpperCase() + subtag.slice(1)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function compose(parts: Omit<LanguageTag, "tag">): BCP47.LanguageTag {
|
|
88
|
+
return [parts.language, parts.script, parts.region, ...parts.variants, ...parts.extensions, parts.privateUse]
|
|
89
|
+
.filter((part): part is string => typeof part === "string" && part !== "")
|
|
90
|
+
.join("-") as BCP47.LanguageTag
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Parses, validates and canonicalizes a BCP 47 tag in any case.
|
|
95
|
+
*
|
|
96
|
+
* A refusal reports `shape` for an ill-formed tag and `not-found-in-snapshot` for a well-formed
|
|
97
|
+
* tag with an unregistered or misplaced subtag, with the offending subtag as `part`.
|
|
98
|
+
*/
|
|
99
|
+
export function parseLanguageTag(input: string): Parsed<LanguageTag> {
|
|
100
|
+
const lower = input.toLowerCase()
|
|
101
|
+
const grandfathered = grandfatheredTags().get(lower)
|
|
102
|
+
|
|
103
|
+
if (grandfathered) {
|
|
104
|
+
if (grandfathered.preferredValue) return parseLanguageTag(grandfathered.preferredValue)
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
ok: true,
|
|
108
|
+
value: {
|
|
109
|
+
tag: grandfathered.subtag as BCP47.LanguageTag,
|
|
110
|
+
language: grandfathered.subtag,
|
|
111
|
+
script: null,
|
|
112
|
+
region: null,
|
|
113
|
+
variants: [],
|
|
114
|
+
extensions: [],
|
|
115
|
+
privateUse: null,
|
|
116
|
+
},
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const redundant = redundantTags().get(lower)
|
|
121
|
+
|
|
122
|
+
if (redundant?.preferredValue) return parseLanguageTag(redundant.preferredValue)
|
|
123
|
+
|
|
124
|
+
const subtags = lower.split("-")
|
|
125
|
+
|
|
126
|
+
if (subtags.some((subtag) => !SUBTAG.test(subtag))) return refuse(input, ParseFailure.Shape)
|
|
127
|
+
|
|
128
|
+
let position = 0
|
|
129
|
+
const at = (): string | undefined => subtags[position]
|
|
130
|
+
const matches = (grammar: RegExp): boolean => grammar.test(at() ?? "")
|
|
131
|
+
|
|
132
|
+
if (at() === "x") {
|
|
133
|
+
if (!subtags[1]) return refuse(input, ParseFailure.Shape)
|
|
134
|
+
|
|
135
|
+
const privateUse = subtags.join("-")
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
ok: true,
|
|
139
|
+
value: {
|
|
140
|
+
tag: privateUse as BCP47.LanguageTag,
|
|
141
|
+
language: "",
|
|
142
|
+
script: null,
|
|
143
|
+
region: null,
|
|
144
|
+
variants: [],
|
|
145
|
+
extensions: [],
|
|
146
|
+
privateUse,
|
|
147
|
+
},
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
let language = at()
|
|
152
|
+
|
|
153
|
+
if (!language || !LANGUAGE.test(language)) return refuse(input, ParseFailure.Shape)
|
|
154
|
+
|
|
155
|
+
const languageRow = languages().get(language)
|
|
156
|
+
|
|
157
|
+
if (!languageRow) return refuse(input, ParseFailure.NotFound, language)
|
|
158
|
+
|
|
159
|
+
language = languageRow.preferredValue ?? language
|
|
160
|
+
|
|
161
|
+
position++
|
|
162
|
+
|
|
163
|
+
// One extlang is valid.
|
|
164
|
+
// The grammar admits three, so a second is well-formed and refused as invalid.
|
|
165
|
+
const extlangsSeen: string[] = []
|
|
166
|
+
|
|
167
|
+
while (SHORT_LANGUAGE.test(subtags[0]!) && matches(EXTLANG)) {
|
|
168
|
+
extlangsSeen.push(at()!)
|
|
169
|
+
|
|
170
|
+
position++
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (extlangsSeen.length > MAX_EXTLANGS) return refuse(input, ParseFailure.Shape)
|
|
174
|
+
|
|
175
|
+
if (extlangsSeen.length > 1) return refuse(input, ParseFailure.NotFound, extlangsSeen[1])
|
|
176
|
+
|
|
177
|
+
const extlang = extlangsSeen[0]
|
|
178
|
+
|
|
179
|
+
if (extlang) {
|
|
180
|
+
const extlangRow = extlangs().get(extlang)
|
|
181
|
+
|
|
182
|
+
if (!extlangRow || extlangRow.prefixes?.[0] !== subtags[0]) return refuse(input, ParseFailure.NotFound, extlang)
|
|
183
|
+
|
|
184
|
+
language = extlangRow.preferredValue ?? extlang
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
let script: string | null = null
|
|
188
|
+
|
|
189
|
+
if (matches(SCRIPT)) {
|
|
190
|
+
const row = scripts().get(at()!)
|
|
191
|
+
|
|
192
|
+
if (!row) return refuse(input, ParseFailure.NotFound, at())
|
|
193
|
+
|
|
194
|
+
script = titleCase(row.preferredValue ?? at()!)
|
|
195
|
+
|
|
196
|
+
position++
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
let region: string | null = null
|
|
200
|
+
|
|
201
|
+
if (matches(REGION)) {
|
|
202
|
+
const row = regions().get(at()!)
|
|
203
|
+
|
|
204
|
+
if (!row) return refuse(input, ParseFailure.NotFound, at())
|
|
205
|
+
|
|
206
|
+
region = (row.preferredValue ?? at()!).toUpperCase()
|
|
207
|
+
|
|
208
|
+
position++
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const variantsSeen: string[] = []
|
|
212
|
+
|
|
213
|
+
while (at()) {
|
|
214
|
+
const subtag = at()!
|
|
215
|
+
|
|
216
|
+
if (!VARIANT.test(subtag)) break
|
|
217
|
+
|
|
218
|
+
const row = variants().get(subtag)
|
|
219
|
+
|
|
220
|
+
if (!row) return refuse(input, ParseFailure.NotFound, subtag)
|
|
221
|
+
|
|
222
|
+
const canonical = row.preferredValue ?? subtag
|
|
223
|
+
|
|
224
|
+
if (variantsSeen.includes(canonical)) return refuse(input, ParseFailure.NotFound, subtag)
|
|
225
|
+
|
|
226
|
+
variantsSeen.push(canonical)
|
|
227
|
+
|
|
228
|
+
position++
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const extensions: string[] = []
|
|
232
|
+
const singletons = new Set<string>()
|
|
233
|
+
let privateUse: string | null = null
|
|
234
|
+
|
|
235
|
+
while (at()) {
|
|
236
|
+
const singleton = at()!
|
|
237
|
+
|
|
238
|
+
if (!SINGLETON.test(singleton)) return refuse(input, ParseFailure.Shape, singleton)
|
|
239
|
+
|
|
240
|
+
position++
|
|
241
|
+
|
|
242
|
+
const body: string[] = []
|
|
243
|
+
|
|
244
|
+
if (singleton === "x") {
|
|
245
|
+
while (at()) {
|
|
246
|
+
body.push(at()!)
|
|
247
|
+
|
|
248
|
+
position++
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (!body.length) return refuse(input, ParseFailure.Shape, singleton)
|
|
252
|
+
|
|
253
|
+
privateUse = ["x", ...body].join("-")
|
|
254
|
+
|
|
255
|
+
break
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
while (matches(EXTENSION_BODY)) {
|
|
259
|
+
body.push(at()!)
|
|
260
|
+
|
|
261
|
+
position++
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (!body.length) return refuse(input, ParseFailure.Shape, singleton)
|
|
265
|
+
|
|
266
|
+
if (singletons.has(singleton)) return refuse(input, ParseFailure.NotFound, singleton)
|
|
267
|
+
|
|
268
|
+
singletons.add(singleton)
|
|
269
|
+
extensions.push([singleton, ...body].join("-"))
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
extensions.sort()
|
|
273
|
+
|
|
274
|
+
const parts = { language, script, region, variants: variantsSeen, extensions, privateUse }
|
|
275
|
+
|
|
276
|
+
return { ok: true, value: { tag: compose(parts), ...parts } }
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Whether `input` is a valid tag already in canonical form.
|
|
281
|
+
*/
|
|
282
|
+
export function isLanguageTag(input: string): input is BCP47.LanguageTag {
|
|
283
|
+
const parsed = parseLanguageTag(input)
|
|
284
|
+
|
|
285
|
+
return parsed.ok && parsed.value.tag === input
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* The script a tag's text is written in: its explicit script subtag,
|
|
290
|
+
* else its language's suppressed script, else `null`.
|
|
291
|
+
*
|
|
292
|
+
* `ja-Jpan` and `ja` both give `Jpan`; `zh` gives `null`, because Chinese is written
|
|
293
|
+
* in `Hans` or `Hant` and the registry suppresses neither.
|
|
294
|
+
*/
|
|
295
|
+
export function impliedScript(tag: LanguageTag): BCP47.ScriptCode | null {
|
|
296
|
+
if (tag.script) return tag.script as BCP47.ScriptCode
|
|
297
|
+
|
|
298
|
+
const suppressed = languages().get(tag.language)?.suppressScript
|
|
299
|
+
|
|
300
|
+
return suppressed ? (suppressed as BCP47.ScriptCode) : null
|
|
301
|
+
}
|
package/lib/errors.ts
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
|
-
* @copyright Sister Software
|
|
3
|
-
* @license AGPL-3.0
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
5
|
* The small error vocabulary a strict parse answers with.
|
|
7
6
|
*
|
|
8
|
-
* The four reasons are distinct so a caller can tell a misspelling from an absence.
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
7
|
+
* The four reasons are distinct so a caller can tell a misspelling from an absence.
|
|
8
|
+
* `shape` is the wrong length or alphabet.
|
|
9
|
+
*
|
|
10
|
+
* `case` is the right letters in the wrong case.
|
|
11
|
+
* `not-found-in-snapshot` is a canonical spelling the profile holds no record for.
|
|
12
|
+
*
|
|
13
|
+
* `status-excluded` is a code the profile holds under a status the strict brand leaves out.
|
|
14
|
+
* The messages name no record, so a membership-only import pulls in no name dictionary.
|
|
12
15
|
*/
|
|
13
16
|
|
|
14
17
|
/**
|
|
@@ -26,7 +29,17 @@ export type ParseFailure = (typeof ParseFailure)[keyof typeof ParseFailure]
|
|
|
26
29
|
/**
|
|
27
30
|
* A structured parse result: the branded value, or why the input was refused.
|
|
28
31
|
*/
|
|
29
|
-
export type Parsed<T> =
|
|
32
|
+
export type Parsed<T> =
|
|
33
|
+
| { ok: true; value: T }
|
|
34
|
+
| {
|
|
35
|
+
ok: false
|
|
36
|
+
reason: ParseFailure
|
|
37
|
+
input: string
|
|
38
|
+
/**
|
|
39
|
+
* The part of a compound input that was refused, such as one subtag of a BCP 47 tag.
|
|
40
|
+
*/
|
|
41
|
+
part?: string
|
|
42
|
+
}
|
|
30
43
|
|
|
31
44
|
/**
|
|
32
45
|
* Reports which refusal a fixed-length grammar gives an input, or `null` when the shape passes.
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
|
-
* @copyright Sister Software
|
|
3
|
-
* @license AGPL-3.0
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
5
|
* Airport record retrieval and the located-in-country relation over the `iata-airport-records` artifact.
|
|
7
6
|
*/
|
|
8
7
|
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
|
-
*
|
|
3
|
-
* @license AGPL-3.0
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
+
* The strict airport-code predicate over the `iata-airport-membership` bitset.
|
|
5
6
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
7
|
+
* An import of it retains that 2,197-byte bitset and the reader,
|
|
8
|
+
* and no airport record, coordinate or name.
|
|
8
9
|
*/
|
|
9
10
|
|
|
10
11
|
import { iataAirportMembershipArtifact } from "#artifacts/iata-airport/membership"
|
package/lib/index.ts
CHANGED
|
@@ -1,22 +1,29 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
+
* `@mailwoman/registry`: standardized identifiers and their relationships,
|
|
6
|
+
* compiled from pinned open sources.
|
|
5
7
|
*
|
|
6
|
-
*
|
|
8
|
+
* The root entry re-exports the narrow modules.
|
|
9
|
+
* Every export is a function or a type, and no module decodes an artifact until its
|
|
10
|
+
* first call, so importing the root costs the modules the bundler keeps and no table.
|
|
7
11
|
*
|
|
8
|
-
* The root entry re-exports the narrow modules. Every export is a function or a type, and no module decodes an
|
|
9
|
-
* artifact until its first call, so importing the root costs the modules the bundler keeps and no table.
|
|
10
12
|
* Import a subpath such as `@mailwoman/registry/iso3166/alpha2` when a bundle must retain one table only.
|
|
11
13
|
*/
|
|
12
14
|
|
|
15
|
+
export type { LanguageTag } from "#bcp47/tag"
|
|
16
|
+
export { impliedScript, isLanguageTag, parseLanguageTag } from "#bcp47/tag"
|
|
13
17
|
export type { ParseFailure, Parsed } from "#errors"
|
|
14
18
|
export { getAirportByIATA, airportsInCountry, airportRecordCount, airportRecordsHeader } from "#iata/airports/records"
|
|
15
19
|
export { isAirportCode, parseAirportCode } from "#iata/airports/validate"
|
|
16
20
|
export type { ArtifactInspection } from "#inspect"
|
|
17
21
|
export { inspectArtifact } from "#inspect"
|
|
22
|
+
export type { ScriptRecord } from "#iso15924/codes"
|
|
23
|
+
export { getScript, isScriptCode, isWrittenInScript, parseScriptCode, unicodeScriptsOf } from "#iso15924/codes"
|
|
18
24
|
export { isCountryCodeAlpha2, parseCountryCodeAlpha2 } from "#iso3166/alpha2"
|
|
19
25
|
export { isCountryCodeAlpha3, parseCountryCodeAlpha3 } from "#iso3166/alpha3"
|
|
26
|
+
|
|
20
27
|
export {
|
|
21
28
|
countryRecordCount,
|
|
22
29
|
countryRecordsHeader,
|
|
@@ -24,10 +31,13 @@ export {
|
|
|
24
31
|
getCountryByAlpha3,
|
|
25
32
|
getCountryByNumeric,
|
|
26
33
|
} from "#iso3166/records"
|
|
34
|
+
|
|
27
35
|
export { alpha2ForAlpha3, alpha2ForNumeric, alpha3ForAlpha2 } from "#iso3166/relations"
|
|
36
|
+
export type { LanguageRecord } from "#iso639/languages"
|
|
37
|
+
export { getLanguage, isLanguageSubtag, parseLanguageSubtag } from "#iso639/languages"
|
|
28
38
|
export type { Normalization } from "#normalize"
|
|
29
39
|
export { canonicalSpelling } from "#normalize"
|
|
30
40
|
export type { CodeMatch } from "#overlap/country-airport"
|
|
31
41
|
export { interpretCountryOrAirportCode, isCountryAirportCollision } from "#overlap/country-airport"
|
|
32
|
-
export type { AirportRecord, CountryRecord, IATA, ICAO, ISO } from "#types"
|
|
33
|
-
export { AirportCountryStatus, CountryStatus } from "#types"
|
|
42
|
+
export type { AirportRecord, BCP47, CountryRecord, IATA, ICAO, ISO, SubtagRow } from "#types"
|
|
43
|
+
export { AirportCountryStatus, CountryStatus, SubtagType } from "#types"
|
package/lib/inspect.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
|
-
* Coverage and provenance inspection: what an artifact's profile holds, read from its header and directory.
|
|
5
|
+
* Coverage and provenance inspection: what an artifact's profile holds,
|
|
6
|
+
* read from its header and directory.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { type Artifact, type ArtifactHeader, Artifact as RGY1Artifact } from "#rgy1/reader"
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* ISO 15924 script codes, read from the script subtags of the IANA Language Subtag Registry.
|
|
6
|
+
*
|
|
7
|
+
* An import of this module retains the 225-row script table and no language or region row.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { scriptSubtagRows } from "#artifacts/bcp47/script"
|
|
11
|
+
import { lazySubtagIndex } from "#bcp47/subtag-index"
|
|
12
|
+
import { type Parsed, ParseFailure } from "#errors"
|
|
13
|
+
import type { BCP47 } from "#types"
|
|
14
|
+
|
|
15
|
+
const index = lazySubtagIndex(scriptSubtagRows)
|
|
16
|
+
|
|
17
|
+
const CANONICAL = /^[A-Z][a-z]{3}$/
|
|
18
|
+
const LETTERS = /^[A-Za-z]{4}$/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* One script as the registry describes it.
|
|
22
|
+
*/
|
|
23
|
+
export interface ScriptRecord {
|
|
24
|
+
code: BCP47.ScriptCode
|
|
25
|
+
/**
|
|
26
|
+
* The registry's first description, such as `Japanese (alias for Han + Hiragana + Katakana)`.
|
|
27
|
+
*/
|
|
28
|
+
name: string
|
|
29
|
+
descriptions: readonly string[]
|
|
30
|
+
/**
|
|
31
|
+
* The date the registry deprecated the code, or `null`.
|
|
32
|
+
*/
|
|
33
|
+
deprecated: string | null
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Whether `input` is a script code the registry holds, in its canonical title case.
|
|
38
|
+
*
|
|
39
|
+
* `Latn` and `Jpan` are.
|
|
40
|
+
* `latn`, `LATN` and `Latin` are not.
|
|
41
|
+
*/
|
|
42
|
+
export function isScriptCode(input: string): input is BCP47.ScriptCode {
|
|
43
|
+
return CANONICAL.test(input) && index().get(input) !== null
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The branded script code, or the reason the input was refused.
|
|
48
|
+
*/
|
|
49
|
+
export function parseScriptCode(input: string): Parsed<BCP47.ScriptCode> {
|
|
50
|
+
if (!LETTERS.test(input)) return { ok: false, reason: ParseFailure.Shape, input }
|
|
51
|
+
|
|
52
|
+
if (!CANONICAL.test(input)) return { ok: false, reason: ParseFailure.Case, input }
|
|
53
|
+
|
|
54
|
+
if (!isScriptCode(input)) return { ok: false, reason: ParseFailure.NotFound, input }
|
|
55
|
+
|
|
56
|
+
return { ok: true, value: input }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The record of a canonical script code, or `null`.
|
|
61
|
+
*/
|
|
62
|
+
export function getScript(code: string): ScriptRecord | null {
|
|
63
|
+
if (!CANONICAL.test(code)) return null
|
|
64
|
+
|
|
65
|
+
const row = index().get(code)
|
|
66
|
+
|
|
67
|
+
if (!row) return null
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
code: code as BCP47.ScriptCode,
|
|
71
|
+
name: row.descriptions[0] ?? code,
|
|
72
|
+
descriptions: row.descriptions,
|
|
73
|
+
deprecated: row.deprecated ?? null,
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* ISO 15924 codes that name a combination or a variant of Unicode scripts, mapped to the
|
|
79
|
+
* Unicode Script property values (short aliases) whose characters they cover.
|
|
80
|
+
*
|
|
81
|
+
* Unicode encodes the characters of a variant code, such as Fraktur `Latf`, under the base script.
|
|
82
|
+
* A combined code, such as Japanese `Jpan`, covers several scripts at once.
|
|
83
|
+
*/
|
|
84
|
+
const UNICODE_SCRIPT_EXPANSIONS: Readonly<Record<string, readonly string[]>> = {
|
|
85
|
+
Jpan: ["Hani", "Hira", "Kana"],
|
|
86
|
+
Kore: ["Hang", "Hani"],
|
|
87
|
+
Hans: ["Hani"],
|
|
88
|
+
Hant: ["Hani"],
|
|
89
|
+
Hanb: ["Hani", "Bopo"],
|
|
90
|
+
Hrkt: ["Hira", "Kana"],
|
|
91
|
+
Latf: ["Latn"],
|
|
92
|
+
Latg: ["Latn"],
|
|
93
|
+
Aran: ["Arab"],
|
|
94
|
+
Cyrs: ["Cyrl"],
|
|
95
|
+
Syre: ["Syrc"],
|
|
96
|
+
Syrj: ["Syrc"],
|
|
97
|
+
Syrn: ["Syrc"],
|
|
98
|
+
Geok: ["Geor"],
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function isUnicodeScriptValue(code: string): boolean {
|
|
102
|
+
try {
|
|
103
|
+
new RegExp(`\\p{Script=${code}}`, "u")
|
|
104
|
+
|
|
105
|
+
return true
|
|
106
|
+
} catch {
|
|
107
|
+
return false
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The Unicode Script property values whose characters a script code covers.
|
|
113
|
+
*
|
|
114
|
+
* `Jpan` gives `Hani`, `Hira` and `Kana`, and `Latn` gives `Latn`.
|
|
115
|
+
* A code with no Unicode script of its own, such as `Zmth` (mathematical notation)
|
|
116
|
+
* or a private-use code, gives an empty list.
|
|
117
|
+
*
|
|
118
|
+
* The runtime's regular-expression engine decides which values it knows.
|
|
119
|
+
*/
|
|
120
|
+
export function unicodeScriptsOf(code: BCP47.ScriptCode): readonly string[] {
|
|
121
|
+
const expanded = UNICODE_SCRIPT_EXPANSIONS[code]
|
|
122
|
+
|
|
123
|
+
if (expanded) return expanded
|
|
124
|
+
|
|
125
|
+
return isUnicodeScriptValue(code) ? [code] : []
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Whether every letter in `text` belongs to one of the scripts the code covers.
|
|
130
|
+
*
|
|
131
|
+
* Digits, punctuation, spaces and other non-letters are ignored, so `ニューヨーク州 10001` is written in `Jpan`.
|
|
132
|
+
* A text without letters returns `false`, because it shows no script at all.
|
|
133
|
+
*
|
|
134
|
+
* Common and Inherited letters, such as the prolonged sound mark `ー` in `ニューヨーク`,
|
|
135
|
+
* are accepted beside any script and don't count as evidence of one.
|
|
136
|
+
*
|
|
137
|
+
* Letters of an `alsoAccepted` script pass without counting, so `金龍酒家, 12 Gerrard Street`
|
|
138
|
+
* is written in `Latn` when `Hani` is also accepted: the Latin letters make up
|
|
139
|
+
* the address, and the Han name sits inside it.
|
|
140
|
+
*/
|
|
141
|
+
export function isWrittenInScript(
|
|
142
|
+
text: string,
|
|
143
|
+
code: BCP47.ScriptCode,
|
|
144
|
+
alsoAccepted: readonly BCP47.ScriptCode[] = []
|
|
145
|
+
): boolean {
|
|
146
|
+
const scripts = unicodeScriptsOf(code)
|
|
147
|
+
|
|
148
|
+
if (!scripts.length) return false
|
|
149
|
+
|
|
150
|
+
const own = scriptClass(scripts)
|
|
151
|
+
const accepted = scriptClass([...alsoAccepted.flatMap(unicodeScriptsOf), "Zyyy", "Zinh"])
|
|
152
|
+
let letters = 0
|
|
153
|
+
|
|
154
|
+
for (const character of text) {
|
|
155
|
+
if (!/\p{L}|\p{M}/u.test(character)) continue
|
|
156
|
+
|
|
157
|
+
if (own.test(character)) {
|
|
158
|
+
letters++
|
|
159
|
+
} else if (!accepted.test(character)) {
|
|
160
|
+
return false
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return letters > 0
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function scriptClass(scripts: readonly string[]): RegExp {
|
|
168
|
+
return new RegExp(`^[${scripts.map((script) => `\\p{Script=${script}}`).join("")}]$`, "u")
|
|
169
|
+
}
|