Skip to content

Commit

Permalink
feat: sort entries and metadata by key when exported
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanaubrey committed Mar 11, 2024
1 parent 8c79b82 commit d770474
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@
"map",
"redis"
]
}
}
41 changes: 39 additions & 2 deletions packages/core/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,19 @@ export class Core {
throw new ArgsError('@cachemap/core expected keys to be an array.');
}

return this._entries(keys);
const entries = await this._entries<T>(keys);

return entries.sort(([a], [b]) => {
if (a < b) {
return -1;
}

if (a > b) {
return 1;
}

return 0;
});
}

public async export<T>(options: ExportOptions = {}): Promise<ExportResult<T>> {
Expand All @@ -256,7 +268,32 @@ export class Core {
throw new GroupedError('@cachemap/core export argument validation errors.', errors);
}

return this._export(options);
const { entries, metadata } = await this._export<T>(options);

return {
entries: entries.sort(([a], [b]) => {
if (a < b) {
return -1;
}

if (a > b) {
return 1;
}

return 0;
}),
metadata: metadata.sort((a, b) => {
if (a.key < b.key) {
return -1;
}

if (a.key > b.key) {
return 1;
}

return 0;
}),
};
}

public async get<T>(key: string, options: { hash?: boolean } = {}): Promise<T | undefined> {
Expand Down

0 comments on commit d770474

Please sign in to comment.