Skip to content

Commit

Permalink
Merge pull request #252 from DestinyItemManager/es-toolkit
Browse files Browse the repository at this point in the history
Swap from lodash to es-toolkit
  • Loading branch information
bhollis authored Dec 11, 2024
2 parents 8c762bb + 196a968 commit 0e8895a
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 44 deletions.
6 changes: 3 additions & 3 deletions api/apps/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Sentry from '@sentry/node';
import { ListToken } from '@stately-cloud/client';
import { keyBy } from 'es-toolkit';
import { RequestHandler } from 'express';
import _ from 'lodash';
import { getAllApps as getAllAppsPostgres } from '../db/apps-queries.js';
import { pool } from '../db/index.js';
import { metrics } from '../metrics/index.js';
Expand Down Expand Up @@ -114,7 +114,7 @@ async function fetchAppsFromPostgres() {
const client = await pool.connect();
try {
apps = await getAllAppsPostgres(client);
appsByApiKey = _.keyBy(apps, (a) => a.dimApiKey.toLowerCase());
appsByApiKey = keyBy(apps, (a) => a.dimApiKey.toLowerCase());
origins = new Set<string>();
for (const app of apps) {
origins.add(app.origin);
Expand All @@ -126,7 +126,7 @@ async function fetchAppsFromPostgres() {
}

function digestApps() {
appsByApiKey = _.keyBy(apps, (a) => a.dimApiKey.toLowerCase());
appsByApiKey = keyBy(apps, (a) => a.dimApiKey.toLowerCase());
origins = new Set<string>();
for (const app of apps) {
origins.add(app.origin);
Expand Down
4 changes: 2 additions & 2 deletions api/db/searches-queries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from 'lodash';
import { uniqBy } from 'es-toolkit';
import { ClientBase, QueryResult } from 'pg';
import { metrics } from '../metrics/index.js';
import { ExportResponse } from '../shapes/export.js';
Expand Down Expand Up @@ -58,7 +58,7 @@ export async function getSearchesForProfile(
text: 'SELECT query, saved, usage_count, search_type, last_updated_at FROM searches WHERE membership_id = $1 and destiny_version = $2 order by last_updated_at DESC, usage_count DESC LIMIT 500',
values: [bungieMembershipId, destinyVersion],
});
return _.uniqBy(
return uniqBy(
results.rows
.map(convertSearch)
.concat(destinyVersion === 2 ? cannedSearchesForD2 : cannedSearchesForD1),
Expand Down
4 changes: 2 additions & 2 deletions api/routes/auth-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import asyncHandler from 'express-async-handler';
import util from 'util';
import { AuthTokenRequest, AuthTokenResponse } from '../shapes/auth.js';

import { sortBy } from 'es-toolkit/compat';
import jwt, { type Secret, type SignOptions } from 'jsonwebtoken';
import _ from 'lodash';
import { metrics } from '../metrics/index.js';
import { ApiApp } from '../shapes/app.js';
import { badRequest } from '../utils.js';
Expand Down Expand Up @@ -66,7 +66,7 @@ export const authTokenHandler = asyncHandler(async (req, res) => {
const serverMembershipId = responseData.Response.bungieNetUser.membershipId;
if (serverMembershipId === membershipId) {
const primaryMembershipId = responseData.Response.primaryMembershipId;
const profileIds = _.sortBy(
const profileIds = sortBy(
responseData.Response.destinyMemberships
// Filter out accounts that are tied to another platform's cross-save account.
// .filter((m) => !m.crossSaveOverride || m.crossSaveOverride === m.membershipType)
Expand Down
4 changes: 2 additions & 2 deletions api/routes/import.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isEmpty } from 'es-toolkit/compat';
import asyncHandler from 'express-async-handler';
import _ from 'lodash';
import { readTransaction, transaction } from '../db/index.js';
import { updateItemAnnotation } from '../db/item-annotations-queries.js';
import { updateItemHashTag } from '../db/item-hash-tags-queries.js';
Expand Down Expand Up @@ -46,7 +46,7 @@ export const importHandler = asyncHandler(async (req, res) => {
extractImportData(importData);

if (
_.isEmpty(settings) &&
isEmpty(settings) &&
loadouts.length === 0 &&
itemAnnotations.length === 0 &&
triumphs.length === 0 &&
Expand Down
4 changes: 2 additions & 2 deletions api/routes/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { captureMessage } from '@sentry/node';
import { isEmpty } from 'es-toolkit/compat';
import express from 'express';
import asyncHandler from 'express-async-handler';
import _ from 'lodash';
import { ClientBase } from 'pg';
import { readTransaction, transaction } from '../db/index.js';
import {
Expand Down Expand Up @@ -129,7 +129,7 @@ export const updateHandler = asyncHandler(async (req, res) => {
extractImportData(exportResponse);

if (
_.isEmpty(settings) &&
isEmpty(settings) &&
loadouts.length === 0 &&
itemAnnotations.length === 0 &&
triumphs.length === 0 &&
Expand Down
13 changes: 9 additions & 4 deletions api/stately/loadouts-queries.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { toBinary } from '@bufbuild/protobuf';
import _ from 'lodash';
import { omit } from 'es-toolkit';
import { v4 as uuid } from 'uuid';
import { defaultLoadoutParameters, Loadout, LoadoutParameters } from '../shapes/loadouts.js';
import { client } from './client.js';
Expand Down Expand Up @@ -48,9 +48,14 @@ it('can roundtrip between DIM loadout and Stately loadout', () => {
const statelyLoadout = convertLoadoutToStately(loadout, platformMembershipId, 2);
expect(() => toBinary(LoadoutSchema, statelyLoadout)).not.toThrow();
const loadout2 = convertLoadoutFromStately(statelyLoadout);
expect(_.omit(loadout2, 'profileId', 'destinyVersion', 'createdAt', 'lastUpdatedAt')).toEqual(
loadout,
);
expect(
omit(loadout2, [
'profileId' as keyof Loadout,
'destinyVersion' as keyof Loadout,
'createdAt',
'lastUpdatedAt',
]),
).toEqual(loadout);
});

it('can roundtrip loadout parameters', () => {
Expand Down
8 changes: 4 additions & 4 deletions api/stately/loadouts-queries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MessageInitShape } from '@bufbuild/protobuf';
import { keyPath } from '@stately-cloud/client';
import { DestinyClass } from 'bungie-api-ts/destiny2';
import _ from 'lodash';
import { isEmpty } from 'es-toolkit/compat';
import { DestinyVersion } from '../shapes/general.js';
import {
AssumeArmorMasterwork,
Expand Down Expand Up @@ -146,7 +146,7 @@ export function convertLoadoutParametersFromStately(
// DIM's AssumArmorMasterwork enum starts at 1
assumeArmorMasterwork: (assumeArmorMasterwork ?? 0) + 1,
statConstraints: statConstraintsFromStately(statConstraints),
modsByBucket: _.isEmpty(modsByBucket)
modsByBucket: isEmpty(modsByBucket)
? undefined
: listToMap('bucketHash', 'modHashes', modsByBucket),
artifactUnlocks: artifactUnlocks ? stripTypeName(artifactUnlocks) : undefined,
Expand Down Expand Up @@ -205,7 +205,7 @@ function convertLoadoutItemFromStately(item: StatelyLoadoutItem): LoadoutItem {
if (item.id) {
result.id = item.id.toString();
}
if (!_.isEmpty(item.socketOverrides)) {
if (!isEmpty(item.socketOverrides)) {
result.socketOverrides = listToMap('socketIndex', 'itemHash', item.socketOverrides);
}
if (item.craftedDate) {
Expand Down Expand Up @@ -297,7 +297,7 @@ export function convertLoadoutParametersToStately(
loParameters: LoadoutParameters | undefined,
): MessageInitShape<typeof LoadoutParametersSchema> | undefined {
let loParametersFixed: MessageInitShape<typeof LoadoutParametersSchema> | undefined;
if (!_.isEmpty(loParameters)) {
if (!isEmpty(loParameters)) {
const {
assumeArmorMasterwork,
exoticArmorHash,
Expand Down
6 changes: 3 additions & 3 deletions api/stately/searches-queries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { keyPath } from '@stately-cloud/client';
import _ from 'lodash';
import { sortBy, uniqBy } from 'es-toolkit';
import crypto from 'node:crypto';
import { metrics } from '../metrics/index.js';
import { ExportResponse } from '../shapes/export.js';
Expand Down Expand Up @@ -79,8 +79,8 @@ export async function getSearchesForProfile(

results.push(...(destinyVersion === 2 ? cannedSearchesForD2 : cannedSearchesForD1));

return _.sortBy(
_.uniqBy(results, (s) => s.query),
return sortBy(
uniqBy(results, (s) => s.query),
[(s) => -s.lastUsage, (s) => s.usageCount],
);
}
Expand Down
6 changes: 3 additions & 3 deletions api/stately/settings-queries.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from 'lodash';
import { omit } from 'es-toolkit';
import { defaultLoadoutParameters } from '../shapes/loadouts.js';
import { defaultSettings, Settings } from '../shapes/settings.js';
import {
Expand All @@ -14,14 +14,14 @@ it('can roundtrip between DIM settings and Stately settings', () => {
const settings: Settings = defaultSettings;
const statelySettings = convertToStatelyItem(settings, 1234);
const settings2 = convertToDimSettings(statelySettings);
expect(_.omit(settings2, 'memberId')).toEqual(settings);
expect(omit(settings2, ['memberId' as keyof Settings])).toEqual(settings);
});

it('can roundtrip between DIM settings and Stately settings with loadout parameters', () => {
const settings: Settings = { ...defaultSettings, loParameters: defaultLoadoutParameters };
const statelySettings = convertToStatelyItem(settings, 1234);
const settings2 = convertToDimSettings(statelySettings);
expect(_.omit(settings2, 'memberId')).toEqual(settings);
expect(omit(settings2, ['memberId' as keyof Settings])).toEqual(settings);
});

it('can insert settings where none exist before', async () => {
Expand Down
10 changes: 5 additions & 5 deletions api/stately/stately-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Utilities for dealing with Stately Items (protobufs) and other Stately-specific utilities.

import _ from 'lodash';
import { mapValues } from 'es-toolkit';

/** Recursively convert bigints to regular numbers in an object. */
type ObjectBigIntToNumber<T> = {
Expand All @@ -25,8 +25,8 @@ export function bigIntToNumber<T>(value: T): BigIntToNumber<T> {
return Number(value) as BigIntToNumber<T>;
} else if (Array.isArray(value)) {
return value.map(bigIntToNumber) as BigIntToNumber<T>;
} else if (typeof value === 'object') {
return _.mapValues(value, bigIntToNumber) as BigIntToNumber<T>;
} else if (typeof value === 'object' && value !== null) {
return mapValues(value, bigIntToNumber) as BigIntToNumber<T>;
}
return value as BigIntToNumber<T>;
}
Expand All @@ -51,8 +51,8 @@ export function numberToBigInt<T>(value: T): NumberToBigInt<T> {
return BigInt(value) as NumberToBigInt<T>;
} else if (Array.isArray(value)) {
return value.map(numberToBigInt) as NumberToBigInt<T>;
} else if (typeof value === 'object') {
return _.mapValues(value, numberToBigInt) as NumberToBigInt<T>;
} else if (typeof value === 'object' && value !== null) {
return mapValues(value, numberToBigInt) as NumberToBigInt<T>;
}
return value as NumberToBigInt<T>;
}
Expand Down
4 changes: 2 additions & 2 deletions api/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { camelCase, mapKeys } from 'es-toolkit';
import { Response } from 'express';
import _ from 'lodash';

/**
* This is a utility function to extract the types of a subset of value types
Expand Down Expand Up @@ -95,7 +95,7 @@ export type KeysToSnakeCase<T> = {
* Convert an object to a new object with snake_case keys replaced with camelCase.
*/
export function camelize<T extends object>(data: KeysToSnakeCase<T>): T {
return _.mapKeys(data, (_value, key) => _.camelCase(key)) as T;
return mapKeys(data, (_value, key) => camelCase(key as string)) as T;
}

export function badRequest(res: Response, message: string) {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"@types/express": "^4.17.21",
"@types/jest": "^29.5.14",
"@types/jsonwebtoken": "^9.0.7",
"@types/lodash": "^4.17.13",
"@types/morgan": "^1.9.9",
"@types/pg": "^8.11.10",
"@types/uuid": "^10.0.0",
Expand Down Expand Up @@ -76,14 +75,14 @@
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"ejs": "^3.1.10",
"es-toolkit": "^1.29.0",
"express": "^4.21.1",
"express-async-handler": "^1.2.0",
"express-hot-shots": "^1.0.2",
"express-jwt": "^8.4.1",
"hi-base32": "^0.5.1",
"hot-shots": "^10.2.1",
"jsonwebtoken": "^9.0.2",
"lodash": "^4.17.21",
"morgan": "^1.10.0",
"pg": "^8.13.1",
"pg-protocol": "^1.7.0",
Expand Down
18 changes: 8 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0e8895a

Please sign in to comment.