aboutsummaryrefslogtreecommitdiff
path: root/src
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
parentda393520c4a1b33bffdfe26974e817f2c65267d3 (diff)
Port to svelte
Diffstat (limited to 'src')
-rw-r--r--src/app.html13
-rw-r--r--src/global.d.ts1
-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
-rw-r--r--src/routes/[insult].svelte20
-rw-r--r--src/routes/index.svelte10
8 files changed, 171 insertions, 0 deletions
diff --git a/src/app.html b/src/app.html
new file mode 100644
index 0000000..b624a15
--- /dev/null
+++ b/src/app.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8" />
+ <link rel="icon" href="/favicon.png" />
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
+ <link rel="stylesheet" href="/global.css" />
+ %svelte.head%
+ </head>
+ <body>
+ <div id="svelte">%svelte.body%</div>
+ </body>
+</html>
diff --git a/src/global.d.ts b/src/global.d.ts
new file mode 100644
index 0000000..63908c6
--- /dev/null
+++ b/src/global.d.ts
@@ -0,0 +1 @@
+/// <reference types="@sveltejs/kit" />
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() {};
+ });
+};
diff --git a/src/routes/[insult].svelte b/src/routes/[insult].svelte
new file mode 100644
index 0000000..e577f73
--- /dev/null
+++ b/src/routes/[insult].svelte
@@ -0,0 +1,20 @@
+<script context="module">
+ export async function load({ page }) {
+ return {
+ props: {
+ insult: page.params.insult
+ }
+ }
+ }
+</script>
+<script>
+ import Insult from '$lib/components/insult.svelte';
+ export let insult;
+</script>
+
+<svelte:head>
+ <title>{insult} - insultos @ unlimited.pizza</title>
+ <meta name="description" content="Tu insulto del día es: {insult}" />
+</svelte:head>
+
+<Insult insult={insult} />
diff --git a/src/routes/index.svelte b/src/routes/index.svelte
new file mode 100644
index 0000000..13bb4d0
--- /dev/null
+++ b/src/routes/index.svelte
@@ -0,0 +1,10 @@
+<script>
+ import NewInsult from '$lib/components/new_insult.svelte';
+</script>
+
+<svelte:head>
+ <title>insultos @ unlimited.pizza</title>
+ <meta name="description" content="Insultos para toda la familia." />
+</svelte:head>
+
+<NewInsult />