68 lines
1.3 KiB
JavaScript
Executable File
68 lines
1.3 KiB
JavaScript
Executable File
function roundCurrency(value) {
|
|
const amount = Number(value ?? 0);
|
|
if (!Number.isFinite(amount)) {
|
|
return 0;
|
|
}
|
|
|
|
return Number(amount.toFixed(2));
|
|
}
|
|
|
|
function normalizeAmount(value) {
|
|
const amount = Number(value);
|
|
if (!Number.isFinite(amount)) {
|
|
return null;
|
|
}
|
|
|
|
return roundCurrency(amount);
|
|
}
|
|
|
|
function normalizeUserKey(name) {
|
|
return String(name ?? '').replace(/\s+/g, '');
|
|
}
|
|
|
|
function createUserNameResolver(users = []) {
|
|
const exactNames = new Set();
|
|
const normalizedNameMap = new Map();
|
|
|
|
for (const user of users) {
|
|
const userName = String(user?.name ?? '').trim();
|
|
if (!userName) {
|
|
continue;
|
|
}
|
|
|
|
exactNames.add(userName);
|
|
|
|
const userKey = normalizeUserKey(userName);
|
|
if (!normalizedNameMap.has(userKey)) {
|
|
normalizedNameMap.set(userKey, new Set());
|
|
}
|
|
normalizedNameMap.get(userKey).add(userName);
|
|
}
|
|
|
|
return (inputName, fallbackName = '') => {
|
|
const rawName = String(inputName ?? '').trim();
|
|
|
|
if (!rawName) {
|
|
return fallbackName;
|
|
}
|
|
|
|
if (exactNames.has(rawName)) {
|
|
return rawName;
|
|
}
|
|
|
|
const matches = [...(normalizedNameMap.get(normalizeUserKey(rawName)) || [])];
|
|
if (matches.length === 1) {
|
|
return matches[0];
|
|
}
|
|
|
|
return rawName;
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
createUserNameResolver,
|
|
normalizeAmount,
|
|
normalizeUserKey,
|
|
roundCurrency,
|
|
};
|