Skip to content

Commit

Permalink
Fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ackuq committed Sep 17, 2023
1 parent 09f3654 commit a0c6b36
Show file tree
Hide file tree
Showing 14 changed files with 2,044 additions and 1,034 deletions.
5 changes: 3 additions & 2 deletions packages/party-data/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ async function categorize() {
}));

for (const standpoint of standpoints) {
await select({
const choice = await select({
message: `
Party: ${standpoint.party}
URL: ${standpoint.url}
Title: ${standpoint.title}
Opinions:
${standpoint.opinions.map((opinion) => `\t${opinion}`).join("\n")}`,
choices: subjectChoices,
});
standpoint.subject = choice;
}
}

Expand Down
14 changes: 4 additions & 10 deletions packages/party-data/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ export interface Standpoint {
url: string;
opinions: string[];
fetchDate: string;
party: string;
subject?: string;
}

export type StandpointWithParty = Standpoint & { party: Party };

export function getSubjects(): Subject[] {
return Object.values(subjects);
}
Expand All @@ -60,20 +59,15 @@ function getPartyData(abbreviation: string) {
throw new Error(`No such party ${abbreviation}`);
}

export function readPartyStandpoints(
abbreviation: Party,
): StandpointWithParty[] {
return Object.values(getPartyData(abbreviation)).map((standpoint) => ({
...standpoint,
party: abbreviation,
}));
export function readPartyStandpoints(abbreviation: Party): Standpoint[] {
return Object.values(getPartyData(abbreviation) as unknown);
}

export function readPartyDataForSubject(party: Party, subjectName: string) {
const partyData = readPartyStandpoints(party);
return partyData.filter((subject) => subject.subject === subjectName);
}

export function readAllStandpoints(): StandpointWithParty[] {
export function readAllStandpoints(): Standpoint[] {
return Object.values(Party).map(readPartyStandpoints).flat();
}
19 changes: 18 additions & 1 deletion packages/party-data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export interface PartyData {
[url: string]: Standpoint;
}

export type PartyDataWithoutPartyName = Omit<Standpoint, "party">;

interface SubjectData {
[name: string]: Subject;
}
Expand All @@ -18,7 +20,20 @@ const partyFileName = (abbreviation: string) =>
/**
* Function to append, update, or delete party data, with a new snapshot.
*/
export function writePartyData(abbreviation: string, data: PartyData) {
export function writePartyData(
abbreviation: string,
list: PartyDataWithoutPartyName[],
) {
const data = list.reduce((prev, current) => {
const entry: PartyData[string] = {
...current,
party: abbreviation,
};
return {
...prev,
[current.url]: entry,
};
}, {} as PartyData);
const fileName = partyFileName(abbreviation);

if (!fs.existsSync(fileName)) {
Expand Down Expand Up @@ -62,6 +77,8 @@ export function writePartyData(abbreviation: string, data: PartyData) {
fs.writeFileSync(fileName, JSON.stringify(storedData, null, 2));
}

// export function updateSubject(abbreviation: string, standpoint: Standpoint) {}

function readSubjectData() {
return JSON.parse(fs.readFileSync(SUBJECTS_FILE).toString()) as SubjectData;
}
Expand Down
Loading

0 comments on commit a0c6b36

Please sign in to comment.