Skip to content

Instantly share code, notes, and snippets.

@Nimdis
Created February 6, 2023 08:55
Show Gist options
  • Save Nimdis/fee9b8d56d539dfe0e57320f1fed1595 to your computer and use it in GitHub Desktop.
Save Nimdis/fee9b8d56d539dfe0e57320f1fed1595 to your computer and use it in GitHub Desktop.
const isZiinaUser = (phoneNumber) => {
return false;
};
const contactsMap = new Map(); // key - useId, val -> set of phone numbers
const phoneToUserMap = new Map(); // key - phone number, val -> set of userIds
const syncContacts = (userId, phoneNumbers) => {
const prevPhoneNumbersSet = contactsMap.get(userId) ?? new Set();
const phoneNumbersSet = new Set(phoneNumbers);
const phoneNumberToDelArr = [];
for (const phoneNumber of prevPhoneNumbersSet) {
if (!phoneNumbersSet.has(phoneNumber)) {
phoneNumberToDelArr.push(phoneNumber);
}
}
for (const phoneNumber of phoneNumberToDelArr) {
phoneToUserMap.get(phoneNumber)?.delete(userId);
}
contactsMap.set(userId, phoneNumbersSet);
for (const phoneNumber of phoneNumbersSet) {
if (phoneToUserMap.has(phoneNumber)) {
phoneToUserMap.get(phoneNumber).add(userId)
continue;
}
phoneToUserMap.set(phoneNumber, new Set([userId]))
}
};
/*
type ProspectiveUser {
phoneNumber: string,
friendsOnZiina: int
}
*/
const prospectiveUsers = (userId) => {
console.log(contactsMap)
console.log(phoneToUserMap)
let prospectiveUsersArr = [];
for (const phoneNumber of (contactsMap.get(userId) ?? [])) {
if (isZiinaUser(phoneNumber)) {
continue;
}
prospectiveUsersArr.push({
phoneNumber,
friendsOnZiina: phoneToUserMap.get(phoneNumber)?.size ?? 0
})
}
return prospectiveUsersArr
};
const testFn = () => {
syncContacts(1, ["12345", "54321", "56788", "123123"]);
syncContacts(2, ["12345", "54321"]);
console.log(prospectiveUsers(1));
console.log(prospectiveUsers(2));
console.log('====')
syncContacts(1, ["54321", "56788", "123123"]);
console.log(prospectiveUsers(1));
console.log(prospectiveUsers(2));
};
testFn();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment