Skip to content

Commit 3950be2

Browse files
committed
Use Map for non-quirky sorting order
1 parent 52294be commit 3950be2

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

index.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ async function process_chunks (path) {
4747
const dir = await opendir(path)
4848
let result = {}
4949
for await (const chunk of dir) {
50+
if (chunk.name === '.DS_Store') continue
51+
console.log(`Processing chunk ${chunk.name}`)
5052
const chunk_run = await read_json_file(`${path}/${chunk.name}`)
5153
const scored_chunk = process_raw_results(chunk_run)
5254
if (!result.run_info) {
@@ -67,34 +69,34 @@ async function process_chunks (path) {
6769

6870
// Sorts the keys of the test_scores object alphabetically
6971
function sort_test_results (result) {
70-
result.test_scores = sort_object(result.test_scores)
72+
console.log('Sorting results')
73+
result.test_scores = into_sorted_map(result.test_scores)
7174
}
7275

73-
// Sorts the keys of an objects
76+
// Convert an object into a sorted Map
7477
//
75-
// JS objects serialize keys in insertion order, so to control the order of JSON serialized output
78+
// JS Maps serialize keys in insertion order, so to control the order of JSON serialized output
7679
// we can:
7780
//
7881
// - Convert the object into an array
7982
// - Sort the array by test object key
80-
// - Reinsert the results into a new object in order
83+
// - Reinsert the results into a new Map in order
8184
//
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) {
85+
// Storing the result in a Map rather a regular object allows us to have full control over the order
86+
function into_sorted_map (obj) {
8587
const arr = Object.entries(obj).map(([key, value]) => ({ key, value }))
8688
arr.sort((a, b) => {
8789
if (a.key < b.key) return -1
8890
if (a.key > b.key) return 1
8991
return 0
9092
})
9193

92-
const obj2 = {}
94+
const map = new Map()
9395
for (const entry of arr) {
94-
obj2[entry.key] = entry.value
96+
map[entry.key] = entry.value
9597
}
9698

97-
return obj2
99+
return map
98100
}
99101

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

0 commit comments

Comments
 (0)