* 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 = [])
| 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 |
no test coverage detected