@mark1russell7/text 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -3
- package/build/index.d.ts +60 -2
- package/build/index.d.ts.map +1 -1
- package/build/index.js +80 -3
- package/build/index.js.map +1 -1
- package/package.json +5 -2
- package/src/english.ts +17 -0
- package/src/escape.ts +24 -2
- package/src/index.ts +5 -3
- package/src/search.ts +44 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @mark1russell7/text
|
|
2
2
|
|
|
3
|
-
Plain text helpers
|
|
3
|
+
Plain text helpers in three groups: the escapes of a text for five targets, the English word forms and a word search.
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
6
|
pnpm add @mark1russell7/text
|
|
@@ -12,22 +12,55 @@ pnpm add @mark1russell7/text
|
|
|
12
12
|
| `escapeAttribute` | An HTML attribute value in double or single quotes | `&`, `<`, `>`, `"` and `'`, to `&`, `<`, `>`, `"` and `'` |
|
|
13
13
|
| `escapeRegExp` | A pattern of `new RegExp`, outside a character class | A backslash before each syntax character: `^ $ \ . * + ? ( ) [ ] { } \|` |
|
|
14
14
|
| `escapeMarkdown` | One line of CommonMark or GitHub Markdown | A backslash before each ASCII punctuation character, and a space for each line end |
|
|
15
|
+
| `escapeWorkflowData` | The data of a GitHub Actions workflow command | `%`, CR and LF, to `%25`, `%0D` and `%0A` |
|
|
16
|
+
| `escapeWorkflowProperty` | A property value of a workflow command | The characters of `escapeWorkflowData`, and `:` and `,` to `%3A` and `%2C` |
|
|
15
17
|
|
|
16
18
|
```ts
|
|
17
|
-
import { escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp } from "@mark1russell7/text";
|
|
19
|
+
import { escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp, escapeWorkflowData, escapeWorkflowProperty } from "@mark1russell7/text";
|
|
18
20
|
|
|
19
21
|
escapeHtml("a < b & c"); // "a < b & c"
|
|
20
22
|
escapeAttribute(`say "hi"`); // "say "hi""
|
|
21
23
|
new RegExp(`^${escapeRegExp("1+1=2?")}$`, "u").test("1+1=2?"); // true
|
|
24
|
+
console.log(`::warning title=${escapeWorkflowProperty("a: b")}::${escapeWorkflowData("50%
|
|
25
|
+
next")}`); // "::warning title=a%3A b::50%25%0Anext"
|
|
22
26
|
escapeMarkdown("a | b *c*"); // "a \\| b \\*c\\*"
|
|
23
27
|
```
|
|
24
28
|
|
|
29
|
+
## English word forms
|
|
30
|
+
|
|
31
|
+
`countOf(count, noun, plural?)` gives a count with a noun. Only the count 1 takes the singular noun. The default plural noun is the singular noun with "s" at the end.
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { countOf } from "@mark1russell7/text";
|
|
35
|
+
|
|
36
|
+
countOf(1, "site"); // "1 site"
|
|
37
|
+
countOf(1024, "site"); // "1,024 sites"
|
|
38
|
+
countOf(3, "match", "matches"); // "3 matches"
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## A word search
|
|
42
|
+
|
|
43
|
+
`wordSearch(query)` gives a test of a text. A text agrees when it has each word of the search, in any order and in any case. A word can be a part of a longer word.
|
|
44
|
+
|
|
45
|
+
`searchKey(text)` gives the form that the test compares. It applies NFKC, then the lower case, the upper case and the lower case again. Then it changes the final sigma "ς" to "σ". It uses no rules of a locale.
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { searchKey, wordSearch } from "@mark1russell7/text";
|
|
49
|
+
|
|
50
|
+
wordSearch("news WORLD")("World News Today"); // true
|
|
51
|
+
wordSearch("strasse")("Hauptstraße 1"); // true
|
|
52
|
+
wordSearch("istanbul")("İSTANBUL"); // false: "İ" keeps its dot as U+0307
|
|
53
|
+
searchKey("file"); // "file"
|
|
54
|
+
```
|
|
55
|
+
|
|
25
56
|
## The copies that this package replaces
|
|
26
57
|
|
|
27
|
-
The tests run
|
|
58
|
+
The tests run 58 copies of these functions from other repositories of the owner. Each copy comes from a list of the duplication atlas, at a pinned commit. The file `test/copies.json` has each copy, and `test/differences.ts` has each difference from the function of this package, with its reason. A copy that is not in the list gives the same output on each of the 1000 test strings.
|
|
28
59
|
|
|
29
60
|
## Limits
|
|
30
61
|
|
|
31
62
|
- `escapeHtml` does not escape quotes. For an attribute value, use `escapeAttribute`.
|
|
32
63
|
- `escapeRegExp` gives a pattern for use outside a character class. In a class, `-` also has a meaning.
|
|
33
64
|
- `escapeMarkdown` does not change spaces. Four spaces at the start of a line make a code block.
|
|
65
|
+
- `wordSearch` keeps accents, so "cafe" does not find "café".
|
|
66
|
+
- `countOf` knows no irregular plural. Give the plural noun for each noun that does not add "s".
|
package/build/index.d.ts
CHANGED
|
@@ -1,7 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The entry of `@mark1russell7/text`: plain text helpers. The
|
|
3
|
-
*
|
|
2
|
+
* The entry of `@mark1russell7/text`: plain text helpers. The groups are the escapes for HTML, attributes, regular
|
|
3
|
+
* expressions, Markdown and GitHub Actions workflow commands, the English word forms and a word search.
|
|
4
4
|
*/
|
|
5
|
+
/**
|
|
6
|
+
* This module has the English word forms.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* This function gives a count with a noun, for example "1 site" or "1,024 sites". Only the count 1 takes the singular
|
|
10
|
+
* noun. Each other count takes the plural noun, also 0 and -1. The count has the digit groups of English and at most
|
|
11
|
+
* three decimal places.
|
|
12
|
+
*
|
|
13
|
+
* @param count - The number of things.
|
|
14
|
+
* @param noun - The singular noun.
|
|
15
|
+
* @param plural - The plural noun. The default is the singular noun with the letter "s" at the end.
|
|
16
|
+
* @returns The count, a space and the noun.
|
|
17
|
+
*/
|
|
18
|
+
export declare function countOf(count: number, noun: string, plural?: string): string;
|
|
5
19
|
/**
|
|
6
20
|
* This function escapes a text for the content of an HTML element. It changes `&`, `<` and `>` to named character
|
|
7
21
|
* references. The quotes stay, because they have no meaning in element content.
|
|
@@ -39,4 +53,48 @@ export declare function escapeRegExp(text: string): string;
|
|
|
39
53
|
* @returns The text for one line of Markdown.
|
|
40
54
|
*/
|
|
41
55
|
export declare function escapeMarkdown(text: string): string;
|
|
56
|
+
/**
|
|
57
|
+
* This function escapes a text for the data of a GitHub Actions workflow command, the part after `::`. It changes `%`,
|
|
58
|
+
* the carriage return and the line feed to `%25`, `%0D` and `%0A`, so the command stays on one line.
|
|
59
|
+
*
|
|
60
|
+
* @param text - The plain text.
|
|
61
|
+
* @returns The data of the command. The runner decodes it to the text.
|
|
62
|
+
*/
|
|
63
|
+
export declare function escapeWorkflowData(text: string): string;
|
|
64
|
+
/**
|
|
65
|
+
* This function escapes a text for a property value of a GitHub Actions workflow command, for example `title=`. It
|
|
66
|
+
* changes the characters of `escapeWorkflowData`, and also `:` and `,` to `%3A` and `%2C`.
|
|
67
|
+
*
|
|
68
|
+
* @param text - The plain text.
|
|
69
|
+
* @returns The property value. The runner decodes it to the text.
|
|
70
|
+
*/
|
|
71
|
+
export declare function escapeWorkflowProperty(text: string): string;
|
|
72
|
+
/**
|
|
73
|
+
* This function gives the search key of a text. `wordSearch` compares the keys of texts. The function does these steps:
|
|
74
|
+
*
|
|
75
|
+
* 1. It applies the Unicode normalization NFKC. Thus the ligature "fi" agrees with "fi", and full-width letters agree
|
|
76
|
+
* with ASCII letters.
|
|
77
|
+
* 2. It applies the full lower case, then the full upper case, then the full lower case. Thus "ß", "ẞ" and "SS" give
|
|
78
|
+
* "ss".
|
|
79
|
+
* 3. It changes the small final sigma "ς" to "σ", as Unicode case folding does.
|
|
80
|
+
* 4. It applies NFKC again.
|
|
81
|
+
*
|
|
82
|
+
* The steps use no rules of a locale. Thus the Turkish "İ" gives "i" with a combining dot above (U+0307), and the
|
|
83
|
+
* Turkish "ı" gives "i". The key of a key is the same key. Accents stay, so "é" does not agree with "e".
|
|
84
|
+
*
|
|
85
|
+
* @param text - The plain text.
|
|
86
|
+
* @returns The search key of the text.
|
|
87
|
+
*/
|
|
88
|
+
export declare function searchKey(text: string): string;
|
|
89
|
+
/**
|
|
90
|
+
* This function gives the test of a search. The function makes the key of the search with `searchKey`. Then it splits
|
|
91
|
+
* the key into words at each run of white space, the class `\s` of a regular expression.
|
|
92
|
+
*
|
|
93
|
+
* The test tells if the key of a text contains each word, in any order. A word can be a part of a longer word, so the
|
|
94
|
+
* search "art" finds "Party". A search with no words agrees with each text.
|
|
95
|
+
*
|
|
96
|
+
* @param query - The search, as a person types it.
|
|
97
|
+
* @returns The test of a text.
|
|
98
|
+
*/
|
|
99
|
+
export declare function wordSearch(query: string): (text: string) => boolean;
|
|
42
100
|
//# sourceMappingURL=index.d.ts.map
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/escape.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/english.ts","../src/escape.ts","../src/search.ts"],"mappings":";;;;;;;;;;;;;;;;;wBAcgB,QAAQ,eAAe,cAAc;;;;;;;;wBCCrC,WAAW;;;;;;;;wBAWX,gBAAgB;;;;;;;;;wBAYhB,aAAa;;;;;;;;;;;;wBAeb,eAAe;;;;;;;;wBAWf,mBAAmB;;;;;;;;wBAWnB,uBAAuB;;;;;;;;;;;;;;;;;wBCnDvB,UAAU;;;;;;;;;;;wBAcV,WAAW,iBAAiB"}
|
package/build/index.js
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
|
+
// src/english.ts
|
|
2
|
+
/**
|
|
3
|
+
* This module has the English word forms.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* This function gives a count with a noun, for example "1 site" or "1,024 sites". Only the count 1 takes the singular
|
|
7
|
+
* noun. Each other count takes the plural noun, also 0 and -1. The count has the digit groups of English and at most
|
|
8
|
+
* three decimal places.
|
|
9
|
+
*
|
|
10
|
+
* @param count - The number of things.
|
|
11
|
+
* @param noun - The singular noun.
|
|
12
|
+
* @param plural - The plural noun. The default is the singular noun with the letter "s" at the end.
|
|
13
|
+
* @returns The count, a space and the noun.
|
|
14
|
+
*/
|
|
15
|
+
function countOf(count, noun, plural = `${noun}s`) {
|
|
16
|
+
return `${count.toLocaleString("en")} ${count === 1 ? noun : plural}`;
|
|
17
|
+
}
|
|
1
18
|
// src/escape.ts
|
|
2
19
|
/**
|
|
3
|
-
* This module has the escapes of plain text for
|
|
4
|
-
* regular expression
|
|
20
|
+
* This module has the escapes of plain text for five targets. The targets are HTML content, an HTML attribute, a
|
|
21
|
+
* regular expression, one line of Markdown and a GitHub Actions workflow command. Each function changes only the characters that have a meaning in its target.
|
|
5
22
|
*/
|
|
6
23
|
const HTML = {
|
|
7
24
|
"&": "&",
|
|
@@ -58,6 +75,66 @@ function escapeRegExp(text) {
|
|
|
58
75
|
function escapeMarkdown(text) {
|
|
59
76
|
return text.replace(/\r\n?|\n/g, " ").replace(/[!-/:-@[-`{-~]/g, "\\$&");
|
|
60
77
|
}
|
|
61
|
-
|
|
78
|
+
/**
|
|
79
|
+
* This function escapes a text for the data of a GitHub Actions workflow command, the part after `::`. It changes `%`,
|
|
80
|
+
* the carriage return and the line feed to `%25`, `%0D` and `%0A`, so the command stays on one line.
|
|
81
|
+
*
|
|
82
|
+
* @param text - The plain text.
|
|
83
|
+
* @returns The data of the command. The runner decodes it to the text.
|
|
84
|
+
*/
|
|
85
|
+
function escapeWorkflowData(text) {
|
|
86
|
+
return text.replaceAll("%", "%25").replaceAll("\r", "%0D").replaceAll("\n", "%0A");
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* This function escapes a text for a property value of a GitHub Actions workflow command, for example `title=`. It
|
|
90
|
+
* changes the characters of `escapeWorkflowData`, and also `:` and `,` to `%3A` and `%2C`.
|
|
91
|
+
*
|
|
92
|
+
* @param text - The plain text.
|
|
93
|
+
* @returns The property value. The runner decodes it to the text.
|
|
94
|
+
*/
|
|
95
|
+
function escapeWorkflowProperty(text) {
|
|
96
|
+
return escapeWorkflowData(text).replaceAll(":", "%3A").replaceAll(",", "%2C");
|
|
97
|
+
}
|
|
98
|
+
// src/search.ts
|
|
99
|
+
/**
|
|
100
|
+
* This module has a word search of plain text. A text agrees with a search when the text has each word of the search,
|
|
101
|
+
* in any order. The case of the letters is not significant.
|
|
102
|
+
*/
|
|
103
|
+
/** The small final sigma. Unicode case folding changes it to the small sigma, so a word agrees in each position. */
|
|
104
|
+
const FINAL_SIGMA = /ς/gu;
|
|
105
|
+
/**
|
|
106
|
+
* This function gives the search key of a text. `wordSearch` compares the keys of texts. The function does these steps:
|
|
107
|
+
*
|
|
108
|
+
* 1. It applies the Unicode normalization NFKC. Thus the ligature "fi" agrees with "fi", and full-width letters agree
|
|
109
|
+
* with ASCII letters.
|
|
110
|
+
* 2. It applies the full lower case, then the full upper case, then the full lower case. Thus "ß", "ẞ" and "SS" give
|
|
111
|
+
* "ss".
|
|
112
|
+
* 3. It changes the small final sigma "ς" to "σ", as Unicode case folding does.
|
|
113
|
+
* 4. It applies NFKC again.
|
|
114
|
+
*
|
|
115
|
+
* The steps use no rules of a locale. Thus the Turkish "İ" gives "i" with a combining dot above (U+0307), and the
|
|
116
|
+
* Turkish "ı" gives "i". The key of a key is the same key. Accents stay, so "é" does not agree with "e".
|
|
117
|
+
*
|
|
118
|
+
* @param text - The plain text.
|
|
119
|
+
* @returns The search key of the text.
|
|
120
|
+
*/
|
|
121
|
+
function searchKey(text) {
|
|
122
|
+
return text.normalize("NFKC").toLowerCase().toUpperCase().toLowerCase().replace(FINAL_SIGMA, "σ").normalize("NFKC");
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* This function gives the test of a search. The function makes the key of the search with `searchKey`. Then it splits
|
|
126
|
+
* the key into words at each run of white space, the class `\s` of a regular expression.
|
|
127
|
+
*
|
|
128
|
+
* The test tells if the key of a text contains each word, in any order. A word can be a part of a longer word, so the
|
|
129
|
+
* search "art" finds "Party". A search with no words agrees with each text.
|
|
130
|
+
*
|
|
131
|
+
* @param query - The search, as a person types it.
|
|
132
|
+
* @returns The test of a text.
|
|
133
|
+
*/
|
|
134
|
+
function wordSearch(query) {
|
|
135
|
+
const words = searchKey(query).split(/\s+/u).filter((word) => word !== "");
|
|
136
|
+
return (text) => words.length === 0 || words.every((word) => searchKey(text).includes(word));
|
|
137
|
+
}
|
|
138
|
+
export { countOf, escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp, escapeWorkflowData, escapeWorkflowProperty, searchKey, wordSearch };
|
|
62
139
|
|
|
63
140
|
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/escape.ts"],"sourcesContent":["/**\n * This module has the escapes of plain text for
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/english.ts","../src/escape.ts","../src/search.ts"],"sourcesContent":["/**\n * This module has the English word forms.\n */\n\n/**\n * This function gives a count with a noun, for example \"1 site\" or \"1,024 sites\". Only the count 1 takes the singular\n * noun. Each other count takes the plural noun, also 0 and -1. The count has the digit groups of English and at most\n * three decimal places.\n *\n * @param count - The number of things.\n * @param noun - The singular noun.\n * @param plural - The plural noun. The default is the singular noun with the letter \"s\" at the end.\n * @returns The count, a space and the noun.\n */\nexport function countOf(count: number, noun: string, plural = `${noun}s`): string {\n return `${count.toLocaleString(\"en\")} ${count === 1 ? noun : plural}`;\n}\n","/**\n * This module has the escapes of plain text for five targets. The targets are HTML content, an HTML attribute, a\n * regular expression, one line of Markdown and a GitHub Actions workflow command. Each function changes only the characters that have a meaning in its target.\n */\n\nconst HTML: Readonly<Record<string, string>> = { \"&\": \"&\", \"<\": \"<\", \">\": \">\" };\nconst ATTRIBUTE: Readonly<Record<string, string>> = { ...HTML, '\"': \""\", \"'\": \"'\" };\n\n/**\n * This function escapes a text for the content of an HTML element. It changes `&`, `<` and `>` to named character\n * references. The quotes stay, because they have no meaning in element content.\n *\n * @param text - The plain text.\n * @returns The text with `&`, `<` and `>` in place of the three characters.\n */\nexport function escapeHtml(text: string): string {\n return text.replace(/[&<>]/g, (char) => HTML[char] ?? char);\n}\n\n/**\n * This function escapes a text for an HTML attribute value in double quotes or in single quotes. It changes `&`, `<`,\n * `>`, `\"` and `'`. The result is also correct in element content.\n *\n * @param text - The plain text.\n * @returns The text with `&`, `<`, `>`, `"` and `'` in place of the five characters.\n */\nexport function escapeAttribute(text: string): string {\n return text.replace(/[&<>\"']/g, (char) => ATTRIBUTE[char] ?? char);\n}\n\n/**\n * This function escapes a text for the pattern of a regular expression, outside a character class. It puts a backslash\n * before each syntax character of ECMAScript: `^ $ \\ . * + ? ( ) [ ] { } |`. The pattern matches only the text, with\n * the flag `u`, with the flag `v` and with no flag.\n *\n * @param text - The plain text.\n * @returns A pattern for `new RegExp` that matches the text.\n */\nexport function escapeRegExp(text: string): string {\n return text.replace(/[\\\\^$.*+?()[\\]{}|]/g, \"\\\\$&\");\n}\n\n/**\n * This function escapes a text for one line of CommonMark or GitHub Markdown: a paragraph, a heading or a table cell.\n * It puts a backslash before each ASCII punctuation character, and changes each line end to a space.\n *\n * A backslash before an ASCII punctuation character always gives the character itself. Thus no character of the result\n * starts emphasis, code, a link, HTML, an entity, a list or a table column. Four spaces at the start of a line still\n * make a code block.\n *\n * @param text - The plain text.\n * @returns The text for one line of Markdown.\n */\nexport function escapeMarkdown(text: string): string {\n return text.replace(/\\r\\n?|\\n/g, \" \").replace(/[!-/:-@[-`{-~]/g, \"\\\\$&\");\n}\n\n/**\n * This function escapes a text for the data of a GitHub Actions workflow command, the part after `::`. It changes `%`,\n * the carriage return and the line feed to `%25`, `%0D` and `%0A`, so the command stays on one line.\n *\n * @param text - The plain text.\n * @returns The data of the command. The runner decodes it to the text.\n */\nexport function escapeWorkflowData(text: string): string {\n return text.replaceAll(\"%\", \"%25\").replaceAll(\"\\r\", \"%0D\").replaceAll(\"\\n\", \"%0A\");\n}\n\n/**\n * This function escapes a text for a property value of a GitHub Actions workflow command, for example `title=`. It\n * changes the characters of `escapeWorkflowData`, and also `:` and `,` to `%3A` and `%2C`.\n *\n * @param text - The plain text.\n * @returns The property value. The runner decodes it to the text.\n */\nexport function escapeWorkflowProperty(text: string): string {\n return escapeWorkflowData(text).replaceAll(\":\", \"%3A\").replaceAll(\",\", \"%2C\");\n}\n","/**\n * This module has a word search of plain text. A text agrees with a search when the text has each word of the search,\n * in any order. The case of the letters is not significant.\n */\n\n/** The small final sigma. Unicode case folding changes it to the small sigma, so a word agrees in each position. */\nconst FINAL_SIGMA = /ς/gu;\n\n/**\n * This function gives the search key of a text. `wordSearch` compares the keys of texts. The function does these steps:\n *\n * 1. It applies the Unicode normalization NFKC. Thus the ligature \"fi\" agrees with \"fi\", and full-width letters agree\n * with ASCII letters.\n * 2. It applies the full lower case, then the full upper case, then the full lower case. Thus \"ß\", \"ẞ\" and \"SS\" give\n * \"ss\".\n * 3. It changes the small final sigma \"ς\" to \"σ\", as Unicode case folding does.\n * 4. It applies NFKC again.\n *\n * The steps use no rules of a locale. Thus the Turkish \"İ\" gives \"i\" with a combining dot above (U+0307), and the\n * Turkish \"ı\" gives \"i\". The key of a key is the same key. Accents stay, so \"é\" does not agree with \"e\".\n *\n * @param text - The plain text.\n * @returns The search key of the text.\n */\nexport function searchKey(text: string): string {\n return text.normalize(\"NFKC\").toLowerCase().toUpperCase().toLowerCase().replace(FINAL_SIGMA, \"σ\").normalize(\"NFKC\");\n}\n\n/**\n * This function gives the test of a search. The function makes the key of the search with `searchKey`. Then it splits\n * the key into words at each run of white space, the class `\\s` of a regular expression.\n *\n * The test tells if the key of a text contains each word, in any order. A word can be a part of a longer word, so the\n * search \"art\" finds \"Party\". A search with no words agrees with each text.\n *\n * @param query - The search, as a person types it.\n * @returns The test of a text.\n */\nexport function wordSearch(query: string): (text: string) => boolean {\n const words = searchKey(query)\n .split(/\\s+/u)\n .filter((word) => word !== \"\");\n return (text) => words.length === 0 || words.every((word) => searchKey(text).includes(word));\n}\n"],"mappings":";;;;;;;;;;;;;;AAcA,SAAgB,QAAQ,OAAe,MAAc,SAAS,GAAG,KAAK,IAAY;CAChF,OAAO,GAAG,MAAM,eAAe,IAAI,EAAE,GAAG,UAAU,IAAI,OAAO;AAC/D;;;;;;ACXA,MAAM,OAAyC;CAAE,KAAK;CAAS,KAAK;CAAQ,KAAK;AAAO;AACxF,MAAM,YAA8C;CAAE,GAAG;CAAM,MAAK;CAAU,KAAK;AAAQ;;;;;;;;AAS3F,SAAgB,WAAW,MAAsB;CAC/C,OAAO,KAAK,QAAQ,WAAW,SAAS,KAAK,SAAS,IAAI;AAC5D;;;;;;;;AASA,SAAgB,gBAAgB,MAAsB;CACpD,OAAO,KAAK,QAAQ,aAAa,SAAS,UAAU,SAAS,IAAI;AACnE;;;;;;;;;AAUA,SAAgB,aAAa,MAAsB;CACjD,OAAO,KAAK,QAAQ,uBAAuB,MAAM;AACnD;;;;;;;;;;;;AAaA,SAAgB,eAAe,MAAsB;CACnD,OAAO,KAAK,QAAQ,aAAa,GAAG,CAAC,CAAC,QAAQ,mBAAmB,MAAM;AACzE;;;;;;;;AASA,SAAgB,mBAAmB,MAAsB;CACvD,OAAO,KAAK,WAAW,KAAK,KAAK,CAAC,CAAC,WAAW,MAAM,KAAK,CAAC,CAAC,WAAW,MAAM,KAAK;AACnF;;;;;;;;AASA,SAAgB,uBAAuB,MAAsB;CAC3D,OAAO,mBAAmB,IAAI,CAAC,CAAC,WAAW,KAAK,KAAK,CAAC,CAAC,WAAW,KAAK,KAAK;AAC9E;;;;;;;ACvEA,MAAM,cAAc;;;;;;;;;;;;;;;;;AAkBpB,SAAgB,UAAU,MAAsB;CAC9C,OAAO,KAAK,UAAU,MAAM,CAAC,CAAC,YAAY,CAAC,CAAC,YAAY,CAAC,CAAC,YAAY,CAAC,CAAC,QAAQ,aAAa,GAAG,CAAC,CAAC,UAAU,MAAM;AACpH;;;;;;;;;;;AAYA,SAAgB,WAAW,OAA0C;CACnE,MAAM,QAAQ,UAAU,KAAK,CAAC,CAC3B,MAAM,MAAM,CAAC,CACb,QAAQ,SAAS,SAAS,EAAE;CAC/B,QAAQ,SAAS,MAAM,WAAW,KAAK,MAAM,OAAO,SAAS,UAAU,IAAI,CAAC,CAAC,SAAS,IAAI,CAAC;AAC7F"}
|
package/package.json
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/text",
|
|
4
|
-
"version": "0.0.
|
|
5
|
-
"description": "Plain text helpers
|
|
4
|
+
"version": "0.0.3",
|
|
5
|
+
"description": "Plain text helpers: the escapes for HTML, attributes, patterns, Markdown and workflow commands, English word forms (countOf) and a word search (wordSearch).",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"text",
|
|
8
8
|
"escape",
|
|
9
9
|
"html",
|
|
10
10
|
"regexp",
|
|
11
11
|
"markdown",
|
|
12
|
+
"search",
|
|
13
|
+
"plural",
|
|
12
14
|
"typescript"
|
|
13
15
|
],
|
|
14
16
|
"homepage": "https://github.com/mark1russell7/text#readme",
|
|
@@ -48,6 +50,7 @@
|
|
|
48
50
|
"@mark1russell7/cue": "^2.0.0",
|
|
49
51
|
"@mark1russell7/rigor-generators": "^0.1.8",
|
|
50
52
|
"@types/node": "^26.6.5",
|
|
53
|
+
"@vitest/coverage-v8": "^5.0.3",
|
|
51
54
|
"esbuild": "^0.28.2",
|
|
52
55
|
"tsdown": "^0.23.7",
|
|
53
56
|
"typescript": "^7.0.2",
|
package/src/english.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module has the English word forms.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* This function gives a count with a noun, for example "1 site" or "1,024 sites". Only the count 1 takes the singular
|
|
7
|
+
* noun. Each other count takes the plural noun, also 0 and -1. The count has the digit groups of English and at most
|
|
8
|
+
* three decimal places.
|
|
9
|
+
*
|
|
10
|
+
* @param count - The number of things.
|
|
11
|
+
* @param noun - The singular noun.
|
|
12
|
+
* @param plural - The plural noun. The default is the singular noun with the letter "s" at the end.
|
|
13
|
+
* @returns The count, a space and the noun.
|
|
14
|
+
*/
|
|
15
|
+
export function countOf(count: number, noun: string, plural = `${noun}s`): string {
|
|
16
|
+
return `${count.toLocaleString("en")} ${count === 1 ? noun : plural}`;
|
|
17
|
+
}
|
package/src/escape.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* This module has the escapes of plain text for
|
|
3
|
-
* regular expression
|
|
2
|
+
* This module has the escapes of plain text for five targets. The targets are HTML content, an HTML attribute, a
|
|
3
|
+
* regular expression, one line of Markdown and a GitHub Actions workflow command. Each function changes only the characters that have a meaning in its target.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
const HTML: Readonly<Record<string, string>> = { "&": "&", "<": "<", ">": ">" };
|
|
@@ -54,3 +54,25 @@ export function escapeRegExp(text: string): string {
|
|
|
54
54
|
export function escapeMarkdown(text: string): string {
|
|
55
55
|
return text.replace(/\r\n?|\n/g, " ").replace(/[!-/:-@[-`{-~]/g, "\\$&");
|
|
56
56
|
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* This function escapes a text for the data of a GitHub Actions workflow command, the part after `::`. It changes `%`,
|
|
60
|
+
* the carriage return and the line feed to `%25`, `%0D` and `%0A`, so the command stays on one line.
|
|
61
|
+
*
|
|
62
|
+
* @param text - The plain text.
|
|
63
|
+
* @returns The data of the command. The runner decodes it to the text.
|
|
64
|
+
*/
|
|
65
|
+
export function escapeWorkflowData(text: string): string {
|
|
66
|
+
return text.replaceAll("%", "%25").replaceAll("\r", "%0D").replaceAll("\n", "%0A");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* This function escapes a text for a property value of a GitHub Actions workflow command, for example `title=`. It
|
|
71
|
+
* changes the characters of `escapeWorkflowData`, and also `:` and `,` to `%3A` and `%2C`.
|
|
72
|
+
*
|
|
73
|
+
* @param text - The plain text.
|
|
74
|
+
* @returns The property value. The runner decodes it to the text.
|
|
75
|
+
*/
|
|
76
|
+
export function escapeWorkflowProperty(text: string): string {
|
|
77
|
+
return escapeWorkflowData(text).replaceAll(":", "%3A").replaceAll(",", "%2C");
|
|
78
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The entry of `@mark1russell7/text`: plain text helpers. The
|
|
3
|
-
*
|
|
2
|
+
* The entry of `@mark1russell7/text`: plain text helpers. The groups are the escapes for HTML, attributes, regular
|
|
3
|
+
* expressions, Markdown and GitHub Actions workflow commands, the English word forms and a word search.
|
|
4
4
|
*/
|
|
5
|
-
export {
|
|
5
|
+
export { countOf } from "./english.ts";
|
|
6
|
+
export { escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp, escapeWorkflowData, escapeWorkflowProperty } from "./escape.ts";
|
|
7
|
+
export { searchKey, wordSearch } from "./search.ts";
|
package/src/search.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module has a word search of plain text. A text agrees with a search when the text has each word of the search,
|
|
3
|
+
* in any order. The case of the letters is not significant.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** The small final sigma. Unicode case folding changes it to the small sigma, so a word agrees in each position. */
|
|
7
|
+
const FINAL_SIGMA = /ς/gu;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* This function gives the search key of a text. `wordSearch` compares the keys of texts. The function does these steps:
|
|
11
|
+
*
|
|
12
|
+
* 1. It applies the Unicode normalization NFKC. Thus the ligature "fi" agrees with "fi", and full-width letters agree
|
|
13
|
+
* with ASCII letters.
|
|
14
|
+
* 2. It applies the full lower case, then the full upper case, then the full lower case. Thus "ß", "ẞ" and "SS" give
|
|
15
|
+
* "ss".
|
|
16
|
+
* 3. It changes the small final sigma "ς" to "σ", as Unicode case folding does.
|
|
17
|
+
* 4. It applies NFKC again.
|
|
18
|
+
*
|
|
19
|
+
* The steps use no rules of a locale. Thus the Turkish "İ" gives "i" with a combining dot above (U+0307), and the
|
|
20
|
+
* Turkish "ı" gives "i". The key of a key is the same key. Accents stay, so "é" does not agree with "e".
|
|
21
|
+
*
|
|
22
|
+
* @param text - The plain text.
|
|
23
|
+
* @returns The search key of the text.
|
|
24
|
+
*/
|
|
25
|
+
export function searchKey(text: string): string {
|
|
26
|
+
return text.normalize("NFKC").toLowerCase().toUpperCase().toLowerCase().replace(FINAL_SIGMA, "σ").normalize("NFKC");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* This function gives the test of a search. The function makes the key of the search with `searchKey`. Then it splits
|
|
31
|
+
* the key into words at each run of white space, the class `\s` of a regular expression.
|
|
32
|
+
*
|
|
33
|
+
* The test tells if the key of a text contains each word, in any order. A word can be a part of a longer word, so the
|
|
34
|
+
* search "art" finds "Party". A search with no words agrees with each text.
|
|
35
|
+
*
|
|
36
|
+
* @param query - The search, as a person types it.
|
|
37
|
+
* @returns The test of a text.
|
|
38
|
+
*/
|
|
39
|
+
export function wordSearch(query: string): (text: string) => boolean {
|
|
40
|
+
const words = searchKey(query)
|
|
41
|
+
.split(/\s+/u)
|
|
42
|
+
.filter((word) => word !== "");
|
|
43
|
+
return (text) => words.length === 0 || words.every((word) => searchKey(text).includes(word));
|
|
44
|
+
}
|