Skip to content

Instantly share code, notes, and snippets.

@Humberd
Created July 5, 2020 17:30
Show Gist options
  • Save Humberd/fe4cbbbcf7fc0fbc92b011d5be0557b8 to your computer and use it in GitHub Desktop.
Save Humberd/fe4cbbbcf7fc0fbc92b011d5be0557b8 to your computer and use it in GitHub Desktop.
poprawione task 2
function areTransationsTheSame (t1, t2) {
return t1.sourceAccount === t2.sourceAccount
&& t1.targetAccount === t2.targetAccount
// ... and the rest
}
export const findDuplicateTransactions = (transactions = []) => {
const groupedTransactionsMap = transactions
.map((transaction) => {
let key = {
sourceAccount: transaction.sourceAccount,
targetAccount: transaction.targetAccount,
category: transaction.category,
amount: transaction.amount,
};
return { k: key, v: transaction };
})
.reduce((accumulator, transactionKeyValue) => {
const stringifiedKey = `${transationKeyValue.sourceAccount};${transationKeyValue.targetAccount}`;
if (!accumulator.has(stringifiedKey)) {
accumulator.set(stringifiedKey, [transactionKeyValue.v]);
} else {
const current = accumulator.get(stringifiedKey);
current.push(transactionKeyValue.v);
// accumulator.set(stringifiedKey, current);
}
return accumulator;
}, new Map());
const result = [];
const oneMinuteInMs = 60 * 1000; // 60s * 1000ms
for (const transactionsGroup of groupedTransactionsMap.values()) {
transactionsGroup.sort(function (a, b) {
let timeA = Date.parse(a.time);
let timeB = Date.parse(b.time);
return timeA - timeB;
});
let resultCandidate = [];
for (const currentTransaction of transactionsGroup) {
const timeDiff = Date.parse(currentTransaction.time) - Date.parse(resultCandidate[resultCandidate.length - 1].time);
if (
resultCandidate.length >= 1 &&
timeDiff > oneMinuteInMs
) {
if (resultCandidate.length >= 2) {
result.push(resultCandidate);
}
resultCandidate = [];
}
resultCandidate.push(currentTransaction);
}
if (resultCandidate.length >= 2) {
result.push(resultCandidate);
}
}
result.sort(function (groupA, groupB) {
let timeGroupA = Date.parse(groupA[0].time);
let timeGroupB = Date.parse(groupB[0].time);
return timeGroupA - timeGroupB;
});
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment