@mark1russell7/text 0.0.6 → 0.0.7

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 in six groups: the escapes of a text for five targets, the English word forms and a word search. The other three groups are the text of a thrown value, the removal of secrets, and lengths with cuts and padding.
3
+ Plain text helpers in seven groups: the escapes of a text for five targets, the English word forms and a word search. The other four groups are the text of a thrown value, the removal of secrets, lengths with cuts and padding, and marked parts.
4
4
 
5
5
  ```sh
6
6
  pnpm add @mark1russell7/text
@@ -144,6 +144,28 @@ pad("ab", 5, { fill: "😀" }); // "ab😀": the emoji has two code units, so on
144
144
  - The side `both` puts the half of the fill at the start and the other half at the end. For an odd count, the end gets one more unit.
145
145
  - An empty fill, a bad side and a `width` that is not an integer of 0 or more give a `RangeError`.
146
146
 
147
+ ## Marked parts
148
+
149
+ The entry `@mark1russell7/text/parts` divides a text into parts. A part with `hit` is in a range or is a copy of a search. A page puts a `mark` element on each part with `hit`.
150
+
151
+ - `partsAt(text, ranges)` gives the parts at ranges of UTF-16 code units, `[start, end)`. It sorts the ranges, joins the ranges that overlap and clamps each range to the text.
152
+ - `partsOf(text, find)` gives the parts at each copy of `find`, from the start, with no overlap. The comparison is exact.
153
+
154
+ ```ts
155
+ import { partsAt, partsOf } from "@mark1russell7/text/parts";
156
+
157
+ partsOf("one cat, two cats", "cat");
158
+ // [{ text: "one ", hit: false }, { text: "cat", hit: true }, { text: ", two ", hit: false }, { text: "cat", hit: true }, { text: "s", hit: false }]
159
+ partsAt("A wide river", [[2, 6]]);
160
+ // [{ text: "A ", hit: false }, { text: "wide", hit: true }, { text: " river", hit: false }]
161
+ ```
162
+
163
+ The parts obey these rules:
164
+
165
+ - The texts of the parts, in their order, make the text again. No part is empty.
166
+ - Two ranges that touch stay two parts. Two ranges that overlap become one part.
167
+ - An empty range, an empty `find` and an empty text give no part with `hit`.
168
+
147
169
  ## The copies that this package replaces
148
170
 
149
171
  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.
@@ -161,4 +183,5 @@ The tests also examine 18 copies of a cut with a mark, from a later list of the
161
183
  - `redactSecrets` removes only the forms in its table. A secret in a JSON string with escapes, or in a different encoding, stays.
162
184
  - `textLength`, `truncate` and `pad` count graphemes with the `Intl.Segmenter` of the runtime. An old runtime can count a new emoji as more than one grapheme.
163
185
  - In `pad`, a grapheme of the fill can join the text or the next copy of the fill. A combining mark and a lone regional indicator do this. Then the result has fewer graphemes than the width.
186
+ - `partsAt` counts in code units, as `String.prototype.slice` does. A range can cut a surrogate pair or a grapheme.
164
187
  - The three functions count units, not the width on a screen. A wide character, for example æ—¥, is one unit, as a narrow character is.
@@ -0,0 +1,32 @@
1
+ /**
2
+ * This module divides a text into marked parts: the parts at ranges, or the parts at each copy of a text. A page puts a
3
+ * `mark` element on each part with `hit`, and a terminal puts a color on it.
4
+ */
5
+ /** A part of a text. A part with `hit` is in a range or is a copy of the text that the search found. */
6
+ export interface TextPart {
7
+ readonly text: string;
8
+ readonly hit: boolean;
9
+ }
10
+ /**
11
+ * This function divides a text into parts at ranges of UTF-16 code units, `[start, end)`. Each range gives a part with
12
+ * `hit`, and the text between the ranges gives parts with no `hit`. The function sorts the ranges, joins the ranges
13
+ * that overlap, and clamps each range to the text. Two ranges that touch stay two parts. An empty range gives no part.
14
+ *
15
+ * The texts of the parts, in their order, make the text again.
16
+ *
17
+ * @param text - The text.
18
+ * @param ranges - The ranges, as `[start, end)` in code units, in any order.
19
+ * @returns The parts of the text, with no empty part.
20
+ */
21
+ export declare function partsAt(text: string, ranges: readonly (readonly [start: number, end: number])[]): TextPart[];
22
+ /**
23
+ * This function divides a text into parts at each copy of `find`, from the start, with no overlap. Each copy gives a
24
+ * part with `hit`. An empty `find` gives one part with no `hit`. The comparison is exact: the case and the
25
+ * normalization of the letters are significant.
26
+ *
27
+ * @param text - The text.
28
+ * @param find - The text to find.
29
+ * @returns The parts of the text, with no empty part.
30
+ */
31
+ export declare function partsOf(text: string, find: string): TextPart[];
32
+ //# sourceMappingURL=parts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parts.d.ts","names":[],"sources":["../src/parts.ts"],"mappings":";;;;;iBAMiB;WACN;WACA;;;;;;;;;;;;;wBAcK,QAAQ,cAAc,4BAA4B,eAAe,kBAAkB;;;;;;;;;;wBAgCnF,QAAQ,cAAc,eAAe"}
package/build/parts.js ADDED
@@ -0,0 +1,61 @@
1
+ // src/parts.ts
2
+ /**
3
+ * This function divides a text into parts at ranges of UTF-16 code units, `[start, end)`. Each range gives a part with
4
+ * `hit`, and the text between the ranges gives parts with no `hit`. The function sorts the ranges, joins the ranges
5
+ * that overlap, and clamps each range to the text. Two ranges that touch stay two parts. An empty range gives no part.
6
+ *
7
+ * The texts of the parts, in their order, make the text again.
8
+ *
9
+ * @param text - The text.
10
+ * @param ranges - The ranges, as `[start, end)` in code units, in any order.
11
+ * @returns The parts of the text, with no empty part.
12
+ */
13
+ function partsAt(text, ranges) {
14
+ const clamp = (n) => Math.min(Math.max(Math.trunc(n), 0), text.length);
15
+ const sorted = ranges.map(([start, end]) => [clamp(start), clamp(end)]).filter(([start, end]) => end > start).sort((a, b) => a[0] - b[0]);
16
+ const parts = [];
17
+ let at = 0;
18
+ for (const [start, end] of sorted) {
19
+ if (end <= at) continue;
20
+ if (start < at) {
21
+ const last = parts[parts.length - 1];
22
+ parts[parts.length - 1] = {
23
+ text: last.text + text.slice(at, end),
24
+ hit: true
25
+ };
26
+ } else {
27
+ if (start > at) parts.push({
28
+ text: text.slice(at, start),
29
+ hit: false
30
+ });
31
+ parts.push({
32
+ text: text.slice(start, end),
33
+ hit: true
34
+ });
35
+ }
36
+ at = end;
37
+ }
38
+ if (at < text.length) parts.push({
39
+ text: text.slice(at),
40
+ hit: false
41
+ });
42
+ return parts;
43
+ }
44
+ /**
45
+ * This function divides a text into parts at each copy of `find`, from the start, with no overlap. Each copy gives a
46
+ * part with `hit`. An empty `find` gives one part with no `hit`. The comparison is exact: the case and the
47
+ * normalization of the letters are significant.
48
+ *
49
+ * @param text - The text.
50
+ * @param find - The text to find.
51
+ * @returns The parts of the text, with no empty part.
52
+ */
53
+ function partsOf(text, find) {
54
+ if (find === "") return partsAt(text, []);
55
+ const ranges = [];
56
+ for (let at = text.indexOf(find); at >= 0; at = text.indexOf(find, at + find.length)) ranges.push([at, at + find.length]);
57
+ return partsAt(text, ranges);
58
+ }
59
+ export { partsAt, partsOf };
60
+
61
+ //# sourceMappingURL=parts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parts.js","names":[],"sources":["../src/parts.ts"],"sourcesContent":["/**\n * This module divides a text into marked parts: the parts at ranges, or the parts at each copy of a text. A page puts a\n * `mark` element on each part with `hit`, and a terminal puts a color on it.\n */\n\n/** A part of a text. A part with `hit` is in a range or is a copy of the text that the search found. */\nexport interface TextPart {\n readonly text: string;\n readonly hit: boolean;\n}\n\n/**\n * This function divides a text into parts at ranges of UTF-16 code units, `[start, end)`. Each range gives a part with\n * `hit`, and the text between the ranges gives parts with no `hit`. The function sorts the ranges, joins the ranges\n * that overlap, and clamps each range to the text. Two ranges that touch stay two parts. An empty range gives no part.\n *\n * The texts of the parts, in their order, make the text again.\n *\n * @param text - The text.\n * @param ranges - The ranges, as `[start, end)` in code units, in any order.\n * @returns The parts of the text, with no empty part.\n */\nexport function partsAt(text: string, ranges: readonly (readonly [start: number, end: number])[]): TextPart[] {\n const clamp = (n: number): number => Math.min(Math.max(Math.trunc(n), 0), text.length);\n const sorted = ranges\n .map(([start, end]) => [clamp(start), clamp(end)] as const)\n .filter(([start, end]) => end > start)\n .sort((a, b) => a[0] - b[0]);\n const parts: TextPart[] = [];\n let at = 0;\n for (const [start, end] of sorted) {\n if (end <= at) continue;\n if (start < at) {\n const last = parts[parts.length - 1]!;\n parts[parts.length - 1] = { text: last.text + text.slice(at, end), hit: true };\n } else {\n if (start > at) parts.push({ text: text.slice(at, start), hit: false });\n parts.push({ text: text.slice(start, end), hit: true });\n }\n at = end;\n }\n if (at < text.length) parts.push({ text: text.slice(at), hit: false });\n return parts;\n}\n\n/**\n * This function divides a text into parts at each copy of `find`, from the start, with no overlap. Each copy gives a\n * part with `hit`. An empty `find` gives one part with no `hit`. The comparison is exact: the case and the\n * normalization of the letters are significant.\n *\n * @param text - The text.\n * @param find - The text to find.\n * @returns The parts of the text, with no empty part.\n */\nexport function partsOf(text: string, find: string): TextPart[] {\n if (find === \"\") return partsAt(text, []);\n const ranges: (readonly [number, number])[] = [];\n for (let at = text.indexOf(find); at >= 0; at = text.indexOf(find, at + find.length)) ranges.push([at, at + find.length]);\n return partsAt(text, ranges);\n}\n"],"mappings":";;;;;;;;;;;;AAsBA,SAAgB,QAAQ,MAAc,QAAwE;CAC5G,MAAM,SAAS,MAAsB,KAAK,IAAI,KAAK,IAAI,KAAK,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,MAAM;CACrF,MAAM,SAAS,OACZ,KAAK,CAAC,OAAO,SAAS,CAAC,MAAM,KAAK,GAAG,MAAM,GAAG,CAAC,CAAU,CAAC,CAC1D,QAAQ,CAAC,OAAO,SAAS,MAAM,KAAK,CAAC,CACrC,MAAM,GAAG,MAAM,EAAE,KAAK,EAAE,EAAE;CAC7B,MAAM,QAAoB,CAAC;CAC3B,IAAI,KAAK;CACT,KAAK,MAAM,CAAC,OAAO,QAAQ,QAAQ;EACjC,IAAI,OAAO,IAAI;EACf,IAAI,QAAQ,IAAI;GACd,MAAM,OAAO,MAAM,MAAM,SAAS;GAClC,MAAM,MAAM,SAAS,KAAK;IAAE,MAAM,KAAK,OAAO,KAAK,MAAM,IAAI,GAAG;IAAG,KAAK;GAAK;EAC/E,OAAO;GACL,IAAI,QAAQ,IAAI,MAAM,KAAK;IAAE,MAAM,KAAK,MAAM,IAAI,KAAK;IAAG,KAAK;GAAM,CAAC;GACtE,MAAM,KAAK;IAAE,MAAM,KAAK,MAAM,OAAO,GAAG;IAAG,KAAK;GAAK,CAAC;EACxD;EACA,KAAK;CACP;CACA,IAAI,KAAK,KAAK,QAAQ,MAAM,KAAK;EAAE,MAAM,KAAK,MAAM,EAAE;EAAG,KAAK;CAAM,CAAC;CACrE,OAAO;AACT;;;;;;;;;;AAWA,SAAgB,QAAQ,MAAc,MAA0B;CAC9D,IAAI,SAAS,IAAI,OAAO,QAAQ,MAAM,CAAC,CAAC;CACxC,MAAM,SAAwC,CAAC;CAC/C,KAAK,IAAI,KAAK,KAAK,QAAQ,IAAI,GAAG,MAAM,GAAG,KAAK,KAAK,QAAQ,MAAM,KAAK,KAAK,MAAM,GAAG,OAAO,KAAK,CAAC,IAAI,KAAK,KAAK,MAAM,CAAC;CACxH,OAAO,QAAQ,MAAM,MAAM;AAC7B"}
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.6",
4
+ "version": "0.0.7",
5
5
  "description": "Plain text helpers: the escapes for HTML, attributes, patterns, Markdown and workflow commands, English word forms (countOf), a word search (wordSearch), the text of a thrown value (errorMessage), the removal of secrets (redactSecrets), and lengths in code units, code points and graphemes with truncation and padding (textLength, truncate, pad).",
6
6
  "keywords": [
7
7
  "text",
@@ -48,6 +48,10 @@
48
48
  "types": "./build/length.d.ts",
49
49
  "default": "./build/length.js"
50
50
  },
51
+ "./parts": {
52
+ "types": "./build/parts.d.ts",
53
+ "default": "./build/parts.js"
54
+ },
51
55
  "./package.json": "./package.json"
52
56
  },
53
57
  "files": [
package/src/parts.ts ADDED
@@ -0,0 +1,60 @@
1
+ /**
2
+ * This module divides a text into marked parts: the parts at ranges, or the parts at each copy of a text. A page puts a
3
+ * `mark` element on each part with `hit`, and a terminal puts a color on it.
4
+ */
5
+
6
+ /** A part of a text. A part with `hit` is in a range or is a copy of the text that the search found. */
7
+ export interface TextPart {
8
+ readonly text: string;
9
+ readonly hit: boolean;
10
+ }
11
+
12
+ /**
13
+ * This function divides a text into parts at ranges of UTF-16 code units, `[start, end)`. Each range gives a part with
14
+ * `hit`, and the text between the ranges gives parts with no `hit`. The function sorts the ranges, joins the ranges
15
+ * that overlap, and clamps each range to the text. Two ranges that touch stay two parts. An empty range gives no part.
16
+ *
17
+ * The texts of the parts, in their order, make the text again.
18
+ *
19
+ * @param text - The text.
20
+ * @param ranges - The ranges, as `[start, end)` in code units, in any order.
21
+ * @returns The parts of the text, with no empty part.
22
+ */
23
+ export function partsAt(text: string, ranges: readonly (readonly [start: number, end: number])[]): TextPart[] {
24
+ const clamp = (n: number): number => Math.min(Math.max(Math.trunc(n), 0), text.length);
25
+ const sorted = ranges
26
+ .map(([start, end]) => [clamp(start), clamp(end)] as const)
27
+ .filter(([start, end]) => end > start)
28
+ .sort((a, b) => a[0] - b[0]);
29
+ const parts: TextPart[] = [];
30
+ let at = 0;
31
+ for (const [start, end] of sorted) {
32
+ if (end <= at) continue;
33
+ if (start < at) {
34
+ const last = parts[parts.length - 1]!;
35
+ parts[parts.length - 1] = { text: last.text + text.slice(at, end), hit: true };
36
+ } else {
37
+ if (start > at) parts.push({ text: text.slice(at, start), hit: false });
38
+ parts.push({ text: text.slice(start, end), hit: true });
39
+ }
40
+ at = end;
41
+ }
42
+ if (at < text.length) parts.push({ text: text.slice(at), hit: false });
43
+ return parts;
44
+ }
45
+
46
+ /**
47
+ * This function divides a text into parts at each copy of `find`, from the start, with no overlap. Each copy gives a
48
+ * part with `hit`. An empty `find` gives one part with no `hit`. The comparison is exact: the case and the
49
+ * normalization of the letters are significant.
50
+ *
51
+ * @param text - The text.
52
+ * @param find - The text to find.
53
+ * @returns The parts of the text, with no empty part.
54
+ */
55
+ export function partsOf(text: string, find: string): TextPart[] {
56
+ if (find === "") return partsAt(text, []);
57
+ const ranges: (readonly [number, number])[] = [];
58
+ for (let at = text.indexOf(find); at >= 0; at = text.indexOf(find, at + find.length)) ranges.push([at, at + find.length]);
59
+ return partsAt(text, ranges);
60
+ }