@mark1russell7/text 0.0.17 → 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 CHANGED
@@ -97,6 +97,23 @@ const corpus = ["The release gate", "The topic graph of the site", "A graph page
97
97
  rankDocuments(corpus, searchWords("topic graph")); // [{ index: 1, … }, { index: 2, … }, { index: 0, score: 0 }]
98
98
  ```
99
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
+
100
117
  ## The order of texts
101
118
 
102
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.
@@ -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"}
@@ -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,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1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@mark1russell7/text",
4
- "version": "0.0.17",
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,6 +69,10 @@
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"
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
+ }