aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorRuben Beltran del Rio <ruben@unlimited.pizza>2021-12-12 20:47:13 +0100
committerRuben Beltran del Rio <ruben@unlimited.pizza>2021-12-12 20:47:13 +0100
commitc0d27c055055f4aada0a96b779e0bd661347160c (patch)
tree7fe4dea3b7328d7b15305f21e3e295405786ff88 /src/lib
parentda393520c4a1b33bffdfe26974e817f2c65267d3 (diff)
Port to svelte
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/components/insult.svelte15
-rw-r--r--src/lib/components/new_insult.svelte18
-rw-r--r--src/lib/grammar.js44
-rw-r--r--src/lib/stores/insults.js50
4 files changed, 127 insertions, 0 deletions
diff --git a/src/lib/components/insult.svelte b/src/lib/components/insult.svelte
new file mode 100644
index 0000000..61c17ec
--- /dev/null
+++ b/src/lib/components/insult.svelte
@@ -0,0 +1,15 @@
+<script>
+ export let insult
+</script>
+
+<article>
+ <header>
+ Eres un/a
+ </header>
+ <main>
+ {insult}
+ </main>
+ <footer>
+ <a href="/">Insulto nuevo.</a>
+ </footer>
+</article>
diff --git a/src/lib/components/new_insult.svelte b/src/lib/components/new_insult.svelte
new file mode 100644
index 0000000..525741a
--- /dev/null
+++ b/src/lib/components/new_insult.svelte
@@ -0,0 +1,18 @@
+<script>
+ import { getInsult } from '$lib/stores/insults';
+ import { goto } from '$app/navigation';
+
+ $: insult = getInsult();
+
+ $: {
+ if ($insult) {
+ goto(`/${$insult}`);
+ }
+ }
+</script>
+
+<article>
+ <main>
+ Insultando...
+ </main>
+</article>
diff --git a/src/lib/grammar.js b/src/lib/grammar.js
new file mode 100644
index 0000000..4831240
--- /dev/null
+++ b/src/lib/grammar.js
@@ -0,0 +1,44 @@
+import { Conjugator } from '@jirimracek/conjugate-esp';
+
+const internals = {
+ kVowels: ['a', 'e', 'i', 'o', 'u'],
+};
+
+export const pluralize = function (noun) {
+
+ const lastLetter = noun[noun.length - 1];
+
+ if (lastLetter === 's') {
+ return noun;
+ }
+
+ if (internals.kVowels.indexOf(lastLetter) >= 0) {
+ return noun + 's'
+ }
+
+ return noun + 'es';
+};
+
+export const conjugate = async function(verb) {
+
+ const conjugator = new Conjugator();
+ try {
+ const conjugation = await conjugator.conjugate(verb);
+
+ if (!conjugation || conjugation.length === 0) {
+ console.error('Could not conjugate: ', verb);
+ throw new Error(`Could not conjugate: ${verb}`)
+ }
+
+ const presentIndicative = conjugation[0]
+ .conjugation
+ .Indicativo
+ .Presente[2];
+
+ return presentIndicative;
+ }
+ catch (err) {
+ return verb.replace(/r$/, '');
+ }
+
+};
diff --git a/src/lib/stores/insults.js b/src/lib/stores/insults.js
new file mode 100644
index 0000000..d8bd340
--- /dev/null
+++ b/src/lib/stores/insults.js
@@ -0,0 +1,50 @@
+import { readable } from 'svelte/store';
+import { browser } from '$app/env';
+import { conjugate, pluralize } from '$lib/grammar';
+
+const internals = {
+ kVersion: '1:', // in case we need to force a re-fetch
+ kHost: import.meta.env.VITE_PUBLIC_BASE_PATH || 'http://localhost:3000',
+ kDataPrefix: '/data/',
+ kVerbsPath: '/verbs.json',
+ kNounsPath: '/nouns.json',
+
+ async get(path) {
+
+ const targetFile = internals.kHost + internals.kDataPrefix + path;
+ const data = browser && localStorage.getItem(internals.kVersion + targetFile);
+
+ if (data) {
+ return JSON.parse(data);
+ }
+
+ let newData = await (await fetch(targetFile)).json();
+ browser && localStorage.setItem(internals.kVersion + targetFile, JSON.stringify(newData));
+
+ return newData;
+ },
+
+ random(list) {
+
+ return list[Math.floor(Math.random() * list.length)];
+ }
+};
+
+export const getInsult = function () {
+
+ return readable(null, function (set) {
+
+ (async function() {
+
+ const verbs = await internals.get(internals.kVerbsPath);
+ const nouns = await internals.get(internals.kNounsPath);
+
+ const verb = await conjugate(internals.random(verbs));
+ const noun = pluralize(internals.random(nouns));
+
+ set(`${verb} ${noun}`.toLowerCase());
+ })();
+
+ return function stop() {};
+ });
+};