Avatar

lbb00

lalala
5 public vals
Joined October 7, 2023
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import { html } from "https://esm.town/v/lbb00/html";
const TitleTextDefault = ":)";
const Title = (text) => html`<h1>${text || TitleTextDefault}</h1>`;
export function __htmlTesting() {
assertEquals(html`${undefined}`, "undefined");
assertEquals(html`${{ a: 1 }}`, `{"a":1}`);
assertEquals(html`${() => 0}`, "0");
assertEquals(html`${() => () => 0}`, "0");
// --- Usage caces ---
assertEquals(
html`<html><body>${Title}</body></html>`,
"<html><body><h1>:)</h1></body></html>",
);
assertEquals(
html`<html><body>${Title("!")}</body></html>`,
"<html><body><h1>!</h1></body></html>",
);
}

Render html by tagged templates

You can use this simple function to compose components, and you can also format the content within html`` elegantly in VSCode.

Example

const TitleTextDefault = ':)'
const Title = (text)=> html`<h1>${text || TitleTextDefault}</h1>`

// -> "<html><body><h1>:)</h1></body></html>"
html`<html><body>${Title}</body></html>` 

// -> "<html><body><h1>!</h1></body></html>"
html`<html><body>${Title('!')}</body></html>` 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function getValue(value: unknown) {
if (value instanceof Function) {
return getValue(value());
}
if (typeof value === "object") {
return JSON.stringify(value);
}
return value;
}
export function html(strings: TemplateStringsArray, ...values: unknown[]) {
const len = strings.length;
return strings.reduce((renderStr, str, idx) => {
const value = idx >= len - 1 ? "" : getValue(values[idx]);
return renderStr + str + value;
}, "");
}
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { html } from "https://esm.town/v/lbb00/html";
export async function LoginRegistryExample(request: Request): Promise<Response> {
return new Response(
html`
<html>
<style>
form {
display: none;
}
input[type='radio']:checked + label + form {
display: block;
}
</style>
<body>
<input name="method" id="login" type="radio" checked />
<label for="login">Log in</label>
<form id="loginForm">
<div>
<label for="account">Account</label>
<input name="account" type="email" autocomplete="email" placeholder="Email" required />
</div>
<div>
<label for="password">Password</label>
<input
name="password"
type="password"
name="password"
autocomplete="password"
placeholder="Password"
required
/>
</div>
<button>Log in</button>
</form>
<input name="method" id="signup" type="radio" />
<label for="signup">Sign up</label>
<form id="signupForm">
<div>
<label for="account">Account</label>
<input name="account" type="email" autocomplete="email" placeholder="Email" required />
</div>
<div>
<label for="password">Password</label>
<input
name="password"
type="password"
name="password"
autocomplete="new-password"
placeholder="Password"
required
/>
</div>
<div>
<label for="retype-password">Confirm password</label>
<input
name="retype-password"
type="password"
autocomplete="new-password"
placeholder="Retype password"
required
/>
</div>
<button>Sign up</button>
</form>
<div id="content"></div>
</body>
<script>
const forms = document.querySelectorAll('form')
forms.forEach((form) => {
form.addEventListener('submit', (e) => {
e.preventDefault()
const formData = new FormData(form)
const data = {}
for (const [key, value] of formData.entries()) {
data[key] = value
}
const div = document.querySelector('#content')
div.innerHTML = JSON.stringify(data, null, 2)
})
})
</script>
</html>
`,
{ headers: { "Content-Type": "text/html" } },
);
}
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
import { fetch } from "https://esm.town/v/std/fetch";
import process from "node:process";
export async function checkTensorArtWebStatus() {
async function sendLarkMessage(message) {
return fetch(process.env.larkTensorRobotUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
msg_type: "text",
content: {
text: message,
},
}),
}).then((resp) => resp.json());
}
const urls = [
"https://tusiart.com",
"https://tusiart.com/models/595381491993673733",
"https://tusiart.com/images/641647408012381047?post_id=641648696498376106",
"https://tensor.art",
"https://tensor.art/models/595381491993673733",
"https://tensor.art/images/651688517816058150?post_id=651693375419913839",
];
try {
const responses = await Promise.all(
urls.map((url) => {
return fetch(url);
}),
);
responses.forEach((response, idx) => {
if (response.status === 200) {
throw `{${urls[idx]}} status: ${response.status}`;
}
});
} catch (e) {
console.log(e.message);
await sendLarkMessage(`[TA checker] ${e.message}`);
return false;
}
await sendLarkMessage("[TA checker]: All urls ok");
return true;
}

https://csswizardry.com/2023/09/the-ultimate-lqip-lcp-technique

Usage

https://api.val.town/v1/run/lbb00.checkBPPRequirement?args=[imageUrl,minDisplayWidth,minDisplayHeight]

Example

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
import { fetch } from "https://esm.town/v/std/fetch";
export async function checkBPPRequirement(imageUrl, minWidth, minHeight) {
try {
const response = await fetch(imageUrl);
const buffer = await response.arrayBuffer();
const totalPixels = minWidth * minHeight;
const bppRequirement = 0.05 * totalPixels;
const minBytes = Math.ceil(bppRequirement / 8);
const actualBytes = buffer.byteLength;
console.log(`Total Pixels: ${totalPixels}`);
console.log(`BPP Requirement: ${bppRequirement.toFixed(2)} bits`);
console.log(`Minimum Bytes: ${minBytes} bytes`);
console.log(`Actual Bytes: ${actualBytes} bytes`);
if (actualBytes >= minBytes) {
console.log("👏 Image meets BPP requirement.");
return true;
}
else {
console.log("🙁 Image does not meet BPP requirement.");
return false;
}
}
catch (error) {
console.error("❌ Error fetching image:", error.message);
}
}
Next