@mark1russell7/text 0.0.2 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @mark1russell7/text
2
2
 
3
- Plain text helpers. The first group is the escapes of a text for five targets.
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
@@ -26,6 +26,33 @@ next")}`); // "::warning title=a%3A b::50%25%0Anext"
26
26
  escapeMarkdown("a | b *c*"); // "a \\| b \\*c\\*"
27
27
  ```
28
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
+
29
56
  ## The copies that this package replaces
30
57
 
31
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.
@@ -35,3 +62,5 @@ The tests run 58 copies of these functions from other repositories of the owner.
35
62
  - `escapeHtml` does not escape quotes. For an attribute value, use `escapeAttribute`.
36
63
  - `escapeRegExp` gives a pattern for use outside a character class. In a class, `-` also has a meaning.
37
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 first group is the escapes for HTML, attributes,
3
- * regular expressions, Markdown and GitHub Actions workflow commands.
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.
@@ -55,4 +69,32 @@ export declare function escapeWorkflowData(text: string): string;
55
69
  * @returns The property value. The runner decodes it to the text.
56
70
  */
57
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;
58
100
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/escape.ts"],"mappings":";;;;;;;;;;;wBAegB,WAAW;;;;;;;;wBAWX,gBAAgB;;;;;;;;;wBAYhB,aAAa;;;;;;;;;;;;wBAeb,eAAe;;;;;;;;wBAWf,mBAAmB;;;;;;;;wBAWnB,uBAAuB"}
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,3 +1,20 @@
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
20
  * This module has the escapes of plain text for five targets. The targets are HTML content, an HTML attribute, a
@@ -78,6 +95,46 @@ function escapeWorkflowData(text) {
78
95
  function escapeWorkflowProperty(text) {
79
96
  return escapeWorkflowData(text).replaceAll(":", "%3A").replaceAll(",", "%2C");
80
97
  }
81
- export { escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp, escapeWorkflowData, escapeWorkflowProperty };
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 };
82
139
 
83
140
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/escape.ts"],"sourcesContent":["/**\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>> = { \"&\": \"&amp;\", \"<\": \"&lt;\", \">\": \"&gt;\" };\nconst ATTRIBUTE: Readonly<Record<string, string>> = { ...HTML, '\"': \"&quot;\", \"'\": \"&#39;\" };\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 `&amp;`, `&lt;` and `&gt;` 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 `&amp;`, `&lt;`, `&gt;`, `&quot;` and `&#39;` 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"],"mappings":";;;;;AAKA,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"}
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>> = { \"&\": \"&amp;\", \"<\": \"&lt;\", \">\": \"&gt;\" };\nconst ATTRIBUTE: Readonly<Record<string, string>> = { ...HTML, '\"': \"&quot;\", \"'\": \"&#39;\" };\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 `&amp;`, `&lt;` and `&gt;` 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 `&amp;`, `&lt;`, `&gt;`, `&quot;` and `&#39;` 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.2",
5
- "description": "Plain text helpers. The first group is the escapes: escapeHtml, escapeAttribute, escapeRegExp, escapeMarkdown, escapeWorkflowData and escapeWorkflowProperty.",
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,7 +50,7 @@
48
50
  "@mark1russell7/cue": "^2.0.0",
49
51
  "@mark1russell7/rigor-generators": "^0.1.8",
50
52
  "@types/node": "^26.6.5",
51
- "@vitest/coverage-v8": "5.1.0",
53
+ "@vitest/coverage-v8": "^5.0.3",
52
54
  "esbuild": "^0.28.2",
53
55
  "tsdown": "^0.23.7",
54
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/index.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  /**
2
- * The entry of `@mark1russell7/text`: plain text helpers. The first group is the escapes for HTML, attributes,
3
- * regular expressions, Markdown and GitHub Actions workflow commands.
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 { countOf } from "./english.ts";
5
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
+ }