morph-data 1.0.0

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.
Files changed (3) hide show
  1. package/README.md +25 -0
  2. package/index.js +134 -0
  3. package/package.json +17 -0
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # morph-data
2
+
3
+ Professional data conversion between CSV, JSON, and YAML formats. Optimized for reliability and developer experience.
4
+
5
+ ## ✨ Features
6
+
7
+ - **CSV ↔ JSON**: Support for quoted fields and auto-type detection.
8
+ - **JSON ↔ YAML**: Clean serialization.
9
+ - **Markdown Tables**: Generate professional tables for documentation.
10
+
11
+ ## 🚀 Installation
12
+
13
+ ```bash
14
+ npm install morph-data
15
+ ```
16
+
17
+ ## 📖 Usage
18
+
19
+ ```javascript
20
+ const transform = require('morph-data');
21
+ const json = transform.csvToJson(csvString);
22
+ ```
23
+
24
+ ## ⚖️ License
25
+ MIT
package/index.js ADDED
@@ -0,0 +1,134 @@
1
+ /**
2
+ * data-morph-pro
3
+ * Professional data conversion utility.
4
+ */
5
+
6
+ class MorphData {
7
+ /**
8
+ * Converts CSV string to JSON array of objects.
9
+ * @param {string} csv - The CSV content.
10
+ * @param {string} delimiter - Default is comma.
11
+ * @returns {Array<Object>}
12
+ */
13
+ csvToJson(csv, delimiter = ',') {
14
+ if (!csv) return [];
15
+ const lines = csv.split(/\r?\n/).filter(line => line.trim() !== '');
16
+ if (lines.length < 2) return [];
17
+
18
+ const headers = lines[0].split(delimiter).map(h => h.trim().replace(/^"|"$/g, ''));
19
+
20
+ return lines.slice(1).map(line => {
21
+ const values = this._parseCsvLine(line, delimiter);
22
+ return headers.reduce((obj, header, index) => {
23
+ let val = values[index] || '';
24
+ val = val.trim().replace(/^"|"$/g, '');
25
+ // Auto-type conversion
26
+ if (val === 'true') val = true;
27
+ else if (val === 'false') val = false;
28
+ else if (!isNaN(val) && val !== '') val = Number(val);
29
+
30
+ obj[header] = val;
31
+ return obj;
32
+ }, {});
33
+ });
34
+ }
35
+
36
+ /**
37
+ * Converts JSON array of objects to CSV string.
38
+ * @param {Array<Object>} json - Array of objects.
39
+ * @param {string} delimiter - Default is comma.
40
+ * @returns {string}
41
+ */
42
+ jsonToCsv(json, delimiter = ',') {
43
+ if (!Array.isArray(json) || json.length === 0) return '';
44
+
45
+ const headers = Object.keys(json[0]);
46
+ const csvLines = [headers.join(delimiter)];
47
+
48
+ json.forEach(obj => {
49
+ const values = headers.map(header => {
50
+ let val = obj[header];
51
+ if (val === null || val === undefined) val = '';
52
+ val = String(val);
53
+ // Escape quotes and wrap in quotes if contains delimiter or newline
54
+ if (val.includes(delimiter) || val.includes('"') || val.includes('\n')) {
55
+ val = `"${val.replace(/"/g, '""')}"`;
56
+ }
57
+ return val;
58
+ });
59
+ csvLines.push(values.join(delimiter));
60
+ });
61
+
62
+ return csvLines.join('\n');
63
+ }
64
+
65
+ /**
66
+ * Internal helper to handle quoted values in CSV.
67
+ */
68
+ _parseCsvLine(line, delimiter) {
69
+ const result = [];
70
+ let curVal = '';
71
+ let inQuotes = false;
72
+
73
+ for (let i = 0; i < line.length; i++) {
74
+ const char = line[i];
75
+ if (char === '"') {
76
+ inQuotes = !inQuotes;
77
+ } else if (char === delimiter && !inQuotes) {
78
+ result.push(curVal);
79
+ curVal = '';
80
+ } else {
81
+ curVal += char;
82
+ }
83
+ }
84
+ result.push(curVal);
85
+ return result;
86
+ }
87
+
88
+ /**
89
+ * Helper to convert JSON to a basic YAML format.
90
+ * (Simplified version for direct usage)
91
+ */
92
+ jsonToYaml(json, indent = 2) {
93
+ const format = (data, level = 0) => {
94
+ const spacing = ' '.repeat(level * indent);
95
+ if (Array.isArray(data)) {
96
+ return data.map(item => `\n${spacing}- ${format(item, level + 1)}`).join('').trim();
97
+ } else if (typeof data === 'object' && data !== null) {
98
+ return Object.entries(data)
99
+ .map(([key, val]) => `\n${spacing}${key}: ${format(val, level + 1)}`)
100
+ .join('')
101
+ .trim();
102
+ }
103
+ return String(data);
104
+ };
105
+ return format(json);
106
+ }
107
+
108
+ /**
109
+ * Converts JSON array of objects to a Markdown Table string.
110
+ * @param {Array<Object>} json
111
+ * @returns {string}
112
+ */
113
+ jsonToMarkdownTable(json) {
114
+ if (!Array.isArray(json) || json.length === 0) return '';
115
+
116
+ const headers = Object.keys(json[0]);
117
+ const headerRow = `| ${headers.join(' | ')} |`;
118
+ const separatorRow = `| ${headers.map(() => '---').join(' | ')} |`;
119
+
120
+ const bodyRows = json.map(obj => {
121
+ const values = headers.map(header => {
122
+ let val = obj[header];
123
+ if (val === null || val === undefined) val = '';
124
+ return String(val).replace(/\|/g, '\\|'); // Escape pipes
125
+ });
126
+ return `| ${values.join(' | ')} |`;
127
+ });
128
+
129
+ return [headerRow, separatorRow, ...bodyRows].join('\n');
130
+ }
131
+ }
132
+
133
+ module.exports = new MorphData();
134
+ module.exports.MorphData = MorphData;
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "morph-data",
3
+ "version": "1.0.0",
4
+ "description": "High-fidelity data conversion between CSV, JSON, and YAML formats. Optimized for reliability and developer experience.",
5
+ "main": "index.js",
6
+ "keywords": [
7
+ "csv",
8
+ "json",
9
+ "yaml",
10
+ "markdown-table",
11
+ "data-conversion",
12
+ "data-morphism",
13
+ "formatter"
14
+ ],
15
+ "author": "Antigravity",
16
+ "license": "MIT"
17
+ }