Browse Source

added some shuffle to jsonserver results

Skylsmoi 7 years ago
parent
commit
e1a338fd25
1 changed files with 23 additions and 1 deletions
  1. 23 1
      jsonserver/server.js

+ 23 - 1
jsonserver/server.js View File

@@ -5,6 +5,25 @@ const router = jsonServer.router() // for persistance : jsonServer.router('stati
5 5
 const middlewares = jsonServer.defaults()
6 6
 const GLOBAL_PORT = 3001
7 7
 
8
+function shuffle(array) {
9
+  var currentIndex = array.length, temporaryValue, randomIndex;
10
+
11
+  // While there remain elements to shuffle...
12
+  while (0 !== currentIndex) {
13
+
14
+    // Pick a remaining element...
15
+    randomIndex = Math.floor(Math.random() * currentIndex);
16
+    currentIndex -= 1;
17
+
18
+    // And swap it with the current element.
19
+    temporaryValue = array[currentIndex];
20
+    array[currentIndex] = array[randomIndex];
21
+    array[randomIndex] = temporaryValue;
22
+  }
23
+
24
+  return array;
25
+}
26
+
8 27
 server.use(middlewares)
9 28
 server.use(jsonServer.bodyParser)
10 29
 
@@ -31,7 +50,10 @@ server.get('/user/is_logged_in', (req, res) => res.jsonp(jsonDb.user_logged))
31 50
 
32 51
 server.get('/user/:id/workspace', (req, res) => res.jsonp(jsonDb.workspace_list))
33 52
 
34
-server.get('/workspace/:id', (req, res) => res.jsonp(jsonDb.workspace_detail))
53
+server.get('/workspace/:id', (req, res) => res.jsonp(
54
+  // {...jsonDb.workspace_detail, content: shuffle(jsonDb.workspace_detail.content)})
55
+  Object.assign({}, jsonDb.workspace_detail, {content: shuffle(jsonDb.workspace_detail.content)})
56
+))
35 57
 
36 58
 server.get('/workspace/:idws/content/:idc', (req, res) => {
37 59
   switch (req.params.idc) {