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

Method replaceSlice

src/ArrayUtil.js:179–195  ·  view source on GitHub ↗

* Finds all occurences of the given slice inside a target array and replaces * them with the given replacement slice. * @param {array} array Target array * @param {array} slice Slice elements to be removed * @param {array} [replacement=[]] Replacement slice * @return {array} Resulting

(array, slice, replacement = [])

Source from the content-addressed store, hash-verified

177 * @return {array} Resulting array
178 */
179 static replaceSlice (array, slice, replacement = []) {
180 let i = 0
181 let j = -1
182 let result = []
183 // Find next occurence of the given slice
184 while ((j = this.indexOfSlice(array, slice, j + 1)) !== -1) {
185 // Append elements in between of the last and current slice
186 result = result.concat(array.slice(i, j))
187 // Append replacement elements
188 result = result.concat(replacement)
189 // Move the cursor behind the current slice
190 i = j + slice.length
191 }
192 // Append the trailing array elements behind last slice
193 result = result.concat(array.slice(i))
194 return result
195 }
196
197 /**
198 * Composes a new array with elements having the given number of bits and

Callers 1

performDecodeMethod · 0.80

Calls 1

indexOfSliceMethod · 0.95

Tested by

no test coverage detected