This commit is contained in:
18
plugins/cook/api/index.js
Normal file
18
plugins/cook/api/index.js
Normal file
@ -0,0 +1,18 @@
|
||||
import getDb from '../db/index.js';
|
||||
|
||||
export async function getRandomRecipe(limit = 5) {
|
||||
const data = await getDb();
|
||||
const len = data.length;
|
||||
const list = [];
|
||||
const indexList = new Set();
|
||||
|
||||
while (indexList.size === limit) {
|
||||
const index = Math.floor(Math.random() * len);
|
||||
if (!indexList.has(index)) {
|
||||
indexList.set(index);
|
||||
list.push(data[index]);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
Reference in New Issue
Block a user