123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- /**
- * 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);
- }
|