This commit is contained in:
@ -1,23 +1,18 @@
|
|||||||
import getDb from '../db/index.js';
|
import getDb from '../db/index.js';
|
||||||
|
import logger from '#root/utils/logger.js'
|
||||||
|
|
||||||
export async function getRandomRecipe(limit = 5) {
|
export async function getRandomRecipe(limit = 5) {
|
||||||
logger(`随机${limit}个食谱`)
|
logger(`随机${limit}个食谱`)
|
||||||
const data = await getDb();
|
const data = await getDb();
|
||||||
const len = data.length;
|
const len = data.length;
|
||||||
logger(len)
|
|
||||||
const list = [];
|
const list = [];
|
||||||
const indexList = new Set();
|
const indexList = new Set();
|
||||||
|
while (indexList.size < limit) {
|
||||||
while (indexList.size === limit) {
|
|
||||||
const index = Math.floor(Math.random() * len);
|
const index = Math.floor(Math.random() * len);
|
||||||
logger(`index: ${index}`)
|
|
||||||
if (!indexList.has(index)) {
|
if (!indexList.has(index)) {
|
||||||
indexList.set(index);
|
indexList.add(index);
|
||||||
list.push(data[index]);
|
list.push(data[index]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger(list.length)
|
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user