Skip to content

Commit 92098a0

Browse files
committed
Replace lodash with native code
1 parent d89f11a commit 92098a0

3 files changed

Lines changed: 18 additions & 16 deletions

File tree

module/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
1515
"license": "AGPL-3.0-or-later",
1616
"devDependencies": {
1717
"@types/dedent": "^0.7.0",
18-
"@types/lodash": "^4.14.168",
1918
"typescript": "^5.9.2"
2019
},
2120
"dependencies": {
2221
"@httptoolkit/util": "^0.1.5",
2322
"@types/node": "*",
2423
"async-mutex": "^0.2.6",
2524
"dedent": "^0.7.0",
26-
"jose": "^5.2.0",
27-
"lodash": "^4.17.21"
25+
"jose": "^5.2.0"
2826
}
2927
}

module/src/auth.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as _ from 'lodash';
21
import { Mutex } from 'async-mutex';
32
import { asErrorLike, CustomError } from '@httptoolkit/util';
43

@@ -450,6 +449,13 @@ async function parseBillingData(billingData: UserBillingData | null): Promise<Bi
450449
};
451450
}
452451

452+
// Checks all fields except the listed optional ones are non-null/undefined
453+
function hasAllRequiredFields(obj: Record<string, unknown>, optionalFields: string[]) {
454+
return Object.entries(obj)
455+
.filter(([key]) => !optionalFields.includes(key))
456+
.every(([_, v]) => v != null);
457+
}
458+
453459
function parseSubscriptionData(rawData: SubscriptionData) {
454460
const sku = rawData.subscription_sku
455461
?? getSKUForPaddleId(rawData.subscription_plan_id);
@@ -472,7 +478,7 @@ function parseSubscriptionData(rawData: SubscriptionData) {
472478
canManageSubscription: !!rawData.can_manage_subscription
473479
};
474480

475-
if (_.some(subscription) && !subscription.plan) {
481+
if (Object.values(subscription).some(Boolean) && !subscription.plan) {
476482
// No plan means no recognized plan, i.e. an unknown id. This should never happen,
477483
// but error reports suggest it's happened at least once.
478484
console.warn('Invalid raw subscription data', rawData)
@@ -485,10 +491,7 @@ function parseSubscriptionData(rawData: SubscriptionData) {
485491
'cancelSubscriptionUrl'
486492
];
487493

488-
const isCompleteSubscriptionData = _.every(
489-
_.omit(subscription, ...optionalFields),
490-
v => !_.isNil(v) // Not just truthy: canManageSubscription can be false on valid sub
491-
);
494+
const isCompleteSubscriptionData = hasAllRequiredFields(subscription, optionalFields);
492495

493496
// Use undefined rather than {} or partial data when there's any missing required sub fields
494497
return isCompleteSubscriptionData

module/src/plans.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as _ from 'lodash';
21
import { delay, doWhile } from '@httptoolkit/util';
32
import { SKU, SubscriptionPricing } from './types';
43

@@ -44,9 +43,9 @@ async function loadPlanPrices() {
4443
const productPrices = data.response.products as Array<SubscriptionPricing>;
4544

4645
productPrices.forEach((productPrice) => {
47-
const plan = _.find(SubscriptionPlans,
48-
{ paddleId: productPrice.product_id }
49-
) as SubscriptionPlan | undefined;
46+
const plan = Object.values(SubscriptionPlans).find(
47+
plan => plan.paddleId === productPrice.product_id
48+
);
5049

5150
if (!plan) return;
5251

@@ -77,7 +76,7 @@ export async function loadPlanPricesUntilSuccess() {
7776
]).then(() => delay(1000)), // Limit the frequency
7877

7978
// While: if any subs didn't successfully get data, try again:
80-
() => _.some(SubscriptionPlans, (plan) => !plan.prices),
79+
() => Object.values(SubscriptionPlans).some((plan) => !plan.prices),
8180
);
8281

8382
return SubscriptionPlans;
@@ -87,12 +86,14 @@ function formatPrice(currency: string, price: number) {
8786
return Number(price).toLocaleString(undefined, {
8887
style: "currency",
8988
currency: currency,
90-
minimumFractionDigits: _.round(price) === price ? 0 : 2,
89+
minimumFractionDigits: Math.round(price) === price ? 0 : 2,
9190
maximumFractionDigits: 2
9291
})
9392
}
9493

9594
export const getPlanByCode = (sku: SKU) => SubscriptionPlans[sku];
9695

9796
export const getSKUForPaddleId = (paddleId: number | undefined) =>
98-
_.findKey(SubscriptionPlans, { paddleId: paddleId }) as SKU | undefined;
97+
(Object.keys(SubscriptionPlans) as SKU[]).find(sku =>
98+
SubscriptionPlans[sku].paddleId === paddleId
99+
);

0 commit comments

Comments
 (0)