mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
foldPlaceName drops a leading "the", so "The Hague" over upstate New York accepted Hague, NY as an exact nearby match. Split the script-aware fold into foldPlaceText and use it for keyless toponyms; foldPlaceName keeps its behaviour for the packs and street matchers. Also: changelog fragment credits #1585, folding test pins the Latin letters NFD does not decompose (ß, ø, ł), and CURRENT-STATE notes that non-Latin names match only when spelled with their marks.
55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
/** Latin letters that Unicode decomposition does not fold to plain ASCII. */
|
||
const FOLD = {
|
||
ß: 'ss',
|
||
ø: 'o',
|
||
æ: 'ae',
|
||
ł: 'l',
|
||
đ: 'd',
|
||
ı: 'i',
|
||
œ: 'oe',
|
||
þ: 'th',
|
||
};
|
||
|
||
/**
|
||
* Script-aware fold of a place name: case folded, accents removed from Latin
|
||
* letters only, punctuation and symbols collapsed to single spaces. Other
|
||
* scripts keep their marks, so "कुल मार्ग" and "काली मार्ग" stay distinct while
|
||
* "São Paulo", "sao paulo" and "SÃO PAULO!" meet. Every word is kept: "The
|
||
* Hague" is not "Hague".
|
||
*
|
||
* @param {unknown} value
|
||
* @returns {string}
|
||
*/
|
||
export function foldPlaceText(value) {
|
||
return String(value ?? '')
|
||
.toLowerCase()
|
||
.replace(/[ßøæłđıœþ]/g, (c) => FOLD[c])
|
||
.normalize('NFKD')
|
||
.replace(/(\p{Script=Latin})\p{M}+/gu, '$1')
|
||
.normalize('NFKC')
|
||
.replace(/[^\p{L}\p{M}\p{N}]+/gu, ' ')
|
||
.trim();
|
||
}
|
||
|
||
/**
|
||
* Comparable form of a place name, shared by the bundled packs, the
|
||
* OpenFreeMap street and grounds matchers and the Nominatim server:
|
||
* `foldPlaceText` with a leading "the" dropped.
|
||
*
|
||
* @param {unknown} value
|
||
* @returns {string}
|
||
*/
|
||
export function foldPlaceName(value) {
|
||
return foldPlaceText(value).replace(/^the /, '');
|
||
}
|
||
|
||
/**
|
||
* Fold a name for matching (see `foldPlaceName`), with "saint" and "sainte"
|
||
* shortened the way US place names abbreviate them.
|
||
*/
|
||
export function normalizePlaceName(value) {
|
||
return foldPlaceName(value)
|
||
.replace(/\bsaint\b/g, 'st')
|
||
.replace(/\bsainte\b/g, 'ste');
|
||
}
|