06 plugin.js (4/5)

  function report(books) {
    const byDir = new Map();
    for (const b of books) {
      const d = byDir.get(b.dir) || { n: 0, size: 0 };
      d.n++; d.size += b.size; byDir.set(b.dir, d);
    }
    const groups = new Map();
    for (const b of books) (groups.get(key(b.name)) || groups.set(key(b.name), []).get(key(b.name))).push(b);

    duplicates = [];
    const dupRows = [];
    for (const list of groups.values()) {
      if (list.length < 2) continue;
      list.sort((a, b) => b.size - a.size);           // keep the biggest copy
      for (const extra of list.slice(1)) duplicates.push(extra.path);
      dupRows.push(`<li><b>${esc(list[0].path)}</b> (kept)<br>` +
        list.slice(1).map((b) => esc(b.path)).join('<br>') + '</li>');
    }

    const total = books.reduce((s, b) => s + b.size, 0);
    $('[data-report]').innerHTML = `
      <p><b>${books.length}</b> books, ${fmt(total)} total.</p>
      <table style="width:100%;border-collapse:collapse">
        ${[...byDir].sort((a, b) => b[1].size - a[1].size).map(([d, v]) =>
          `<tr><td>${esc(d)}</td><td style="text-align:right">${v.n}</td>
               <td style="text-align:right">${fmt(v.size)}</td></tr>`).join('')}
      </table>
      <hr><p><b>${duplicates.length}</b> probable duplicates</p>
      <ul>${dupRows.join('')}</ul>`;
    $('[data-act=move]').disabled = duplicates.length === 0;
  }
‹ PreviousReader file (.txt)Next ›