]>
git.r.bdr.sh - rbdr/generador-de-insultos/blob - src/lib/stores/insults.js
1 import { readable
} from 'svelte/store';
2 import { browser
} from '$app/env';
3 import { conjugate
, pluralize
} from '$lib/grammar';
6 kVersion: '1:', // in case we need to force a re-fetch
7 kHost: import.meta
.env
.VITE_PUBLIC_BASE_PATH
|| 'http://localhost:3000',
9 kVerbsPath: 'verbs.json',
10 kNounsPath: 'nouns.json',
14 const targetFile
= internals
.kHost
+ internals
.kDataPrefix
+ path
;
15 const data
= browser
&& localStorage
.getItem(internals
.kVersion
+ targetFile
);
18 return JSON
.parse(data
);
21 let newData
= await (await
fetch(targetFile
)).json();
22 browser
&& localStorage
.setItem(internals
.kVersion
+ targetFile
, JSON
.stringify(newData
));
29 return list
[Math
.floor(Math
.random() * list
.length
)];
33 export const getInsult = function () {
35 return readable(null, function (set) {
39 const verbs
= await internals
.get(internals
.kVerbsPath
);
40 const nouns
= await internals
.get(internals
.kNounsPath
);
42 const verb
= await
conjugate(internals
.random(verbs
));
43 const noun
= pluralize(internals
.random(nouns
));
45 set(`${verb} ${noun}`.toLowerCase());
48 return function stop() {};