Refactor canonicalizing array indices to a helper function