Add cycle (repeat an array N times)

Open
#13 0 comments 0 reactions 1 assignee View on GitHub

@ramongr is already working on this.

Since May 6, 2026.

Assessment

This issue has not been assessed yet.

Description

additive v3-accepted

Updated 2026-05 to match the v2 standalone-function API and the issue template used by the rest of the helper backlog. Original 2021 description used the v1 `Array.prototype` style.

Operation

`cycle`

Category

transformations

Proposed signature

```ts
export function cycle(values: readonly T[], times: number): T[]
```

Why this isn't covered by native ES/TS

Expressible as `Array(n).fill(values).flat()`, but that allocates an intermediate array of arrays and is unergonomic. No dedicated native helper.

Empty / edge-case behaviour
  • `times === 0` → `[]`.
  • `times === 1` → shallow copy of `values`.
  • `times < 0` or non-integer → throws `RangeError` (consistent with `inGroups` / `inGroupsOf`).
  • Empty `values` → `[]` regardless of `times`.
  • Returns a new array (no mutation).
Examples

```ts
cycle([1, 2, 3], 2); // [1, 2, 3, 1, 2, 3]
cycle([1, 2], 0); // []
cycle([], 5); // []
```

Acceptance criteria
  • Implementation in `src/transformations/cycle.ts`
  • Re-exported from `src/transformations/index.ts`
  • Tests in `tests/transformations/cycle.test.ts` (incl. times=0, empty input, invalid times)
  • Docs added in `docs/transformations.md`
  • README API table updated
Dominant language
TypeScript
Stars
2
Forks
0
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ramongr/op-array

All issues in ramongr/op-array

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.