Skip to content

Commit

Permalink
✨ feat(utils.ts): add shuffle function
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinsOnuoha committed Oct 28, 2023
1 parent cd47188 commit 397583a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/stores/db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineStore } from 'pinia'
import statements from './statements.db'
import categories from './categories.db'
import { shuffle } from '@/utils/util'

export type Statement = {
title: string
Expand All @@ -17,7 +18,7 @@ export const useDBStore = defineStore('db', {
state: () => {
return {
categories: categories.data,
statements: statements.data
statements: shuffle(statements.data)
}
},
getters: {}
Expand Down
17 changes: 17 additions & 0 deletions src/utils/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function shuffle(t: Array<any>) {
let n
let last = t.length
while (last > 0) {
n = rand(last)
swap(t, n, --last)
}
return t
}
const rand = (n: number) => 0 | (Math.random() * n)

function swap(t: Array<any>, i: number, j: number) {
const q = t[i]
t[i] = t[j]
t[j] = q
return t
}

0 comments on commit 397583a

Please sign in to comment.