File tree Expand file tree Collapse file tree 2 files changed +43
-1
lines changed
Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ type P = {
2929type R = Promise < unknown > ;
3030
3131export 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.
You can’t perform that action at this time.
0 commit comments