Updated JScript Enumerator (markdown)

Namhyeon Go 2023-11-24 14:34:23 +09:00
parent e988781229
commit 0681bd14ac

@ -7,35 +7,41 @@ For example, when accessing the value of an array, it is expressed as `arr(i)` i
### Convert MS/JScript Enumerator to JSObject ### Convert MS/JScript Enumerator to JSObject
```js ```js
// MS JScript Enumerator to Array // The provided code snippet has been corrected by ChatGPT.
if (!Enumerator.prototype.toArray) { // https://chat.openai.com/share/eaab056c-d265-4ee3-b355-9f29176a9caa
// Related issues: welsonjs#75 welsonjs#42 welsonjs#30
Enumerator.prototype.toArray = function() { Enumerator.prototype.toArray = function() {
var a = []; var result = [];
for (; !this.atEnd(); this.moveNext()) { while (!this.atEnd()) {
var x = {}; var currentItem = this.item();
var b = new Enumerator(this.item().Properties_); var currentItemProperties = currentItem.Properties_;
for (; !b.atEnd(); b.moveNext()) { var itemObject = {};
var c = b.item();
if (typeof c.value !== "unknown") { var propertiesEnumerator = new Enumerator(currentItemProperties);
x[c.name] = c.value; while (!propertiesEnumerator.atEnd()) {
var property = propertiesEnumerator.item();
if (typeof property.value !== "unknown") { // The type "Unknown" is Array
itemObject[property.name] = property.value;
} else { } else {
var i = 0, d = []; var arrayValues = [];
var index = 0;
while (true) { while (true) {
try { try {
d.push(c.value(i)); arrayValues.push(property.value(index));
i++; index++;
} catch (e) { } catch (e) {
break; break;
} }
} }
x[c.name] = d; itemObject[property.name] = arrayValues;
} }
propertiesEnumerator.moveNext();
} }
a.push(x); result.push(itemObject);
this.moveNext();
} }
return a; return result;
}; };
}
``` ```
### References ### References
@ -44,3 +50,4 @@ if (!Enumerator.prototype.toArray) {
* http://www.java2s.com/Tutorial/JavaScript/0600__MS-JScript/Enumeratoritem.htm * http://www.java2s.com/Tutorial/JavaScript/0600__MS-JScript/Enumeratoritem.htm
* https://stackoverflow.com/questions/973016/jscript-enumerator-and-list-of-properties * https://stackoverflow.com/questions/973016/jscript-enumerator-and-list-of-properties
* https://stackoverflow.com/questions/6346766/javascript-enumerator/6346909 * https://stackoverflow.com/questions/6346766/javascript-enumerator/6346909
* https://chat.openai.com/share/eaab056c-d265-4ee3-b355-9f29176a9caa