-
Notifications
You must be signed in to change notification settings - Fork 923
Expand file tree
/
Copy pathpaypal-utils.js
More file actions
56 lines (50 loc) · 1.95 KB
/
paypal-utils.js
File metadata and controls
56 lines (50 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
(function attachPayPalUtils(root, factory) {
root.PayPalUtils = factory();
})(typeof self !== 'undefined' ? self : globalThis, function createPayPalUtils() {
function normalizePayPalAccount(account = {}) {
const normalizedEmail = String(account.email || '').trim().toLowerCase();
const now = Date.now();
return {
id: String(account.id || crypto.randomUUID()),
email: normalizedEmail,
password: String(account.password || ''),
createdAt: Number.isFinite(Number(account.createdAt)) ? Number(account.createdAt) : now,
updatedAt: Number.isFinite(Number(account.updatedAt)) ? Number(account.updatedAt) : now,
lastUsedAt: Number.isFinite(Number(account.lastUsedAt)) ? Number(account.lastUsedAt) : 0,
};
}
function normalizePayPalAccounts(accounts) {
if (!Array.isArray(accounts)) return [];
const deduped = new Map();
for (const account of accounts) {
const normalized = normalizePayPalAccount(account);
if (!normalized.email) continue;
deduped.set(normalized.id, normalized);
}
return [...deduped.values()];
}
function findPayPalAccount(accounts = [], accountId = '') {
const normalizedId = String(accountId || '').trim();
if (!normalizedId) return null;
return normalizePayPalAccounts(accounts).find((account) => account.id === normalizedId) || null;
}
function upsertPayPalAccountInList(accounts = [], nextAccount = null) {
if (!nextAccount) {
return normalizePayPalAccounts(accounts);
}
const normalizedNext = normalizePayPalAccount(nextAccount);
const list = normalizePayPalAccounts(accounts);
const existingIndex = list.findIndex((account) => account.id === normalizedNext.id);
if (existingIndex >= 0) {
list[existingIndex] = normalizedNext;
return list;
}
return [...list, normalizedNext];
}
return {
findPayPalAccount,
normalizePayPalAccount,
normalizePayPalAccounts,
upsertPayPalAccountInList,
};
});