feat: dynamic import recipe.json

This commit is contained in:
YunYouJun
2023-07-30 20:59:52 +08:00
parent 38b31a5654
commit b526aae2d0
7 changed files with 26 additions and 23 deletions

View File

@@ -1,19 +1,17 @@
import type { RecipeItem, Recipes } from '../types'
/**
* 生成随机菜谱,默认一道
* 生成随机数组
* @param recipes
* @returns
*/
export function generateRandomRecipe(recipes: Recipes, total = 1) {
const randomRecipes: RecipeItem[] = []
export function generateRandomArray(length: number, total = 1) {
const randomArr: number[] = []
for (let i = 0; i < total; i++) {
const randomIndex = Math.floor(Math.random() * recipes.length)
if (randomRecipes.includes(recipes[randomIndex])) {
const randomIndex = Math.floor(Math.random() * length)
if (randomArr.includes(randomIndex)) {
i--
continue
}
randomRecipes.push(recipes[randomIndex])
randomArr.push(randomIndex)
}
return randomRecipes
return randomArr
}