Skip to content

Commit

Permalink
Merge pull request #11 from gouz/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
gouz authored Sep 22, 2024
2 parents 0fc5260 + b8f2918 commit efc3ce8
Show file tree
Hide file tree
Showing 16 changed files with 346 additions and 542 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,6 @@ exe/
.vscode
.scannerwork

.DS_Store
.DS_Store

tmp
31 changes: 31 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true,
"defaultBranch": "main"
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"correctness": {
"noUnusedImports": "error",
"noUnusedVariables": "error",
"useExhaustiveDependencies": "warn"
}
}
},
"formatter": {
"enabled": true,
"indentStyle": "space",
"indentWidth": 2
},
"files": {
"ignore": ["example", "src/plugins.json", "docs"]
}
}
Binary file modified bun.lockb
Binary file not shown.
9 changes: 9 additions & 0 deletions choc.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module "*.html" {
const content: string;
export default content;
}

declare module "*.css" {
const content: string;
export default content;
}
12 changes: 7 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
{
"name": "choc",
"version": "0.1.13",
"version": "0.2.0",
"module": "src/index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
"typescript": "^5.4.5"
},
"bin": {
"choc": "./bin/index.js"
},
"scripts": {
"make": "bun build src/index.ts --outfile exe/choc --compile --minify"
"dev": "bun src/index.ts",
"make": "bun build src/index.ts --outfile exe/choc --compile --minify",
"format": "bunx @biomejs/biome format ./ --write",
"lint": "bunx @biomejs/biome lint ./ --write"
},
"dependencies": {
"commander": "^12.0.0"
"commander": "^12.1.0",
"open": "^10.1.0"
}
}
167 changes: 23 additions & 144 deletions src/DTO.ts
Original file line number Diff line number Diff line change
@@ -1,145 +1,24 @@
import type { Options, SpeakerData, Talk, TalkRow } from "./types";
import { splitString } from "./utils";
import type { ConferenceHallTalk, TalkRow } from "./types";

const getCompanies = (speakerHash: Map<string, SpeakerData>, uid: string) => ({
company: speakerHash.get(uid)?.company,
});

const getAdresses = (speakerHash: Map<string, SpeakerData>, uid: string) => ({
addresses: speakerHash.get(uid)?.address,
});

const getFormats = (formatsHash: Map<string, string>, format: string) => ({
format: formatsHash.get(format),
});

const getCategories = (
categoriesHash: Map<string, string>,
category: string
) => ({
categories: categoriesHash.get(category),
});

export const DTO = (
talks: Talk[],
options: Options,
speakerHash: Map<string, SpeakerData>,
formatsHash: Map<string, string>,
categoriesHash: Map<string, string>
): TalkRow[] =>
talks.flatMap(
(
{
id,
title,
speakers,
formats,
categories,
rating,
loves,
hates,
language,
},
position: number
) => {
const lines: TalkRow[] = [];
let titleSplit: string[] = splitString(title, options.titlelength).map(
(text) => text.padEnd(options.titlelength, " ")
);
speakers.forEach((uid: string, i: number) => {
if (i === 0) {
lines.push({
position: position + 1,
title: titleSplit.shift(),
...(options.withFormats ? getFormats(formatsHash, formats) : {}),
...(options.withCategories
? getCategories(categoriesHash, categories)
: {}),
speakers: speakerHash.get(uid)?.name,
...(options.withCompanies ? getCompanies(speakerHash, uid) : {}),
...(options.withAddresses ? getAdresses(speakerHash, uid) : {}),
...(options.withLanguages ? { language } : {}),
rating: Number(Number(rating ?? "0").toFixed(2)),
loves,
hates,
...(options.links
? {
link: `https://conference-hall.io/organizer/event/${options.links}/proposals/${id}`,
}
: {}),
});
} else
lines.push({
title: titleSplit.shift() ?? "",
speakers: speakerHash.get(uid)?.name,
...(options.withCompanies ? getCompanies(speakerHash, uid) : {}),
...(options.withAddresses ? getAdresses(speakerHash, uid) : {}),
});
});
titleSplit.forEach((title) => lines.push({ title }));
lines.push({});
return lines;
}
);

const cleanArray = (arrayString: (string | undefined)[]): string[] =>
Array.from(
new Set(arrayString.map((c) => c?.toLowerCase() ?? "").filter((c) => c))
).sort((a, b) => a.localeCompare(b));

