Skip to content

Commit 7b5e614

Browse files
committed
feat: Array.Shuffle
1 parent 18796ca commit 7b5e614

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/nodes/Array.Shuffle.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { ModuleCompute, ModuleDefinition } from '@nodescript/core/types';
2+
3+
type P = {
4+
array: unknown[];
5+
};
6+
7+
type R = unknown[];
8+
9+
export const module: ModuleDefinition<P, R> = {
10+
version: '1.0.0',
11+
moduleName: 'Array / Shuffle',
12+
description: `Returns a copy of an array with its items appearing in random order. Uses Fisher-Yates algorithm.`,
13+
keywords: ['randomize'],
14+
params: {
15+
array: {
16+
schema: {
17+
type: 'array',
18+
items: { type: 'any' },
19+
},
20+
},
21+
},
22+
result: {
23+
schema: {
24+
type: 'array',
25+
items: { type: 'any' },
26+
}
27+
},
28+
};
29+
30+
export const compute: ModuleCompute<P, R> = params => {
31+
const arr = params.array.slice();
32+
let currentIndex = arr.length;
33+
while (currentIndex >= 1) {
34+
const randomIndex = Math.floor(Math.random() * currentIndex);
35+
currentIndex -= 1;
36+
// Swap random with current
37+
const t = arr[randomIndex];
38+
arr[randomIndex] = arr[currentIndex];
39+
arr[currentIndex] = t;
40+
}
41+
return arr;
42+
};

src/nodes/Web.HttpRequest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type P = {
2929
type R = Promise<unknown>;
3030

3131
export const module: ModuleDefinition<P, R> = {
32-
version: '2.6.1',
32+
version: '2.6.2',
3333
moduleName: 'Web / Http Request',
3434
description: `
3535
Sends an HTTP request using backend-powered HTTP client.

0 commit comments

Comments
 (0)