blob: e4d8ea582e4bdc7ab3cc6d5534844b36f682ceb1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Calculates the maximum width of a multi-line ASCII art string.
* @param asciiArt The ASCII art string.
* @returns The length of the longest line in the ASCII art.
*/
export const getAsciiArtWidth = (asciiArt: string): number => {
if (!asciiArt) {
return 0;
}
const lines = asciiArt.split('\n');
return Math.max(...lines.map((line) => line.length));
};
/*
* -------------------------------------------------------------------------
* Unicode‑aware helpers (work at the code‑point level rather than UTF‑16
* code units so that surrogate‑pair emoji count as one "column".)
* ---------------------------------------------------------------------- */
export function toCodePoints(str: string): string[] {
// [...str] or Array.from both iterate by UTF‑32 code point, handling
// surrogate pairs correctly.
return Array.from(str);
}
export function cpLen(str: string): number {
return toCodePoints(str).length;
}
export function cpSlice(str: string, start: number, end?: number): string {
// Slice by code‑point indices and re‑join.
const arr = toCodePoints(str).slice(start, end);
return arr.join('');
}
|