export const DTOExport = (
talks: Talk[],
options: Options,
speakerHash: Map<string, SpeakerData>,
formatsHash: Map<string, string>,
categoriesHash: Map<string, string>
): TalkRow[] =>
talks.flatMap(
(
{
title,
speakers,
formats,
categories,
rating,
loves,
hates,
language,
id,
},
position: number
) => ({
position: position + 1,
title: title,
...(options.withFormats ? getFormats(formatsHash, formats) : {}),
...(options.withCategories
? getCategories(categoriesHash, categories)
: {}),
speakers: speakers
.map((uid: string) => speakerHash.get(uid)?.name)
.join(", "),
...(options.withCompanies
? {
company: cleanArray(
speakers.map((uid: string) => speakerHash.get(uid)?.company)
).join(", "),
}
: {}),
...(options.withAddresses
? {
address: cleanArray(
speakers.map((uid: string) => speakerHash.get(uid)?.address)
),
}
: {}),
...(options.withLanguages ? { language } : {}),
rating: Number(Number(rating ?? "0").toFixed(2)),
loves,
hates,
...(options.links
? {
link: `https://conference-hall.io/organizer/event/${options.links}/proposals/${id}`,
}
: {}),
})
);
export const DTO = (talks: ConferenceHallTalk[]): TalkRow[] =>
talks.map((talk) => ({
id: talk.id,
title: talk.title,
abstract: talk.abstract,
format: talk.formats.shift()?.name,
category: talk.categories.shift()?.name,
speakers: talk.speakers?.map(({ name, company, location, picture }) => ({
name,
company,
location,
picture,
})),
rating: talk.reviews?.average,
positives: talk.reviews?.positives,
negatives: talk.reviews?.negatives,
deliberationStatus: talk.deliberationStatus,
confirmationStatus: talk.confirmationStatus,
level: talk.level,
languages: talk.languages,
link: `https://conference-hall.io/organizer/event/${Bun.env.EVENTID}/proposals/${talk.id}`,
}));
105 changes: 50 additions & 55 deletions src/choc.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,55 @@
import { DTO, DTOExport } from "./DTO";
import render from "./render";
import type {
Format,
Options,
Speaker,
SpeakerData,
Talk,
TalkRow,
} from "./types";
import open from "open";
import { DTO } from "./DTO";
import html from "./render/index.html" with { type: "text" };
import js from "./render/main.js" with { type: "text" };
import css from "./render/main.css" with { type: "text" };
import type { Options } from "./types.js";

const choc = async (file: string, options: Options) => {
const json = await Bun.file(file).json();
if (options.render) {
render(json, options);
} else {
const { talks, speakers, formats, categories } = json;
const formatsHash = new Map<string, string>();
(formats as Format[]).forEach(({ id, name }) => {
formatsHash.set(id, name);
});
const speakerHash = new Map<string, SpeakerData>();
(speakers as Speaker[]).forEach(
({ uid, displayName, company, address }) => {
speakerHash.set(uid, {
name: displayName,
company: company ?? "",
address: address?.formattedAddress ?? "",
});
}
);
const categoriesHash = new Map<string, string>();
(categories as Format[]).forEach(({ id, name }) => {
categoriesHash.set(id, name);
});
const talksLines = (talks as Talk[]).sort((a, b) =>
a.rating <= b.rating ? 1 : -1
);
if (options.export) {
const exportLines = DTOExport(
talksLines,
options,
speakerHash,
formatsHash,
categoriesHash
);
Bun.write(
options.export,
`${Object.keys(exportLines[0]).join("\t")}\n${exportLines
.map((t: TalkRow) => Object.values(t).join("\t"))
.join("\n")}`
);
} else
console.table(
DTO(talksLines, options, speakerHash, formatsHash, categoriesHash)
);
}
const talks = DTO(await Bun.file(file).json());
const server = Bun.serve({
port: 1337,
async fetch(req) {
const path = new URL(req.url).pathname;
if (path === "/main.css")
return new Response(
`
${css}
${options.withAddresses ? ".addresses {display: table-cell;}" : ""}
${options.withLanguages ? ".languages {display: table-cell;}" : ""}
${options.withFormats ? ".format {display: table-cell;}" : ""}
${options.withCompanies ? ".companies {display: table-cell;}" : ""}
${options.withCategories ? ".category {display: table-cell;}" : ""}
`,
{
headers: { "content-type": "text/css" },
},
);
if (path === "/main.js")
return new Response(
`
const talks = ${JSON.stringify(talks).replaceAll("\n", "<br>")};
const options = ${JSON.stringify(options)};
${js}
`.trim(),
{
headers: {
"Content-Type": "application/javascript",
},
status: 200,
},
);
return new Response(html, {
headers: {
"Content-Type": "text/html",
},
status: 200,
});
},
});

console.log("🍫 is listening on \x1b[1m\x1b[35;49m%s\x1b[0m", server.url);
open(server.url.toString());
};

export default choc;
Loading

0 comments on commit efc3ce8

Please sign in to comment.