Skip to content

Commit

Permalink
Add function for random emojis
Browse files Browse the repository at this point in the history
  • Loading branch information
ajuvonen committed Sep 19, 2024
1 parent 7599d14 commit 0bac627
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
41 changes: 3 additions & 38 deletions src/components/ToastMessage.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup lang="ts">
import {getRandomEmojis} from '@/utils/helpers';
import {useElementHover, useTimeout} from '@vueuse/core';
import {ref, watchEffect} from 'vue';
Expand All @@ -25,43 +26,7 @@ watchEffect(() => {
}
});
const veggieEmojis = [
'πŸ₯',
'πŸ₯₯',
'πŸ‡',
'🍈',
'πŸ‰',
'🍊',
'πŸ‹',
'🍌',
'🍍',
'πŸ₯­',
'🍎',
'🍏',
'🍐',
'πŸ‘',
'πŸ’',
'πŸ“',
'🫐',
'πŸ…',
'πŸ†',
'🌽',
'🌢️',
'πŸ«‘',
'πŸ₯‘',
'πŸ₯’',
'πŸ₯¬',
'πŸ₯¦',
'πŸ₯”',
'πŸ§„',
'πŸ§…',
'πŸ₯•',
'πŸ«›',
'πŸ₯œ',
'🫘',
'🌿',
];
const emoji = veggieEmojis[Math.floor(Math.random() * veggieEmojis.length)];
const emoji = getRandomEmojis();
</script>
<template>
<div
Expand All @@ -72,7 +37,7 @@ const emoji = veggieEmojis[Math.floor(Math.random() * veggieEmojis.length)];
>
<div class="toast-message__content">
<span class="text-2xl" aria-hidden="true">
{{ emoji }}
{{ emoji[0] }}
</span>
<span>{{ text }}</span>
</div>
Expand Down
8 changes: 7 additions & 1 deletion src/components/__tests__/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {describe, it, expect} from 'vitest';
import {DateTime} from 'luxon';
import {ALL_VEGGIES} from '@/utils/constants';
import {dateParser, getCategoryForVeggie, getRandomVeggie} from '@/utils/helpers';
import {dateParser, getCategoryForVeggie, getRandomEmojis, getRandomVeggie} from '@/utils/helpers';
import {Category, type Challenge} from '@/utils/types';
import {unique} from 'remeda';

describe('helpers', () => {
it('returns correct veggie categories', () => {
Expand Down Expand Up @@ -35,4 +36,9 @@ describe('helpers', () => {
const parsed: {foo: number; bar: number} = JSON.parse('{"foo": 1, "bar": 2}', dateParser);
expect(parsed).toEqual({foo: 1, bar: 2});
});

it('gives unique emojis', () => {
const emojis = getRandomEmojis(15);
expect(unique(emojis)).toHaveLength(15);
});
});
44 changes: 44 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,47 @@ export const dateParser = (key: string, value: any) => {
}
return value;
};

const veggieEmojis = [
'πŸ₯',
'πŸ₯₯',
'πŸ‡',
'🍈',
'πŸ‰',
'🍊',
'πŸ‹',
'🍌',
'🍍',
'πŸ₯­',
'🍎',
'🍏',
'🍐',
'πŸ‘',
'πŸ’',
'πŸ“',
'🫐',
'πŸ…',
'πŸ†',
'🌽',
'🌢️',
'πŸ«‘',
'πŸ₯‘',
'πŸ₯’',
'πŸ₯¬',
'πŸ₯¦',
'πŸ₯”',
'πŸ§„',
'πŸ§…',
'πŸ₯•',
'πŸ«›',
'πŸ₯œ',
'🫘',
'🌿',
];
export const getRandomEmojis = (amount: number = 1) => {
const emojis = new Set<string>();
while (emojis.size < amount) {
emojis.add(veggieEmojis[Math.floor(Math.random() * veggieEmojis.length)]);
}
return Array.from(emojis);
};

0 comments on commit 0bac627

Please sign in to comment.