From 0681bd14acd0460f94502270509153ba05406675 Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Fri, 24 Nov 2023 14:34:23 +0900 Subject: [PATCH] Updated JScript Enumerator (markdown) --- JScript-Enumerator.md | 59 ++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/JScript-Enumerator.md b/JScript-Enumerator.md index dc3624f..3bf1583 100644 --- a/JScript-Enumerator.md +++ b/JScript-Enumerator.md @@ -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 ```js -// MS JScript Enumerator to Array -if (!Enumerator.prototype.toArray) { - Enumerator.prototype.toArray = function() { - var a = []; - for (; !this.atEnd(); this.moveNext()) { - var x = {}; - var b = new Enumerator(this.item().Properties_); - for (; !b.atEnd(); b.moveNext()) { - var c = b.item(); - if (typeof c.value !== "unknown") { - x[c.name] = c.value; - } else { - var i = 0, d = []; - while (true) { - try { - d.push(c.value(i)); - i++; - } catch (e) { - break; - } +// The provided code snippet has been corrected by ChatGPT. +// https://chat.openai.com/share/eaab056c-d265-4ee3-b355-9f29176a9caa +// Related issues: welsonjs#75 welsonjs#42 welsonjs#30 +Enumerator.prototype.toArray = function() { + var result = []; + while (!this.atEnd()) { + var currentItem = this.item(); + var currentItemProperties = currentItem.Properties_; + var itemObject = {}; + + var propertiesEnumerator = new Enumerator(currentItemProperties); + while (!propertiesEnumerator.atEnd()) { + var property = propertiesEnumerator.item(); + if (typeof property.value !== "unknown") { // The type "Unknown" is Array + itemObject[property.name] = property.value; + } else { + var arrayValues = []; + var index = 0; + while (true) { + try { + arrayValues.push(property.value(index)); + index++; + } catch (e) { + break; } - x[c.name] = d; } + itemObject[property.name] = arrayValues; } - a.push(x); + propertiesEnumerator.moveNext(); } - return a; - }; -} + result.push(itemObject); + this.moveNext(); + } + return result; +}; ``` ### References @@ -43,4 +49,5 @@ if (!Enumerator.prototype.toArray) { * https://social.technet.microsoft.com/Forums/systemcenter/en-US/2a0078db-2053-4e21-9262-62ffbc156862/enumerating-fields-returned-with-a-wmi-query?forum=configmgrgeneral * 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/6346766/javascript-enumerator/6346909 \ No newline at end of file + * https://stackoverflow.com/questions/6346766/javascript-enumerator/6346909 + * https://chat.openai.com/share/eaab056c-d265-4ee3-b355-9f29176a9caa \ No newline at end of file