@mark1russell7/text 0.0.16 → 0.0.18
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 +32 -0
- package/build/index.d.ts +5 -2
- package/build/index.d.ts.map +1 -1
- package/build/passage.d.ts +34 -0
- package/build/passage.d.ts.map +1 -0
- package/build/passage.js +91 -0
- package/build/passage.js.map +1 -0
- package/build/shell.d.ts +15 -0
- package/build/shell.d.ts.map +1 -0
- package/build/shell.js +27 -0
- package/build/shell.js.map +1 -0
- package/package.json +9 -1
- package/src/index.ts +5 -2
- package/src/passage.ts +96 -0
- package/src/shell.ts +26 -0
package/README.md
CHANGED
|
@@ -26,6 +26,20 @@ next")}`); // "::warning title=a%3A b::50%25%0Anext"
|
|
|
26
26
|
escapeMarkdown("a | b *c*"); // "a \\| b \\*c\\*"
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
+
### One word of a POSIX shell
|
|
30
|
+
|
|
31
|
+
`escapeShell(text)` gives one word that a POSIX shell reads as the text, with no expansion and no split into words. A text of safe characters only (letters, digits and `_ @ % + = : , . / -`) stays as it is. Each other text goes in single quotes, and each `'` becomes `'\''`. The empty text becomes `''`.
|
|
32
|
+
|
|
33
|
+
The entry `@mark1russell7/text/shell` has `escapeShell`. The root entry does not have it, to keep its size.
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { escapeShell } from "@mark1russell7/text/shell";
|
|
37
|
+
|
|
38
|
+
escapeShell("notes.txt"); // "notes.txt"
|
|
39
|
+
escapeShell("my file's name"); // "'my file'\\''s name'"
|
|
40
|
+
escapeShell("$HOME"); // "'$HOME'"
|
|
41
|
+
```
|
|
42
|
+
|
|
29
43
|
## English word forms
|
|
30
44
|
|
|
31
45
|
`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.
|
|
@@ -83,6 +97,23 @@ const corpus = ["The release gate", "The topic graph of the site", "A graph page
|
|
|
83
97
|
rankDocuments(corpus, searchWords("topic graph")); // [{ index: 1, … }, { index: 2, … }, { index: 0, score: 0 }]
|
|
84
98
|
```
|
|
85
99
|
|
|
100
|
+
### A passage in a text
|
|
101
|
+
|
|
102
|
+
The entry `@mark1russell7/text/passage` finds a passage in a text, for example a quote in the text of its source. Since 0.0.18.
|
|
103
|
+
|
|
104
|
+
- `passageKey(text)` gives the key of a text. It applies `searchKey` to each grapheme and removes the characters with no width.
|
|
105
|
+
- The key changes each curly quote to a straight quote, each dash to a hyphen-minus, and each run of white space to one space.
|
|
106
|
+
- `findPassage(text, passage)` gives the first span `{ start, end }` of the passage in the text, or `undefined`.
|
|
107
|
+
- The span is in the text, not in the key. Thus `text.slice(start, end)` gives the words of the source.
|
|
108
|
+
- Accents and punctuation stay. A hyphen at the end of a line stays, because it can be a real hyphen.
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
import { findPassage } from "@mark1russell7/text/passage";
|
|
112
|
+
|
|
113
|
+
const text = "Water boils at lower\ntemperatures as altitude increases.";
|
|
114
|
+
findPassage(text, "water boils at lower temperatures"); // { start: 0, end: 33 }
|
|
115
|
+
```
|
|
116
|
+
|
|
86
117
|
## The order of texts
|
|
87
118
|
|
|
88
119
|
The entry `@mark1russell7/text/order` has two orders of texts. Each order is the same in each locale and in each copy of a program. The name of each function tells its unit.
|
|
@@ -320,6 +351,7 @@ The tests also examine 18 copies of a cut with a mark, from a later list of the
|
|
|
320
351
|
- `escapeHtml` does not escape quotes. For an attribute value, use `escapeAttribute`.
|
|
321
352
|
- `escapeRegExp` gives a pattern for use outside a character class. In a class, `-` also has a meaning.
|
|
322
353
|
- `escapeMarkdown` does not change spaces. Four spaces at the start of a line make a code block.
|
|
354
|
+
- `escapeShell` gives one word for a POSIX shell, not for `cmd.exe` or PowerShell. It throws a `RangeError` for a text with NUL.
|
|
323
355
|
- `wordSearch` keeps accents, so "cafe" does not find "café".
|
|
324
356
|
- `countOf` knows no irregular plural. Give the plural noun for each noun that does not add "s".
|
|
325
357
|
- `listOf` does not change the parts. A part with a comma makes a list that a reader cannot split again.
|
package/build/index.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The entry of `@mark1russell7/text`: plain text helpers. The first group is the escapes for HTML, attributes, regular
|
|
3
3
|
* expressions, Markdown and GitHub Actions workflow commands. The other groups are the English word forms, a word
|
|
4
|
-
* search and the text of a thrown value.
|
|
4
|
+
* search and the text of a thrown value.
|
|
5
|
+
*
|
|
6
|
+
* The entry `@mark1russell7/text/redact` removes secrets from a text, and the entry
|
|
5
7
|
* `@mark1russell7/text/order` has two orders of texts. The entry `@mark1russell7/text/english` adds the list join `listOf`.
|
|
6
|
-
* The entry `@mark1russell7/text/search` adds a ranked search with TF-IDF.
|
|
8
|
+
* The entry `@mark1russell7/text/search` adds a ranked search with TF-IDF. The entry `@mark1russell7/text/shell` has the
|
|
9
|
+
* escape of a POSIX shell word.
|
|
7
10
|
*/
|
|
8
11
|
import { countOf } from "./english.js";
|
|
9
12
|
import { searchKey, wordSearch } from "./search.js";
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/error.ts","../src/escape.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/error.ts","../src/escape.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAkBgB,aAAa;;;;;;;;wBCHb,WAAW;;;;;;;;wBAWX,gBAAgB;;;;;;;;;wBAYhB,aAAa;;;;;;;;;;;;wBAeb,eAAe;;;;;;;;wBAWf,mBAAmB;;;;;;;;wBAWnB,uBAAuB"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This function gives the key of a text for a passage search. `findPassage` compares the keys of two texts. The function
|
|
3
|
+
* does these steps on each grapheme of the text:
|
|
4
|
+
*
|
|
5
|
+
* 1. It removes the characters with no width: the soft hyphen, the zero-width space, the joiners and the byte order mark.
|
|
6
|
+
* 2. It applies `searchKey`. Thus the case of the letters and the NFKC forms are not significant.
|
|
7
|
+
* 3. It changes each curly or low quote mark and each prime to the apostrophe or to the double quote. It changes each dash and the minus sign to the hyphen-minus.
|
|
8
|
+
* 4. It changes each run of white space to one space, and it removes the white space at the start and at the end.
|
|
9
|
+
*
|
|
10
|
+
* Accents and punctuation stay. A hyphen at the end of a line stays, because the function cannot tell it from a real hyphen.
|
|
11
|
+
*
|
|
12
|
+
* @param text - The plain text.
|
|
13
|
+
* @returns The key of the text.
|
|
14
|
+
*/
|
|
15
|
+
export declare function passageKey(text: string): string;
|
|
16
|
+
/** The place of a passage in a text, in UTF-16 code units. The passage is `text.slice(start, end)`. */
|
|
17
|
+
export interface PassageSpan {
|
|
18
|
+
readonly start: number;
|
|
19
|
+
readonly end: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* This function finds a passage in a text, for example a quote in the text of its source. It has no I/O.
|
|
23
|
+
*
|
|
24
|
+
* - The text holds the passage when the key of the text (`passageKey`) contains the key of the passage.
|
|
25
|
+
* - The result is the first place, from the start of the first grapheme to the end of the last grapheme of the match.
|
|
26
|
+
* - A passage can start or end inside a word, as in `wordSearch`. The caller tests the edges when a word must be whole.
|
|
27
|
+
* - A passage with an empty key, for example only white space, gives `undefined`. It is not a match at 0.
|
|
28
|
+
*
|
|
29
|
+
* @param text - The text to search, for example the text of a source.
|
|
30
|
+
* @param passage - The passage, for example a quote.
|
|
31
|
+
* @returns The span of the passage in the text, or `undefined` when the text does not hold it.
|
|
32
|
+
*/
|
|
33
|
+
export declare function findPassage(text: string, passage: string): PassageSpan | undefined;
|
|
34
|
+
//# sourceMappingURL=passage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"passage.d.ts","names":[],"sources":["../src/passage.ts"],"mappings":";;;;;;;;;;;;;;wBAkEgB,WAAW;;iBAKV;WACN;WACA;;;;;;;;;;;;;;wBAeK,YAAY,cAAc,kBAAkB"}
|
package/build/passage.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { searchKey } from "./search.js";
|
|
2
|
+
// src/passage.ts
|
|
3
|
+
/**
|
|
4
|
+
* This module finds a passage in a text, for example a quote in the text of its source. The key of a text
|
|
5
|
+
* (`passageKey`) makes the case, the quote marks, the dashes and the white space not significant. `findPassage` gives
|
|
6
|
+
* the span of the passage in the text. Each function has no I/O.
|
|
7
|
+
*/
|
|
8
|
+
/** The quote marks that `passageKey` changes to the apostrophe: the single quotes, the low and the reversed single quotes, and the prime. */
|
|
9
|
+
const SINGLE_QUOTES = /[\u2018\u2019\u201A\u201B\u2032\u02BC]/gu;
|
|
10
|
+
/** The quote marks that `passageKey` changes to the double quote: the double quotes, the low and the reversed double quotes and the guillemets. NFKC changes a double prime to two primes. */
|
|
11
|
+
const DOUBLE_QUOTES = /[\u201C\u201D\u201E\u201F\u00AB\u00BB]/gu;
|
|
12
|
+
/** The dashes that `passageKey` changes to the hyphen-minus. They are the hyphen, the non-breaking hyphen, the figure dash, the en dash and the em dash, the bar and the minus sign. */
|
|
13
|
+
const DASHES = /[\u2010\u2011\u2012\u2013\u2014\u2015\u2212]/gu;
|
|
14
|
+
/** The characters with no width that `passageKey` removes: the soft hyphen, the zero-width space, the joiners and the byte order mark. */
|
|
15
|
+
const NO_WIDTH = /[\u00AD\u200B\u200C\u200D\u2060\uFEFF]/gu;
|
|
16
|
+
/** A grapheme of white space only. */
|
|
17
|
+
const SPACE = /^\s+$/u;
|
|
18
|
+
/** The grapheme segmenter of `passageKey` and `findPassage`. The module makes it one time, at the first use. */
|
|
19
|
+
let graphemeSegmenter;
|
|
20
|
+
/** This function gives the key of a text and the place of each code unit of the key in the text. */
|
|
21
|
+
function passageParts(text) {
|
|
22
|
+
graphemeSegmenter ??= new Intl.Segmenter("en", { granularity: "grapheme" });
|
|
23
|
+
let key = "";
|
|
24
|
+
const starts = [];
|
|
25
|
+
const ends = [];
|
|
26
|
+
for (const { segment, index } of graphemeSegmenter.segment(text)) {
|
|
27
|
+
const clean = segment.replace(NO_WIDTH, "");
|
|
28
|
+
let part;
|
|
29
|
+
if (clean === "") part = "";
|
|
30
|
+
else if (SPACE.test(clean)) part = key === "" || key.endsWith(" ") ? "" : " ";
|
|
31
|
+
else part = searchKey(clean).replace(SINGLE_QUOTES, "'").replace(DOUBLE_QUOTES, "\"").replace(DASHES, "-");
|
|
32
|
+
for (let unit = 0; unit < part.length; unit++) {
|
|
33
|
+
starts.push(index);
|
|
34
|
+
ends.push(index + segment.length);
|
|
35
|
+
}
|
|
36
|
+
key += part;
|
|
37
|
+
}
|
|
38
|
+
if (key.endsWith(" ")) return {
|
|
39
|
+
key: key.slice(0, -1),
|
|
40
|
+
starts: starts.slice(0, -1),
|
|
41
|
+
ends: ends.slice(0, -1)
|
|
42
|
+
};
|
|
43
|
+
return {
|
|
44
|
+
key,
|
|
45
|
+
starts,
|
|
46
|
+
ends
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* This function gives the key of a text for a passage search. `findPassage` compares the keys of two texts. The function
|
|
51
|
+
* does these steps on each grapheme of the text:
|
|
52
|
+
*
|
|
53
|
+
* 1. It removes the characters with no width: the soft hyphen, the zero-width space, the joiners and the byte order mark.
|
|
54
|
+
* 2. It applies `searchKey`. Thus the case of the letters and the NFKC forms are not significant.
|
|
55
|
+
* 3. It changes each curly or low quote mark and each prime to the apostrophe or to the double quote. It changes each dash and the minus sign to the hyphen-minus.
|
|
56
|
+
* 4. It changes each run of white space to one space, and it removes the white space at the start and at the end.
|
|
57
|
+
*
|
|
58
|
+
* Accents and punctuation stay. A hyphen at the end of a line stays, because the function cannot tell it from a real hyphen.
|
|
59
|
+
*
|
|
60
|
+
* @param text - The plain text.
|
|
61
|
+
* @returns The key of the text.
|
|
62
|
+
*/
|
|
63
|
+
function passageKey(text) {
|
|
64
|
+
return passageParts(text).key;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* This function finds a passage in a text, for example a quote in the text of its source. It has no I/O.
|
|
68
|
+
*
|
|
69
|
+
* - The text holds the passage when the key of the text (`passageKey`) contains the key of the passage.
|
|
70
|
+
* - The result is the first place, from the start of the first grapheme to the end of the last grapheme of the match.
|
|
71
|
+
* - A passage can start or end inside a word, as in `wordSearch`. The caller tests the edges when a word must be whole.
|
|
72
|
+
* - A passage with an empty key, for example only white space, gives `undefined`. It is not a match at 0.
|
|
73
|
+
*
|
|
74
|
+
* @param text - The text to search, for example the text of a source.
|
|
75
|
+
* @param passage - The passage, for example a quote.
|
|
76
|
+
* @returns The span of the passage in the text, or `undefined` when the text does not hold it.
|
|
77
|
+
*/
|
|
78
|
+
function findPassage(text, passage) {
|
|
79
|
+
const needle = passageKey(passage);
|
|
80
|
+
if (needle === "") return void 0;
|
|
81
|
+
const parts = passageParts(text);
|
|
82
|
+
const at = parts.key.indexOf(needle);
|
|
83
|
+
if (at < 0) return void 0;
|
|
84
|
+
return {
|
|
85
|
+
start: parts.starts[at] ?? 0,
|
|
86
|
+
end: parts.ends[at + needle.length - 1] ?? 0
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
export { findPassage, passageKey };
|
|
90
|
+
|
|
91
|
+
//# sourceMappingURL=passage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"passage.js","names":[],"sources":["../src/passage.ts"],"sourcesContent":["/**\n * This module finds a passage in a text, for example a quote in the text of its source. The key of a text\n * (`passageKey`) makes the case, the quote marks, the dashes and the white space not significant. `findPassage` gives\n * the span of the passage in the text. Each function has no I/O.\n */\n\nimport { searchKey } from \"./search.ts\";\n\n/** The quote marks that `passageKey` changes to the apostrophe: the single quotes, the low and the reversed single quotes, and the prime. */\nconst SINGLE_QUOTES = /[\\u2018\\u2019\\u201A\\u201B\\u2032\\u02BC]/gu;\n/** The quote marks that `passageKey` changes to the double quote: the double quotes, the low and the reversed double quotes and the guillemets. NFKC changes a double prime to two primes. */\nconst DOUBLE_QUOTES = /[\\u201C\\u201D\\u201E\\u201F\\u00AB\\u00BB]/gu;\n/** The dashes that `passageKey` changes to the hyphen-minus. They are the hyphen, the non-breaking hyphen, the figure dash, the en dash and the em dash, the bar and the minus sign. */\nconst DASHES = /[\\u2010\\u2011\\u2012\\u2013\\u2014\\u2015\\u2212]/gu;\n/** The characters with no width that `passageKey` removes: the soft hyphen, the zero-width space, the joiners and the byte order mark. */\nconst NO_WIDTH = /[\\u00AD\\u200B\\u200C\\u200D\\u2060\\uFEFF]/gu;\n/** A grapheme of white space only. */\nconst SPACE = /^\\s+$/u;\n\n/** The grapheme segmenter of `passageKey` and `findPassage`. The module makes it one time, at the first use. */\nlet graphemeSegmenter: Intl.Segmenter | undefined;\n\n/** The key of a text for a passage search, with the start and the end in the text of each code unit of the key. */\ninterface PassageParts {\n readonly key: string;\n readonly starts: readonly number[];\n readonly ends: readonly number[];\n}\n\n/** This function gives the key of a text and the place of each code unit of the key in the text. */\nfunction passageParts(text: string): PassageParts {\n graphemeSegmenter ??= new Intl.Segmenter(\"en\", { granularity: \"grapheme\" });\n let key = \"\";\n const starts: number[] = [];\n const ends: number[] = [];\n for (const { segment, index } of graphemeSegmenter.segment(text)) {\n // A joiner is a part of the grapheme before it, so the test of white space comes after the removal.\n const clean = segment.replace(NO_WIDTH, \"\");\n let part: string;\n if (clean === \"\") part = \"\";\n else if (SPACE.test(clean)) part = key === \"\" || key.endsWith(\" \") ? \"\" : \" \";\n else part = searchKey(clean).replace(SINGLE_QUOTES, \"'\").replace(DOUBLE_QUOTES, '\"').replace(DASHES, \"-\");\n for (let unit = 0; unit < part.length; unit++) {\n starts.push(index);\n ends.push(index + segment.length);\n }\n key += part;\n }\n if (key.endsWith(\" \")) return { key: key.slice(0, -1), starts: starts.slice(0, -1), ends: ends.slice(0, -1) };\n return { key, starts, ends };\n}\n\n/**\n * This function gives the key of a text for a passage search. `findPassage` compares the keys of two texts. The function\n * does these steps on each grapheme of the text:\n *\n * 1. It removes the characters with no width: the soft hyphen, the zero-width space, the joiners and the byte order mark.\n * 2. It applies `searchKey`. Thus the case of the letters and the NFKC forms are not significant.\n * 3. It changes each curly or low quote mark and each prime to the apostrophe or to the double quote. It changes each dash and the minus sign to the hyphen-minus.\n * 4. It changes each run of white space to one space, and it removes the white space at the start and at the end.\n *\n * Accents and punctuation stay. A hyphen at the end of a line stays, because the function cannot tell it from a real hyphen.\n *\n * @param text - The plain text.\n * @returns The key of the text.\n */\nexport function passageKey(text: string): string {\n return passageParts(text).key;\n}\n\n/** The place of a passage in a text, in UTF-16 code units. The passage is `text.slice(start, end)`. */\nexport interface PassageSpan {\n readonly start: number;\n readonly end: number;\n}\n\n/**\n * This function finds a passage in a text, for example a quote in the text of its source. It has no I/O.\n *\n * - The text holds the passage when the key of the text (`passageKey`) contains the key of the passage.\n * - The result is the first place, from the start of the first grapheme to the end of the last grapheme of the match.\n * - A passage can start or end inside a word, as in `wordSearch`. The caller tests the edges when a word must be whole.\n * - A passage with an empty key, for example only white space, gives `undefined`. It is not a match at 0.\n *\n * @param text - The text to search, for example the text of a source.\n * @param passage - The passage, for example a quote.\n * @returns The span of the passage in the text, or `undefined` when the text does not hold it.\n */\nexport function findPassage(text: string, passage: string): PassageSpan | undefined {\n const needle = passageKey(passage);\n if (needle === \"\") return undefined;\n const parts = passageParts(text);\n const at = parts.key.indexOf(needle);\n if (at < 0) return undefined;\n return { start: parts.starts[at] ?? 0, end: parts.ends[at + needle.length - 1] ?? 0 };\n}\n"],"mappings":";;;;;;;;AASA,MAAM,gBAAgB;;AAEtB,MAAM,gBAAgB;;AAEtB,MAAM,SAAS;;AAEf,MAAM,WAAW;;AAEjB,MAAM,QAAQ;;AAGd,IAAI;;AAUJ,SAAS,aAAa,MAA4B;CAChD,sBAAsB,IAAI,KAAK,UAAU,MAAM,EAAE,aAAa,WAAW,CAAC;CAC1E,IAAI,MAAM;CACV,MAAM,SAAmB,CAAC;CAC1B,MAAM,OAAiB,CAAC;CACxB,KAAK,MAAM,EAAE,SAAS,WAAW,kBAAkB,QAAQ,IAAI,GAAG;EAEhE,MAAM,QAAQ,QAAQ,QAAQ,UAAU,EAAE;EAC1C,IAAI;EACJ,IAAI,UAAU,IAAI,OAAO;OACpB,IAAI,MAAM,KAAK,KAAK,GAAG,OAAO,QAAQ,MAAM,IAAI,SAAS,GAAG,IAAI,KAAK;OACrE,OAAO,UAAU,KAAK,CAAC,CAAC,QAAQ,eAAe,GAAG,CAAC,CAAC,QAAQ,eAAe,IAAG,CAAC,CAAC,QAAQ,QAAQ,GAAG;EACxG,KAAK,IAAI,OAAO,GAAG,OAAO,KAAK,QAAQ,QAAQ;GAC7C,OAAO,KAAK,KAAK;GACjB,KAAK,KAAK,QAAQ,QAAQ,MAAM;EAClC;EACA,OAAO;CACT;CACA,IAAI,IAAI,SAAS,GAAG,GAAG,OAAO;EAAE,KAAK,IAAI,MAAM,GAAG,EAAE;EAAG,QAAQ,OAAO,MAAM,GAAG,EAAE;EAAG,MAAM,KAAK,MAAM,GAAG,EAAE;CAAE;CAC5G,OAAO;EAAE;EAAK;EAAQ;CAAK;AAC7B;;;;;;;;;;;;;;;AAgBA,SAAgB,WAAW,MAAsB;CAC/C,OAAO,aAAa,IAAI,CAAC,CAAC;AAC5B;;;;;;;;;;;;;AAoBA,SAAgB,YAAY,MAAc,SAA0C;CAClF,MAAM,SAAS,WAAW,OAAO;CACjC,IAAI,WAAW,IAAI,OAAO,KAAA;CAC1B,MAAM,QAAQ,aAAa,IAAI;CAC/B,MAAM,KAAK,MAAM,IAAI,QAAQ,MAAM;CACnC,IAAI,KAAK,GAAG,OAAO,KAAA;CACnB,OAAO;EAAE,OAAO,MAAM,OAAO,OAAO;EAAG,KAAK,MAAM,KAAK,KAAK,OAAO,SAAS,MAAM;CAAE;AACtF"}
|
package/build/shell.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This function escapes a text for one word of a POSIX shell command, for example an argument of a command in a script.
|
|
3
|
+
* The shell reads the word as the text, with no expansion and no split into words.
|
|
4
|
+
*
|
|
5
|
+
* - A text of safe characters only (letters, digits and `_ @ % + = : , . / -`) stays as it is.
|
|
6
|
+
* - Each other text goes in single quotes. Each single quote of the text becomes `'\''`.
|
|
7
|
+
* - The empty text becomes `''`.
|
|
8
|
+
*
|
|
9
|
+
* A shell word cannot hold the NUL character. Thus the function throws a `RangeError` for a text with NUL.
|
|
10
|
+
*
|
|
11
|
+
* @param text - The plain text.
|
|
12
|
+
* @returns One word that a POSIX shell reads as the text.
|
|
13
|
+
*/
|
|
14
|
+
export declare function escapeShell(text: string): string;
|
|
15
|
+
//# sourceMappingURL=shell.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shell.d.ts","names":[],"sources":["../src/shell.ts"],"mappings":";;;;;;;;;;;;;wBAqBgB,YAAY"}
|
package/build/shell.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/shell.ts
|
|
2
|
+
/**
|
|
3
|
+
* The entry `@mark1russell7/text/shell`: the escape of a text for one word of a POSIX shell command. It is a separate
|
|
4
|
+
* entry, so the root entry keeps its size.
|
|
5
|
+
*/
|
|
6
|
+
const SAFE = /^[A-Za-z0-9_@%+=:,./-]+$/;
|
|
7
|
+
/**
|
|
8
|
+
* This function escapes a text for one word of a POSIX shell command, for example an argument of a command in a script.
|
|
9
|
+
* The shell reads the word as the text, with no expansion and no split into words.
|
|
10
|
+
*
|
|
11
|
+
* - A text of safe characters only (letters, digits and `_ @ % + = : , . / -`) stays as it is.
|
|
12
|
+
* - Each other text goes in single quotes. Each single quote of the text becomes `'\''`.
|
|
13
|
+
* - The empty text becomes `''`.
|
|
14
|
+
*
|
|
15
|
+
* A shell word cannot hold the NUL character. Thus the function throws a `RangeError` for a text with NUL.
|
|
16
|
+
*
|
|
17
|
+
* @param text - The plain text.
|
|
18
|
+
* @returns One word that a POSIX shell reads as the text.
|
|
19
|
+
*/
|
|
20
|
+
function escapeShell(text) {
|
|
21
|
+
if (text.includes("\0")) throw new RangeError("A shell word cannot hold the NUL character.");
|
|
22
|
+
if (SAFE.test(text)) return text;
|
|
23
|
+
return `'${text.replaceAll("'", "'\\''")}'`;
|
|
24
|
+
}
|
|
25
|
+
export { escapeShell };
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=shell.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shell.js","names":[],"sources":["../src/shell.ts"],"sourcesContent":["/**\n * The entry `@mark1russell7/text/shell`: the escape of a text for one word of a POSIX shell command. It is a separate\n * entry, so the root entry keeps its size.\n */\n\n// The characters that a POSIX shell reads as plain word characters at each place of a word. It is the safe set of `shlex.quote` of Python.\nconst SAFE = /^[A-Za-z0-9_@%+=:,./-]+$/;\n\n/**\n * This function escapes a text for one word of a POSIX shell command, for example an argument of a command in a script.\n * The shell reads the word as the text, with no expansion and no split into words.\n *\n * - A text of safe characters only (letters, digits and `_ @ % + = : , . / -`) stays as it is.\n * - Each other text goes in single quotes. Each single quote of the text becomes `'\\''`.\n * - The empty text becomes `''`.\n *\n * A shell word cannot hold the NUL character. Thus the function throws a `RangeError` for a text with NUL.\n *\n * @param text - The plain text.\n * @returns One word that a POSIX shell reads as the text.\n */\nexport function escapeShell(text: string): string {\n if (text.includes(\"\\0\")) throw new RangeError(\"A shell word cannot hold the NUL character.\");\n if (SAFE.test(text)) return text;\n return `'${text.replaceAll(\"'\", \"'\\\\''\")}'`;\n}\n"],"mappings":";;;;;AAMA,MAAM,OAAO;;;;;;;;;;;;;;AAeb,SAAgB,YAAY,MAAsB;CAChD,IAAI,KAAK,SAAS,IAAI,GAAG,MAAM,IAAI,WAAW,6CAA6C;CAC3F,IAAI,KAAK,KAAK,IAAI,GAAG,OAAO;CAC5B,OAAO,IAAI,KAAK,WAAW,KAAK,OAAO,EAAE;AAC3C"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/text",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.18",
|
|
5
5
|
"description": "Plain text helpers: the escapes for HTML, attributes, patterns, Markdown and workflow commands, English word forms (countOf, listOf), a word search (wordSearch), the text of a thrown value (errorMessage), the orders of texts by code units and by code points (compareCodeUnits, compareCodePoints), the removal of secrets (redactSecrets) and the shapes of tokens and of secrets by their context (findSecretShapes, redactSecretShapes, findSecretShapesInValue, CONTEXT_SHAPES, isSecretName), and lengths in code units, code points and graphemes with truncation and padding (textLength, truncate, pad), the marked parts of a text (partsAt, partsOf), and a scan of HTML for its text and its start tags (scanHtml) with the decode of its character references (decodeReferences).",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"text",
|
|
@@ -69,10 +69,18 @@
|
|
|
69
69
|
"types": "./build/english.d.ts",
|
|
70
70
|
"default": "./build/english.js"
|
|
71
71
|
},
|
|
72
|
+
"./passage": {
|
|
73
|
+
"types": "./build/passage.d.ts",
|
|
74
|
+
"default": "./build/passage.js"
|
|
75
|
+
},
|
|
72
76
|
"./search": {
|
|
73
77
|
"types": "./build/search.d.ts",
|
|
74
78
|
"default": "./build/search.js"
|
|
75
79
|
},
|
|
80
|
+
"./shell": {
|
|
81
|
+
"types": "./build/shell.d.ts",
|
|
82
|
+
"default": "./build/shell.js"
|
|
83
|
+
},
|
|
76
84
|
"./package.json": "./package.json"
|
|
77
85
|
},
|
|
78
86
|
"files": [
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The entry of `@mark1russell7/text`: plain text helpers. The first group is the escapes for HTML, attributes, regular
|
|
3
3
|
* expressions, Markdown and GitHub Actions workflow commands. The other groups are the English word forms, a word
|
|
4
|
-
* search and the text of a thrown value.
|
|
4
|
+
* search and the text of a thrown value.
|
|
5
|
+
*
|
|
6
|
+
* The entry `@mark1russell7/text/redact` removes secrets from a text, and the entry
|
|
5
7
|
* `@mark1russell7/text/order` has two orders of texts. The entry `@mark1russell7/text/english` adds the list join `listOf`.
|
|
6
|
-
* The entry `@mark1russell7/text/search` adds a ranked search with TF-IDF.
|
|
8
|
+
* The entry `@mark1russell7/text/search` adds a ranked search with TF-IDF. The entry `@mark1russell7/text/shell` has the
|
|
9
|
+
* escape of a POSIX shell word.
|
|
7
10
|
*/
|
|
8
11
|
export { countOf } from "./english.ts";
|
|
9
12
|
export { errorMessage } from "./error.ts";
|
package/src/passage.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module finds a passage in a text, for example a quote in the text of its source. The key of a text
|
|
3
|
+
* (`passageKey`) makes the case, the quote marks, the dashes and the white space not significant. `findPassage` gives
|
|
4
|
+
* the span of the passage in the text. Each function has no I/O.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { searchKey } from "./search.ts";
|
|
8
|
+
|
|
9
|
+
/** The quote marks that `passageKey` changes to the apostrophe: the single quotes, the low and the reversed single quotes, and the prime. */
|
|
10
|
+
const SINGLE_QUOTES = /[\u2018\u2019\u201A\u201B\u2032\u02BC]/gu;
|
|
11
|
+
/** The quote marks that `passageKey` changes to the double quote: the double quotes, the low and the reversed double quotes and the guillemets. NFKC changes a double prime to two primes. */
|
|
12
|
+
const DOUBLE_QUOTES = /[\u201C\u201D\u201E\u201F\u00AB\u00BB]/gu;
|
|
13
|
+
/** The dashes that `passageKey` changes to the hyphen-minus. They are the hyphen, the non-breaking hyphen, the figure dash, the en dash and the em dash, the bar and the minus sign. */
|
|
14
|
+
const DASHES = /[\u2010\u2011\u2012\u2013\u2014\u2015\u2212]/gu;
|
|
15
|
+
/** The characters with no width that `passageKey` removes: the soft hyphen, the zero-width space, the joiners and the byte order mark. */
|
|
16
|
+
const NO_WIDTH = /[\u00AD\u200B\u200C\u200D\u2060\uFEFF]/gu;
|
|
17
|
+
/** A grapheme of white space only. */
|
|
18
|
+
const SPACE = /^\s+$/u;
|
|
19
|
+
|
|
20
|
+
/** The grapheme segmenter of `passageKey` and `findPassage`. The module makes it one time, at the first use. */
|
|
21
|
+
let graphemeSegmenter: Intl.Segmenter | undefined;
|
|
22
|
+
|
|
23
|
+
/** The key of a text for a passage search, with the start and the end in the text of each code unit of the key. */
|
|
24
|
+
interface PassageParts {
|
|
25
|
+
readonly key: string;
|
|
26
|
+
readonly starts: readonly number[];
|
|
27
|
+
readonly ends: readonly number[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** This function gives the key of a text and the place of each code unit of the key in the text. */
|
|
31
|
+
function passageParts(text: string): PassageParts {
|
|
32
|
+
graphemeSegmenter ??= new Intl.Segmenter("en", { granularity: "grapheme" });
|
|
33
|
+
let key = "";
|
|
34
|
+
const starts: number[] = [];
|
|
35
|
+
const ends: number[] = [];
|
|
36
|
+
for (const { segment, index } of graphemeSegmenter.segment(text)) {
|
|
37
|
+
// A joiner is a part of the grapheme before it, so the test of white space comes after the removal.
|
|
38
|
+
const clean = segment.replace(NO_WIDTH, "");
|
|
39
|
+
let part: string;
|
|
40
|
+
if (clean === "") part = "";
|
|
41
|
+
else if (SPACE.test(clean)) part = key === "" || key.endsWith(" ") ? "" : " ";
|
|
42
|
+
else part = searchKey(clean).replace(SINGLE_QUOTES, "'").replace(DOUBLE_QUOTES, '"').replace(DASHES, "-");
|
|
43
|
+
for (let unit = 0; unit < part.length; unit++) {
|
|
44
|
+
starts.push(index);
|
|
45
|
+
ends.push(index + segment.length);
|
|
46
|
+
}
|
|
47
|
+
key += part;
|
|
48
|
+
}
|
|
49
|
+
if (key.endsWith(" ")) return { key: key.slice(0, -1), starts: starts.slice(0, -1), ends: ends.slice(0, -1) };
|
|
50
|
+
return { key, starts, ends };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* This function gives the key of a text for a passage search. `findPassage` compares the keys of two texts. The function
|
|
55
|
+
* does these steps on each grapheme of the text:
|
|
56
|
+
*
|
|
57
|
+
* 1. It removes the characters with no width: the soft hyphen, the zero-width space, the joiners and the byte order mark.
|
|
58
|
+
* 2. It applies `searchKey`. Thus the case of the letters and the NFKC forms are not significant.
|
|
59
|
+
* 3. It changes each curly or low quote mark and each prime to the apostrophe or to the double quote. It changes each dash and the minus sign to the hyphen-minus.
|
|
60
|
+
* 4. It changes each run of white space to one space, and it removes the white space at the start and at the end.
|
|
61
|
+
*
|
|
62
|
+
* Accents and punctuation stay. A hyphen at the end of a line stays, because the function cannot tell it from a real hyphen.
|
|
63
|
+
*
|
|
64
|
+
* @param text - The plain text.
|
|
65
|
+
* @returns The key of the text.
|
|
66
|
+
*/
|
|
67
|
+
export function passageKey(text: string): string {
|
|
68
|
+
return passageParts(text).key;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The place of a passage in a text, in UTF-16 code units. The passage is `text.slice(start, end)`. */
|
|
72
|
+
export interface PassageSpan {
|
|
73
|
+
readonly start: number;
|
|
74
|
+
readonly end: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* This function finds a passage in a text, for example a quote in the text of its source. It has no I/O.
|
|
79
|
+
*
|
|
80
|
+
* - The text holds the passage when the key of the text (`passageKey`) contains the key of the passage.
|
|
81
|
+
* - The result is the first place, from the start of the first grapheme to the end of the last grapheme of the match.
|
|
82
|
+
* - A passage can start or end inside a word, as in `wordSearch`. The caller tests the edges when a word must be whole.
|
|
83
|
+
* - A passage with an empty key, for example only white space, gives `undefined`. It is not a match at 0.
|
|
84
|
+
*
|
|
85
|
+
* @param text - The text to search, for example the text of a source.
|
|
86
|
+
* @param passage - The passage, for example a quote.
|
|
87
|
+
* @returns The span of the passage in the text, or `undefined` when the text does not hold it.
|
|
88
|
+
*/
|
|
89
|
+
export function findPassage(text: string, passage: string): PassageSpan | undefined {
|
|
90
|
+
const needle = passageKey(passage);
|
|
91
|
+
if (needle === "") return undefined;
|
|
92
|
+
const parts = passageParts(text);
|
|
93
|
+
const at = parts.key.indexOf(needle);
|
|
94
|
+
if (at < 0) return undefined;
|
|
95
|
+
return { start: parts.starts[at] ?? 0, end: parts.ends[at + needle.length - 1] ?? 0 };
|
|
96
|
+
}
|
package/src/shell.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry `@mark1russell7/text/shell`: the escape of a text for one word of a POSIX shell command. It is a separate
|
|
3
|
+
* entry, so the root entry keeps its size.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// The characters that a POSIX shell reads as plain word characters at each place of a word. It is the safe set of `shlex.quote` of Python.
|
|
7
|
+
const SAFE = /^[A-Za-z0-9_@%+=:,./-]+$/;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* This function escapes a text for one word of a POSIX shell command, for example an argument of a command in a script.
|
|
11
|
+
* The shell reads the word as the text, with no expansion and no split into words.
|
|
12
|
+
*
|
|
13
|
+
* - A text of safe characters only (letters, digits and `_ @ % + = : , . / -`) stays as it is.
|
|
14
|
+
* - Each other text goes in single quotes. Each single quote of the text becomes `'\''`.
|
|
15
|
+
* - The empty text becomes `''`.
|
|
16
|
+
*
|
|
17
|
+
* A shell word cannot hold the NUL character. Thus the function throws a `RangeError` for a text with NUL.
|
|
18
|
+
*
|
|
19
|
+
* @param text - The plain text.
|
|
20
|
+
* @returns One word that a POSIX shell reads as the text.
|
|
21
|
+
*/
|
|
22
|
+
export function escapeShell(text: string): string {
|
|
23
|
+
if (text.includes("\0")) throw new RangeError("A shell word cannot hold the NUL character.");
|
|
24
|
+
if (SAFE.test(text)) return text;
|
|
25
|
+
return `'${text.replaceAll("'", "'\\''")}'`;
|
|
26
|
+
}
|