Skip to content

Commit 4ccce9b

Browse files
committed
chore: remove lodash dependency
Many functionalities previously offered by lodash are now available natively or can be implemented easily using current Node.js features. This change reduces project dependencies.
1 parent 7b9c178 commit 4ccce9b

File tree

5 files changed

+10
-14
lines changed

5 files changed

+10
-14
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,6 @@ Required to run the project before your modifications
545545
| express-session | Simple session middleware for Express. |
546546
| jquery | Front-end JS library to interact with HTML elements. |
547547
| lastfm | Last.fm API library. |
548-
| lodash | A utility library for working with arrays, numbers, objects, strings. |
549548
| lusca | CSRF middleware. |
550549
| mailchecker | Verifies that an email address is valid and not a disposable address. |
551550
| moment | Parse, validate, compute dates and times. |

config/passport.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const { OAuthStrategy } = require('passport-oauth');
1212
const { OAuth2Strategy } = require('passport-oauth');
1313
const OpenIDConnectStrategy = require('passport-openidconnect');
1414
const { OAuth } = require('oauth');
15-
const _ = require('lodash');
1615
const moment = require('moment');
1716

1817
const User = require('../models/User');
@@ -254,7 +253,14 @@ passport.use(
254253
if (existingUser) {
255254
return done(null, existingUser);
256255
}
257-
const emailValue = _.get(_.orderBy(profile.emails, ['primary', 'verified'], ['desc', 'desc']), [0, 'value'], null);
256+
// Github may return a list of email addresses instead of just one
257+
// Sort by primary, then by verified, then pick the first one in the list
258+
const sortedEmails = (profile.emails || []).slice().sort((a, b) => {
259+
if (b.primary !== a.primary) return b.primary - a.primary;
260+
if (b.verified !== a.verified) return b.verified - a.verified;
261+
return 0;
262+
});
263+
const emailValue = sortedEmails.length > 0 ? sortedEmails[0].value : null;
258264
if (profile._json.email === null) {
259265
const existingEmailUser = await User.findOne({
260266
email: { $eq: emailValue },

controllers/user.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const crypto = require('crypto');
22
const passport = require('passport');
3-
const _ = require('lodash');
43
const validator = require('validator');
54
const mailChecker = require('mailchecker');
65
const User = require('../models/User');
@@ -325,14 +324,14 @@ exports.getOauthUnlink = async (req, res, next) => {
325324
// that another login method exists.
326325
if (!(user.email && user.password) && tokensWithoutProviderToUnlink.length === 0) {
327326
req.flash('errors', {
328-
msg: `The ${_.startCase(_.toLower(provider))} account cannot be unlinked without another form of login enabled. Please link another account or add an email address and password.`,
327+
msg: `The ${provider.charAt(0).toUpperCase() + provider.slice(1).toLowerCase()} account cannot be unlinked without another form of login enabled. Please link another account or add an email address and password.`,
329328
});
330329
return res.redirect('/account');
331330
}
332331
user.tokens = tokensWithoutProviderToUnlink;
333332
await user.save();
334333
req.flash('info', {
335-
msg: `${_.startCase(_.toLower(provider))} account has been unlinked.`,
334+
msg: `${provider.charAt(0).toUpperCase() + provider.slice(1).toLowerCase()} account has been unlinked.`,
336335
});
337336
res.redirect('/account');
338337
} catch (err) {

package-lock.json

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
"express-session": "^1.18.1",
4747
"jquery": "^3.7.1",
4848
"lastfm": "^0.9.4",
49-
"lodash": "^4.17.21",
5049
"lusca": "^1.7.0",
5150
"mailchecker": "^6.0.17",
5251
"moment": "^2.30.1",

0 commit comments

Comments
 (0)