Skip to content

Commit

Permalink
Merge pull request #1578 from aeternity/avoid-lodash-get
Browse files Browse the repository at this point in the history
refactor: don't use lodash's `get` and similar
  • Loading branch information
davidyuk authored Feb 11, 2025
2 parents f6d3ce1 + 1d9340a commit 9c140e5
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 34 deletions.
6 changes: 2 additions & 4 deletions src/components/ListItemCircle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
</template>

<script>
import { get } from 'lodash-es';
export default {
computed: {
hasIcon() {
return get(this.$slots.default, '[0].tag', '') === 'svg'
&& get(this.$slots.default, '[0].data.staticClass', '').includes('icon');
return this.$slots.default[0]?.tag === 'svg'
&& this.$slots.default[0]?.data?.staticClass.includes('icon');
},
},
};
Expand Down
3 changes: 1 addition & 2 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable max-classes-per-file */
import { get } from 'lodash-es';
import { AENS_DOMAIN } from './constants';

export const toUrl = (url) => new URL((/^\w+:\//.test(url) ? '' : 'http://') + url);
Expand All @@ -14,7 +13,7 @@ export const isNotFoundError = (error) => error.statusCode === 404;
export const isInternalServerError = (error) => [500, 503].includes(error.statusCode);

export const isAccountNotFoundError = (error) => isNotFoundError(error) && (
get(error, 'response.body.reason') === 'Account not found'
error.response?.body?.reason === 'Account not found'
|| error.message.includes('Account not found')
);

Expand Down
3 changes: 1 addition & 2 deletions src/pages/mobile/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@

<script>
import { mapState } from 'vuex';
import { get } from 'lodash-es';
import AeCard from '../../components/AeCard.vue';
import Page from '../../components/Page.vue';
import Guide from '../../components/Guide.vue';
Expand Down Expand Up @@ -181,7 +180,7 @@ export default {
remoteConnectionsCount: ({ mobile }) => Object
.entries(mobile.followers).filter(([, f]) => f.connected).length,
appsAccountAccessCount: ({ apps }) => apps
.filter((app) => get(app, 'permissions.accessToAccounts.length', 0)).length,
.filter((app) => app.permissions.accessToAccounts.length).length,
mnemonic: ({ accounts: { hdWallet: { mnemonic } } }) => mnemonic,
removableAccounts: ({ accounts: { list } }) => list
.map((account, idx) => ({ ...account, idx }))
Expand Down
3 changes: 1 addition & 2 deletions src/pages/mobile/SettingsAppList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

<script>
import { mapState } from 'vuex';
import { get } from 'lodash-es';
import Page from '../../components/Page.vue';
import AeCard from '../../components/AeCard.vue';
import ListItem from '../../components/ListItem.vue';
Expand All @@ -46,7 +45,7 @@ export default {
computed: mapState({
apps({ apps }, getters) {
return apps
.filter((app) => get(app, 'permissions.accessToAccounts.length', 0))
.filter((app) => app.permissions.accessToAccounts.length)
.map((app) => {
const c = app.permissions.accessToAccounts.length;
return {
Expand Down
3 changes: 0 additions & 3 deletions src/store/__mocks__/crypto-api.js

This file was deleted.

20 changes: 8 additions & 12 deletions src/store/modules/root.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* eslint no-param-reassign: ["error", { "ignorePropertyModificationsFor": ["state"] }] */

import Vue from 'vue';
import {
update, mergeWith, isPlainObject, camelCase,
} from 'lodash-es';
import { mergeWith, isPlainObject, camelCase } from 'lodash-es';
import { Node, _Middleware } from '@aeternity/aepp-sdk-next';
import networksRegistry from '../../lib/networksRegistry';
import { genRandomBuffer } from '../utils';
Expand Down Expand Up @@ -93,15 +91,13 @@ export default {
state.customNetworks.splice(networkIdx - networksRegistry.length, 1);
},
toggleAccessToAccount({ apps }, { appHost, accountAddress }) {
if (!getAppByHost(apps, appHost)) apps.push({ host: appHost });
const app = getAppByHost(apps, appHost);
update(
app,
'permissions.accessToAccounts',
(arr = []) => (arr.includes(accountAddress)
? arr.filter((address) => address !== accountAddress)
: [...arr, accountAddress]),
);
if (!getAppByHost(apps, appHost)) {
apps.push({ host: appHost, permissions: { accessToAccounts: [] } });
}
const { permissions: { accessToAccounts } } = getAppByHost(apps, appHost);
const idx = accessToAccounts.indexOf(accountAddress);
if (idx === -1) accessToAccounts.push(accountAddress);
else accessToAccounts.splice(idx, 1);
},
setOnLine(state, onLine) {
state.onLine = onLine;
Expand Down
4 changes: 2 additions & 2 deletions src/store/plugins/initSdk.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get, isEqual } from 'lodash-es';
import { isEqual } from 'lodash-es';
import { handleUnknownError } from '../../lib/utils';

export default (store) => {
Expand All @@ -18,7 +18,7 @@ export default (store) => {
}

async #ensureCurrentAccountAccessPure() {
const accessToAccounts = get(store.getters.getApp(this.host), 'permissions.accessToAccounts', []);
const accessToAccounts = store.getters.getApp(this.host)?.permissions.accessToAccounts ?? [];
if (accessToAccounts.includes(store.getters['accounts/active'].address)) return;

const controller = new AbortController();
Expand Down
8 changes: 3 additions & 5 deletions src/store/plugins/ui/appsMetadata.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint no-param-reassign: ["error", { "ignorePropertyModificationsFor": ["state"] }] */

import { flatMap } from 'lodash-es';
import Vue from 'vue';
import { handleUnknownError } from '../../../lib/utils';
import { PROTOCOL_DEFAULT } from '../../../lib/constants';
Expand All @@ -22,10 +21,9 @@ export default (store) => store.registerModule('appsMetadata', {
name: manifest.short_name || manifest.name || host,
};

const icons = flatMap(
manifest.icons || [],
({ sizes = '', ...icon }) => sizes.split(' ').map((size) => ({ ...icon, size })),
)
const icons = (manifest.icons || [])
.map(({ sizes = '', ...icon }) => sizes.split(' ').map((size) => ({ ...icon, size })))
.flat()
.map(({ size, ...icon }) => ({ ...icon, side: Math.max(...size.split('x')) }));
const icon = icons.reduce((p, i) => {
if (!p) return i || p;
Expand Down
3 changes: 1 addition & 2 deletions src/store/plugins/ui/names.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint no-param-reassign: ["error", { "ignorePropertyModificationsFor": ["state"] }] */
import { get } from 'lodash-es';
import BigNumber from 'bignumber.js';
import Vue from 'vue';
import { isAddressValid, produceNameId } from '@aeternity/aepp-sdk-next';
Expand All @@ -13,7 +12,7 @@ export default (store) => {
namespaced: true,
state: {
names: {},
defaults: get(store.state, 'names.defaults', {}),
defaults: store.state.names?.defaults ?? {},
owned: [],
},
getters: {
Expand Down

0 comments on commit 9c140e5

Please sign in to comment.