src/vs/base/common/normalization.ts

63 LOC · 57 covered · 6 uncovered · 14 ranges · 4768 concepts · 4 introducers · 2418 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- normalization.ts ×5
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { LRUCache } from './map.js';
7 >
8 > const nfcCache = new LRUCache<string, string>(10000); // bounded to 10000 elements
9 > export function normalizeNFC(str: string): string {
10 return normalize(str, 'NFC', nfcCache);
11 }
13 > const nfdCache = new LRUCache<string, string>(10000); // bounded to 10000 elements
14 > export function normalizeNFD(str: string): string {
15 > return normalize(str, 'NFD', nfdCache); normalization.ts ×7
16 > }
18 > const nonAsciiCharactersPattern = /[^\u0000-\u0080]/;
19 > function normalize(str: string, form: string, normalizedCache: LRUCache<string, string>): string { normalization.ts ×7
20 > if (!str) {
21 return str;
22 }
24 > const cached = normalizedCache.get(str);
25 > if (cached) {
26 return cached;
27 }
29 > let res: string;
30 > if (nonAsciiCharactersPattern.test(str)) {
31 > res = str.normalize(form); normalization.ts ×1
32 > } else { normalization.ts ×7
33 > res = str; normalization.ts ×1
34 > }
36 > // Use the cache for fast lookup
37 > normalizedCache.set(str, res);
38 >
39 > return res;
40 > }
42 > /**
43 > * Attempts to normalize the string to Unicode base format (NFD -> remove accents -> lower case).
44 > * When original string contains accent characters directly, only lower casing will be performed.
45 > * This is done so as to keep the string length the same and not affect indices.
46 > *
47 > * @see https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript/37511463#37511463
48 > */
49 > export const tryNormalizeToBase: (str: string) => string = function () {
50 > const cache = new LRUCache<string, string>(10000); // bounded to 10000 elements
51 > const accentsRegex = /[\u0300-\u036f]/g;
52 > return function (str: string): string {
53 > const cached = cache.get(str); normalization.ts ×7
54 > if (cached) {
55 > return cached;
56 > }
57 >
58 > const noAccents = normalizeNFD(str).replace(accentsRegex, '');
59 > const result = (noAccents.length === str.length ? noAccents : str).toLowerCase();
60 > cache.set(str, result);
61 > return result;
62 > };