Avatar

joey

14 public vals
Joined January 1, 2023

Blob Array

Create an array as a Database, instantly!

Just create a new val for your array:

Create valimport { BlobArray } from "https://esm.town/v/joey/BlobArray"; type User = { email: string }; // specify the type inside the TS generic <> to define the type of your array export let myUserArray = BlobArray<User>("my-user-array");

Now import it and start using it in your vals!

Create valimport { myUserArray } from "https://esm.town/v/joey/myUserArray"; // overwrite the entire array await myUserArray.set([{ email: "user1@gmail.com" }, { email: "user2@yahoo.com" }]); // get all the entries in your array const allUsers = await myUserArray.get(); // call any array method you want, just use "await" await myUserArray.push({ email: "johndoe@gmail.com" }); const gmailUsers = await myUserArray.filter(u => u.email.includes("@gmail.com"));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { blob } from "https://esm.town/v/std/blob";
export const BlobArray = <T extends any>(id: string) => {
const getValue = (): Promise<T[]> => blob.getJSON(id).then(json => JSON.parse(json || "[]"));
const setValue = (arr: T[]) => blob.setJSON(id, JSON.stringify(arr)).then(res => arr);
return {
set: setValue,
get: getValue,
// mutational
push: async (...args: Parameters<Array<T>["push"]>) =>
getValue().then(async (arr) => {
const res = arr.push(...args);
await setValue(arr);
return res;
}),
pop: () =>
getValue().then(async (arr) => {
const res = arr.pop();
await setValue(arr);
return res;
}),
splice: async (...args: Parameters<Array<T>["splice"]>) => {
const current = await getValue();
const res = current.splice(...args);
await setValue(current);
return res;
},
unshift: async (...args: Parameters<Array<T>["unshift"]>) => {
const current = await getValue();
const res = current.unshift(...args);
await setValue(current);
return res;
},
reverse: async (...args: Parameters<Array<T>["reverse"]>) => {
const current = await getValue();
const res = current.reverse(...args);
await setValue(current);
return res;
},
sort: async (...args: Parameters<Array<T>["sort"]>) => {
const current = await getValue();
const res = current.sort(...args);
await setValue(current);
return res;
},
// non-mutation
concat: (...args: Parameters<Array<T>["concat"]>) => getValue().then(arr => arr.concat(...args)),
every: (...args: Parameters<Array<T>["every"]>) => getValue().then(arr => arr.every(...args)),
filter: (...args: Parameters<Array<T>["filter"]>) => getValue().then(arr => arr.filter(...args)),
find: (...args: Parameters<Array<T>["find"]>) => getValue().then(arr => arr.find(...args)),
findIndex: (...args: Parameters<Array<T>["findIndex"]>) => getValue().then(arr => arr.findIndex(...args)),
flat: (...args: Parameters<Array<T>["flat"]>) => getValue().then(arr => arr.flat(...args)),
flatMap: (...args: Parameters<Array<T>["flatMap"]>) => getValue().then(arr => arr.flatMap(...args)),
forEach: (...args: Parameters<Array<T>["forEach"]>) => getValue().then(arr => arr.forEach(...args)),
includes: (...args: Parameters<Array<T>["includes"]>) => getValue().then(arr => arr.includes(...args)),
indexOf: (...args: Parameters<Array<T>["indexOf"]>) => getValue().then(arr => arr.indexOf(...args)),
lastIndexOf: (...args: Parameters<Array<T>["lastIndexOf"]>) => getValue().then(arr => arr.lastIndexOf(...args)),
map: (...args: Parameters<Array<T>["map"]>) => getValue().then(arr => arr.map(...args)),
reduce: (...args: Parameters<Array<T>["reduce"]>) => getValue().then(arr => arr.reduce(...args)),
slice: (...args: Parameters<Array<T>["slice"]>) => getValue().then(arr => arr.slice(...args)),
some: (...args: Parameters<Array<T>["some"]>) => getValue().then(arr => arr.some(...args)),
};
};
1
2
3
4
5
6
7
export const helloWorld = (...args: any) => {
console.log("hi");
return {
message: "hello from valtown",
yourArgs: args,
};
};
1
2
3
export const myPublicVal = (a: string, b: string) => {
return "hello world this is from val town, your args:" + a + b;
};
1
export const joeysPublicArray = [];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { getGithubStarsForRepo } from "https://esm.town/v/joey/getGithubStarsForRepo";
import { PUBLIC_STATE } from "https://esm.town/v/joey/PUBLIC_STATE";
import { email } from "https://esm.town/v/std/email?v=9";
import { runVal } from "https://esm.town/v/std/runVal";
// Notifies you when you get a new star on your repo
// Usage:
// Pass username and project name from url: github.com/joeroddy/bridg =>
// const githubCron = async() => @joey.checkGithubStars('joeroddy','bridg')
export const emailOnGithubStar = async (username: string, project: string, toEmail: string) => {
const count = await getGithubStarsForRepo(username, project);
const prevCount = PUBLIC_STATE.STAR_COUNTS[project] || 0;
if (count && count > prevCount) {
console.log(`New star on ${project}: ${count}`);
email(
{
to: toEmail,
subject: `New star on your project! ${project}`,
text: `You now have ${count} stars, they were wrong about you. You're a major success!`,
},
);
}
if (count || count === 0) {
await runVal("joey.setPublicState", `STAR_COUNTS/${project}`, count);
}
};
1
2
3
4
5
6
7
8
9
10
11
12
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
// Returns stars on Github repo
// Usage:
// await @joey.getGithubStarsForRepo('joeroddy','bridg');
// username, project name from url: github.com/joeroddy/bridg
export const getGithubStarsForRepo = async (username: string, project: string) => {
const res = await fetchJSON(
`https://api.github.com/repos/${username}/${project}`
);
return parseInt(res.stargazers_count);
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { set } from "https://esm.town/v/std/set?v=11";
import { PUBLIC_STATE } from "https://esm.town/v/joey/PUBLIC_STATE";
export const setPublicState = async (key: string, value: any) => {
const keys = key.split("/");
let current = PUBLIC_STATE;
for (let i = 0; i < keys.length - 1; i++) {
const key = keys[i];
if (!current[key]) {
current[key] = {};
}
current = current[key];
}
current[keys[keys.length - 1]] = value;
await set("PUBLIC_STATE", PUBLIC_STATE);
};
1
2
// set at Tue May 14 2024 09:26:46 GMT+0000 (Coordinated Universal Time)
export let PUBLIC_STATE = {"STAR_COUNTS":{"bridg":125,"combust":7,"prismany":17}};
1
2
3
4
5
6
7
8
9
// USAGE:
// const [odd, even] = @joey.partitionArray([1,2,3,4,5,6], n=> n % 2 !== 0);
// result: [[1,3,5],[2,4,6]]
export const partitionArray = (array, isValid) =>
array.reduce(
([pass, fail], elem) =>
isValid(elem) ? [[...pass, elem], fail] : [pass, [...fail, elem]],
[[], []]
);
1
2
3
4
5
6
7
8
9
10
11
12
import { renderHtml } from "https://esm.town/v/joey/renderHtml";
export const untitled_sendForm = (...args) => {
return renderHtml(
args,
`<form action="https://api.val.town/eval/@joey.untitled_handleForm">
Subject <input name="subject" /> <br/>
<textarea name="body" placeholder="Enter message body.."></textarea><br/>
<button>Submit</button>
</form>`
);
};