@cogitator-ai/wasm-tools 0.4.1 → 0.5.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.
- package/README.md +141 -6
- package/dist/index.d.ts +227 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +194 -0
- package/dist/index.js.map +1 -1
- package/dist/plugins/compression.d.ts +2 -0
- package/dist/plugins/compression.d.ts.map +1 -0
- package/dist/plugins/compression.js +530 -0
- package/dist/plugins/compression.js.map +1 -0
- package/dist/plugins/csv.d.ts +2 -0
- package/dist/plugins/csv.d.ts.map +1 -0
- package/dist/plugins/csv.js +138 -0
- package/dist/plugins/csv.js.map +1 -0
- package/dist/plugins/datetime.d.ts +2 -0
- package/dist/plugins/datetime.d.ts.map +1 -0
- package/dist/plugins/datetime.js +184 -0
- package/dist/plugins/datetime.js.map +1 -0
- package/dist/plugins/diff.d.ts +2 -0
- package/dist/plugins/diff.d.ts.map +1 -0
- package/dist/plugins/diff.js +187 -0
- package/dist/plugins/diff.js.map +1 -0
- package/dist/plugins/markdown.d.ts +2 -0
- package/dist/plugins/markdown.d.ts.map +1 -0
- package/dist/plugins/markdown.js +203 -0
- package/dist/plugins/markdown.js.map +1 -0
- package/dist/plugins/regex.d.ts +2 -0
- package/dist/plugins/regex.d.ts.map +1 -0
- package/dist/plugins/regex.js +119 -0
- package/dist/plugins/regex.js.map +1 -0
- package/dist/plugins/signing.d.ts +2 -0
- package/dist/plugins/signing.d.ts.map +1 -0
- package/dist/plugins/signing.js +565 -0
- package/dist/plugins/signing.js.map +1 -0
- package/dist/plugins/slug.d.ts +2 -0
- package/dist/plugins/slug.d.ts.map +1 -0
- package/dist/plugins/slug.js +180 -0
- package/dist/plugins/slug.js.map +1 -0
- package/dist/plugins/validation.d.ts +2 -0
- package/dist/plugins/validation.d.ts.map +1 -0
- package/dist/plugins/validation.js +105 -0
- package/dist/plugins/validation.js.map +1 -0
- package/dist/plugins/xml.d.ts +2 -0
- package/dist/plugins/xml.d.ts.map +1 -0
- package/dist/plugins/xml.js +293 -0
- package/dist/plugins/xml.js.map +1 -0
- package/dist/temp/compression.js +614 -0
- package/dist/temp/csv.js +154 -0
- package/dist/temp/datetime.js +196 -0
- package/dist/temp/diff.js +198 -0
- package/dist/temp/markdown.js +220 -0
- package/dist/temp/regex.js +140 -0
- package/dist/temp/signing.js +569 -0
- package/dist/temp/slug.js +198 -0
- package/dist/temp/validation.js +125 -0
- package/dist/temp/xml.js +289 -0
- package/dist/wasm/base64.wasm +0 -0
- package/dist/wasm/calc.wasm +0 -0
- package/dist/wasm/compression.wasm +0 -0
- package/dist/wasm/csv.wasm +0 -0
- package/dist/wasm/datetime.wasm +0 -0
- package/dist/wasm/diff.wasm +0 -0
- package/dist/wasm/hash.wasm +0 -0
- package/dist/wasm/json.wasm +0 -0
- package/dist/wasm/markdown.wasm +0 -0
- package/dist/wasm/regex.wasm +0 -0
- package/dist/wasm/signing.wasm +0 -0
- package/dist/wasm/slug.wasm +0 -0
- package/dist/wasm/validation.wasm +0 -0
- package/dist/wasm/xml.wasm +0 -0
- package/package.json +2 -2
package/dist/temp/csv.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/plugins/csv.ts
|
|
21
|
+
var csv_exports = {};
|
|
22
|
+
__export(csv_exports, {
|
|
23
|
+
csv: () => csv
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(csv_exports);
|
|
26
|
+
function parseCsv(data, delimiter, quote, hasHeaders) {
|
|
27
|
+
const rows = [];
|
|
28
|
+
let currentRow = [];
|
|
29
|
+
let currentField = "";
|
|
30
|
+
let inQuotes = false;
|
|
31
|
+
let i = 0;
|
|
32
|
+
while (i < data.length) {
|
|
33
|
+
const char = data[i];
|
|
34
|
+
const nextChar = data[i + 1];
|
|
35
|
+
if (inQuotes) {
|
|
36
|
+
if (char === quote) {
|
|
37
|
+
if (nextChar === quote) {
|
|
38
|
+
currentField += quote;
|
|
39
|
+
i += 2;
|
|
40
|
+
continue;
|
|
41
|
+
} else {
|
|
42
|
+
inQuotes = false;
|
|
43
|
+
i++;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
currentField += char;
|
|
48
|
+
i++;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (char === quote) {
|
|
53
|
+
inQuotes = true;
|
|
54
|
+
i++;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (char === delimiter) {
|
|
58
|
+
currentRow.push(currentField);
|
|
59
|
+
currentField = "";
|
|
60
|
+
i++;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (char === "\r" && nextChar === "\n") {
|
|
64
|
+
currentRow.push(currentField);
|
|
65
|
+
rows.push(currentRow);
|
|
66
|
+
currentRow = [];
|
|
67
|
+
currentField = "";
|
|
68
|
+
i += 2;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (char === "\n" || char === "\r") {
|
|
72
|
+
currentRow.push(currentField);
|
|
73
|
+
rows.push(currentRow);
|
|
74
|
+
currentRow = [];
|
|
75
|
+
currentField = "";
|
|
76
|
+
i++;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
currentField += char;
|
|
80
|
+
i++;
|
|
81
|
+
}
|
|
82
|
+
if (currentField || currentRow.length > 0) {
|
|
83
|
+
currentRow.push(currentField);
|
|
84
|
+
rows.push(currentRow);
|
|
85
|
+
}
|
|
86
|
+
const filteredRows = rows.filter((row) => row.length > 0 && !(row.length === 1 && row[0] === ""));
|
|
87
|
+
if (hasHeaders && filteredRows.length > 0) {
|
|
88
|
+
const headers = filteredRows[0];
|
|
89
|
+
return { rows: filteredRows.slice(1), headers };
|
|
90
|
+
}
|
|
91
|
+
return { rows: filteredRows };
|
|
92
|
+
}
|
|
93
|
+
function stringifyCsv(data, delimiter, quote, headers) {
|
|
94
|
+
const rows = [];
|
|
95
|
+
const escapeField = (field) => {
|
|
96
|
+
if (field === null || field === void 0) return "";
|
|
97
|
+
const str = String(field);
|
|
98
|
+
const needsQuotes = str.includes(delimiter) || str.includes(quote) || str.includes("\n") || str.includes("\r");
|
|
99
|
+
if (needsQuotes) {
|
|
100
|
+
const escaped = str.replace(new RegExp(quote, "g"), quote + quote);
|
|
101
|
+
return quote + escaped + quote;
|
|
102
|
+
}
|
|
103
|
+
return str;
|
|
104
|
+
};
|
|
105
|
+
if (headers && headers.length > 0) {
|
|
106
|
+
rows.push(headers.map(escapeField).join(delimiter));
|
|
107
|
+
}
|
|
108
|
+
for (const row of data) {
|
|
109
|
+
rows.push(row.map(escapeField).join(delimiter));
|
|
110
|
+
}
|
|
111
|
+
return rows.join("\n");
|
|
112
|
+
}
|
|
113
|
+
function csv() {
|
|
114
|
+
try {
|
|
115
|
+
const inputStr = Host.inputString();
|
|
116
|
+
const input = JSON.parse(inputStr);
|
|
117
|
+
const delimiter = input.delimiter ?? ",";
|
|
118
|
+
const quote = input.quote ?? '"';
|
|
119
|
+
let result;
|
|
120
|
+
let rowCount;
|
|
121
|
+
let columnCount;
|
|
122
|
+
let headers;
|
|
123
|
+
if (input.operation === "parse") {
|
|
124
|
+
const hasHeaders = input.headers ?? false;
|
|
125
|
+
const parsed = parseCsv(input.data, delimiter, quote, hasHeaders);
|
|
126
|
+
result = parsed.rows;
|
|
127
|
+
headers = parsed.headers;
|
|
128
|
+
rowCount = parsed.rows.length;
|
|
129
|
+
columnCount = parsed.rows[0]?.length ?? 0;
|
|
130
|
+
} else {
|
|
131
|
+
headers = input.headers;
|
|
132
|
+
result = stringifyCsv(input.data, delimiter, quote, headers);
|
|
133
|
+
rowCount = input.data.length;
|
|
134
|
+
columnCount = input.data[0]?.length ?? 0;
|
|
135
|
+
}
|
|
136
|
+
const output = {
|
|
137
|
+
result,
|
|
138
|
+
rowCount,
|
|
139
|
+
columnCount,
|
|
140
|
+
headers
|
|
141
|
+
};
|
|
142
|
+
Host.outputString(JSON.stringify(output));
|
|
143
|
+
return 0;
|
|
144
|
+
} catch (error) {
|
|
145
|
+
const output = {
|
|
146
|
+
result: [],
|
|
147
|
+
rowCount: 0,
|
|
148
|
+
columnCount: 0,
|
|
149
|
+
error: error instanceof Error ? error.message : String(error)
|
|
150
|
+
};
|
|
151
|
+
Host.outputString(JSON.stringify(output));
|
|
152
|
+
return 1;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/plugins/datetime.ts
|
|
21
|
+
var datetime_exports = {};
|
|
22
|
+
__export(datetime_exports, {
|
|
23
|
+
datetime: () => datetime
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(datetime_exports);
|
|
26
|
+
function parseOffset(tz) {
|
|
27
|
+
if (!tz || tz === "UTC" || tz === "Z") return 0;
|
|
28
|
+
const match = tz.match(/^([+-])(\d{2}):?(\d{2})$/);
|
|
29
|
+
if (!match) return 0;
|
|
30
|
+
const sign = match[1] === "+" ? 1 : -1;
|
|
31
|
+
const hours = parseInt(match[2], 10);
|
|
32
|
+
const minutes = parseInt(match[3], 10);
|
|
33
|
+
return sign * (hours * 60 + minutes) * 60 * 1e3;
|
|
34
|
+
}
|
|
35
|
+
function parseDate(dateStr) {
|
|
36
|
+
const isoMatch = dateStr.match(
|
|
37
|
+
/^(\d{4})-(\d{2})-(\d{2})(?:T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{3}))?(?:Z|([+-]\d{2}:?\d{2}))?)?$/
|
|
38
|
+
);
|
|
39
|
+
if (isoMatch) {
|
|
40
|
+
const [, year, month, day, hour = "0", min = "0", sec = "0", ms = "0", tz] = isoMatch;
|
|
41
|
+
const date = new Date(Date.UTC(
|
|
42
|
+
parseInt(year, 10),
|
|
43
|
+
parseInt(month, 10) - 1,
|
|
44
|
+
parseInt(day, 10),
|
|
45
|
+
parseInt(hour, 10),
|
|
46
|
+
parseInt(min, 10),
|
|
47
|
+
parseInt(sec, 10),
|
|
48
|
+
parseInt(ms, 10)
|
|
49
|
+
));
|
|
50
|
+
if (tz) {
|
|
51
|
+
const offset = parseOffset(tz);
|
|
52
|
+
date.setTime(date.getTime() - offset);
|
|
53
|
+
}
|
|
54
|
+
return date;
|
|
55
|
+
}
|
|
56
|
+
const timestamp = Date.parse(dateStr);
|
|
57
|
+
if (!isNaN(timestamp)) {
|
|
58
|
+
return new Date(timestamp);
|
|
59
|
+
}
|
|
60
|
+
throw new Error(`Cannot parse date: ${dateStr}`);
|
|
61
|
+
}
|
|
62
|
+
function formatDate(date, formatStr, offsetMs = 0) {
|
|
63
|
+
const adjusted = new Date(date.getTime() + offsetMs);
|
|
64
|
+
const year = adjusted.getUTCFullYear();
|
|
65
|
+
const month = adjusted.getUTCMonth() + 1;
|
|
66
|
+
const day = adjusted.getUTCDate();
|
|
67
|
+
const hours = adjusted.getUTCHours();
|
|
68
|
+
const minutes = adjusted.getUTCMinutes();
|
|
69
|
+
const seconds = adjusted.getUTCSeconds();
|
|
70
|
+
const ms = adjusted.getUTCMilliseconds();
|
|
71
|
+
const pad = (n, len = 2) => String(n).padStart(len, "0");
|
|
72
|
+
const offsetHours = Math.floor(Math.abs(offsetMs) / 36e5);
|
|
73
|
+
const offsetMins = Math.floor(Math.abs(offsetMs) % 36e5 / 6e4);
|
|
74
|
+
const offsetSign = offsetMs >= 0 ? "+" : "-";
|
|
75
|
+
const offsetStr = offsetMs === 0 ? "Z" : `${offsetSign}${pad(offsetHours)}:${pad(offsetMins)}`;
|
|
76
|
+
return formatStr.replace("YYYY", String(year)).replace("MM", pad(month)).replace("DD", pad(day)).replace("HH", pad(hours)).replace("mm", pad(minutes)).replace("ss", pad(seconds)).replace("SSS", pad(ms, 3)).replace("Z", offsetStr);
|
|
77
|
+
}
|
|
78
|
+
function addToDate(date, amount, unit) {
|
|
79
|
+
const result = new Date(date.getTime());
|
|
80
|
+
switch (unit) {
|
|
81
|
+
case "years":
|
|
82
|
+
result.setUTCFullYear(result.getUTCFullYear() + amount);
|
|
83
|
+
break;
|
|
84
|
+
case "months":
|
|
85
|
+
result.setUTCMonth(result.getUTCMonth() + amount);
|
|
86
|
+
break;
|
|
87
|
+
case "days":
|
|
88
|
+
result.setUTCDate(result.getUTCDate() + amount);
|
|
89
|
+
break;
|
|
90
|
+
case "hours":
|
|
91
|
+
result.setUTCHours(result.getUTCHours() + amount);
|
|
92
|
+
break;
|
|
93
|
+
case "minutes":
|
|
94
|
+
result.setUTCMinutes(result.getUTCMinutes() + amount);
|
|
95
|
+
break;
|
|
96
|
+
case "seconds":
|
|
97
|
+
result.setUTCSeconds(result.getUTCSeconds() + amount);
|
|
98
|
+
break;
|
|
99
|
+
case "milliseconds":
|
|
100
|
+
result.setTime(result.getTime() + amount);
|
|
101
|
+
break;
|
|
102
|
+
default:
|
|
103
|
+
throw new Error(`Unknown unit: ${unit}`);
|
|
104
|
+
}
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
function dateDiff(date1, date2, unit) {
|
|
108
|
+
const diffMs = date2.getTime() - date1.getTime();
|
|
109
|
+
switch (unit) {
|
|
110
|
+
case "years":
|
|
111
|
+
return date2.getUTCFullYear() - date1.getUTCFullYear();
|
|
112
|
+
case "months":
|
|
113
|
+
return (date2.getUTCFullYear() - date1.getUTCFullYear()) * 12 + (date2.getUTCMonth() - date1.getUTCMonth());
|
|
114
|
+
case "days":
|
|
115
|
+
return Math.floor(diffMs / (24 * 60 * 60 * 1e3));
|
|
116
|
+
case "hours":
|
|
117
|
+
return Math.floor(diffMs / (60 * 60 * 1e3));
|
|
118
|
+
case "minutes":
|
|
119
|
+
return Math.floor(diffMs / (60 * 1e3));
|
|
120
|
+
case "seconds":
|
|
121
|
+
return Math.floor(diffMs / 1e3);
|
|
122
|
+
case "milliseconds":
|
|
123
|
+
return diffMs;
|
|
124
|
+
default:
|
|
125
|
+
return diffMs;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function datetime() {
|
|
129
|
+
try {
|
|
130
|
+
const inputStr = Host.inputString();
|
|
131
|
+
const input = JSON.parse(inputStr);
|
|
132
|
+
const offsetMs = parseOffset(input.timezone);
|
|
133
|
+
let date;
|
|
134
|
+
let result;
|
|
135
|
+
switch (input.operation) {
|
|
136
|
+
case "now":
|
|
137
|
+
date = /* @__PURE__ */ new Date();
|
|
138
|
+
result = date.toISOString();
|
|
139
|
+
break;
|
|
140
|
+
case "parse":
|
|
141
|
+
if (!input.date) throw new Error("date is required for parse operation");
|
|
142
|
+
date = parseDate(input.date);
|
|
143
|
+
result = date.toISOString();
|
|
144
|
+
break;
|
|
145
|
+
case "format":
|
|
146
|
+
if (!input.date) throw new Error("date is required for format operation");
|
|
147
|
+
date = parseDate(input.date);
|
|
148
|
+
const format = input.format ?? "YYYY-MM-DDTHH:mm:ssZ";
|
|
149
|
+
result = formatDate(date, format, offsetMs);
|
|
150
|
+
break;
|
|
151
|
+
case "add":
|
|
152
|
+
if (!input.date) throw new Error("date is required for add operation");
|
|
153
|
+
if (input.amount === void 0) throw new Error("amount is required for add operation");
|
|
154
|
+
if (!input.unit) throw new Error("unit is required for add operation");
|
|
155
|
+
date = parseDate(input.date);
|
|
156
|
+
date = addToDate(date, input.amount, input.unit);
|
|
157
|
+
result = date.toISOString();
|
|
158
|
+
break;
|
|
159
|
+
case "subtract":
|
|
160
|
+
if (!input.date) throw new Error("date is required for subtract operation");
|
|
161
|
+
if (input.amount === void 0) throw new Error("amount is required for subtract operation");
|
|
162
|
+
if (!input.unit) throw new Error("unit is required for subtract operation");
|
|
163
|
+
date = parseDate(input.date);
|
|
164
|
+
date = addToDate(date, -input.amount, input.unit);
|
|
165
|
+
result = date.toISOString();
|
|
166
|
+
break;
|
|
167
|
+
case "diff":
|
|
168
|
+
if (!input.date) throw new Error("date is required for diff operation");
|
|
169
|
+
if (!input.endDate) throw new Error("endDate is required for diff operation");
|
|
170
|
+
date = parseDate(input.date);
|
|
171
|
+
const endDate = parseDate(input.endDate);
|
|
172
|
+
result = dateDiff(date, endDate, input.unit ?? "milliseconds");
|
|
173
|
+
break;
|
|
174
|
+
default:
|
|
175
|
+
throw new Error(`Unknown operation: ${input.operation}`);
|
|
176
|
+
}
|
|
177
|
+
const outputDate = input.operation === "diff" ? parseDate(input.date) : date;
|
|
178
|
+
const output = {
|
|
179
|
+
result,
|
|
180
|
+
iso: outputDate.toISOString(),
|
|
181
|
+
unix: Math.floor(outputDate.getTime() / 1e3),
|
|
182
|
+
formatted: input.format ? formatDate(outputDate, input.format, offsetMs) : void 0
|
|
183
|
+
};
|
|
184
|
+
Host.outputString(JSON.stringify(output));
|
|
185
|
+
return 0;
|
|
186
|
+
} catch (error) {
|
|
187
|
+
const output = {
|
|
188
|
+
result: "",
|
|
189
|
+
iso: "",
|
|
190
|
+
unix: 0,
|
|
191
|
+
error: error instanceof Error ? error.message : String(error)
|
|
192
|
+
};
|
|
193
|
+
Host.outputString(JSON.stringify(output));
|
|
194
|
+
return 1;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/plugins/diff.ts
|
|
21
|
+
var diff_exports = {};
|
|
22
|
+
__export(diff_exports, {
|
|
23
|
+
diff: () => diff
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(diff_exports);
|
|
26
|
+
function myersDiff(oldArr, newArr) {
|
|
27
|
+
const n = oldArr.length;
|
|
28
|
+
const m = newArr.length;
|
|
29
|
+
const max = n + m;
|
|
30
|
+
if (max === 0) return [];
|
|
31
|
+
const v = { 1: 0 };
|
|
32
|
+
const trace = [];
|
|
33
|
+
outer: for (let d = 0; d <= max; d++) {
|
|
34
|
+
trace.push({ ...v });
|
|
35
|
+
for (let k = -d; k <= d; k += 2) {
|
|
36
|
+
let x2;
|
|
37
|
+
if (k === -d || k !== d && v[k - 1] < v[k + 1]) {
|
|
38
|
+
x2 = v[k + 1];
|
|
39
|
+
} else {
|
|
40
|
+
x2 = v[k - 1] + 1;
|
|
41
|
+
}
|
|
42
|
+
let y2 = x2 - k;
|
|
43
|
+
while (x2 < n && y2 < m && oldArr[x2] === newArr[y2]) {
|
|
44
|
+
x2++;
|
|
45
|
+
y2++;
|
|
46
|
+
}
|
|
47
|
+
v[k] = x2;
|
|
48
|
+
if (x2 >= n && y2 >= m) {
|
|
49
|
+
break outer;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const changes = [];
|
|
54
|
+
let x = n;
|
|
55
|
+
let y = m;
|
|
56
|
+
for (let d = trace.length - 1; d >= 0; d--) {
|
|
57
|
+
const vPrev = trace[d];
|
|
58
|
+
const k = x - y;
|
|
59
|
+
let prevK;
|
|
60
|
+
if (k === -d || k !== d && vPrev[k - 1] < vPrev[k + 1]) {
|
|
61
|
+
prevK = k + 1;
|
|
62
|
+
} else {
|
|
63
|
+
prevK = k - 1;
|
|
64
|
+
}
|
|
65
|
+
const prevX = vPrev[prevK];
|
|
66
|
+
const prevY = prevX - prevK;
|
|
67
|
+
while (x > prevX && y > prevY) {
|
|
68
|
+
changes.unshift({
|
|
69
|
+
type: "equal",
|
|
70
|
+
value: oldArr[x - 1],
|
|
71
|
+
lineNumber: { original: x, modified: y }
|
|
72
|
+
});
|
|
73
|
+
x--;
|
|
74
|
+
y--;
|
|
75
|
+
}
|
|
76
|
+
if (d > 0) {
|
|
77
|
+
if (x === prevX) {
|
|
78
|
+
changes.unshift({
|
|
79
|
+
type: "add",
|
|
80
|
+
value: newArr[y - 1],
|
|
81
|
+
lineNumber: { modified: y }
|
|
82
|
+
});
|
|
83
|
+
y--;
|
|
84
|
+
} else {
|
|
85
|
+
changes.unshift({
|
|
86
|
+
type: "remove",
|
|
87
|
+
value: oldArr[x - 1],
|
|
88
|
+
lineNumber: { original: x }
|
|
89
|
+
});
|
|
90
|
+
x--;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return changes;
|
|
95
|
+
}
|
|
96
|
+
function formatUnified(changes, context) {
|
|
97
|
+
const lines = [];
|
|
98
|
+
let i = 0;
|
|
99
|
+
while (i < changes.length) {
|
|
100
|
+
let start = i;
|
|
101
|
+
while (start > 0 && i - start < context && changes[start - 1].type === "equal") {
|
|
102
|
+
start--;
|
|
103
|
+
}
|
|
104
|
+
let end = i;
|
|
105
|
+
while (end < changes.length) {
|
|
106
|
+
if (changes[end].type !== "equal") {
|
|
107
|
+
let nextChange = end + 1;
|
|
108
|
+
while (nextChange < changes.length && changes[nextChange].type === "equal") {
|
|
109
|
+
nextChange++;
|
|
110
|
+
}
|
|
111
|
+
if (nextChange - end <= context * 2 && nextChange < changes.length) {
|
|
112
|
+
end = nextChange;
|
|
113
|
+
} else {
|
|
114
|
+
end = Math.min(end + context, changes.length - 1);
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
end++;
|
|
119
|
+
}
|
|
120
|
+
if (end >= changes.length) end = changes.length - 1;
|
|
121
|
+
let hasChanges = false;
|
|
122
|
+
for (let j = start; j <= end; j++) {
|
|
123
|
+
if (changes[j].type !== "equal") {
|
|
124
|
+
hasChanges = true;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (hasChanges) {
|
|
129
|
+
for (let j = start; j <= end; j++) {
|
|
130
|
+
const change = changes[j];
|
|
131
|
+
if (change.type === "add") {
|
|
132
|
+
lines.push(`+ ${change.value}`);
|
|
133
|
+
} else if (change.type === "remove") {
|
|
134
|
+
lines.push(`- ${change.value}`);
|
|
135
|
+
} else {
|
|
136
|
+
lines.push(` ${change.value}`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
i = end + 1;
|
|
141
|
+
while (i < changes.length && changes[i].type === "equal") {
|
|
142
|
+
i++;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return lines.join("\n");
|
|
146
|
+
}
|
|
147
|
+
function formatInline(changes) {
|
|
148
|
+
const lines = [];
|
|
149
|
+
for (const change of changes) {
|
|
150
|
+
if (change.type === "add") {
|
|
151
|
+
lines.push(`[+] ${change.value}`);
|
|
152
|
+
} else if (change.type === "remove") {
|
|
153
|
+
lines.push(`[-] ${change.value}`);
|
|
154
|
+
} else {
|
|
155
|
+
lines.push(` ${change.value}`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return lines.join("\n");
|
|
159
|
+
}
|
|
160
|
+
function diff() {
|
|
161
|
+
try {
|
|
162
|
+
const inputStr = Host.inputString();
|
|
163
|
+
const input = JSON.parse(inputStr);
|
|
164
|
+
const format = input.format ?? "unified";
|
|
165
|
+
const context = input.context ?? 3;
|
|
166
|
+
const oldLines = input.original.split("\n");
|
|
167
|
+
const newLines = input.modified.split("\n");
|
|
168
|
+
const changes = myersDiff(oldLines, newLines);
|
|
169
|
+
const additions = changes.filter((c) => c.type === "add").length;
|
|
170
|
+
const deletions = changes.filter((c) => c.type === "remove").length;
|
|
171
|
+
let diffStr;
|
|
172
|
+
if (format === "json") {
|
|
173
|
+
diffStr = JSON.stringify(changes);
|
|
174
|
+
} else if (format === "inline") {
|
|
175
|
+
diffStr = formatInline(changes);
|
|
176
|
+
} else {
|
|
177
|
+
diffStr = formatUnified(changes, context);
|
|
178
|
+
}
|
|
179
|
+
const output = {
|
|
180
|
+
diff: diffStr,
|
|
181
|
+
additions,
|
|
182
|
+
deletions,
|
|
183
|
+
changes
|
|
184
|
+
};
|
|
185
|
+
Host.outputString(JSON.stringify(output));
|
|
186
|
+
return 0;
|
|
187
|
+
} catch (error) {
|
|
188
|
+
const output = {
|
|
189
|
+
diff: "",
|
|
190
|
+
additions: 0,
|
|
191
|
+
deletions: 0,
|
|
192
|
+
changes: [],
|
|
193
|
+
error: error instanceof Error ? error.message : String(error)
|
|
194
|
+
};
|
|
195
|
+
Host.outputString(JSON.stringify(output));
|
|
196
|
+
return 1;
|
|
197
|
+
}
|
|
198
|
+
}
|