[-1, 0, 1, 2, 3, 5, 7, 8, 9, 11, 14, 15, 17, 18, 20]
[[-1, 0, 1, 2, 3], [5], [7, 8, 9], [11], [14, 15], [17, 18], [20]]
const groupAdjacent = (arr, newGroup) =>
arr.reduce((acc, n, i, a) => (
(!i || newGroup(n, a[~-i])) && acc.push([]),
acc[~-acc.length].push(n),
acc
), []);
const result = groupAdjacent(arr, (c, p) => c !== -~p);
function group(data, key) {
const grouped = new Map;
let i = -1;
for (const n of data) {
const k = key(n, ++i);
grouped.set(k, grouped.get(k) ?? []).get(k).push(n);
}
return grouped;
}
const result = [...group(arr, (n, i) => n - i).values()];