-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathchanges.ts
More file actions
250 lines (218 loc) Β· 7.28 KB
/
Copy pathchanges.ts
File metadata and controls
250 lines (218 loc) Β· 7.28 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
export type CompanySnapshot = {
id: number;
name: string;
slug: string;
batch?: string;
one_liner?: string;
url?: string;
[key: string]: unknown;
};
export type CompanyFieldChange = {
before: unknown;
after: unknown;
};
export type UpdatedCompany = {
id: number;
name: string;
slug: string;
batch: string | undefined;
url: string | undefined;
changed_fields: string[];
changes: Record<string, CompanyFieldChange>;
};
export type CompanyChangeSet = {
generated_at: string;
summary: {
previous_total: number;
current_total: number;
added: number;
removed: number;
updated: number;
};
added: CompanySnapshot[];
removed: CompanySnapshot[];
updated: UpdatedCompany[];
};
export type BuildCompanyChangeSetOptions = {
previousCompanies: CompanySnapshot[];
currentCompanies: CompanySnapshot[];
generatedAt: string;
};
const compareCompanies = (a: CompanySnapshot, b: CompanySnapshot): number => {
const idComparison = a.id - b.id;
if (idComparison !== 0) return idComparison;
return a.slug.localeCompare(b.slug);
};
const stableStringify = (value: unknown): string => {
if (Array.isArray(value)) {
return `[${value.map((item) => stableStringify(item)).join(",")}]`;
}
if (value && typeof value === "object") {
const entries = Object.entries(value as Record<string, unknown>)
.sort(([a], [b]) => a.localeCompare(b));
return `{${
entries.map(([key, nestedValue]) =>
`${JSON.stringify(key)}:${stableStringify(nestedValue)}`
).join(",")
}}`;
}
return JSON.stringify(value);
};
const valuesEqual = (a: unknown, b: unknown): boolean =>
stableStringify(a) === stableStringify(b);
const buildCompanyMap = (
companies: CompanySnapshot[],
): Map<number, CompanySnapshot> =>
new Map(companies.map((company) => [company.id, company]));
export const buildCompanyChangeSet = ({
previousCompanies,
currentCompanies,
generatedAt,
}: BuildCompanyChangeSetOptions): CompanyChangeSet => {
const previousById = buildCompanyMap(previousCompanies);
const currentById = buildCompanyMap(currentCompanies);
const added = currentCompanies
.filter((company) => !previousById.has(company.id))
.sort(compareCompanies);
const removed = previousCompanies
.filter((company) => !currentById.has(company.id))
.sort(compareCompanies);
const updated = currentCompanies
.filter((company) => previousById.has(company.id))
.map((company) => {
const previousCompany = previousById.get(company.id)!;
const fieldNames = Array.from(
new Set([
...Object.keys(previousCompany),
...Object.keys(company),
]),
).sort((a, b) => a.localeCompare(b));
const changedFields = fieldNames.filter((fieldName) =>
!valuesEqual(previousCompany[fieldName], company[fieldName])
);
if (changedFields.length === 0) return undefined;
const changes = Object.fromEntries(
changedFields.map((fieldName) => [fieldName, {
before: previousCompany[fieldName],
after: company[fieldName],
}]),
) as Record<string, CompanyFieldChange>;
return {
id: company.id,
name: company.name,
slug: company.slug,
batch: company.batch,
url: company.url,
changed_fields: changedFields,
changes,
};
})
.filter((company): company is UpdatedCompany => company !== undefined)
.sort((a, b) => {
const idComparison = a.id - b.id;
if (idComparison !== 0) return idComparison;
return a.slug.localeCompare(b.slug);
});
return {
generated_at: generatedAt,
summary: {
previous_total: previousCompanies.length,
current_total: currentCompanies.length,
added: added.length,
removed: removed.length,
updated: updated.length,
},
added,
removed,
updated,
};
};
export const hasCompanyChanges = (changeSet: CompanyChangeSet): boolean =>
changeSet.summary.added > 0 || changeSet.summary.removed > 0 ||
changeSet.summary.updated > 0;
const truncate = (value: string): string => {
const normalized = value.replace(/\s+/g, " ").trim();
if (normalized.length <= 160) return normalized;
return `${normalized.slice(0, 157)}...`;
};
const valueToMarkdown = (value: unknown): string => {
if (typeof value === "string") return truncate(value);
if (value === null || value === undefined) return String(value);
return truncate(JSON.stringify(value));
};
const companyLink = (company: Pick<CompanySnapshot, "name" | "url">): string =>
company.url ? `[${company.name}](${company.url})` : company.name;
const renderCompanyBullet = (company: CompanySnapshot): string => {
const details = [company.batch, company.one_liner]
.filter((value): value is string =>
typeof value === "string" && value.length > 0
)
.join(" β ");
return `- ${companyLink(company)}${details ? ` (${details})` : ""}`;
};
export const renderCompanyChangesMarkdown = (
changeSet: CompanyChangeSet,
): string => {
const date = changeSet.generated_at.slice(0, 10);
let markdown = `# YC company changes for ${date}\n\n`;
markdown += `- Previous total: ${changeSet.summary.previous_total}\n`;
markdown += `- Current total: ${changeSet.summary.current_total}\n`;
markdown += `- Added: ${changeSet.summary.added}\n`;
markdown += `- Removed: ${changeSet.summary.removed}\n`;
markdown += `- Updated: ${changeSet.summary.updated}\n\n`;
if (!hasCompanyChanges(changeSet)) {
markdown += "No company records changed.\n";
return markdown;
}
if (changeSet.added.length > 0) {
markdown += "## Added companies\n\n";
markdown += changeSet.added.map(renderCompanyBullet).join("\n");
markdown += "\n\n";
}
if (changeSet.removed.length > 0) {
markdown += "## Removed companies\n\n";
markdown += changeSet.removed.map(renderCompanyBullet).join("\n");
markdown += "\n\n";
}
if (changeSet.updated.length > 0) {
markdown += "## Updated companies\n\n";
for (const company of changeSet.updated) {
markdown += `### ${companyLink(company)}\n\n`;
for (const fieldName of company.changed_fields) {
const change = company.changes[fieldName];
markdown += `- \`${fieldName}\`: ${valueToMarkdown(change.before)} β ${
valueToMarkdown(change.after)
}\n`;
}
markdown += "\n";
}
}
return markdown;
};
export type WriteCompanyChangeFilesOptions = {
directory: string;
changeSet: CompanyChangeSet;
};
export type WriteCompanyChangeFilesResult = {
written: string[];
};
export const writeCompanyChangeFiles = async ({
directory,
changeSet,
}: WriteCompanyChangeFilesOptions): Promise<WriteCompanyChangeFilesResult> => {
if (!hasCompanyChanges(changeSet)) return { written: [] };
await Deno.mkdir(directory, { recursive: true });
const date = changeSet.generated_at.slice(0, 10);
const json = JSON.stringify(changeSet, null, 2) + "\n";
const markdown = renderCompanyChangesMarkdown(changeSet);
const files = [
{ name: `${date}.json`, content: json },
{ name: `${date}.md`, content: markdown },
{ name: "latest.json", content: json },
{ name: "latest.md", content: markdown },
];
for (const file of files) {
await Deno.writeTextFile(`${directory}/${file.name}`, file.content);
}
return { written: files.map((file) => file.name) };
};