Skip to content

JScript Enumerator

Namhyeon, Go edited this page Nov 24, 2023 · 7 revisions

JScript Enumerator

JScript has a class called Enumerator. This shows a small difference from the standard JavaScript.

For example, when accessing the value of an array, it is expressed as arr(i) instead of arr[i]. In this case, the type of array in JScript is marked as unknown, so in some cases, it has to be converted appropriately.

Convert MS/JScript Enumerator to JSObject

// 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;
                    }
                }
                itemObject[property.name] = arrayValues;
            }
            propertiesEnumerator.moveNext();
        }
        result.push(itemObject);
        this.moveNext();
    }
    return result;
};

References