@mark1russell7/text 0.0.12 → 0.0.13
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/build/length.d.ts +9 -4
- package/build/length.d.ts.map +1 -1
- package/build/length.js +21 -10
- package/build/length.js.map +1 -1
- package/package.json +1 -1
- package/src/length.ts +28 -14
package/build/length.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* This module has three functions for a text: its length in a unit, a cut to a length and padding to a width. The
|
|
3
3
|
* unit is a UTF-16 code unit, a Unicode code point or a grapheme. A grapheme is one character as a person sees it, for
|
|
4
|
-
* example a letter with an accent, a flag or an emoji sequence.
|
|
4
|
+
* example a letter with an accent, a flag or an emoji sequence. A cut can also keep each word whole.
|
|
5
5
|
*/
|
|
6
6
|
/**
|
|
7
7
|
* The unit of a length. `code-unit` is the unit of `String.prototype.length`. `code-point` is one Unicode code point,
|
|
@@ -9,10 +9,11 @@
|
|
|
9
9
|
*/
|
|
10
10
|
export type TextUnit = "code-unit" | "code-point" | "grapheme";
|
|
11
11
|
/**
|
|
12
|
-
* The smallest part of a text that a cut or a fill keeps whole.
|
|
13
|
-
*
|
|
12
|
+
* The smallest part of a text that a cut or a fill keeps whole. A `word` part is the text between two word boundaries
|
|
13
|
+
* of Unicode (UAX #29): a word, a run of white space or a punctuation mark. The function uses the larger of this part
|
|
14
|
+
* and the unit, so the unit `grapheme` keeps each grapheme whole with each boundary.
|
|
14
15
|
*/
|
|
15
|
-
export type TextBoundary = "code-point" | "grapheme";
|
|
16
|
+
export type TextBoundary = "code-point" | "grapheme" | "word";
|
|
16
17
|
/** The options of `truncate`. */
|
|
17
18
|
export interface TruncateOptions {
|
|
18
19
|
/** The unit of the length `max`. The default is `code-unit`. */
|
|
@@ -62,6 +63,10 @@ export declare function textLength(text: string, unit?: TextUnit): number;
|
|
|
62
63
|
* than `max`.
|
|
63
64
|
* - When the mark has more than `max` units, the result has no mark. It is the start of the text, with `max` units
|
|
64
65
|
* or fewer.
|
|
66
|
+
* - With `word`, the cut keeps each word whole. The white space and the punctuation at the end of the cut text go out
|
|
67
|
+
* before the mark. When no word fits, the cut keeps the graphemes that fit, as the boundary `grapheme` does.
|
|
68
|
+
* - The word boundaries come from `Intl.Segmenter` in the locale of the runtime. For a language with no spaces, for
|
|
69
|
+
* example Japanese or Thai, the segmenter uses a dictionary.
|
|
65
70
|
*
|
|
66
71
|
* @param text - The text to cut.
|
|
67
72
|
* @param max - The most units in the result. It is an integer of 0 or more.
|
package/build/length.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"length.d.ts","names":[],"sources":["../src/length.ts"],"mappings":";;;;;;;;;YAUY
|
|
1
|
+
{"version":3,"file":"length.d.ts","names":[],"sources":["../src/length.ts"],"mappings":";;;;;;;;;YAUY;;;;;;YAOA;;iBAGK;;WAEN,OAAO;;WAEP,WAAW;;WAEX;;;iBAIM;;WAEN,OAAO;;WAEP,WAAW;;WAEX;;;;;WAKA;;;;;;;;;;;;;;;wBAiEK,WAAW,cAAc,OAAM;;;;;;;;;;;;;;;;;;;;;;;;wBA4B/B,SAAS,cAAc,aAAa,UAAS;;;;;;;;;;;;;;;;;;wBA+B7C,IAAI,cAAc,eAAe,UAAS"}
|
package/build/length.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
// src/length.ts
|
|
2
|
-
|
|
2
|
+
const segmenters = {};
|
|
3
3
|
/** This function gives the parts of a text at a boundary, in their order. */
|
|
4
4
|
function* partsOf(text, boundary) {
|
|
5
5
|
if (boundary === "code-point") yield* text;
|
|
6
|
-
else for (const { segment } of (
|
|
6
|
+
else for (const { segment } of (segmenters[boundary] ??= new Intl.Segmenter(void 0, { granularity: boundary })).segment(text)) yield segment;
|
|
7
|
+
}
|
|
8
|
+
/** This function gives the count of units in a part of a text, with no check of the unit. */
|
|
9
|
+
function sizeOf(part, unit) {
|
|
10
|
+
if (unit === "code-unit") return part.length;
|
|
11
|
+
let count = 0;
|
|
12
|
+
for (const _ of partsOf(part, unit)) count += 1;
|
|
13
|
+
return count;
|
|
7
14
|
}
|
|
8
15
|
/** This function stops a bad option with a `RangeError` and its message. */
|
|
9
16
|
function check(good, message) {
|
|
@@ -16,8 +23,8 @@ function checkCount(value, name) {
|
|
|
16
23
|
/** This function stops a unit and a boundary that are not in their types, and gives the boundary to use. */
|
|
17
24
|
function boundaryOf(unit, boundary) {
|
|
18
25
|
check(unit === "code-unit" || unit === "code-point" || unit === "grapheme", `unit is ${String(unit)}, not code-unit, code-point or grapheme`);
|
|
19
|
-
check(boundary === "code-point" || boundary === "grapheme", `boundary is ${String(boundary)}, not code-point or
|
|
20
|
-
return unit === "grapheme" ? unit : boundary;
|
|
26
|
+
check(boundary === "code-point" || boundary === "grapheme" || boundary === "word", `boundary is ${String(boundary)}, not code-point, grapheme or word`);
|
|
27
|
+
return unit === "grapheme" && boundary === "code-point" ? unit : boundary;
|
|
21
28
|
}
|
|
22
29
|
/**
|
|
23
30
|
* This function gives the end of the longest start of a text that has `room` units or fewer. The end is at a boundary,
|
|
@@ -27,7 +34,7 @@ function endOf(text, room, unit, boundary) {
|
|
|
27
34
|
let size = 0;
|
|
28
35
|
let end = 0;
|
|
29
36
|
for (const part of partsOf(text, boundary)) {
|
|
30
|
-
size += unit
|
|
37
|
+
size += unit !== "code-unit" && unit === boundary ? 1 : sizeOf(part, unit);
|
|
31
38
|
if (size > room) break;
|
|
32
39
|
end += part.length;
|
|
33
40
|
}
|
|
@@ -48,10 +55,7 @@ function endOf(text, room, unit, boundary) {
|
|
|
48
55
|
*/
|
|
49
56
|
function textLength(text, unit = "code-unit") {
|
|
50
57
|
boundaryOf(unit, "code-point");
|
|
51
|
-
|
|
52
|
-
let count = 0;
|
|
53
|
-
for (const _ of partsOf(text, unit)) count += 1;
|
|
54
|
-
return count;
|
|
58
|
+
return sizeOf(text, unit);
|
|
55
59
|
}
|
|
56
60
|
/**
|
|
57
61
|
* This function cuts a text to a length, and puts a mark at the end of the cut text. The result has `max` units or
|
|
@@ -65,6 +69,10 @@ function textLength(text, unit = "code-unit") {
|
|
|
65
69
|
* than `max`.
|
|
66
70
|
* - When the mark has more than `max` units, the result has no mark. It is the start of the text, with `max` units
|
|
67
71
|
* or fewer.
|
|
72
|
+
* - With `word`, the cut keeps each word whole. The white space and the punctuation at the end of the cut text go out
|
|
73
|
+
* before the mark. When no word fits, the cut keeps the graphemes that fit, as the boundary `grapheme` does.
|
|
74
|
+
* - The word boundaries come from `Intl.Segmenter` in the locale of the runtime. For a language with no spaces, for
|
|
75
|
+
* example Japanese or Thai, the segmenter uses a dictionary.
|
|
68
76
|
*
|
|
69
77
|
* @param text - The text to cut.
|
|
70
78
|
* @param max - The most units in the result. It is an integer of 0 or more.
|
|
@@ -78,7 +86,10 @@ function truncate(text, max, options = {}) {
|
|
|
78
86
|
checkCount(max, "max");
|
|
79
87
|
if (text.length <= max || unit !== "code-unit" && endOf(text, max, unit, unit) === text.length) return text;
|
|
80
88
|
const end = textLength(mark, unit) <= max ? mark : "";
|
|
81
|
-
|
|
89
|
+
const room = max - textLength(end, unit);
|
|
90
|
+
const start = text.slice(0, endOf(text, room, unit, boundary));
|
|
91
|
+
if (boundary !== "word") return start + end;
|
|
92
|
+
return (start.replace(/[\s\p{P}]+$/u, "") || text.slice(0, endOf(text, room, unit, "grapheme"))) + end;
|
|
82
93
|
}
|
|
83
94
|
/**
|
|
84
95
|
* This function adds a fill to a text, at the start, at the end or at the two sides, to a width. The result has `width`
|
package/build/length.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"length.js","names":[],"sources":["../src/length.ts"],"sourcesContent":["/**\n * This module has three functions for a text: its length in a unit, a cut to a length and padding to a width. The\n * unit is a UTF-16 code unit, a Unicode code point or a grapheme. A grapheme is one character as a person sees it, for\n * example a letter with an accent, a flag or an emoji sequence.\n */\n\n/**\n * The unit of a length. `code-unit` is the unit of `String.prototype.length`. `code-point` is one Unicode code point,\n * so a surrogate pair is one unit. `grapheme` is one extended grapheme cluster of Unicode (UAX #29).\n */\nexport type TextUnit = \"code-unit\" | \"code-point\" | \"grapheme\";\n\n/**\n * The smallest part of a text that a cut or a fill keeps whole. The function uses the larger of this part and the unit,\n * so the unit `grapheme` keeps each grapheme whole with each boundary.\n */\nexport type TextBoundary = \"code-point\" | \"grapheme\";\n\n/** The options of `truncate`. */\nexport interface TruncateOptions {\n /** The unit of the length `max`. The default is `code-unit`. */\n readonly unit?: TextUnit;\n /** The part of the text that the cut keeps whole. The default is `grapheme`. */\n readonly boundary?: TextBoundary;\n /** The text at the end of a text that the function cuts. The default is the ellipsis \"…\" (U+2026). */\n readonly mark?: string;\n}\n\n/** The options of `pad`. */\nexport interface PadOptions {\n /** The unit of the width. The default is `code-unit`. */\n readonly unit?: TextUnit;\n /** The part of the fill that the function keeps whole. The default is `grapheme`. */\n readonly boundary?: TextBoundary;\n /** The text that the function adds. It can have more than one unit. The default is one space. */\n readonly fill?: string;\n /**\n * The side of the fill. `both` puts the half of the fill at each side. For an odd count, the end gets one more unit.\n * The default is `end`.\n */\n readonly side?: \"start\" | \"end\" | \"both\";\n}\n\nlet segmenter: Intl.Segmenter | undefined;\n\n/** This function gives the parts of a text at a boundary, in their order. */\nfunction* partsOf(text: string, boundary: TextBoundary): Generator<string> {\n if (boundary === \"code-point\") yield* text;\n else for (const { segment } of (segmenter ??= new Intl.Segmenter(undefined, { granularity: \"grapheme\" })).segment(text)) yield segment;\n}\n\n/** This function stops a bad option with a `RangeError` and its message. */\nfunction check(good: boolean, message: string): void {\n if (!good) throw new RangeError(message);\n}\n\n/** This function stops a count that is not an integer of 0 or more. */\nfunction checkCount(value: number, name: string): void {\n check(Number.isInteger(value) && value >= 0, `${name} is ${String(value)}, not an integer of 0 or more`);\n}\n\n/** This function stops a unit and a boundary that are not in their types, and gives the boundary to use. */\nfunction boundaryOf(unit: TextUnit, boundary: TextBoundary): TextBoundary {\n check(unit === \"code-unit\" || unit === \"code-point\" || unit === \"grapheme\", `unit is ${String(unit)}, not code-unit, code-point or grapheme`);\n check(boundary === \"code-point\" || boundary === \"grapheme\", `boundary is ${String(boundary)}, not code-point or grapheme`);\n return unit === \"grapheme\" ? unit : boundary;\n}\n\n/**\n * This function gives the end of the longest start of a text that has `room` units or fewer. The end is at a boundary,\n * as an index of code units.\n */\nfunction endOf(text: string, room: number, unit: TextUnit, boundary: TextBoundary): number {\n let size = 0;\n let end = 0;\n for (const part of partsOf(text, boundary)) {\n size += unit === \"code-unit\" ? part.length : unit === \"code-point\" ? [...part].length : 1;\n if (size > room) break;\n end += part.length;\n }\n return end;\n}\n\n/**\n * This function gives the length of a text in a unit.\n *\n * - `code-unit` gives `text.length`.\n * - `code-point` counts a surrogate pair as one unit, and a lone surrogate as one unit.\n * - `grapheme` counts the extended grapheme clusters of `Intl.Segmenter`. The rules of Unicode do not change with the\n * locale.\n *\n * @param text - The text.\n * @param unit - The unit of the length. The default is `code-unit`.\n * @returns The count of units in the text.\n * @throws RangeError for a unit that is not in the type `TextUnit`.\n */\nexport function textLength(text: string, unit: TextUnit = \"code-unit\"): number {\n boundaryOf(unit, \"code-point\");\n if (unit === \"code-unit\") return text.length;\n let count = 0;\n for (const _ of partsOf(text, unit)) count += 1;\n return count;\n}\n\n/**\n * This function cuts a text to a length, and puts a mark at the end of the cut text. The result has `max` units or\n * fewer, with the mark.\n *\n * - A text with `max` units or fewer comes back as it is, with no mark.\n * - The cut keeps each part of `boundary` whole. The default part is the grapheme, so the cut does not break a flag,\n * an emoji sequence or a letter with a combining mark. With `code-point`, the cut keeps only each surrogate pair\n * whole.\n * - A part that does not fit in the length goes out with all the parts after it. Thus the result can be shorter\n * than `max`.\n * - When the mark has more than `max` units, the result has no mark. It is the start of the text, with `max` units\n * or fewer.\n *\n * @param text - The text to cut.\n * @param max - The most units in the result. It is an integer of 0 or more.\n * @param options - The unit, the boundary and the mark.\n * @returns The text, or the start of the text with the mark at the end.\n * @throws RangeError for a `max` that is not an integer of 0 or more, and for a bad unit or boundary.\n */\nexport function truncate(text: string, max: number, options: TruncateOptions = {}): string {\n const { unit = \"code-unit\", mark = \"…\" } = options;\n const boundary = boundaryOf(unit, options.boundary ?? \"grapheme\");\n checkCount(max, \"max\");\n // The count of code units is the largest count. Thus a text with `max` code units or fewer fits in each unit.\n if (text.length <= max || (unit !== \"code-unit\" && endOf(text, max, unit, unit) === text.length)) return text;\n const end = textLength(mark, unit) <= max ? mark : \"\";\n // A grapheme of the text can join a grapheme of the mark. A join gives fewer graphemes, never more (UAX #29).\n return text.slice(0, endOf(text, max - textLength(end, unit), unit, boundary)) + end;\n}\n\n/**\n * This function adds a fill to a text, at the start, at the end or at the two sides, to a width. The result has `width`\n * units or fewer.\n *\n * - A text with `width` units or more comes back as it is. The function does not cut a text.\n * - The fill goes again and again. The last copy of the fill can be a part of the fill, as in\n * `String.prototype.padEnd`. That part keeps each part of `boundary` whole, so the function does not add a part of a\n * grapheme by default.\n * - When the next part of the fill does not fit, the function stops. Thus the result can be shorter than `width`.\n *\n * @param text - The text.\n * @param width - The length of the result with the fill. It is an integer of 0 or more.\n * @param options - The unit, the boundary, the fill and the side.\n * @returns The text with the fill.\n * @throws RangeError for a `width` that is not an integer of 0 or more, for an empty fill, and for a bad unit, boundary\n * or side.\n */\nexport function pad(text: string, width: number, options: PadOptions = {}): string {\n const { unit = \"code-unit\", fill = \" \", side = \"end\" } = options;\n const boundary = boundaryOf(unit, options.boundary ?? \"grapheme\");\n checkCount(width, \"width\");\n check(side === \"start\" || side === \"end\" || side === \"both\", `side is ${String(side)}, not start, end or both`);\n const size = textLength(fill, unit);\n check(size > 0, \"fill is an empty text\");\n const missing = width - textLength(text, unit);\n if (missing <= 0) return text;\n const fillOf = (count: number): string => {\n const copies = Math.floor(count / size);\n return fill.repeat(copies) + fill.slice(0, endOf(fill, count - copies * size, unit, boundary));\n };\n const start = side === \"start\" ? missing : side === \"end\" ? 0 : Math.floor(missing / 2);\n return fillOf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in a unit, a cut to a length and padding to a width. The\n * unit is a UTF-16 code unit, a Unicode code point or a grapheme. A grapheme is one character as a person sees it, for\n * example a letter with an accent, a flag or an emoji sequence. A cut can also keep each word whole.\n */\n\n/**\n * The unit of a length. `code-unit` is the unit of `String.prototype.length`. `code-point` is one Unicode code point,\n * so a surrogate pair is one unit. `grapheme` is one extended grapheme cluster of Unicode (UAX #29).\n */\nexport type TextUnit = \"code-unit\" | \"code-point\" | \"grapheme\";\n\n/**\n * The smallest part of a text that a cut or a fill keeps whole. A `word` part is the text between two word boundaries\n * of Unicode (UAX #29): a word, a run of white space or a punctuation mark. The function uses the larger of this part\n * and the unit, so the unit `grapheme` keeps each grapheme whole with each boundary.\n */\nexport type TextBoundary = \"code-point\" | \"grapheme\" | \"word\";\n\n/** The options of `truncate`. */\nexport interface TruncateOptions {\n /** The unit of the length `max`. The default is `code-unit`. */\n readonly unit?: TextUnit;\n /** The part of the text that the cut keeps whole. The default is `grapheme`. */\n readonly boundary?: TextBoundary;\n /** The text at the end of a text that the function cuts. The default is the ellipsis \"…\" (U+2026). */\n readonly mark?: string;\n}\n\n/** The options of `pad`. */\nexport interface PadOptions {\n /** The unit of the width. The default is `code-unit`. */\n readonly unit?: TextUnit;\n /** The part of the fill that the function keeps whole. The default is `grapheme`. */\n readonly boundary?: TextBoundary;\n /** The text that the function adds. It can have more than one unit. The default is one space. */\n readonly fill?: string;\n /**\n * The side of the fill. `both` puts the half of the fill at each side. For an odd count, the end gets one more unit.\n * The default is `end`.\n */\n readonly side?: \"start\" | \"end\" | \"both\";\n}\n\nconst segmenters: { grapheme?: Intl.Segmenter; word?: Intl.Segmenter } = {};\n\n/** This function gives the parts of a text at a boundary, in their order. */\nfunction* partsOf(text: string, boundary: TextBoundary): Generator<string> {\n if (boundary === \"code-point\") yield* text;\n else for (const { segment } of (segmenters[boundary] ??= new Intl.Segmenter(undefined, { granularity: boundary })).segment(text)) yield segment;\n}\n\n/** This function gives the count of units in a part of a text, with no check of the unit. */\nfunction sizeOf(part: string, unit: TextUnit): number {\n if (unit === \"code-unit\") return part.length;\n let count = 0;\n for (const _ of partsOf(part, unit)) count += 1;\n return count;\n}\n\n/** This function stops a bad option with a `RangeError` and its message. */\nfunction check(good: boolean, message: string): void {\n if (!good) throw new RangeError(message);\n}\n\n/** This function stops a count that is not an integer of 0 or more. */\nfunction checkCount(value: number, name: string): void {\n check(Number.isInteger(value) && value >= 0, `${name} is ${String(value)}, not an integer of 0 or more`);\n}\n\n/** This function stops a unit and a boundary that are not in their types, and gives the boundary to use. */\nfunction boundaryOf(unit: TextUnit, boundary: TextBoundary): TextBoundary {\n check(unit === \"code-unit\" || unit === \"code-point\" || unit === \"grapheme\", `unit is ${String(unit)}, not code-unit, code-point or grapheme`);\n check(boundary === \"code-point\" || boundary === \"grapheme\" || boundary === \"word\", `boundary is ${String(boundary)}, not code-point, grapheme or word`);\n return unit === \"grapheme\" && boundary === \"code-point\" ? unit : boundary;\n}\n\n/**\n * This function gives the end of the longest start of a text that has `room` units or fewer. The end is at a boundary,\n * as an index of code units.\n */\nfunction endOf(text: string, room: number, unit: TextUnit, boundary: TextBoundary): number {\n let size = 0;\n let end = 0;\n for (const part of partsOf(text, boundary)) {\n // A part of the boundary of the unit has one unit. A word part has one or more units.\n size += unit !== \"code-unit\" && unit === boundary ? 1 : sizeOf(part, unit);\n if (size > room) break;\n end += part.length;\n }\n return end;\n}\n\n/**\n * This function gives the length of a text in a unit.\n *\n * - `code-unit` gives `text.length`.\n * - `code-point` counts a surrogate pair as one unit, and a lone surrogate as one unit.\n * - `grapheme` counts the extended grapheme clusters of `Intl.Segmenter`. The rules of Unicode do not change with the\n * locale.\n *\n * @param text - The text.\n * @param unit - The unit of the length. The default is `code-unit`.\n * @returns The count of units in the text.\n * @throws RangeError for a unit that is not in the type `TextUnit`.\n */\nexport function textLength(text: string, unit: TextUnit = \"code-unit\"): number {\n boundaryOf(unit, \"code-point\");\n return sizeOf(text, unit);\n}\n\n/**\n * This function cuts a text to a length, and puts a mark at the end of the cut text. The result has `max` units or\n * fewer, with the mark.\n *\n * - A text with `max` units or fewer comes back as it is, with no mark.\n * - The cut keeps each part of `boundary` whole. The default part is the grapheme, so the cut does not break a flag,\n * an emoji sequence or a letter with a combining mark. With `code-point`, the cut keeps only each surrogate pair\n * whole.\n * - A part that does not fit in the length goes out with all the parts after it. Thus the result can be shorter\n * than `max`.\n * - When the mark has more than `max` units, the result has no mark. It is the start of the text, with `max` units\n * or fewer.\n * - With `word`, the cut keeps each word whole. The white space and the punctuation at the end of the cut text go out\n * before the mark. When no word fits, the cut keeps the graphemes that fit, as the boundary `grapheme` does.\n * - The word boundaries come from `Intl.Segmenter` in the locale of the runtime. For a language with no spaces, for\n * example Japanese or Thai, the segmenter uses a dictionary.\n *\n * @param text - The text to cut.\n * @param max - The most units in the result. It is an integer of 0 or more.\n * @param options - The unit, the boundary and the mark.\n * @returns The text, or the start of the text with the mark at the end.\n * @throws RangeError for a `max` that is not an integer of 0 or more, and for a bad unit or boundary.\n */\nexport function truncate(text: string, max: number, options: TruncateOptions = {}): string {\n const { unit = \"code-unit\", mark = \"…\" } = options;\n const boundary = boundaryOf(unit, options.boundary ?? \"grapheme\");\n checkCount(max, \"max\");\n // The count of code units is the largest count. Thus a text with `max` code units or fewer fits in each unit.\n if (text.length <= max || (unit !== \"code-unit\" && endOf(text, max, unit, unit) === text.length)) return text;\n const end = textLength(mark, unit) <= max ? mark : \"\";\n const room = max - textLength(end, unit);\n const start = text.slice(0, endOf(text, room, unit, boundary));\n // A grapheme of the text can join a grapheme of the mark. A join gives fewer graphemes, never more (UAX #29).\n if (boundary !== \"word\") return start + end;\n return (start.replace(/[\\s\\p{P}]+$/u, \"\") || text.slice(0, endOf(text, room, unit, \"grapheme\"))) + end;\n}\n\n/**\n * This function adds a fill to a text, at the start, at the end or at the two sides, to a width. The result has `width`\n * units or fewer.\n *\n * - A text with `width` units or more comes back as it is. The function does not cut a text.\n * - The fill goes again and again. The last copy of the fill can be a part of the fill, as in\n * `String.prototype.padEnd`. That part keeps each part of `boundary` whole, so the function does not add a part of a\n * grapheme by default.\n * - When the next part of the fill does not fit, the function stops. Thus the result can be shorter than `width`.\n *\n * @param text - The text.\n * @param width - The length of the result with the fill. It is an integer of 0 or more.\n * @param options - The unit, the boundary, the fill and the side.\n * @returns The text with the fill.\n * @throws RangeError for a `width` that is not an integer of 0 or more, for an empty fill, and for a bad unit, boundary\n * or side.\n */\nexport function pad(text: string, width: number, options: PadOptions = {}): string {\n const { unit = \"code-unit\", fill = \" \", side = \"end\" } = options;\n const boundary = boundaryOf(unit, options.boundary ?? \"grapheme\");\n checkCount(width, \"width\");\n check(side === \"start\" || side === \"end\" || side === \"both\", `side is ${String(side)}, not start, end or both`);\n const size = textLength(fill, unit);\n check(size > 0, \"fill is an empty text\");\n const missing = width - textLength(text, unit);\n if (missing <= 0) return text;\n const fillOf = (count: number): string => {\n const copies = Math.floor(count / size);\n return fill.repeat(copies) + fill.slice(0, endOf(fill, count - copies * size, unit, boundary));\n };\n const start = side === \"start\" ? missing : side === \"end\" ? 0 : Math.floor(missing / 2);\n return fillOf(start) + text + fillOf(missing - start);\n}\n"],"mappings":";AA4CA,MAAM,aAAmE,CAAC;;AAG1E,UAAU,QAAQ,MAAc,UAA2C;CACzE,IAAI,aAAa,cAAc,OAAO;MACjC,KAAK,MAAM,EAAE,cAAc,WAAW,cAAc,IAAI,KAAK,UAAU,KAAA,GAAW,EAAE,aAAa,SAAS,CAAC,EAAA,CAAG,QAAQ,IAAI,GAAG,MAAM;AAC1I;;AAGA,SAAS,OAAO,MAAc,MAAwB;CACpD,IAAI,SAAS,aAAa,OAAO,KAAK;CACtC,IAAI,QAAQ;CACZ,KAAK,MAAM,KAAK,QAAQ,MAAM,IAAI,GAAG,SAAS;CAC9C,OAAO;AACT;;AAGA,SAAS,MAAM,MAAe,SAAuB;CACnD,IAAI,CAAC,MAAM,MAAM,IAAI,WAAW,OAAO;AACzC;;AAGA,SAAS,WAAW,OAAe,MAA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QAAQ;CACxC,QAAQ,MAAM,QAAQ,gBAAgB,EAAE,KAAK,KAAK,MAAM,GAAG,MAAM,MAAM,MAAM,MAAM,UAAU,CAAC,KAAK;AACrG;;;;;;;;;;;;;;;;;;AAmBA,SAAgB,IAAI,MAAc,OAAe,UAAsB,CAAC,GAAW;CACjF,MAAM,EAAE,OAAO,aAAa,OAAO,KAAK,OAAO,UAAU;CACzD,MAAM,WAAW,WAAW,MAAM,QAAQ,YAAY,UAAU;CAChE,WAAW,OAAO,OAAO;CACzB,MAAM,SAAS,WAAW,SAAS,SAAS,SAAS,QAAQ,WAAW,OAAO,IAAI,EAAE,yBAAyB;CAC9G,MAAM,OAAO,WAAW,MAAM,IAAI;CAClC,MAAM,OAAO,GAAG,uBAAuB;CACvC,MAAM,UAAU,QAAQ,WAAW,MAAM,IAAI;CAC7C,IAAI,WAAW,GAAG,OAAO;CACzB,MAAM,UAAU,UAA0B;EACxC,MAAM,SAAS,KAAK,MAAM,QAAQ,IAAI;EACtC,OAAO,KAAK,OAAO,MAAM,IAAI,KAAK,MAAM,GAAG,MAAM,MAAM,QAAQ,SAAS,MAAM,MAAM,QAAQ,CAAC;CAC/F;CACA,MAAM,QAAQ,SAAS,UAAU,UAAU,SAAS,QAAQ,IAAI,KAAK,MAAM,UAAU,CAAC;CACtF,OAAO,OAAO,KAAK,IAAI,OAAO,OAAO,UAAU,KAAK;AACtD"}
|
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.13",
|
|
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).",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"text",
|
package/src/length.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* This module has three functions for a text: its length in a unit, a cut to a length and padding to a width. The
|
|
3
3
|
* unit is a UTF-16 code unit, a Unicode code point or a grapheme. A grapheme is one character as a person sees it, for
|
|
4
|
-
* example a letter with an accent, a flag or an emoji sequence.
|
|
4
|
+
* example a letter with an accent, a flag or an emoji sequence. A cut can also keep each word whole.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -11,10 +11,11 @@
|
|
|
11
11
|
export type TextUnit = "code-unit" | "code-point" | "grapheme";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
* The smallest part of a text that a cut or a fill keeps whole.
|
|
15
|
-
*
|
|
14
|
+
* The smallest part of a text that a cut or a fill keeps whole. A `word` part is the text between two word boundaries
|
|
15
|
+
* of Unicode (UAX #29): a word, a run of white space or a punctuation mark. The function uses the larger of this part
|
|
16
|
+
* and the unit, so the unit `grapheme` keeps each grapheme whole with each boundary.
|
|
16
17
|
*/
|
|
17
|
-
export type TextBoundary = "code-point" | "grapheme";
|
|
18
|
+
export type TextBoundary = "code-point" | "grapheme" | "word";
|
|
18
19
|
|
|
19
20
|
/** The options of `truncate`. */
|
|
20
21
|
export interface TruncateOptions {
|
|
@@ -41,12 +42,20 @@ export interface PadOptions {
|
|
|
41
42
|
readonly side?: "start" | "end" | "both";
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
|
|
45
|
+
const segmenters: { grapheme?: Intl.Segmenter; word?: Intl.Segmenter } = {};
|
|
45
46
|
|
|
46
47
|
/** This function gives the parts of a text at a boundary, in their order. */
|
|
47
48
|
function* partsOf(text: string, boundary: TextBoundary): Generator<string> {
|
|
48
49
|
if (boundary === "code-point") yield* text;
|
|
49
|
-
else for (const { segment } of (
|
|
50
|
+
else for (const { segment } of (segmenters[boundary] ??= new Intl.Segmenter(undefined, { granularity: boundary })).segment(text)) yield segment;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** This function gives the count of units in a part of a text, with no check of the unit. */
|
|
54
|
+
function sizeOf(part: string, unit: TextUnit): number {
|
|
55
|
+
if (unit === "code-unit") return part.length;
|
|
56
|
+
let count = 0;
|
|
57
|
+
for (const _ of partsOf(part, unit)) count += 1;
|
|
58
|
+
return count;
|
|
50
59
|
}
|
|
51
60
|
|
|
52
61
|
/** This function stops a bad option with a `RangeError` and its message. */
|
|
@@ -62,8 +71,8 @@ function checkCount(value: number, name: string): void {
|
|
|
62
71
|
/** This function stops a unit and a boundary that are not in their types, and gives the boundary to use. */
|
|
63
72
|
function boundaryOf(unit: TextUnit, boundary: TextBoundary): TextBoundary {
|
|
64
73
|
check(unit === "code-unit" || unit === "code-point" || unit === "grapheme", `unit is ${String(unit)}, not code-unit, code-point or grapheme`);
|
|
65
|
-
check(boundary === "code-point" || boundary === "grapheme", `boundary is ${String(boundary)}, not code-point or
|
|
66
|
-
return unit === "grapheme" ? unit : boundary;
|
|
74
|
+
check(boundary === "code-point" || boundary === "grapheme" || boundary === "word", `boundary is ${String(boundary)}, not code-point, grapheme or word`);
|
|
75
|
+
return unit === "grapheme" && boundary === "code-point" ? unit : boundary;
|
|
67
76
|
}
|
|
68
77
|
|
|
69
78
|
/**
|
|
@@ -74,7 +83,8 @@ function endOf(text: string, room: number, unit: TextUnit, boundary: TextBoundar
|
|
|
74
83
|
let size = 0;
|
|
75
84
|
let end = 0;
|
|
76
85
|
for (const part of partsOf(text, boundary)) {
|
|
77
|
-
|
|
86
|
+
// A part of the boundary of the unit has one unit. A word part has one or more units.
|
|
87
|
+
size += unit !== "code-unit" && unit === boundary ? 1 : sizeOf(part, unit);
|
|
78
88
|
if (size > room) break;
|
|
79
89
|
end += part.length;
|
|
80
90
|
}
|
|
@@ -96,10 +106,7 @@ function endOf(text: string, room: number, unit: TextUnit, boundary: TextBoundar
|
|
|
96
106
|
*/
|
|
97
107
|
export function textLength(text: string, unit: TextUnit = "code-unit"): number {
|
|
98
108
|
boundaryOf(unit, "code-point");
|
|
99
|
-
|
|
100
|
-
let count = 0;
|
|
101
|
-
for (const _ of partsOf(text, unit)) count += 1;
|
|
102
|
-
return count;
|
|
109
|
+
return sizeOf(text, unit);
|
|
103
110
|
}
|
|
104
111
|
|
|
105
112
|
/**
|
|
@@ -114,6 +121,10 @@ export function textLength(text: string, unit: TextUnit = "code-unit"): number {
|
|
|
114
121
|
* than `max`.
|
|
115
122
|
* - When the mark has more than `max` units, the result has no mark. It is the start of the text, with `max` units
|
|
116
123
|
* or fewer.
|
|
124
|
+
* - With `word`, the cut keeps each word whole. The white space and the punctuation at the end of the cut text go out
|
|
125
|
+
* before the mark. When no word fits, the cut keeps the graphemes that fit, as the boundary `grapheme` does.
|
|
126
|
+
* - The word boundaries come from `Intl.Segmenter` in the locale of the runtime. For a language with no spaces, for
|
|
127
|
+
* example Japanese or Thai, the segmenter uses a dictionary.
|
|
117
128
|
*
|
|
118
129
|
* @param text - The text to cut.
|
|
119
130
|
* @param max - The most units in the result. It is an integer of 0 or more.
|
|
@@ -128,8 +139,11 @@ export function truncate(text: string, max: number, options: TruncateOptions = {
|
|
|
128
139
|
// The count of code units is the largest count. Thus a text with `max` code units or fewer fits in each unit.
|
|
129
140
|
if (text.length <= max || (unit !== "code-unit" && endOf(text, max, unit, unit) === text.length)) return text;
|
|
130
141
|
const end = textLength(mark, unit) <= max ? mark : "";
|
|
142
|
+
const room = max - textLength(end, unit);
|
|
143
|
+
const start = text.slice(0, endOf(text, room, unit, boundary));
|
|
131
144
|
// A grapheme of the text can join a grapheme of the mark. A join gives fewer graphemes, never more (UAX #29).
|
|
132
|
-
|
|
145
|
+
if (boundary !== "word") return start + end;
|
|
146
|
+
return (start.replace(/[\s\p{P}]+$/u, "") || text.slice(0, endOf(text, room, unit, "grapheme"))) + end;
|
|
133
147
|
}
|
|
134
148
|
|
|
135
149
|
/**
|