|
| 1 | +import { func_remember } from "./func.ts"; |
| 2 | + |
1 | 3 | /** |
2 | 4 | * 反转字符串 |
3 | 5 | */ |
4 | 6 | export const str_reverse = (str: string): string => { |
5 | | - let restr = ""; |
6 | | - /// 不能用 i++ < length 迭代,会有Unicode字符问题 |
7 | | - for (const char of str) { |
8 | | - restr = char + restr; |
9 | | - } |
10 | | - return restr; |
| 7 | + let restr = ""; |
| 8 | + /// 不能用 i++ < length 迭代,会有Unicode字符问题 |
| 9 | + for (const char of str) { |
| 10 | + restr = char + restr; |
| 11 | + } |
| 12 | + return restr; |
11 | 13 | }; |
12 | 14 |
|
13 | 15 | /** |
14 | 16 | * 寻找字符串开头是否指定字符串,是的话,进行替换 |
15 | 17 | */ |
16 | 18 | export const str_replace_start = ( |
17 | | - str: string, |
18 | | - searchValue: string, |
19 | | - replaceValue: string |
| 19 | + str: string, |
| 20 | + searchValue: string, |
| 21 | + replaceValue: string, |
20 | 22 | ): string => { |
21 | | - if (str.startsWith(searchValue)) { |
22 | | - return replaceValue + str.slice(searchValue.length); |
23 | | - } |
24 | | - return str; |
| 23 | + if (str.startsWith(searchValue)) { |
| 24 | + return replaceValue + str.slice(searchValue.length); |
| 25 | + } |
| 26 | + return str; |
25 | 27 | }; |
26 | 28 |
|
27 | 29 | /** |
28 | 30 | * 寻找字符串结尾是否指定字符串,是的话,进行替换 |
29 | 31 | */ |
30 | 32 | export const str_replace_end = ( |
31 | | - str: string, |
32 | | - searchValue: string, |
33 | | - replaceValue: string |
| 33 | + str: string, |
| 34 | + searchValue: string, |
| 35 | + replaceValue: string, |
34 | 36 | ): string => { |
35 | | - if (str.endsWith(searchValue)) { |
36 | | - return str.slice(0, -searchValue.length) + replaceValue; |
37 | | - } |
38 | | - return str; |
| 37 | + if (str.endsWith(searchValue)) { |
| 38 | + return str.slice(0, -searchValue.length) + replaceValue; |
| 39 | + } |
| 40 | + return str; |
39 | 41 | }; |
40 | 42 |
|
41 | 43 | export const str_trim_indent: (str: string) => string = (str: string) => { |
42 | | - const lines = str.split("\n"); |
43 | | - let min = -1; |
44 | | - const lines2 = lines.map((line) => { |
45 | | - const line2 = line.trimStart(); |
46 | | - /// 寻找非空的 |
47 | | - if (line2.length !== 0) { |
48 | | - const start_len = line.length - line2.length; |
49 | | - if (min === -1) { |
50 | | - min = start_len; |
51 | | - } else { |
52 | | - min = Math.min(min, start_len); |
53 | | - } |
54 | | - return line; |
| 44 | + const lines = str.split("\n"); |
| 45 | + let min = -1; |
| 46 | + const lines2 = lines.map((line) => { |
| 47 | + const line2 = line.trimStart(); |
| 48 | + /// 寻找非空的 |
| 49 | + if (line2.length !== 0) { |
| 50 | + const start_len = line.length - line2.length; |
| 51 | + if (min === -1) { |
| 52 | + min = start_len; |
| 53 | + } else { |
| 54 | + min = Math.min(min, start_len); |
| 55 | + } |
| 56 | + return line; |
| 57 | + } |
| 58 | + return line2; |
| 59 | + }); |
| 60 | + if (min === -1) { |
| 61 | + return ""; |
55 | 62 | } |
56 | | - return line2; |
57 | | - }); |
58 | | - if (min === -1) { |
59 | | - return ""; |
60 | | - } |
61 | | - if (min === 0) { |
62 | | - return lines2.join("\n"); |
63 | | - } |
64 | | - let result = ""; |
65 | | - for (const line of lines2) { |
66 | | - // 如果已经有内容,那么要新增一行就要先加一个换行符 |
67 | | - if (result.length > 0) { |
68 | | - result += "\n"; |
| 63 | + if (min === 0) { |
| 64 | + return lines2.join("\n"); |
69 | 65 | } |
70 | | - if (line.length > 0) { |
71 | | - result += line.slice(min); |
| 66 | + let result = ""; |
| 67 | + for (const line of lines2) { |
| 68 | + // 如果已经有内容,那么要新增一行就要先加一个换行符 |
| 69 | + if (result.length > 0) { |
| 70 | + result += "\n"; |
| 71 | + } |
| 72 | + if (line.length > 0) { |
| 73 | + result += line.slice(min); |
| 74 | + } |
72 | 75 | } |
73 | | - } |
74 | | - return result.trimEnd(); |
| 76 | + return result.trimEnd(); |
| 77 | +}; |
| 78 | + |
| 79 | +/** 对于人类来说是空格的字符 */ |
| 80 | +export const str_human_space = new Set([ |
| 81 | + "\u0020", |
| 82 | + "\u00A0", |
| 83 | + "\u1680", |
| 84 | + "\u2000", |
| 85 | + "\u200B", |
| 86 | + "\u200C", |
| 87 | + "\u200D", |
| 88 | + "\u200E", |
| 89 | + "\u200F", |
| 90 | + "\u202A", |
| 91 | + "\u000A", |
| 92 | + "\u000D", |
| 93 | + "\u2028", |
| 94 | + "\u2029", |
| 95 | +]); |
| 96 | + |
| 97 | +// 创建一个正则表达式字符串,使用 Set 中的字符 |
| 98 | +export const human_space_regex = func_remember(() => { |
| 99 | + const pattern = [...str_human_space].map((ch) => ch.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"); |
| 100 | + return { |
| 101 | + start: new RegExp(`^(?:${pattern})+`), |
| 102 | + end: new RegExp(`(?:${pattern})+$`), |
| 103 | + all: new RegExp(`(?:${pattern})+`, "g"), |
| 104 | + }; |
| 105 | +}); |
| 106 | + |
| 107 | +export const str_human_trim = (str: string): string => { |
| 108 | + const regex = human_space_regex(); |
| 109 | + return str.replace(regex.start, "").replace(regex.end, ""); |
75 | 110 | }; |
0 commit comments