MCPcopy Create free account
hub / github.com/neomjs/neo / insert

Method insert

src/util/Array.mjs:64–89  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 4

moveMethod · 0.95
isArrayMethod · 0.80
indexOfMethod · 0.45
spliceMethod · 0.45

Tested by

no test coverage detected