@kvndy/attributed-string 0.0.1 → 0.0.2
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 +69 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# @kvndy/attributed-string
|
|
2
|
+
|
|
3
|
+
## Usage
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install --save-exact @kvndy/attributed-string@0.0.1
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { iterateAttributes, type AttributedString } from "@kvndy/attributed-string";
|
|
11
|
+
|
|
12
|
+
const value: AttributedString = {
|
|
13
|
+
text: "Hello world",
|
|
14
|
+
runs: [
|
|
15
|
+
{ range: { start: 0, end: 5 }, attributes: { foreground: "royalblue" } },
|
|
16
|
+
],
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const segments = [...iterateAttributes(value.text, value.runs, (text, foreground, background) => ({
|
|
20
|
+
text,
|
|
21
|
+
foreground,
|
|
22
|
+
background,
|
|
23
|
+
}))];
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Offsets are UTF-16 code units. Range ends are exclusive. Overlapping runs are
|
|
27
|
+
applied in array order, with each later color overriding that property.
|
|
28
|
+
Invalid ranges throw a `RangeError`.
|
|
29
|
+
|
|
30
|
+
## Type definitions
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
export interface TextRange {
|
|
34
|
+
readonly start: number;
|
|
35
|
+
readonly end: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface TextAttributes {
|
|
39
|
+
readonly foreground?: string;
|
|
40
|
+
readonly background?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface AttributeRun {
|
|
44
|
+
readonly range: TextRange;
|
|
45
|
+
readonly attributes: TextAttributes;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface AttributedString {
|
|
49
|
+
readonly text: string;
|
|
50
|
+
readonly runs: readonly AttributeRun[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export declare function iterateAttributes<T>(
|
|
54
|
+
text: string,
|
|
55
|
+
runs: readonly AttributeRun[],
|
|
56
|
+
make: (
|
|
57
|
+
text: string,
|
|
58
|
+
foreground: string,
|
|
59
|
+
background: string,
|
|
60
|
+
start: number,
|
|
61
|
+
end: number,
|
|
62
|
+
className: string,
|
|
63
|
+
) => T,
|
|
64
|
+
start?: number,
|
|
65
|
+
end?: number,
|
|
66
|
+
backtrack?: boolean,
|
|
67
|
+
direction?: number,
|
|
68
|
+
): Generator<T, void>;
|
|
69
|
+
```
|