Skip to content

Commit 4cc137f

Browse files
Merge pull request #544 from J3m5/refactor/lodash-get
refactor(lodash-get): simplify get lodash get method usage and fix some eslint warnings
2 parents 16dfac8 + e72ad36 commit 4cc137f

File tree

7 files changed

+43
-37
lines changed

7 files changed

+43
-37
lines changed

src/make-find-mixin.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,8 @@ export default function makeFindMixin(options) {
224224
getPaginationForQuery(params = {}) {
225225
const pagination = this[PAGINATION]
226226
const { qid, queryId, pageId } = getQueryInfo(params)
227-
const queryInfo = _get(pagination, `[${qid}][${queryId}]`) || {}
228-
const pageInfo =
229-
_get(pagination, `[${qid}][${queryId}][${pageId}]`) || {}
227+
const queryInfo = _get(pagination, [qid, queryId], {})
228+
const pageInfo = _get(pagination, [qid, queryId, pageId], {})
230229

231230
return { queryInfo, pageInfo }
232231
}

src/service-module/make-base-model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default function makeBaseModel(options: FeathersVuexOptions) {
4949
const { serverAlias } = options
5050

5151
// If this serverAlias already has a BaseModel, return it
52-
const ExistingBaseModel = _get(globalModels, `[${serverAlias}].BaseModel`)
52+
const ExistingBaseModel = _get(globalModels, [serverAlias, 'BaseModel'])
5353
if (ExistingBaseModel) {
5454
return ExistingBaseModel as ModelStatic
5555
}

src/service-module/make-service-plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export default function prepareMakeServicePlugin(
9393
store.registerModule(options.namespace, module, { preserveState: false })
9494

9595
// (2a^) Monkey patch the BaseModel in globalModels
96-
const BaseModel = _get(globalModels, `[${options.serverAlias}].BaseModel`)
96+
const BaseModel = _get(globalModels, [options.serverAlias, 'BaseModel'])
9797
if (BaseModel && !BaseModel.store) {
9898
Object.assign(BaseModel, {
9999
store

src/service-module/service-module.getters.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ import { ServiceState } from '..'
1414
import { Id } from '@feathersjs/feathers'
1515

1616
const FILTERS = ['$sort', '$limit', '$skip', '$select']
17-
const OPERATORS = ['$in', '$nin', '$lt', '$lte', '$gt', '$gte', '$ne', '$or']
1817
const additionalOperators = ['$elemMatch']
19-
const defaultOps = FILTERS.concat(OPERATORS).concat(additionalOperators)
2018

2119
export default function makeServiceGetters() {
2220
return {
@@ -109,10 +107,7 @@ export default function makeServiceGetters() {
109107
if (keepCopiesInStore) {
110108
return state.copiesById[id]
111109
} else {
112-
const Model = _get(
113-
models,
114-
`[${serverAlias}].byServicePath[${servicePath}]`
115-
)
110+
const Model = _get(models, [serverAlias, 'byServicePath', servicePath])
116111

117112
return Model.copiesById[id]
118113
}

src/service-module/service-module.mutations.ts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export type PendingIdServiceMethodName = Exclude<
3434
export default function makeServiceMutations() {
3535
function addItems(state, items) {
3636
const { serverAlias, idField, tempIdField, modelName } = state
37-
const Model = _get(models, `[${serverAlias}][${modelName}]`)
38-
const BaseModel = _get(models, `[${serverAlias}].BaseModel`)
37+
const Model = _get(models, [serverAlias, modelName])
38+
const BaseModel = _get(models, [serverAlias, 'BaseModel'])
3939

4040
for (let item of items) {
4141
const id = getId(item, idField)
@@ -69,8 +69,8 @@ export default function makeServiceMutations() {
6969

7070
function updateItems(state, items) {
7171
const { idField, replaceItems, addOnUpsert, serverAlias, modelName } = state
72-
const Model = _get(models, `[${serverAlias}][${modelName}]`)
73-
const BaseModel = _get(models, `[${state.serverAlias}].BaseModel`)
72+
const Model = _get(models, [serverAlias, modelName])
73+
const BaseModel = _get(models, [state.serverAlias, 'BaseModel'])
7474

7575
for (let item of items) {
7676
const id = getId(item, idField)
@@ -121,7 +121,7 @@ export default function makeServiceMutations() {
121121
}
122122

123123
function mergeInstance(state, item) {
124-
const { serverAlias, idField, tempIdField, modelName } = state
124+
const { idField } = state
125125
const id = getId(item, idField)
126126
const existingItem = state.keyedById[id]
127127
if (existingItem) {
@@ -172,7 +172,7 @@ export default function makeServiceMutations() {
172172
}
173173

174174
// Add _id to temp's clone as well if it exists
175-
const Model = _get(models, `[${state.serverAlias}][${state.modelName}]`)
175+
const Model = _get(models, [state.serverAlias, state.modelName])
176176
const tempClone = Model && Model.copiesById && Model.copiesById[tempId]
177177
if (tempClone) {
178178
tempClone[state.idField] = id
@@ -262,10 +262,7 @@ export default function makeServiceMutations() {
262262
createCopy(state, id) {
263263
const { servicePath, keepCopiesInStore, serverAlias } = state
264264
const current = state.keyedById[id] || state.tempsById[id]
265-
const Model = _get(
266-
models,
267-
`[${serverAlias}].byServicePath[${servicePath}]`
268-
)
265+
const Model = _get(models, [serverAlias, 'byServicePath', servicePath])
269266

270267
if (Model) {
271268
var model = new Model(current, { clone: true })
@@ -291,13 +288,14 @@ export default function makeServiceMutations() {
291288
// Resets the copy to match the original record, locally
292289
resetCopy(state, id) {
293290
const { servicePath, keepCopiesInStore } = state
294-
const Model = _get(
295-
models,
296-
`[${state.serverAlias}].byServicePath[${servicePath}]`
297-
)
291+
const Model = _get(models, [
292+
state.serverAlias,
293+
'byServicePath',
294+
servicePath
295+
])
298296
const copy = keepCopiesInStore
299297
? state.copiesById[id]
300-
: Model && _get(Model, `copiesById[${id}]`)
298+
: Model && _get(Model, ['copiesById', id])
301299

302300
if (copy) {
303301
const original =
@@ -311,13 +309,14 @@ export default function makeServiceMutations() {
311309
// Deep assigns copy to original record, locally
312310
commitCopy(state, id) {
313311
const { servicePath, keepCopiesInStore } = state
314-
const Model = _get(
315-
models,
316-
`[${state.serverAlias}].byServicePath[${servicePath}]`
317-
)
312+
const Model = _get(models, [
313+
state.serverAlias,
314+
'byServicePath',
315+
servicePath
316+
])
318317
const copy = keepCopiesInStore
319318
? state.copiesById[id]
320-
: Model && _get(Model, `copiesById[${id}]`)
319+
: Model && _get(Model, ['copiesById', id])
321320

322321
if (copy) {
323322
const original =
@@ -397,10 +396,15 @@ export default function makeServiceMutations() {
397396
state[`is${uppercaseMethod}Pending`] = false
398397
},
399398

400-
setIdPending(state, payload: { method: PendingIdServiceMethodName, id: Id | Id[] }): void {
399+
setIdPending(
400+
state,
401+
payload: { method: PendingIdServiceMethodName; id: Id | Id[] }
402+
): void {
401403
const { method, id } = payload
402404
const uppercaseMethod = method.charAt(0).toUpperCase() + method.slice(1)
403-
const isIdMethodPending = state[`isId${uppercaseMethod}Pending`] as ServiceState['isIdCreatePending']
405+
const isIdMethodPending = state[
406+
`isId${uppercaseMethod}Pending`
407+
] as ServiceState['isIdCreatePending']
404408
// if `id` is an array, ensure it doesn't have duplicates
405409
const ids = Array.isArray(id) ? [...new Set(id)] : [id]
406410
ids.forEach(id => {
@@ -409,10 +413,15 @@ export default function makeServiceMutations() {
409413
}
410414
})
411415
},
412-
unsetIdPending(state, payload: { method: PendingIdServiceMethodName, id: Id | Id[] }): void {
416+
unsetIdPending(
417+
state,
418+
payload: { method: PendingIdServiceMethodName; id: Id | Id[] }
419+
): void {
413420
const { method, id } = payload
414421
const uppercaseMethod = method.charAt(0).toUpperCase() + method.slice(1)
415-
const isIdMethodPending = state[`isId${uppercaseMethod}Pending`] as ServiceState['isIdCreatePending']
422+
const isIdMethodPending = state[
423+
`isId${uppercaseMethod}Pending`
424+
] as ServiceState['isIdCreatePending']
416425
// if `id` is an array, ensure it doesn't have duplicates
417426
const ids = Array.isArray(id) ? [...new Set(id)] : [id]
418427
ids.forEach(id => {

src/service-module/service-module.state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export default function makeDefaultState(options: MakeServicePluginOptions) {
139139
isIdCreatePending: [],
140140
isIdUpdatePending: [],
141141
isIdPatchPending: [],
142-
isIdRemovePending: [],
142+
isIdRemovePending: []
143143
}
144144

145145
if (options.Model) {

src/service-module/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,10 @@ export interface ModelStatic extends EventEmitter {
269269
* @param id ID of record to retrieve
270270
* @param params Get params
271271
*/
272-
getFromStore<M extends Model = Model>(id: Id | Ref<Id>, params?: Params | Ref<Params>): M | undefined
272+
getFromStore<M extends Model = Model>(
273+
id: Id | Ref<Id>,
274+
params?: Params | Ref<Params>
275+
): M | undefined
273276
}
274277

275278
/** Model instance interface */

0 commit comments

Comments
 (0)