Skip to content

Commit a6e6679

Browse files
committed
Use Map for non-quirky sorting order
Signed-off-by: Nico Burns <[email protected]>
1 parent 52294be commit a6e6679

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

index.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ async function all_runs_sorted (runs_dir) {
3636
const dir = await opendir(`./${runs_dir}`)
3737
const runs = []
3838
for await (const run of dir) {
39+
if (run.name === '.DS_Store') continue
3940
runs.push(run.name)
4041
}
4142

@@ -47,6 +48,8 @@ async function process_chunks (path) {
4748
const dir = await opendir(path)
4849
let result = {}
4950
for await (const chunk of dir) {
51+
if (chunk.name === '.DS_Store') continue
52+
console.log(`Processing chunk ${chunk.name}`)
5053
const chunk_run = await read_json_file(`${path}/${chunk.name}`)
5154
const scored_chunk = process_raw_results(chunk_run)
5255
if (!result.run_info) {
@@ -67,34 +70,34 @@ async function process_chunks (path) {
6770

6871
// Sorts the keys of the test_scores object alphabetically
6972
function sort_test_results (result) {
70-
result.test_scores = sort_object(result.test_scores)
73+
console.log('Sorting results')
74+
result.test_scores = into_sorted_map(result.test_scores)
7175
}
7276

73-
// Sorts the keys of an objects
77+
// Convert an object into a sorted Map
7478
//
75-
// JS objects serialize keys in insertion order, so to control the order of JSON serialized output
79+
// JS Maps serialize keys in insertion order, so to control the order of JSON serialized output
7680
// we can:
7781
//
7882
// - Convert the object into an array
7983
// - Sort the array by test object key
80-
// - Reinsert the results into a new object in order
84+
// - Reinsert the results into a new Map in order
8185
//
82-
// This sort is not perfect as JS will still serialize keys that can be parsed as integers before
83-
// all other keys, but it's a lot better than nothing.
84-
function sort_object (obj) {
86+
// Storing the result in a Map rather a regular object allows us to have full control over the order
87+
function into_sorted_map (obj) {
8588
const arr = Object.entries(obj).map(([key, value]) => ({ key, value }))
8689
arr.sort((a, b) => {
8790
if (a.key < b.key) return -1
8891
if (a.key > b.key) return 1
8992
return 0
9093
})
9194

92-
const obj2 = {}
95+
const map = new Map()
9396
for (const entry of arr) {
94-
obj2[entry.key] = entry.value
97+
map[entry.key] = entry.value
9598
}
9699

97-
return obj2
100+
return map
98101
}
99102

100103
async function add_run (runs_dir, chunks_dir, date) {

0 commit comments

Comments
 (0)