Skip to content

Instantly share code, notes, and snippets.

@motss
Last active April 27, 2023 17:51
Show Gist options
  • Save motss/19338430467cb71bd3aafb0905e903bf to your computer and use it in GitHub Desktop.
Save motss/19338430467cb71bd3aafb0905e903bf to your computer and use it in GitHub Desktop.
Generate custom badge URL using shields.io and deno.bundlejs.com
function tryParseUrl(url) {
try {
const u = new URL(url);
return u;
} catch (error) {
console.error(error);
return '';
}
}
function getBundlejsBadgeUrl(
init
) {
const baseUrl = 'https://img.shields.io/badge/dynamic/json';
const params = new URLSearchParams();
for (const n of ['color', 'label', 'logo', 'prefix', 'query', 'style', 'suffix', 'url']) {
const v = init[n] || '';
if (v) {
if (n === 'url') {
const url = tryParseUrl(v);
if (url) {
const sp = new URLSearchParams();
const l = [];
for (const [k, val] of url.searchParams) {
if (k === 'config') {
sp.set(k, val);
} else {
l.push(`${k}=${val}`)
}
}
url.search = `?${[...l, sp.toString()].join('&')}`;
params.set(n, url.toString());
}
} else {
params.set(n, v);
}
}
}
return `${baseUrl}?${params.toString()}`;
}
const config = JSON.stringify({
compression: {
type: 'brotli',
quality: 11
}
});
getBundlejsBadgeUrl({
label: 'Bundle size',
// logo: 'github',
name: '@ipohjs/calendar',
query: '$.size.size',
style: 'flat-square',
url: `https://deno.bundlejs.com?q=@ipohjs/calendar&config=${config}`,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment