Skip to content

Commit

Permalink
feat: ✨ add Commander
Browse files Browse the repository at this point in the history
  • Loading branch information
gouz committed Apr 18, 2024
1 parent d1a87ca commit 3ac3511
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 119 deletions.
Binary file modified bun.lockb
Binary file not shown.
115 changes: 0 additions & 115 deletions index.ts

This file was deleted.

12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "conferencehall-orga-classment",
"module": "index.ts",
"name": "choc",
"version": "0.1.0",
"module": "src/index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest"
Expand All @@ -9,9 +10,12 @@
"typescript": "^5.0.0"
},
"bin": {
"coc": "./bin/index.js"
"choc": "./bin/index.js"
},
"scripts": {
"make": "bun build index.ts --outfile exe/coc --compile --minify --sourcemap=none"
"make": "bun build src/index.ts --outfile exe/choc --compile --minify --sourcemap=none"
},
"dependencies": {
"commander": "^12.0.0"
}
}
102 changes: 102 additions & 0 deletions src/choc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import type {
Format,
Options,
Speaker,
SpeakerData,
Talk,
TalkRow,
} from "./types";
import { removeEmojis, splitString } from "./utils";

const choc = async (file: string, options: Options) => {
const { talks, speakers, formats, categories } = await Bun.file(file).json();
const formatsHash = new Map<string, string>();
(formats as Format[]).forEach(({ id, name }) => {
formatsHash.set(id, removeEmojis(name));
});
const speakerHash = new Map<string, SpeakerData>();
(speakers as Speaker[]).forEach(({ uid, displayName, company }) => {
speakerHash.set(uid, {
name: removeEmojis(displayName),
company: removeEmojis(company ?? ""),
});
});
const categoriesHash = new Map<string, string>();
(categories as Format[]).forEach(({ id, name }) => {
categoriesHash.set(id, removeEmojis(name));
});
console.table(
(talks as Talk[])
.sort((a, b) => (a.rating <= b.rating ? 1 : -1))
.flatMap(
(
{
title,
speakers,
formats,
categories,
rating,
loves,
hates,
language,
},
position: number
) => {
const lines: TalkRow[] = [];
let titleSplit: string[] = splitString(
removeEmojis(title),
options.titlelength
).map((text) => text.padEnd(options.titlelength, " "));
speakers.forEach((uid: string, i: number) => {
let addCompanies = {};
if (options.withCompanies)
addCompanies = {
company: speakerHash.get(uid)?.company,
};
if (i === 0) {
const line = {
position: position + 1,
title: titleSplit.shift(),
};
let addFormats = {};
if (options.withFormats)
addFormats = {
format: formatsHash.get(formats),
};
let addCategories = {};
if (options.withCategories)
addCategories = {
categories: categoriesHash.get(categories),
};
let addLanguages = {};
if (options.withLanguages)
addLanguages = {
language: language,
};
lines.push({
...line,
...addFormats,
...addCategories,
speakers: speakerHash.get(uid)?.name,
...addCompanies,
...addLanguages,
rating: Number(rating.toFixed(2)),
loves,
hates,
});
} else
lines.push({
title: titleSplit.shift() ?? "",
speakers: speakerHash.get(uid)?.name,
...addCompanies,
});
});
titleSplit.forEach((title) => lines.push({ title }));
lines.push({});
return lines;
}
)
);
};

export default choc;
28 changes: 28 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bun
import { CommanderError, program } from "commander";
import packagejson from "../package.json";
import choc from "./choc";

function myParseInt(value: string) {
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
throw new CommanderError(0, "nan", "Not a number.");
}
return parsedValue;
}

program
.name("choc")
.description("ConferenceHall organization companion")
.version(packagejson.version, "-v, --version");

program
.argument("<json>", "the json export file")
.option("-c, --with-categories", "view categories", false)
.option("-f, --with-formats", "view formats", false)
.option("-e, --with-companies", "view speakers company", false)
.option("-l, --with-languages", "view talks language", false)
.option("-t, --titlelength <int>", "the title length", myParseInt, 100)
.action(choc);

program.parse();
45 changes: 45 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export type TalkRow = {
position?: number;
title?: string;
format?: string;
categories?: string;
speakers?: string;
rating?: number;
loves?: number;
hates?: number;
};

export type Format = {
id: string;
name: string;
};

export type Speaker = {
uid: string;
displayName: string;
company: string;
};

export type SpeakerData = {
name: string;
company: string;
};

export type Talk = {
title: string;
speakers: string[];
formats: string;
categories: string;
rating: number;
loves: number;
hates: number;
language: string;
};

export type Options = {
withCategories: boolean;
withCompanies: boolean;
withFormats: boolean;
withLanguages: boolean;
titlelength: number;
};
22 changes: 22 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const splitString = (str: string, n: number): string[] => {
let arr = str?.split(" ");
let result = [];
let subStr = arr[0];
for (let i = 1; i < arr.length; i++) {
let word = arr[i];
if (subStr.length + word.length + 1 <= n) {
subStr = subStr + " " + word;
} else {
result.push(subStr);
subStr = word;
}
}
if (subStr.length) {
result.push(subStr);
}
return result;
};

export const removeEmojis = (str: string): string => {
return str?.replace(/[^\p{L}\p{N}\p{P}\p{Z}^$\n]/gu, "").trim();
};

0 comments on commit 3ac3511

Please sign in to comment.