wao 0.27.0 → 0.27.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/cjs/encode.js +1452 -792
- package/cjs/toerl.js +173 -0
- package/esm/encode.js +1340 -510
- package/esm/toerl.js +162 -0
- package/package.json +1 -1
package/cjs/toerl.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = toErl;
|
|
7
|
+
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
8
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
9
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
10
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
11
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
12
|
+
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
13
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
14
|
+
function toErl(obj) {
|
|
15
|
+
var indent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
16
|
+
var spaces = " ".repeat(indent);
|
|
17
|
+
|
|
18
|
+
// Handle null (as atom)
|
|
19
|
+
if (obj === null) {
|
|
20
|
+
return "null"; // Erlang atom
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Handle undefined (as atom)
|
|
24
|
+
if (obj === undefined) {
|
|
25
|
+
return "undefined"; // Erlang atom
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Handle boolean (as atom)
|
|
29
|
+
if (typeof obj === "boolean") {
|
|
30
|
+
return obj.toString(); // true/false are atoms in Erlang
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Handle numbers
|
|
34
|
+
if (typeof obj === "number") {
|
|
35
|
+
if (Number.isInteger(obj)) {
|
|
36
|
+
return obj.toString();
|
|
37
|
+
} else {
|
|
38
|
+
// Format float properly
|
|
39
|
+
return obj.toString();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Handle strings
|
|
44
|
+
if (typeof obj === "string") {
|
|
45
|
+
// Escape special characters
|
|
46
|
+
var escaped = obj.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t");
|
|
47
|
+
return "<<\"".concat(escaped, "\">>");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Handle symbols (as atom)
|
|
51
|
+
if (_typeof(obj) === "symbol") {
|
|
52
|
+
var desc = obj.description || "Symbol.for()";
|
|
53
|
+
// Check if it's a simple atom or needs quoting
|
|
54
|
+
if (/^[a-z][a-zA-Z0-9_]*$/.test(desc)) {
|
|
55
|
+
return desc; // Simple atom like: ok, error, undefined
|
|
56
|
+
} else {
|
|
57
|
+
return "'".concat(desc, "'"); // Quoted atom for special cases
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Handle Buffer/Binary
|
|
62
|
+
if (Buffer.isBuffer(obj) || obj && _typeof(obj) === "object" && obj.type === "Buffer" && Array.isArray(obj.data)) {
|
|
63
|
+
var data = Buffer.isBuffer(obj) ? obj : Buffer.from(obj.data);
|
|
64
|
+
if (data.length === 0) {
|
|
65
|
+
return "<<>>";
|
|
66
|
+
}
|
|
67
|
+
var bytes = Array.from(data).join(",");
|
|
68
|
+
return "<<".concat(bytes, ">>");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Handle Arrays
|
|
72
|
+
if (Array.isArray(obj)) {
|
|
73
|
+
if (obj.length === 0) {
|
|
74
|
+
return "[]";
|
|
75
|
+
}
|
|
76
|
+
var items = obj.map(function (item) {
|
|
77
|
+
return toErl(item, indent + 2);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Check if it fits on one line
|
|
81
|
+
var oneLine = "[".concat(items.join(", "), "]");
|
|
82
|
+
if (oneLine.length <= 80 && !items.some(function (item) {
|
|
83
|
+
return item.includes("\n");
|
|
84
|
+
})) {
|
|
85
|
+
return oneLine;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Multi-line format
|
|
89
|
+
var inner = items.map(function (item) {
|
|
90
|
+
return "".concat(spaces, " ").concat(item);
|
|
91
|
+
}).join(",\n");
|
|
92
|
+
return "[\n".concat(inner, "\n").concat(spaces, "]");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Handle Objects/Maps
|
|
96
|
+
if (_typeof(obj) === "object" && obj !== null) {
|
|
97
|
+
var entries = Object.entries(obj);
|
|
98
|
+
if (entries.length === 0) {
|
|
99
|
+
return "#{}";
|
|
100
|
+
}
|
|
101
|
+
var pairs = entries.map(function (_ref) {
|
|
102
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
103
|
+
key = _ref2[0],
|
|
104
|
+
value = _ref2[1];
|
|
105
|
+
var erlangKey = "<<\"".concat(key, "\">>");
|
|
106
|
+
var erlangValue = toErl(value, indent + 2);
|
|
107
|
+
return "".concat(erlangKey, " => ").concat(erlangValue);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Check if it fits on one line
|
|
111
|
+
var _oneLine = "#{ ".concat(pairs.join(", "), " }");
|
|
112
|
+
if (_oneLine.length <= 80 && !pairs.some(function (pair) {
|
|
113
|
+
return pair.includes("\n");
|
|
114
|
+
})) {
|
|
115
|
+
return _oneLine;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Multi-line format
|
|
119
|
+
var _inner = pairs.map(function (pair) {
|
|
120
|
+
return "".concat(spaces, " ").concat(pair);
|
|
121
|
+
}).join(",\n");
|
|
122
|
+
return "#{\n".concat(_inner, "\n").concat(spaces, "}");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Fallback
|
|
126
|
+
return "unknown";
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Helper function to convert JSON string or object
|
|
130
|
+
function convert(input) {
|
|
131
|
+
var obj;
|
|
132
|
+
|
|
133
|
+
// If input is a string, try to parse it as JSON
|
|
134
|
+
if (typeof input === "string") {
|
|
135
|
+
try {
|
|
136
|
+
// First try to parse as JSON
|
|
137
|
+
obj = JSON.parse(input);
|
|
138
|
+
} catch (e) {
|
|
139
|
+
// If it fails, try to evaluate it (for cases with Symbol, Buffer, etc.)
|
|
140
|
+
try {
|
|
141
|
+
obj = eval("(".concat(input, ")"));
|
|
142
|
+
} catch (e2) {
|
|
143
|
+
throw new Error("Invalid input: not valid JSON or JavaScript object");
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
obj = input;
|
|
148
|
+
}
|
|
149
|
+
return toErl(obj);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Function to format test case for Erlang
|
|
153
|
+
function formatTestCase(obj) {
|
|
154
|
+
var testName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "test";
|
|
155
|
+
var erlangData = toErl(obj, 2);
|
|
156
|
+
|
|
157
|
+
// If it's a simple one-liner, put it inline
|
|
158
|
+
if (!erlangData.includes("\n") && erlangData.length < 60) {
|
|
159
|
+
return "".concat(testName, "_test() -> show(").concat(erlangData, ").");
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Multi-line format
|
|
163
|
+
return "".concat(testName, "_test() -> \n show(").concat(erlangData, ").");
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Function to convert multiple test cases
|
|
167
|
+
function convertMultiple(testCases) {
|
|
168
|
+
var baseTestName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "generated";
|
|
169
|
+
return testCases.map(function (testCase, index) {
|
|
170
|
+
var testName = "".concat(baseTestName, "_").concat(index + 1);
|
|
171
|
+
return formatTestCase(testCase, testName);
|
|
172
|
+
}).join("\n\n");
|
|
173
|
+
}
|