UNPKG

everyutil

Version:

A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.

26 lines (25 loc) 901 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.arrayRotateChunks = void 0; /** * Rotates the array in chunks of N elements. * @author @dailker * @template T * @param {T[]} array - The array to rotate. * @param {number} chunkSize - Size of each chunk. * @param {number} count - Number of chunk rotations (positive=right, negative=left). * @returns {T[]} Rotated array. */ function arrayRotateChunks(array, chunkSize, count) { if (chunkSize <= 0) return array.slice(); const chunks = []; for (let i = 0; i < array.length; i += chunkSize) { chunks.push(array.slice(i, i + chunkSize)); } const n = chunks.length; const rot = ((count % n) + n) % n; const rotated = chunks.slice(-rot).concat(chunks.slice(0, -rot)); return rotated.flat(); } exports.arrayRotateChunks = arrayRotateChunks;