While coding DOpus functionalities I often needed the same code to work with different data structures so I compiled this include script file to convert, enumerate, calculate length and differences of arrays, collections, dictionaries, maps and vectors.
inc_DataExtensions.opusscriptinstall (2.1 KB)
// DataExtensions
// (c) 2024 Felix Froemel
// This is an include file script for Directory Opus.
// See https://www.gpsoft.com.au/endpoints/redirect.php?page=scripts for development information.
// Called by Directory Opus to initialize the include file script
function OnInitIncludeFile(initData)
{
initData.name = "Data Extensions";
initData.version = "1.4 (2025-12-29)";
initData.copyright = "(c) 2024 Felix Froemel";
initData.url = "https://resource.dopus.com/t/dataextensions-include-file-to-work-with-collections-maps-vectors-arrays-and-dictionaries/58168";
initData.desc = "Extensions to work with DOpus Collections, Maps, Vectors, JScript Arrays and Dictionaries. v" + initData.version;
initData.min_version = "13.0";
initData.shared = true;
}
// ******************* Conversion ******************* //
function ArrayToVector(arr)
{
var vector = DOpus.Create.Vector();
for (var i = 0; i < arr.length; i++)
vector.push_back(arr[i]);
return vector;
}
function CollectionToArray(collection)
{
var array = [];
ForEach(collection, function(item) { array.push(item); });
return array;
}
function VectorToArray(vector)
{
var array = [];
for(var i = 0; i < vector.count; i++)
{
var item = vector.back()
array.push(item);
vector.pop_back();
}
return array;
}
function DictionaryToMap(dictionary)
{
var map = DOpus.Create.Map();
for (var key in dictionary)
map(key) = dictionary[key];
return map;
}
function MapToDictionary(map)
{
var dict = { };
KeyValueEnum(map, function(key, value) { dict[key] = value; });
return dict;
}
// ******************* Enumeration ******************* //
function ForEach(enumerable, delegate)
{
for (var enumerator = new Enumerator(enumerable); !enumerator.atEnd(); enumerator.moveNext())
delegate(enumerator.item());
}
//abort if delegate returns false
function ForEachAbortable(enumerable, delegate)
{
for (var enumerator = new Enumerator(enumerable); !enumerator.atEnd(); enumerator.moveNext())
if(!delegate(enumerator.item()))
return;
}
function KeyValueEnum(dict, delegate)
{
for (var e = new Enumerator(dict); !e.atEnd(); e.moveNext())
{
var key = e.item();
var value = dict(key);
delegate(key, value);
}
}
// ******************* Length ******************* //
function CollectionLength(collection)
{
var len = 0;
for (var eColl = new Enumerator(collection); !eColl.atEnd(); eColl.moveNext())
len++;
return len;
}
function DictionaryLength(dict)
{
var length = 0;
for(var keys in dict)
length++;
return length;
};
// ******************* Array Extensions ******************* //
function ArrayContains(array, item)
{
for(var i = 0; i < array.length; i++)
if(array[i] === item)
return true;
return false;
}
//https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript
function ArrayDifference(oldArray, newArray)
{
// deal with empty lists
if (oldArray == undefined)
oldArray = [];
if (newArray == undefined)
newArray = [];
// sort both arrays (or this won't work)
oldArray.sort();
newArray.sort();
// don't compare if either list is empty
if (oldArray.length == 0 || newArray.length == 0)
return { added: newArray, removed: oldArray };
// declare temporary variables
var op = 0; var np = 0;
var a = []; var r = [];
// compare arrays and add to add or remove lists
while (op < oldArray.length && np < newArray.length)
{
if (oldArray[op] < newArray[np])
{
r.push(oldArray[op]);
op++;
}
else if (oldArray[op] > newArray[np])
{
a.push(newArray[np]);
np++;
}
else
{
op++;
np++;
}
}
// add remaining items
if (np < newArray.length)
a = a.concat(newArray.slice(np, newArray.length));
if (op < oldArray.length)
r = r.concat(oldArray.slice(op, oldArray.length));
return { added: a, removed: r };
}
function ArrayIndexOf(array, element)
{
for (var i = 0; i < array.length; i++)
if (array[i] == element)
return i;
return -1;
}
function ArrayRemoveItem(array, itemToRemove)
{
var index = ArrayIndexOf(array, itemToRemove);
if (index !== -1)
array.splice(index, 1);
}
//https://stackoverflow.com/questions/8665862/efficient-algorithm-for-difference-of-array-and-a-known-subsequence
function ArrayRemoveItems(array, itemsToRemove)
{
array.sort();
itemsToRemove.sort();
var diff = [];
var i = 0, j = 0, n = array.length, m = itemsToRemove.length;
while (i < n)
{
if (j < m && array[i] === itemsToRemove[j])
j++;
else
diff.push(array[i]);
i++;
}
return diff;
}