MCPcopy Create free account
hub / github.com/cryptii/cryptii / indexOfSlice

Method indexOfSlice

src/ArrayUtil.js:155–169  ·  view source on GitHub ↗

* Returns the index of the first occurrence of the given slice. * @param {array} array Haystack * @param {array} slice Needle slice elements * @param {number} [fromIndex=0] Index at which to begin searching * @return {number} Index of the first slice occurance or -1 if there is none

(array, slice, fromIndex = 0)

Source from the content-addressed store, hash-verified

153 * @return {number} Index of the first slice occurance or -1 if there is none
154 */
155 static indexOfSlice (array, slice, fromIndex = 0) {
156 if (slice.length === 0) {
157 return -1
158 }
159 let i = -1
160 let j = fromIndex - 1
161 // Find the next occurrence of the first element
162 while (i === -1 && (j = array.indexOf(slice[0], j + 1)) !== -1) {
163 // Compare slice
164 if (this.isEqual(array.slice(j, j + slice.length), slice)) {
165 i = j
166 }
167 }
168 return i
169 }
170
171 /**
172 * Finds all occurences of the given slice inside a target array and replaces

Callers 3

replaceSliceMethod · 0.95
performDecodeMethod · 0.80
performTranslateMethod · 0.80

Calls 2

isEqualMethod · 0.95
indexOfMethod · 0.80

Tested by

no test coverage detected