Skip to content

Commit 22a8a7d

Browse files
committed
feat: add weave function and WeaveOptions interface for flexible input processing
1 parent a9ae7be commit 22a8a7d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/helpers.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export interface WeaveOptions<TInput> {
2+
amount: number;
3+
customizer: (input: TInput, index: number) => TInput;
4+
}
5+
6+
const DEFAULT_WEAVE_OPTIONS: WeaveOptions<any> = {
7+
amount: 10,
8+
customizer: (input) => input,
9+
};
10+
11+
export function weave<TInput>(input: TInput | TInput[], opts?: WeaveOptions<TInput>): TInput[] {
12+
const { amount, customizer } = opts ?? DEFAULT_WEAVE_OPTIONS;
13+
14+
// convert single input to array
15+
const inputs = Array.isArray(input) ? input : [input];
16+
const result: TInput[] = [];
17+
18+
for (let i = 0; i < amount; i++) {
19+
for (const item of inputs) {
20+
result.push(customizer(item, i));
21+
}
22+
}
23+
24+
return result;
25+
}

0 commit comments

Comments
 (0)