data-fns
Version:
This library provides utility functions for working with array data that are useful in many contexts, including creative coding. It offers generic functions that perform common operations such as offsetting an array, generating an array based on a callbac
15 lines (14 loc) • 478 B
text/typescript
/**
* Maps an index to a cyclic pattern.
* @param index The original index.
* @param length The length of the sequence.
* @returns The mapped index in the cyclic pattern.
* @example
* cyclic(6, 5)
* // Returns 1
*/
export const cyclic = (index: number, length: number): number => {
// Calculate the mapped index based on the cyclic pattern
const normalizedIndex = index % length
return Math.abs(normalizedIndex >= 0 ? normalizedIndex : length + normalizedIndex)
}