Files
outline/app/editor/extensions/deburrWithMap.ts
T
dependabot[bot]andTom Moor d3aeabef51 chore(deps): bump es-toolkit from 1.47.1 to 1.53.0 (#14472)
* chore(deps): bump es-toolkit from 1.47.1 to 1.53.0

Bumps [es-toolkit](https://github.com/toss/es-toolkit) from 1.47.1 to 1.53.0.
- [Release notes](https://github.com/toss/es-toolkit/releases)
- [Changelog](https://github.com/toss/es-toolkit/blob/main/CHANGELOG.md)
- [Commits](https://github.com/toss/es-toolkit/compare/v1.47.1...v1.53.0)

---
updated-dependencies:
- dependency-name: es-toolkit
  dependency-version: 1.53.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* test: Update deburrWithMap tests for es-toolkit 1.53 deburr behavior

deburr no longer decomposes Hangul, so cover the slow path with letters that still expand.

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Tom Moor <tom@getoutline.com>
2026-11-06 01:39:02 +00:00

63 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { deburr } from "es-toolkit/compat";
/**
* Build a diacritics-stripped version of a string together with a way to map
* any index in the stripped string back to the corresponding index in the
* original string.
*
* `deburr` can change the length of the string – it expands some letters (e.g.
* "ß" is a single UTF-16 code unit but becomes "ss") and removes combining
* marks. When that happens a naive offset no longer lines up with the original,
* so the source index of every emitted code unit is recorded instead.
*
* When deburring does not change the length – the common case for ASCII and
* ordinary accented Latin text – indices line up one-to-one and the cheap
* identity mapping is returned without building any per-character table.
*
* @param text The original text to deburr.
* @returns the deburred text and a `toOriginalIndex` function translating a
* deburred index (0…deburred.length) into the corresponding original index.
*/
export function deburrWithMap(text: string): {
deburred: string;
toOriginalIndex: (index: number) => number;
} {
const deburred = deburr(text);
// Fast path: deburring preserved the length, so indices line up one-to-one
// with the original string and no per-character table is needed.
if (deburred.length === text.length) {
return { deburred, toOriginalIndex: (index) => index };
}
// Slow path: deburring changed the length (e.g. it expanded a letter or
// removed a combining mark). Rebuild the deburred string per code point so it
// stays consistent with the index map, recording the original index of every
// emitted code unit plus a sentinel for the end position.
const parts: string[] = [];
const map: number[] = [];
let originalIndex = 0;
// Iterate by code point so surrogate pairs are deburred as a unit.
for (const char of text) {
// ASCII characters are never altered by deburr, so emit them
// directly and skip the comparatively expensive normalization.
if (char.charCodeAt(0) < 0x80) {
map.push(originalIndex);
parts.push(char);
originalIndex += 1;
continue;
}
const stripped = deburr(char);
for (let i = 0; i < stripped.length; i++) {
map.push(originalIndex);
}
parts.push(stripped);
originalIndex += char.length;
}
map.push(text.length);
return { deburred: parts.join(""), toOriginalIndex: (index) => map[index] };
}