Sets and Maps in PJSR

bulrichl

Well-known member
Roberto's webinar "An Introduction to PixInsight Scripting with PJSR" encouraged me to give it a second try and again engage in learning JavaScript; I just begun, so please be patient.

When I run this code in PixInsight's Script Editor:
Code:
let a = [3, 5, 8, 1, 7, 2, 6, 4, 3, 5, 8];
console.writeln("a.length: ", a.length, "<br>");

let s = new Set(a);

console.writeln("s:           ", s);
console.writeln("s.size:      ", s.size);
console.writeln("s.has(7):    ", s.has(7));
console.writeln();

console.writeln("s.entries(): ", s.entries());
console.writeln("s.keys():    ", s.keys());
console.writeln("s.values():  ", s.values());
console.writeln();

for (let key of s) {
    console.write(key, " ");
}

s.forEach(function(value, key, ownerSet) {
    console.writeln(key, " ", value, " ");
    console.writeln(ownerSet === s);
});

the following output is generated:
Code:
a.length: 11

s:           [object Set]
s.size:      8
s.has(7):    true

s.entries(): [object Set Iterator]
s.keys():    [object Set Iterator]
s.values():  [object Set Iterator]

3 5 8 1 7 2 6 4 
*** Error [022]: C:/Users/Bulrich/Downloads/Test_Set_Map/Test_Set.js, line 22: TypeError: s.forEach is not a function

Obviously a set is constructed, and it is ensured that unique values are in the set when duplicate values are in the underlying array. The set is not sorted. The size property and the methods has(), set() and get() are supported. The set has the iterators entries(), keys() and values(). values() is the default iterator. I can apply a for-of loop on the set s, but a method forEach() is not available for it.

For maps, the situation is similar. When I run this code:
Code:
let m = new Map();

m.set("shape", "square");
m.set("color", "blue");
m.set("width", "5");
m.set("number", "2");

console.writeln("m:              ", m);
console.writeln("m.size:         ", m.size);
console.writeln('m.has("color"): ', m.has("color"));
console.writeln();

console.writeln("m.keys():       ", m.keys());
console.writeln("m.values():     ", m.values());
console.writeln();

for (let [key, value] of m) {
    console.writeln(key, " ", value);
}

m.forEach(function(value, key, ownerMap) {
    console.writeln(key, " ", value);
    console.writeln(ownerMap === m);
});

the following output is generated:
Code:
m:              [object Map]
m.size:         4
m.has("color"): true

m.entries():    [object Map Iterator]
m.keys():       [object Map Iterator]
m.values():     [object Map Iterator]

shape square
color blue
width 5
number 2
*** Error [022]: C:/Users/Bulrich/Downloads/Test_Set_Map/Test_Map.js, line 24: TypeError: m.forEach is not a function

A map is constructed, the size property and the methods has(), set() and get() are supported. The map has the iterators entries(), keys() and values(). entries() is the default iterator. I can apply a for-of loop on the map m, and destructuring the key-value pair works. However, a method forEach() is not available for it.

-----

According to the announcement of the 1.8.8-6 release, PixInsight is still using the SpiderMonkey engine version 24 (this probably means: ECMAScript 5). Nevertheless many modern features that were introduced only with ECMAScript 6 are currently available in PJSR. Besides the support for sets and maps, let and const declarations, arrow functions and probably many more hidden features are already available in PixInsight 1.8.8-7. How is that possible?

Apparently, sets and maps are supported in PJSR, except that the forEach() method is missing. However, currently, there is no documentation of sets and maps in Object Explorer. Are there other objects missing in Object Explorer that are worthwhile exploring?

Bernd
 
Back
Top