RohanVashisht commited on
Commit
0b3e194
·
1 Parent(s): 09c5b79

removed commets and corrected type.

Browse files
Files changed (1) hide show
  1. src/index.ts +2 -82
src/index.ts CHANGED
@@ -43,46 +43,20 @@ const dataStore: DataStore = {
43
  };
44
 
45
  // --- Helper Functions ---
46
-
47
- /**
48
- * Removes the 'readme_content' field from an array of Repo objects.
49
- * Creates a shallow copy of each repo to avoid modifying the original data.
50
- * @param repos - An array of Repo objects.
51
- * @returns A new array of Repo objects without the 'readme_content'.
52
- */
53
  function removeReadmeContent(repos: Repo[]): Repo[] {
54
- return repos.map((repo) => {
55
  const { readme_content, ...rest } = repo; // Destructure to exclude readme_content
56
  return rest;
57
- });
58
  }
59
 
60
- /**
61
- * Sorts repositories by creation date (newest first).
62
- * @param a - First Repo object.
63
- * @param b - Second Repo object.
64
- * @returns A number indicating sort order.
65
- */
66
  const sortByDate = (a: Repo, b: Repo) =>
67
  new Date(b.created_at ?? "").getTime() -
68
  new Date(a.created_at ?? "").getTime();
69
 
70
- /**
71
- * Sorts repositories by stargazers count (most used first).
72
- * @param a - First Repo object.
73
- * @param b - Second Repo object.
74
- * @returns A number indicating sort order.
75
- */
76
  const sortByUsage = (a: Repo, b: Repo) =>
77
  (b.stargazers_count ?? 0) - (a.stargazers_count ?? 0);
78
 
79
- /**
80
- * Filters an array of Repo objects based on a search query and a topic filter.
81
- * @param items - The array of Repo objects to filter.
82
- * @param q - The search query string (case-insensitive).
83
- * @param filter - The topic filter string (case-insensitive).
84
- * @returns A new array of filtered Repo objects.
85
- */
86
  function filterItems(
87
  items: Repo[],
88
  q: string | null,
@@ -114,25 +88,11 @@ function filterItems(
114
  );
115
  }
116
 
117
- /**
118
- * Paginates an array of items.
119
- * @param items - The array of items to paginate.
120
- * @param page - The page number (0-indexed).
121
- * @param size - The number of items per page.
122
- * @returns A new array containing items for the specified page.
123
- */
124
  function getPaginated<T>(items: T[], page = 0, size = DEFAULT_PAGINATION_SIZE): T[] {
125
  const start = page * size;
126
  return items.slice(start, start + size);
127
  }
128
 
