* Inserts an item or Array of items to an array in case it does not already exist. * Duplicates will only get matched by reference. * @param {Array} arr * @param {Number} index * @param {*} items * @returns {Array}
(arr, index, items)
| 62 | * @returns {Array} |
| 63 | */ |
| 64 | static insert(arr, index, items) { |
| 65 | if (!Array.isArray(items)) { |
| 66 | items = [items] |
| 67 | } |
| 68 | |
| 69 | let len = items.length -1, |
| 70 | i = len, |
| 71 | currentIndex, item; |
| 72 | |
| 73 | // Iterate backwards |
| 74 | for (; i > -1; i--) { |
| 75 | item = items[i]; |
| 76 | |
| 77 | currentIndex = arr.indexOf(item); |
| 78 | |
| 79 | if (index !== currentIndex) { |
| 80 | if (currentIndex > -1) { |
| 81 | this.move(arr, currentIndex, index) |
| 82 | } else { |
| 83 | arr.splice(index, 0, item) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return arr |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns an array of items which are present in array1 and array2 |