const [first, second, ...middle, beforeLast, last] = anIterable;
At any step, if the iterator reaches the end of its available values, stop the algorithm.
- set the value of
middle
to a new empty array. - for each variable from the first to
middle
excluded,- iterate anIterable.
- set the value of the variable to the result of the iteration.
- for each variable from
middle
excluded to the last one,- iterate anIterable.
- set the value of the variable to the result of the iteration.
- iterate anIterable.
- take the value of the first variable after
middle
and add it to the array value ofmiddle
. - for each variable from the first variable after
middle
to the last one excluded.- set the value of the current variable to the one of the following variable.
- set the value of the last variable to the result of the iteration.
- go to step 4.
[first, second, ...middle, beforeLast, last] = [1];
// 1, undefined, [], undefined, undefined
[first, second, ...middle, beforeLast, last] = [1, 2, 3];
// 1, 2, [], 3, undefined
[first, second, ...middle, beforeLast, last] = [1, 2, 3, 4];
// 1, 2, [], 3, 4
[first, second, ...middle, beforeLast, last] = [1, 2, 3, 4, 5];
// 1, 2, [3], 4, 5
[first, second, ...middle, beforeLast, last] = [1, 2, 3, 4, 5, 6];
// 1, 2, [3, 4], 5, 6