@hardlydifficult/text 1.0.4 → 1.0.6
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 +51 -1
- package/dist/convertFormat.d.ts +25 -0
- package/dist/convertFormat.d.ts.map +1 -0
- package/dist/convertFormat.js +52 -0
- package/dist/convertFormat.js.map +1 -0
- package/dist/formatWithLineNumbers.d.ts +20 -0
- package/dist/formatWithLineNumbers.d.ts.map +1 -0
- package/dist/formatWithLineNumbers.js +30 -0
- package/dist/formatWithLineNumbers.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @hardlydifficult/text
|
|
2
2
|
|
|
3
|
-
Text utilities for error formatting, template replacement, and
|
|
3
|
+
Text utilities for error formatting, template replacement, chunking, and line numbering.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -99,3 +99,53 @@ formatDuration(3_600_000); // "1h"
|
|
|
99
99
|
formatDuration(500); // "<1s"
|
|
100
100
|
formatDuration(90_000_000); // "1d 1h"
|
|
101
101
|
```
|
|
102
|
+
|
|
103
|
+
### `convertFormat(content: string, to: TextFormat): string`
|
|
104
|
+
|
|
105
|
+
Convert between JSON and YAML string formats. Auto-detects the input format and serializes to the requested output format.
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { convertFormat } from "@hardlydifficult/text";
|
|
109
|
+
|
|
110
|
+
// JSON to YAML
|
|
111
|
+
convertFormat('{"name": "Alice", "age": 30}', "yaml");
|
|
112
|
+
// name: Alice
|
|
113
|
+
// age: 30
|
|
114
|
+
|
|
115
|
+
// YAML to JSON
|
|
116
|
+
convertFormat("name: Alice\nage: 30", "json");
|
|
117
|
+
// {
|
|
118
|
+
// "name": "Alice",
|
|
119
|
+
// "age": 30
|
|
120
|
+
// }
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
The function tries to parse as JSON first, then falls back to YAML. Returns pretty-printed JSON with 2-space indent or clean YAML. Throws a descriptive error if the input is neither valid JSON nor YAML.
|
|
124
|
+
|
|
125
|
+
### `formatWithLineNumbers(content: string, startLine?: number): string`
|
|
126
|
+
|
|
127
|
+
Format text content with right-aligned line numbers. Each line is prefixed with its line number, padded to align properly based on the maximum line number.
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
import { formatWithLineNumbers } from "@hardlydifficult/text";
|
|
131
|
+
|
|
132
|
+
// Default: starts at line 1
|
|
133
|
+
formatWithLineNumbers("foo\nbar\nbaz");
|
|
134
|
+
// 1: foo
|
|
135
|
+
// 2: bar
|
|
136
|
+
// 3: baz
|
|
137
|
+
|
|
138
|
+
// Custom starting line for displaying file ranges
|
|
139
|
+
formatWithLineNumbers("hello\nworld", 10);
|
|
140
|
+
// 10: hello
|
|
141
|
+
// 11: world
|
|
142
|
+
|
|
143
|
+
// Proper alignment for larger line numbers
|
|
144
|
+
formatWithLineNumbers("line 1\nline 2\n...\nline 100", 98);
|
|
145
|
+
// 98: line 1
|
|
146
|
+
// 99: line 2
|
|
147
|
+
// 100: ...
|
|
148
|
+
// 101: line 100
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
This is useful for displaying code snippets, file contents, or log output with line numbers for reference.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type TextFormat = "json" | "yaml";
|
|
2
|
+
/**
|
|
3
|
+
* Convert between JSON and YAML string formats.
|
|
4
|
+
*
|
|
5
|
+
* Auto-detects the input format by trying JSON.parse first, then
|
|
6
|
+
* falling back to YAML parsing. Returns pretty-printed JSON (2-space indent)
|
|
7
|
+
* or clean YAML based on the `to` parameter.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // JSON to YAML
|
|
12
|
+
* convertFormat('{"name": "Alice", "age": 30}', "yaml")
|
|
13
|
+
* // name: Alice
|
|
14
|
+
* // age: 30
|
|
15
|
+
*
|
|
16
|
+
* // YAML to JSON
|
|
17
|
+
* convertFormat("name: Alice\nage: 30", "json")
|
|
18
|
+
* // {
|
|
19
|
+
* // "name": "Alice",
|
|
20
|
+
* // "age": 30
|
|
21
|
+
* // }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare function convertFormat(content: string, to: TextFormat): string;
|
|
25
|
+
//# sourceMappingURL=convertFormat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convertFormat.d.ts","sourceRoot":"","sources":["../src/convertFormat.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,CAAC;AAsBzC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,UAAU,GAAG,MAAM,CAOrE"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertFormat = convertFormat;
|
|
4
|
+
const yaml_1 = require("yaml");
|
|
5
|
+
/**
|
|
6
|
+
* Parse content as JSON or YAML, throwing a descriptive error if both fail.
|
|
7
|
+
*/
|
|
8
|
+
function parseContent(content) {
|
|
9
|
+
// Try JSON first
|
|
10
|
+
try {
|
|
11
|
+
return JSON.parse(content);
|
|
12
|
+
}
|
|
13
|
+
catch (jsonError) {
|
|
14
|
+
// Fall back to YAML
|
|
15
|
+
try {
|
|
16
|
+
return (0, yaml_1.parse)(content);
|
|
17
|
+
}
|
|
18
|
+
catch (yamlError) {
|
|
19
|
+
throw new Error(`Input is neither valid JSON nor YAML.\nJSON error: ${jsonError instanceof Error ? jsonError.message : String(jsonError)}\nYAML error: ${yamlError instanceof Error ? yamlError.message : String(yamlError)}`, { cause: yamlError });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Convert between JSON and YAML string formats.
|
|
25
|
+
*
|
|
26
|
+
* Auto-detects the input format by trying JSON.parse first, then
|
|
27
|
+
* falling back to YAML parsing. Returns pretty-printed JSON (2-space indent)
|
|
28
|
+
* or clean YAML based on the `to` parameter.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* // JSON to YAML
|
|
33
|
+
* convertFormat('{"name": "Alice", "age": 30}', "yaml")
|
|
34
|
+
* // name: Alice
|
|
35
|
+
* // age: 30
|
|
36
|
+
*
|
|
37
|
+
* // YAML to JSON
|
|
38
|
+
* convertFormat("name: Alice\nage: 30", "json")
|
|
39
|
+
* // {
|
|
40
|
+
* // "name": "Alice",
|
|
41
|
+
* // "age": 30
|
|
42
|
+
* // }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
function convertFormat(content, to) {
|
|
46
|
+
const data = parseContent(content);
|
|
47
|
+
if (to === "json") {
|
|
48
|
+
return JSON.stringify(data, null, 2);
|
|
49
|
+
}
|
|
50
|
+
return (0, yaml_1.stringify)(data);
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=convertFormat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convertFormat.js","sourceRoot":"","sources":["../src/convertFormat.ts"],"names":[],"mappings":";;AA8CA,sCAOC;AArDD,+BAAsE;AAItE;;GAEG;AACH,SAAS,YAAY,CAAC,OAAe;IACnC,iBAAiB;IACjB,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAC7B,CAAC;IAAC,OAAO,SAAS,EAAE,CAAC;QACnB,oBAAoB;QACpB,IAAI,CAAC;YACH,OAAO,IAAA,YAAS,EAAC,OAAO,CAAC,CAAC;QAC5B,CAAC;QAAC,OAAO,SAAS,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CACb,sDAAsD,SAAS,YAAY,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,iBAAiB,SAAS,YAAY,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,EAC7M,EAAE,KAAK,EAAE,SAAS,EAAE,CACrB,CAAC;QACJ,CAAC;IACH,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,SAAgB,aAAa,CAAC,OAAe,EAAE,EAAc;IAC3D,MAAM,IAAI,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;IAEnC,IAAI,EAAE,KAAK,MAAM,EAAE,CAAC;QAClB,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;IACvC,CAAC;IACD,OAAO,IAAA,gBAAa,EAAC,IAAI,CAAC,CAAC;AAC7B,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format text content with right-aligned line numbers.
|
|
3
|
+
*
|
|
4
|
+
* @param content - The text content to format
|
|
5
|
+
* @param startLine - The starting line number (default: 1)
|
|
6
|
+
* @returns The formatted text with line numbers
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* formatWithLineNumbers("foo\nbar\nbaz")
|
|
11
|
+
* // Returns:
|
|
12
|
+
* // "1: foo\n2: bar\n3: baz"
|
|
13
|
+
*
|
|
14
|
+
* formatWithLineNumbers("hello\nworld", 10)
|
|
15
|
+
* // Returns:
|
|
16
|
+
* // "10: hello\n11: world"
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function formatWithLineNumbers(content: string, startLine?: number): string;
|
|
20
|
+
//# sourceMappingURL=formatWithLineNumbers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatWithLineNumbers.d.ts","sourceRoot":"","sources":["../src/formatWithLineNumbers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,SAAI,GAAG,MAAM,CAO5E"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatWithLineNumbers = formatWithLineNumbers;
|
|
4
|
+
/**
|
|
5
|
+
* Format text content with right-aligned line numbers.
|
|
6
|
+
*
|
|
7
|
+
* @param content - The text content to format
|
|
8
|
+
* @param startLine - The starting line number (default: 1)
|
|
9
|
+
* @returns The formatted text with line numbers
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* formatWithLineNumbers("foo\nbar\nbaz")
|
|
14
|
+
* // Returns:
|
|
15
|
+
* // "1: foo\n2: bar\n3: baz"
|
|
16
|
+
*
|
|
17
|
+
* formatWithLineNumbers("hello\nworld", 10)
|
|
18
|
+
* // Returns:
|
|
19
|
+
* // "10: hello\n11: world"
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
function formatWithLineNumbers(content, startLine = 1) {
|
|
23
|
+
const lines = content.split("\n");
|
|
24
|
+
const maxNum = startLine + lines.length - 1;
|
|
25
|
+
const width = String(maxNum).length;
|
|
26
|
+
return lines
|
|
27
|
+
.map((line, i) => `${String(startLine + i).padStart(width)}: ${line}`)
|
|
28
|
+
.join("\n");
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=formatWithLineNumbers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatWithLineNumbers.js","sourceRoot":"","sources":["../src/formatWithLineNumbers.ts"],"names":[],"mappings":";;AAkBA,sDAOC;AAzBD;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,qBAAqB,CAAC,OAAe,EAAE,SAAS,GAAG,CAAC;IAClE,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAClC,MAAM,MAAM,GAAG,SAAS,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;IAC5C,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC;IACpC,OAAO,KAAK;SACT,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,MAAM,CAAC,SAAS,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC;SACrE,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,4 +5,7 @@ export { slugify } from "./slugify.js";
|
|
|
5
5
|
export { formatDuration } from "./formatDuration.js";
|
|
6
6
|
export { buildFileTree, FILE_TREE_DEFAULTS } from "./buildFileTree.js";
|
|
7
7
|
export type { BuildTreeOptions } from "./buildFileTree.js";
|
|
8
|
+
export { convertFormat } from "./convertFormat.js";
|
|
9
|
+
export type { TextFormat } from "./convertFormat.js";
|
|
10
|
+
export { formatWithLineNumbers } from "./formatWithLineNumbers.js";
|
|
8
11
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAC9E,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACrE,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACvE,YAAY,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAC9E,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACrE,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACvE,YAAY,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,YAAY,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FILE_TREE_DEFAULTS = exports.buildFileTree = exports.formatDuration = exports.slugify = exports.chunkText = exports.extractPlaceholders = exports.replaceTemplate = exports.formatErrorForLog = exports.formatError = exports.getErrorMessage = void 0;
|
|
3
|
+
exports.formatWithLineNumbers = exports.convertFormat = exports.FILE_TREE_DEFAULTS = exports.buildFileTree = exports.formatDuration = exports.slugify = exports.chunkText = exports.extractPlaceholders = exports.replaceTemplate = exports.formatErrorForLog = exports.formatError = exports.getErrorMessage = void 0;
|
|
4
4
|
var errors_js_1 = require("./errors.js");
|
|
5
5
|
Object.defineProperty(exports, "getErrorMessage", { enumerable: true, get: function () { return errors_js_1.getErrorMessage; } });
|
|
6
6
|
Object.defineProperty(exports, "formatError", { enumerable: true, get: function () { return errors_js_1.formatError; } });
|
|
@@ -17,4 +17,8 @@ Object.defineProperty(exports, "formatDuration", { enumerable: true, get: functi
|
|
|
17
17
|
var buildFileTree_js_1 = require("./buildFileTree.js");
|
|
18
18
|
Object.defineProperty(exports, "buildFileTree", { enumerable: true, get: function () { return buildFileTree_js_1.buildFileTree; } });
|
|
19
19
|
Object.defineProperty(exports, "FILE_TREE_DEFAULTS", { enumerable: true, get: function () { return buildFileTree_js_1.FILE_TREE_DEFAULTS; } });
|
|
20
|
+
var convertFormat_js_1 = require("./convertFormat.js");
|
|
21
|
+
Object.defineProperty(exports, "convertFormat", { enumerable: true, get: function () { return convertFormat_js_1.convertFormat; } });
|
|
22
|
+
var formatWithLineNumbers_js_1 = require("./formatWithLineNumbers.js");
|
|
23
|
+
Object.defineProperty(exports, "formatWithLineNumbers", { enumerable: true, get: function () { return formatWithLineNumbers_js_1.formatWithLineNumbers; } });
|
|
20
24
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,yCAA8E;AAArE,4GAAA,eAAe,OAAA;AAAE,wGAAA,WAAW,OAAA;AAAE,8GAAA,iBAAiB,OAAA;AACxD,6CAAqE;AAA5D,8GAAA,eAAe,OAAA;AAAE,kHAAA,mBAAmB,OAAA;AAC7C,+CAA2C;AAAlC,yGAAA,SAAS,OAAA;AAClB,2CAAuC;AAA9B,qGAAA,OAAO,OAAA;AAChB,yDAAqD;AAA5C,mHAAA,cAAc,OAAA;AACvB,uDAAuE;AAA9D,iHAAA,aAAa,OAAA;AAAE,sHAAA,kBAAkB,OAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,yCAA8E;AAArE,4GAAA,eAAe,OAAA;AAAE,wGAAA,WAAW,OAAA;AAAE,8GAAA,iBAAiB,OAAA;AACxD,6CAAqE;AAA5D,8GAAA,eAAe,OAAA;AAAE,kHAAA,mBAAmB,OAAA;AAC7C,+CAA2C;AAAlC,yGAAA,SAAS,OAAA;AAClB,2CAAuC;AAA9B,qGAAA,OAAO,OAAA;AAChB,yDAAqD;AAA5C,mHAAA,cAAc,OAAA;AACvB,uDAAuE;AAA9D,iHAAA,aAAa,OAAA;AAAE,sHAAA,kBAAkB,OAAA;AAE1C,uDAAmD;AAA1C,iHAAA,aAAa,OAAA;AAEtB,uEAAmE;AAA1D,iIAAA,qBAAqB,OAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hardlydifficult/text",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
"lint": "tsc --noEmit",
|
|
15
15
|
"clean": "rm -rf dist"
|
|
16
16
|
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"yaml": "2.8.2"
|
|
19
|
+
},
|
|
17
20
|
"devDependencies": {
|
|
18
21
|
"@types/node": "25.2.3",
|
|
19
22
|
"typescript": "5.9.3",
|