@kvndy/attributed-string 0.0.0-stage → 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kevin Doughty
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,69 @@
1
- # Temporary Holding Version
1
+ # @kvndy/attributed-string
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
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
+ ```
@@ -0,0 +1,7 @@
1
+ import type { AttributeRun } from "./types.js";
2
+ export type { AttributedString } from "./types.js";
3
+ /** Transitional adapter for the existing component/DOM span makers.
4
+ * Offsets are UTF-16 and half-open. Later runs override individual properties.
5
+ * Invalid ranges throw; input order need not be sorted. No attributes are shifted on edits.
6
+ */
7
+ export declare function iterateAttributes<T>(text: string, runs: readonly AttributeRun[], make: (text: string, foreground: string, background: string, start: number, end: number, className: string) => T, start?: number, end?: number, backtrack?: boolean, direction?: number): Generator<T, void>;
package/dist/main.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { iterateAttributes } from "./attributed-string.js";
2
+ export type { AttributeRun, AttributedString, TextAttributes, TextRange } from "./types.js";
package/dist/main.js ADDED
@@ -0,0 +1,49 @@
1
+ /** Transitional adapter for the existing component/DOM span makers.
2
+ * Offsets are UTF-16 and half-open. Later runs override individual properties.
3
+ * Invalid ranges throw; input order need not be sorted. No attributes are shifted on edits.
4
+ */
5
+ function* iterateAttributes(text, runs, make, start = 0, end = text.length, backtrack = false, direction = 1) {
6
+ start = Math.max(0, Math.min(text.length, start));
7
+ end = Math.max(start, Math.min(text.length, end));
8
+ if (backtrack && start > 0)
9
+ start = text.lastIndexOf("\n", start - 1) + 1;
10
+ const boundaries = new Set([start, end]);
11
+ const active = runs.filter(run => {
12
+ const { start: a, end: b } = run.range;
13
+ if (!Number.isInteger(a) || !Number.isInteger(b) || a < 0 || b < a || b > text.length) {
14
+ throw new RangeError(`Invalid attribute range [${a}, ${b}) for text length ${text.length}`);
15
+ }
16
+ if (a >= end || b <= start || a === b)
17
+ return false;
18
+ boundaries.add(Math.max(start, a));
19
+ boundaries.add(Math.min(end, b));
20
+ return true;
21
+ });
22
+ const points = [...boundaries].sort((a, b) => a - b);
23
+ const segments = [];
24
+ for (let i = 0; i < points.length - 1; i++) {
25
+ const a = points[i], b = points[i + 1];
26
+ if (a === b)
27
+ continue;
28
+ const attributes = {};
29
+ for (const run of active)
30
+ if (run.range.start <= a && run.range.end >= b) {
31
+ if (run.attributes.foreground !== undefined)
32
+ attributes.foreground = run.attributes.foreground;
33
+ if (run.attributes.background !== undefined)
34
+ attributes.background = run.attributes.background;
35
+ }
36
+ segments.push({ start: a, end: b, attributes });
37
+ }
38
+ if (direction < 0)
39
+ segments.reverse();
40
+ for (const segment of segments) {
41
+ let content = text.slice(segment.start, segment.end);
42
+ // Preserve the reference renderer's trailing-newline compensation for now.
43
+ if (segment.end === text.length && content.endsWith("\n"))
44
+ content += " ";
45
+ yield make(content, segment.attributes.foreground ?? "", segment.attributes.background ?? "", segment.start, segment.end, "");
46
+ }
47
+ }
48
+
49
+ export { iterateAttributes };
@@ -0,0 +1,16 @@
1
+ export interface TextRange {
2
+ readonly start: number;
3
+ readonly end: number;
4
+ }
5
+ export interface TextAttributes {
6
+ readonly foreground?: string;
7
+ readonly background?: string;
8
+ }
9
+ export interface AttributeRun {
10
+ readonly range: TextRange;
11
+ readonly attributes: TextAttributes;
12
+ }
13
+ export interface AttributedString {
14
+ readonly text: string;
15
+ readonly runs: readonly AttributeRun[];
16
+ }
package/package.json CHANGED
@@ -1,6 +1,31 @@
1
1
  {
2
- "name": "@kvndy/attributed-string",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@kvndy/attributed-string",
3
+ "version": "0.0.2",
4
+ "description": "String attributes for monospace text and background colors",
5
+ "license": "MIT",
6
+ "author": "Kevin Doughty",
7
+ "type": "module",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://gitlab.com/kevindoughty/attributed-string.git",
11
+ "directory": "attributed-string"
12
+ },
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "main": "./dist/main.js",
17
+ "types": "./dist/main.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/main.d.ts",
21
+ "import": "./dist/main.js"
22
+ }
23
+ },
24
+ "scripts": {
25
+ "build": "tsc && rollup -c"
26
+ },
27
+ "devDependencies": {
28
+ "rollup": "^4.0.0",
29
+ "typescript": "^7.0.2"
30
+ }
31
+ }