Skip to content

Instantly share code, notes, and snippets.

@jimjam88
Last active November 17, 2023 13:09
Show Gist options
  • Save jimjam88/49c22841dfd8ebc828c876c99ccfc714 to your computer and use it in GitHub Desktop.
Save jimjam88/49c22841dfd8ebc828c876c99ccfc714 to your computer and use it in GitHub Desktop.
Pair Programming Scripts
const contacts = {};
let prospectiveUsersCache = {};
const isZiinaUser = (phoneNumber) => {
return false;
};
const syncContacts = (userId, phoneNumbers) => {
contacts[userId] = phoneNumbers;
prospectiveUsersCache[userId] = phoneNumbers.reduce((acc, phoneNumber) => {
if (isZiinaUser(phoneNumber)) {
return acc;
}
const friendsOnZiina = Object.entries(contacts)
.filter(([uid, numbers]) => uid !== userId && numbers.includes(phoneNumber))
.length;
return [
...acc,
{
phoneNumber,
friendsOnZiina,
},
];
}, []);
prospectiveUsersCache = Object.keys(prospectiveUsersCache).map(uid => {
const friends = prospectiveUsersCache[uid];
return friends.map(friend => {
return {
phoneNumber: friend.phoneNumber,
friendsOnZiina: Object.entries(contacts)
.filter(([uid, numbers]) => uid !== userId && numbers.includes(friend.phoneNumber))
.length,
};
});
});
};
const prospectiveUsers = (userId) => {
if (!contacts.hasOwnProperty(userId) || !prospectiveUsersCache.hasOwnProperty(userId)) {
return [];
}
return prospectiveUsersCache[userId];
};
syncContacts(1, [111, 222, 333]);
// console.log(prospectiveUsersCache);
syncContacts(2, [111, 333, 444]);
// console.log(prospectiveUsersCache);
syncContacts(3, [555, 666, 444]);
// console.log(prospectiveUsersCache);
// console.log(contacts);
console.log(prospectiveUsers(1));
console.log(prospectiveUsers(2));
console.log(prospectiveUsers(3));
type ProspectiveUser = {
phoneNumber: string,
friendsOnZiina: number,
}
type User = {
userId: string,
contacts: ProspectiveUser[],
}
const users: Map<string, User> = new Map();
const getZiinaUsersCount = (excludedUserId: string, phoneNumber: string): number => {
let count: number = 0;
users.forEach(user => {
if (
user.userId !== excludedUserId
&& user.contacts.some(contact => contact.phoneNumber === phoneNumber)
) {
count = count + 1;
}
});
return count;
};
const getProspectiveUser = (userId: string, phoneNumber: string): ProspectiveUser => ({
phoneNumber,
friendsOnZiina: getZiinaUsersCount(userId, phoneNumber),
});
const syncContacts = (userId: string, phoneNumbers: string[]): void => {
// Update the user to sync first...
users.set(userId, {
userId,
contacts: phoneNumbers.map(phoneNumber => getProspectiveUser(userId, phoneNumber)),
});
// Then update the remaining users
users.forEach(user => {
if (user.userId !== userId) {
users.set(user.userId, {
...user,
contacts: user.contacts.map(contact => getProspectiveUser(user.userId, contact.phoneNumber)),
});
}
});
};
const prospectiveUsers = (userId: string): ProspectiveUser[] => {
const user = users.get(userId);
return user ? user.contacts : [];
};
syncContacts('1', ['111', '222', '333']);
syncContacts('2', ['111', '222', '333', '444']);
syncContacts('3', ['111', '444', '333']);
syncContacts('4', ['111', '555']);
// Ensure updates are handled
syncContacts('1', ['111', '222', '333', '444']);
const one = prospectiveUsers('1');
const two = prospectiveUsers('2');
const three = prospectiveUsers('3');
const four = prospectiveUsers('4');
const five = prospectiveUsers('5');
console.log('one ==>', one);
console.log('two ==>', two);
console.log('three ==>', three);
console.log('four ==>', four);
console.log('five ==>', five);
// Some very basic tests...
console.assert(one.length === 4);
console.assert(two.length === 4);
console.assert(three.length === 3);
console.assert(four.length === 2);
console.assert(five.length === 0);
const assertNumberAndCount = (users: ProspectiveUser[], phoneNumber: string, count: number): boolean =>
users.find(user => user.phoneNumber === phoneNumber)?.friendsOnZiina === count;
console.assert(assertNumberAndCount(one, '111', 3));
console.assert(assertNumberAndCount(one, '222', 1));
console.assert(assertNumberAndCount(one, '333', 2));
console.assert(assertNumberAndCount(one, '444', 2));
console.assert(assertNumberAndCount(two, '111', 3));
console.assert(assertNumberAndCount(two, '222', 1));
console.assert(assertNumberAndCount(two, '333', 2));
console.assert(assertNumberAndCount(two, '444', 2));
console.assert(assertNumberAndCount(three, '111', 3));
console.assert(assertNumberAndCount(three, '333', 2));
console.assert(assertNumberAndCount(three, '444', 2));
console.assert(assertNumberAndCount(four, '111', 3));
console.assert(assertNumberAndCount(four, '555', 0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment