Do array properties on objects preserve their order, have a pre-defined order, or is it random?
The documentation doesn’t explicitly say that they are, or are not. Usually if something is called an ‘array’ I would expect the sequence to be preserved, but I think I’m seeing examples where it’s not.
Specifically I was creating some edit functions, and the tests were failing. The test:
test("Array property Sequence", async() => {
myObj.arrayProperty = ["a", "b", "c"];
(await verifyOntologyEditFunction(() => testFuncs.mapStringArray(myObj)))
.modifiesObject(
{object: myObj,
properties: {
arrayProperty: ["a", "Boo", "c"]
}}).hasNoMoreEdits();
});
This test fails because the arrayProperty values comes back in Jest as: ["Boo","a","c"]
The function is, annotated with the console.log()
outputs added inline as comments, as below:
@OntologyEditFunction()
public async mapStringArray(myObj: MyObjectType): Promise<void> {
console.log("Original: " + JSON.stringify(myObj.arrayProperty));
// Original: ["a","b","c"]
let oldValues: string[]|undefined = myObj.arrayProperty ? [...myObj.arrayProperty] : undefined;
console.log("Copied: " + JSON.stringify(oldValues));
// Copied: ["a","b","c"]
let newValues: string[]|undefined = oldValues ? oldValues.map((x:string) => x === "b" ? "Boo" : x ) : undefined;
console.log("NewValues: " + JSON.stringify(newValues));
// NewValues: ["a","Boo","c"]
myObj.arrayProperty = newValues;
console.log("Written to object: " + JSON.stringify(myObj.arrayProperty));
// Written to object: ["Boo","a","c"]
};
I see the same behaviour when using the ‘Functions’ tab at the bottom to test the function on real data.
So I have two questions:
- Are there any guarantees on what the sequence for arrays stored as object properties is?
- What’s the best way of using
verifyOntologyEditFunction()
if there are no guarantees? (It appears to be coming back sorted alphabetically, but it’d be important to know what the sorting rules are, e.g. does “å” come before or after “z”?)