Skip to content

Commit

Permalink
refactor: use Array.fromAsync() (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
iuioiua authored Nov 2, 2023
1 parent c7062b2 commit d19880e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
7 changes: 4 additions & 3 deletions tasks/db_dump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ function replacer(_key: unknown, value: unknown) {
return typeof value === "bigint" ? value.toString() : value;
}

const iter = kv.list({ prefix: [] });
const items = [];
for await (const { key, value } of iter) items.push({ key, value });
const items = await Array.fromAsync(
kv.list({ prefix: [] }),
({ key, value }) => ({ key, value }),
);
console.log(JSON.stringify(items, replacer, 2));

kv.close();
4 changes: 1 addition & 3 deletions utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ export const kv = await Deno.openKv(path);
* ```
*/
export async function collectValues<T>(iter: Deno.KvListIterator<T>) {
const values = [];
for await (const { value } of iter) values.push(value);
return values;
return await Array.fromAsync(iter, ({ value }) => value);
}

// Item
Expand Down
16 changes: 7 additions & 9 deletions utils/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,11 @@ export async function getPost(slug: string): Promise<Post | null> {
* ```
*/
export async function getPosts(): Promise<Post[]> {
const files = Deno.readDir("./posts");
const promises = [];
for await (const file of files) {
const slug = file.name.replace(".md", "");
promises.push(getPost(slug));
}
const posts = await Promise.all(promises) as Post[];
posts.sort((a, b) => b.publishedAt.getTime() - a.publishedAt.getTime());
return posts;
const posts = await Array.fromAsync(
Deno.readDir("./posts"),
async (file) => await getPost(file.name.replace(".md", "")),
) as Post[];
return posts.toSorted((a, b) =>
b.publishedAt.getTime() - a.publishedAt.getTime()
);
}

0 comments on commit d19880e

Please sign in to comment.