129
- /**
130
- * Parses a range string (e.g., "0..10") into start and end numbers.
131
- * Ensures the range is within valid bounds.
132
- * @param str - The range string.
133
- * @param max - The maximum possible value for the end of the range.
134
- * @returns A tuple [start, end].
135
- */
136
  function parseRange(str: string | null, max: number): [number, number] {
137
  const match = str?.match(/^(\d+)\.\.(\d+)$/);
138
  let start = 0;
@@ -147,12 +107,6 @@ function parseRange(str: string | null, max: number): [number, number] {
147
  }
148
 
149
  // --- Data Loading and Caching ---
150
-
151
- /**
152
- * Fetches data from a given URL and parses it as JSON.
153
- * @param url - The URL to fetch.
154
- * @returns A Promise that resolves with the parsed JSON data, or null if an error occurs.
155
- */
156
  async function fetchData<T>(url: string): Promise<T | null> {
157
  try {
158
  const response = await fetch(url);
@@ -167,10 +121,6 @@ async function fetchData<T>(url: string): Promise<T | null> {
167
  }
168
  }
169
 
170
- /**
171
- * Loads and processes all repository data (packages and programs).
172
- * Updates the global dataStore and pre-sorts the data.
173
- */
174
  async function loadData() {
175
  console.log("Attempting to load data...");
176
  try {
@@ -200,9 +150,6 @@ async function loadData() {
200
  }
201
  }
202
 
203
- /**
204
- * Checks if data needs to be refreshed and triggers loadData if necessary.
205
- */
206
  async function ensureDataLoaded() {
207
  if (Date.now() - dataStore.lastLoaded > REFRESH_INTERVAL_MS) {
208
  console.log("Data is stale, refreshing...");
@@ -221,13 +168,6 @@ await loadData();
221
  setInterval(ensureDataLoaded, REFRESH_INTERVAL_MS / 2); // Check more frequently than refresh interval
222
 
223
  // --- API Route Handlers ---
224
-
225
- /**
226
- * Handles search requests for packages or programs.
227
- * @param items - The array of Repo items to search.
228
- * @param searchParams - URLSearchParams object from the request.
229
- * @returns A Response object with filtered and truncated results.
230
- */
231
  function handleSearch(items: Repo[], searchParams: URLSearchParams): Response {
232
  const q = searchParams.get("q")?.trim() ?? null;
233
  const filter = searchParams.get("filter")?.trim() ?? null;
@@ -235,12 +175,6 @@ function handleSearch(items: Repo[], searchParams: URLSearchParams): Response {
235
  return Response.json(removeReadmeContent(result), { headers: CORS_HEADERS });
236
  }
237
 
238
- /**
239
- * Handles infinite scroll requests for packages or programs.
240
- * @param items - The array of Repo items to paginate.
241
- * @param searchParams - URLSearchParams object from the request.
242
- * @returns A Response object with paginated results.
243
- */
244
  function handleInfiniteScroll(items: Repo[], searchParams: URLSearchParams): Response {
245
  const page = parseInt(searchParams.get("pageNumber") || "0", 10);
246
  if (isNaN(page) || page < 0) {
@@ -253,14 +187,6 @@ function handleInfiniteScroll(items: Repo[], searchParams: URLSearchParams): Res
253
  return Response.json(removeReadmeContent(result), { headers: CORS_HEADERS });
254
  }
255
 
256
- /**
257
- * Handles requests for a single package or program by owner/repo name.
258
- * @param items - The array of Repo items to search within.
259
- * @param owner - The owner part of the full_name.
260
- * @param repo - The repo part of the full_name.
261
- * @param repoFrom - The repo_from identifier.
262
- * @returns A Response object with the found item or a 404 error.
263
- */
264
  function handleSingleItem(
265
  items: Repo[],
266
  owner: string,
@@ -283,12 +209,6 @@ function handleSingleItem(
283
  );
284
  }
285
 
286
- /**
287
- * Handles requests for index details (latest or most used).
288
- * @param sortedData - The pre-sorted data object (e.g., dataStore.sortedPackages).
289
- * @param searchParams - URLSearchParams object from the request.
290
- * @returns A Response object with sliced sorted results.
291
- */
292
  function handleIndexDetails(
293
  sortedData: { latest: Repo[]; mostUsed: Repo[] },
294
  searchParams: URLSearchParams
 
43
  };
44
 
45
  // --- Helper Functions ---
 
 
 
 
 
 
 
46
  function removeReadmeContent(repos: Repo[]): Repo[] {
47
+ return (repos.map((repo) => {
48
  const { readme_content, ...rest } = repo; // Destructure to exclude readme_content
49
  return rest;
50
+ }) as Repo[]);
51
  }
52
 
 
 
 
 
 
 
53
  const sortByDate = (a: Repo, b: Repo) =>
54
  new Date(b.created_at ?? "").getTime() -
55
  new Date(a.created_at ?? "").getTime();
56
 
 
 
 
 
 
 
57
  const sortByUsage = (a: Repo, b: Repo) =>
58
  (b.stargazers_count ?? 0) - (a.stargazers_count ?? 0);
59
 
 
 
 
 
 
 
 
60
  function filterItems(
61
  items: Repo[],
62
  q: string | null,
 
88
  );
89
  }
90
 
 
 
 
 
 
 
 
91
  function getPaginated<T>(items: T[], page = 0, size = DEFAULT_PAGINATION_SIZE): T[] {
92
  const start = page * size;
93
  return items.slice(start, start + size);
94
  }
95
 
 
 
 
 
 
 
 
96
  function parseRange(str: string | null, max: number): [number, number] {
97
  const match = str?.match(/^(\d+)\.\.(\d+)$/);
98
  let start = 0;
 
107
  }
108
 
109
  // --- Data Loading and Caching ---
 
 
 
 
 
 
110
  async function fetchData<T>(url: string): Promise<T | null> {
111
  try {
112
  const response = await fetch(url);
 
121
  }
122
  }
123
 
 
 
 
 
124
  async function loadData() {
125
  console.log("Attempting to load data...");
126
  try {
 
150
  }
151
  }
152
 
 
 
 
153
  async function ensureDataLoaded() {
154
  if (Date.now() - dataStore.lastLoaded > REFRESH_INTERVAL_MS) {
155
  console.log("Data is stale, refreshing...");
 
168
  setInterval(ensureDataLoaded, REFRESH_INTERVAL_MS / 2); // Check more frequently than refresh interval
169
 
170
  // --- API Route Handlers ---
 
 
 
 
 
 
 
171
  function handleSearch(items: Repo[], searchParams: URLSearchParams): Response {
172
  const q = searchParams.get("q")?.trim() ?? null;
173
  const filter = searchParams.get("filter")?.trim() ?? null;
 
175
  return Response.json(removeReadmeContent(result), { headers: CORS_HEADERS });
176
  }
177
 
 
 
 
 
 
 
178
  function handleInfiniteScroll(items: Repo[], searchParams: URLSearchParams): Response {
179
  const page = parseInt(searchParams.get("pageNumber") || "0", 10);
180
  if (isNaN(page) || page < 0) {
 
187
  return Response.json(removeReadmeContent(result), { headers: CORS_HEADERS });
188
  }
189
 
 
 
 
 
 
 
 
 
190
  function handleSingleItem(
191
  items: Repo[],
192
  owner: string,
 
209
  );
210
  }
211
 
 
 
 
 
 
 
212
  function handleIndexDetails(
213
  sortedData: { latest: Repo[]; mostUsed: Repo[] },
214
  searchParams: URLSearchParams