3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-19 14:45:44 +01:00

Match a reference label to its definition under Unicode case folding

This commit is contained in:
2026-11-15 22:04:28 +01:00
committed by Lillem4n
parent 3b89637f2c
commit fe3bf6e268
13 changed files with 1890 additions and 22 deletions
@@ -0,0 +1,60 @@
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import test from 'node:test'
import { fileURLToPath } from 'node:url'
import { foldCase } from './case-folding.ts'
const source = readFileSync(join(dirname(fileURLToPath(import.meta.url)), '..', '..', '..', 'spec', 'unicode', 'CaseFolding.txt'), 'utf8')
function characters(hexes: string): string {
return hexes
.split(' ')
.map((hex) => String.fromCodePoint(parseInt(hex, 16)))
.join('')
}
// `CaseFolding.txt`'s common and full mappings, as `0041; C; 0061;` and `00DF; F; 0073 0073;` give them.
const mappings = new Map(
source
.split('\n')
.map((line) => /^([0-9A-F]+); [CF]; ([0-9A-F ]+);/.exec(line))
.filter((match) => match !== null)
.map((match): [string, string] => [characters(match[1] ?? ''), characters(match[2] ?? '')]),
)
test('the vendored file gives the mappings the other tests hold the table to', () => {
assert.equal(mappings.size, 1606)
assert.equal(mappings.get('ẞ'), 'ss')
assert.equal(mappings.get('ß'), 'ss')
assert.equal(mappings.get('İ'), 'i̇')
})
test('folds every code point to the mapping the standard gives it', () => {
assert.deepEqual(
[...mappings].filter(([from, to]) => foldCase(from) !== to).map(([from]) => from),
[],
)
})
test('leaves every other code point as it is', () => {
const changed: number[] = []
for (let point = 0; point <= 0x10ffff; point++) {
const character = String.fromCodePoint(point)
if (!mappings.has(character) && foldCase(character) !== character) changed.push(point)
}
assert.deepEqual(changed, [])
})
test('folds a string code point by code point, a surrogate pair as one', () => {
assert.equal(foldCase('Ab\u{10400}Ç ẞ'), 'ab\u{10428}ç ss')
assert.equal(foldCase(''), '')
assert.equal(foldCase('\ud801'), '\ud801')
})
test('keeps the dotless i and the capital I apart, as the standard does', () => {
assert.equal(foldCase('I'), 'i')
assert.equal(foldCase('ı'), 'ı')
assert.notEqual(foldCase('ı'), foldCase('I'))
})