@hardlydifficult/text 1.0.4 → 1.0.5

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
@@ -99,3 +99,25 @@ 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.
@@ -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"}
package/dist/index.d.ts CHANGED
@@ -5,4 +5,6 @@ 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";
8
10
  //# sourceMappingURL=index.d.ts.map
@@ -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"}
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.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,6 @@ 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; } });
20
22
  //# 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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hardlydifficult/text",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
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",