diagram-js
Version:
A toolbox for displaying and modifying diagrams on the web
93 lines (71 loc) • 1.69 kB
JavaScript
/**
* Failsafe remove an element from a collection
*
* @param {Array<Object>} [collection]
* @param {Object} [element]
*
* @return {number} the previous index of the element
*/
export function remove(collection, element) {
if (!collection || !element) {
return -1;
}
var idx = collection.indexOf(element);
if (idx !== -1) {
collection.splice(idx, 1);
}
return idx;
}
/**
* Fail save add an element to the given connection, ensuring
* it does not yet exist.
*
* @param {Array<Object>} collection
* @param {Object} element
* @param {number} [idx]
*/
export function add(collection, element, idx) {
if (!collection || !element) {
return;
}
if (typeof idx !== 'number') {
idx = -1;
}
var currentIdx = collection.indexOf(element);
if (currentIdx !== -1) {
if (currentIdx === idx) {
// nothing to do, position has not changed
return;
} else {
if (idx !== -1) {
// remove from current position
collection.splice(currentIdx, 1);
} else {
// already exists in collection
return;
}
}
}
if (idx !== -1) {
// insert at specified position
collection.splice(idx, 0, element);
} else {
// push to end
collection.push(element);
}
}
/**
* Fail save get the index of an element in a collection.
*
* @param {Array<Object>} collection
* @param {Object} element
*
* @return {number} the index or -1 if collection or element do
* not exist or the element is not contained.
*/
export function indexOf(collection, element) {
if (!collection || !element) {
return -1;
}
return collection.indexOf(element);
}