From fd8627c19c0fe91c61bcad8cd93498e1f1ece0a8 Mon Sep 17 00:00:00 2001 From: sporeball Date: Tue, 21 Jan 2025 15:38:04 -0500 Subject: [PATCH] improve tag order --- gallery.js | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/gallery.js b/gallery.js index 393c24e..a589bb8 100644 --- a/gallery.js +++ b/gallery.js @@ -1,9 +1,17 @@ +const HIGH_PRIORITY_TAGS = ['javascript', 'typescript', 'cli']; + let submissions = []; let tags = []; -// let active_tags = []; -function unique(v, i, a) { - return a.indexOf(v) === i; +function unique(v, i, r) { + return r.indexOf(v) === i; +} + +function partition(r, cb) { + let a = []; + let b = []; + r.forEach((v, i, r2) => (cb(v, i, r2) ? a : b).push(v)); + return [a, b]; } async function get_json(url) { @@ -39,7 +47,7 @@ function append_submission(submission) { e_name.setAttribute('id', submission.name); // tags let e_tags = document.createElement('div'); - for (const tag of submission.tags) { + for (const tag of sort_tags(submission.tags)) { let e_tag = document.createElement('p'); e_tag.classList.add('tag'); e_tag.innerHTML = tag; @@ -156,9 +164,24 @@ function filter_submissions() { } } +/** + * Sort a list of tags. + * @param {string[]} unsorted + * @returns {string[]} + */ +function sort_tags(unsorted) { + // partition the tags into high priority and low priority + let [high_priority, low_priority] = partition(unsorted, v => HIGH_PRIORITY_TAGS.includes(v)); + // maintain the desired order of high priority tags by filtering the constant instead of the partition + high_priority = HIGH_PRIORITY_TAGS.filter(tag => high_priority.includes(tag)); + // sort the low priority tags alphabetically + low_priority.sort(); + return [...high_priority, ...low_priority]; +} + get_json('./submissions.json').then(json => { submissions = json.submissions; - tags = submissions.flatMap(submission => submission.tags).filter(unique); + tags = sort_tags(submissions.flatMap(submission => submission.tags).filter(unique)); // document.getElementById('n_submissions').innerHTML = `${submissions.length} submissions`; document.getElementById('n_submissions_2').innerHTML = `${submissions.length} of ${submissions.length} submissions`; for (let i = 0; i < submissions.length; i++) {