contacts.js 390 B

12345678910111213
  1. export function buildContactIds(contacts, limit) {
  2. return Object.entries(contacts || {})
  3. .map(([id, { n, ts }]) => ({ id, n, ts }))
  4. .sort(sortContacts)
  5. .slice(0, limit)
  6. .map(contact => contact.id)
  7. }
  8. // sort by decreasing count, decreasing timestamp.
  9. // i.e. highest count, most recent first.
  10. function sortContacts(a, b) {
  11. return a.n === b.n ? b.ts - a.ts : b.n - a.n
  12. }