Skip to content

Instantly share code, notes, and snippets.

@bartwttewaall
Created February 8, 2021 08:37
Show Gist options
  • Save bartwttewaall/4b7e10fe9204da5fc65255191da7399e to your computer and use it in GitHub Desktop.
Save bartwttewaall/4b7e10fe9204da5fc65255191da7399e to your computer and use it in GitHub Desktop.
Crossreference 2 lists of users by (fuzzy) name, where users were free to input whatever they wanted. Output as CSV format
import * as users from "/cometchat-users.json";
import * as press from "/press.json";
import * as dealers from "/dealers.json";
import fuzzysort from "fuzzysort";
const toName = (item) => ({
name: item.firstName + " " + item.lastName,
...item,
});
const testUsers = [
{ id: 1, firstName: "Bart", lastName: "Wttewaall", email: "[email protected]" },
{ id: 2, firstName: "Roberto", lastName: "Bertolli", email: "[email protected]" },
{ id: 3, firstName: "Carlos", lastName: "Santana", email: "[email protected]" },
{ id: 4, firstName: "Fre", lastName: "Derique", email: "[email protected]" },
{ id: 5, firstName: "Matt", lastName: "LeBron", email: "[email protected]" },
{ id: 6, firstName: "Matt", lastName: "LeBlanq", email: "[email protected]" },
];
let input = [];
// input = testUsers.map((item) => toName(item));
// input = dealers.data.map((item) => toName(item));
input = press.data.map(item => toName(item));
const fromTime = new Date("2021-01-25T15:00:00").getTime() / 1000;
const toTime = new Date("2021-01-27T19:00:00").getTime() / 1000;
const found = new Map();
const options = {
key: "name",
limit: 2, // don't return more results than you need!
allowTypo: false, // if you don't care about allowing typos
threshold: -500, // don't return bad results
};
const unique = (value, index, array) => array.indexOf(value) === index;
const UNINVITED = "uninvited";
const findMatch = (item) => {
const results = fuzzysort.go(item.name, input, options);
if (!!results) {
const bestResult = results[0];
const key = !bestResult
? UNINVITED
: bestResult.obj.id +
";" +
bestResult.obj.firstName +
";" +
bestResult.obj.lastName +
";" +
bestResult.obj.email;
if (!found.has(key)) found.set(key, [item.name]);
else found.set(key, [...found.get(key), item.name].filter(unique));
}
};
users.data
// .filter(item => item.createdAt >= fromTime && item.createdAt <= toTime)
.forEach(findMatch);
Array.from(found)
.filter((value) => value[0] !== UNINVITED)
.map((value) => value[0] + ";" + value[1].join(",")); //?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment