Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
refactor: replace some lodash methods with native alternatives (#1897)
Browse files Browse the repository at this point in the history
  • Loading branch information
brenopolanski authored May 9, 2020
1 parent df2e9d2 commit ee301c5
Show file tree
Hide file tree
Showing 22 changed files with 75 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@
</template>

<script>
import os from 'os'
import { MARKET } from '@config'
import { isEmpty } from '@/utils'
import { ModalConfirmation } from '@/components/Modal'
import { MenuNavigationItem, MenuOptions, MenuOptionsItem, MenuDropdown } from '@/components/Menu'
import { ButtonSwitch } from '@/components/Button'
import { PluginManageBlacklistModal } from '@/components/PluginManager/PluginManagerModals'
import { isEmpty } from 'lodash'
import os from 'os'
export default {
name: 'AppSidemenuOptionsSettings',
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/Collapse/CollapseAccordion.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</template>

<script>
import { isEmpty } from 'lodash'
import { isEmpty } from '@/utils'
export default {
name: 'CollapseAccordion',
Expand Down
5 changes: 3 additions & 2 deletions src/renderer/components/Input/InputAddress.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ import Cycled from 'cycled'
import InputField from './InputField'
import WalletService from '@/services/wallet'
import truncate from '@/filters/truncate'
import { includes, isEmpty, orderBy, unionBy } from 'lodash'
import { orderBy, unionBy } from 'lodash'
import { isEmpty } from '@/utils'
export default {
name: 'InputAddress',
Expand Down Expand Up @@ -215,7 +216,7 @@ export default {
const value = wallet.name || wallet.address
const searchValue = value.toLowerCase()
if (includes(searchValue, this.inputValue.toLowerCase())) {
if (searchValue && searchValue.includes(this.inputValue.toLowerCase())) {
wallets[wallet.address] = value
}
Expand Down
5 changes: 2 additions & 3 deletions src/renderer/components/Input/InputCurrency.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@
</template>

<script>
import { includes } from 'lodash'
import { required } from 'vuelidate/lib/validators'
import { MARKET } from '@config'
import InputField from './InputField'
import BigNumber from '@/plugins/bignumber'
import InputField from './InputField'
/**
* This component uses a String value internally to avoid several problems, such
Expand Down Expand Up @@ -378,7 +377,7 @@ export default {
...Object.keys(MARKET.currencies),
...Object.values(MARKET.currencies).map(currency => currency.symbol)
]
return includes(currencies, currency)
return currencies.includes(currency)
},
reset () {
Expand Down
5 changes: 3 additions & 2 deletions src/renderer/components/Input/InputDelegate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@

<script>
import { required } from 'vuelidate/lib/validators'
import Cycled from 'cycled'
import { orderBy } from 'lodash'
import { isEmpty } from '@/utils'
import { ButtonModal } from '@/components/Button'
import ModalQrCodeScanner from '@/components/Modal/ModalQrCodeScanner'
import { MenuDropdown } from '@/components/Menu'
import Cycled from 'cycled'
import InputField from './InputField'
import { isEmpty, orderBy } from 'lodash'
export default {
name: 'InputDelegate',
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/Input/InputGrid/InputGrid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</template>

<script>
import { flatten } from 'lodash'
import { flatten } from '@/utils'
import InputGridItem from './InputGridItem'
import InputGridModal from './InputGridModal'
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/components/Menu/MenuDropdown/MenuDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
</template>

<script>
import { zipObject, isEmpty } from 'lodash'
import { zipObject } from 'lodash'
import { isEmpty } from '@/utils'
import MenuDropdownItem from './MenuDropdownItem'
import MenuDropdownHandler from './MenuDropdownHandler'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
</template>

<script>
import { concat } from 'lodash'
import { PLUGINS } from '@config'
import { PluginManagerButtonMenu } from '@/components/PluginManager/PluginManagerButtons'
Expand All @@ -60,7 +59,7 @@ export default {
computed: {
pluginCategories () {
return concat(['all'], PLUGINS.categories.filter(category => !['theme', 'language'].includes(category)))
return ['all'].concat(PLUGINS.categories.filter(category => !['theme', 'language'].includes(category)))
},
otherCategories () {
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/components/Selection/mixin-selection-image.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path'
import { flatten, sortBy } from 'lodash'
import { sortBy } from 'lodash'
import { flatten } from '@/utils'
import imageManager from '@/services/image-manager'

export default {
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/components/Transaction/TransactionModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</template>

<script>
import { camelCase, includes } from 'lodash'
import { camelCase } from 'lodash'
import { upperFirst } from '@/utils'
import { TRANSACTION_GROUPS, TRANSACTION_TYPES } from '@config'
import MultiSignature from '@/services/client-multisig'
Expand Down Expand Up @@ -81,7 +81,7 @@ export default {
type: Number,
required: true,
validator: value => {
return value === TRANSACTION_TYPES.MULTI_SIGN || includes(TRANSACTION_TYPES.GROUP_1, value)
return value === TRANSACTION_TYPES.MULTI_SIGN || Object.values(TRANSACTION_TYPES.GROUP_1).includes(value)
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/pages/Contact/ContactAll.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
</template>

<script>
import { isEqual, some } from 'lodash'
import { isEqual } from 'lodash'
import { ButtonLayout } from '@/components/Button'
import { ContactRemovalConfirmation, ContactRenameModal } from '@/components/Contact'
import { WalletGrid, WalletIdenticonPlaceholder } from '@/components/Wallet'
Expand Down Expand Up @@ -161,7 +161,7 @@ export default {
},
showVotedDelegates () {
return some(this.contacts, contact => Object.prototype.hasOwnProperty.call(contact, 'vote'))
return this.contacts.some(contact => Object.prototype.hasOwnProperty.call(contact, 'vote'))
}
},
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/pages/Profile/ProfileEdition.vue
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,9 @@
</template>

<script>
import { clone, isEmpty } from 'lodash'
import { clone } from 'lodash'
import { BIP39, I18N, MARKET, PLUGINS } from '@config'
import { isEmpty } from '@/utils'
import { ButtonSwitch } from '@/components/Button'
import { InputText } from '@/components/Input'
import { ListDivided, ListDividedItem } from '@/components/ListDivided'
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/pages/Wallet/WalletAll.vue
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
</template>

<script>
import { isEqual, some, uniqBy } from 'lodash'
import { isEqual, uniqBy } from 'lodash'
import { ButtonLayout } from '@/components/Button'
import Loader from '@/components/utils/Loader'
import { ProfileAvatar } from '@/components/Profile'
Expand Down Expand Up @@ -263,7 +263,7 @@ export default {
},
showVotedDelegates () {
return some(this.selectableWallets, wallet => Object.prototype.hasOwnProperty.call(wallet, 'vote'))
return this.selectableWallets.some(wallet => Object.prototype.hasOwnProperty.call(wallet, 'vote'))
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/pages/Wallet/WalletNew.vue
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@
</template>

<script>
import { flatten } from 'lodash'
import { required } from 'vuelidate/lib/validators'
import { flatten } from '@/utils'
import { ButtonClipboard, ButtonReload } from '@/components/Button'
import { InputPassword, InputSwitch, InputText } from '@/components/Input'
import { MenuStep, MenuStepItem } from '@/components/Menu'
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/services/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import TransactionService from '@/services/transaction'
import { TransactionBuilderService } from './crypto/transaction-builder.service'
import { TransactionSigner } from './crypto/transaction-signer'
import BigNumber from '@/plugins/bignumber'
import { cammelToUpperSnake } from '@/utils'
import { camelToUpperSnake } from '@/utils'

export default class ClientService {
/**
Expand Down Expand Up @@ -110,7 +110,7 @@ export default class ClientService {
@config is in UPPER_SNAKE_CASE. Eg: 'BUSSINES_UPDATE'
*/
const groupName = `GROUP_${groupId}`
const parsedTypeName = cammelToUpperSnake(typeName)
const parsedTypeName = camelToUpperSnake(typeName)

const type = TRANSACTION_TYPES[groupName][parsedTypeName]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path'
import fs from 'fs'
import { normalizeJson } from '../utils/normalize-json'
import { isEmpty } from 'lodash'
import { I18N } from '@config'
import { isEmpty } from '@/utils'

export function create (plugin, pluginObject, sandbox, profileId) {
return async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/services/plugin-manager/setup/themes-setup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path'
import fs from 'fs'
import { normalizeJson } from '../utils/normalize-json'
import { isEmpty } from 'lodash'
import { isEmpty } from '@/utils'

export function create (plugin, pluginObject, sandbox, profileId) {
return async () => {
Expand Down
7 changes: 4 additions & 3 deletions src/renderer/services/synchronizer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { flatten, includes, pullAll } from 'lodash'
import { pullAll } from 'lodash'
import { flatten } from '@/utils'
import { announcements, fees, ledger, market, peer, wallets } from './synchronizer/'
/**
* This class adds the possibility to define actions (not to confuse with Vuex actions)
Expand Down Expand Up @@ -194,14 +195,14 @@ export default class Synchronizer {
*/
const run = (options = {}) => {
Object.keys(this.actions).forEach(actionId => {
if (!includes(this.paused, actionId)) {
if (!this.paused.includes(actionId)) {
const action = this.actions[actionId]

if (!action.isCalling) {
if (options.immediate) {
this.call(actionId)
} else {
const mode = includes(this.focused, actionId) ? 'focus' : 'default'
const mode = this.focused.includes(actionId) ? 'focus' : 'default'
const { interval } = action[mode]

// A `null` interval means no interval, so the action does not run
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/store/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Vue from 'vue'
import Vuex from 'vuex'
import localforage from 'localforage'
import { merge, pullAll, keys } from 'lodash'
import { merge, pullAll } from 'lodash'

import packageJson from '@package.json'

Expand Down Expand Up @@ -48,7 +48,7 @@ const vuexMigrations = new VuexPersistMigrations({
}
})

const modulesWithoutPersistence = pullAll(keys(modules), ['delegate', 'market', 'updater'])
const modulesWithoutPersistence = pullAll(Object.keys(modules), ['delegate', 'market', 'updater'])

const vuexPersist = new VuexPersistence({
key: 'ark-desktop',
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/store/modules/network.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import BaseModule from '../base'
import { cloneDeep, isEmpty } from 'lodash'
import { cloneDeep } from 'lodash'
import { Managers } from '@arkecosystem/crypto'
import { NETWORKS } from '@config'
import { isEmpty } from '@/utils'
import eventBus from '@/plugins/event-bus'
import NetworkModel from '@/models/network'
import Client from '@/services/client'
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/store/modules/peer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { isEmpty, random, shuffle } from 'lodash'
import { random, shuffle } from 'lodash'
import { PeerDiscovery } from '@arkecosystem/peers'
import ClientService from '@/services/client'
import config from '@config'
import i18n from '@/i18n'
import { isEmpty } from '@/utils'
import PeerModel from '@/models/peer'
import Vue from 'vue'

Expand Down
54 changes: 35 additions & 19 deletions src/renderer/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import sortByProps from './sort-by-props'

/**
* Converts the first character of `string` to upper case
* @param {String} str
* @return {String} Return the converted string
* Converts camelCaseString to UPPER_CAMEL_CASE_STRING
* @param {String} String the camelCaseString
* @returns {String} The UPPER_CAMEL_CASE
*/
const upperFirst = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`
const camelToUpperSnake = string => string.split(/(?=[A-Z])/).join('_').toUpperCase()

/**
* Converts the first character of `string` to upper case and the remaining to lower case
Expand All @@ -14,6 +14,20 @@ const upperFirst = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`
*/
const capitalize = str => `${str.charAt(0).toUpperCase()}${str.slice(1).toLowerCase()}`

/**
* Flattens `array` a single level deep
* @param {Array} arr
* @return {Array} Return the new flattened array
*/
const flatten = arr => arr.reduce((a, b) => a.concat(b), [])

/**
* Checks if `value` is an empty object, collection, map, or set
* @param {*} val
* @return {Boolean} Return `true` if `value` is empty, else `false`
*/
const isEmpty = val => [Object, Array].includes((val || {}).constructor) && !Object.entries((val || {})).length

/**
* Checks if `value` is `null` or `undefined`
* @param {*} val
Expand All @@ -22,40 +36,42 @@ const capitalize = str => `${str.charAt(0).toUpperCase()}${str.slice(1).toLowerC
const isNil = val => val === null || val === undefined

/**
* Computes the minimum value of `array`
* Computes the maximum value of `array`
* @param {Array} nums
* @return {*} Return the minimum value
* @return {*} Return the maximum value
*/
const min = nums => {
const max = nums => {
if (nums.length) {
return Math.min(...nums)
return Math.max(...nums)
}
}

/**
* Computes the maximum value of `array`
* Computes the minimum value of `array`
* @param {Array} nums
* @return {*} Return the maximum value
* @return {*} Return the minimum value
*/
const max = nums => {
const min = nums => {
if (nums.length) {
return Math.max(...nums)
return Math.min(...nums)
}
}

/**
* Converts cammelCaseString to UPPER_CAMMEL_CASE_STRING.
* @param {string} string The cammelCaseString
* @returns {string} The UPPER_CAMMEL_CASE.
* Converts the first character of `string` to upper case
* @param {String} str
* @return {String} Return the converted string
*/
const cammelToUpperSnake = string => string.split(/(?=[A-Z])/).join('_').toUpperCase()
const upperFirst = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`

export {
upperFirst,
camelToUpperSnake,
capitalize,
flatten,
isEmpty,
isNil,
min,
max,
min,
sortByProps,
cammelToUpperSnake
upperFirst
}

0 comments on commit ee301c5

Please sign in to comment.