Skip to content

Commit c3687e9

Browse files
jskrzypekdaffl
authored andcommitted
Support array namespaces for module nesting (#35)
* Replace deep-assign/clone with lodash's merge/clonedeep * Allow namespaces as arrays * Fix service-module import * Use prepare script to make github install work
1 parent d7c4630 commit c3687e9

File tree

5 files changed

+42
-31
lines changed

5 files changed

+42
-31
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"node": ">= 4.6.0"
2727
},
2828
"scripts": {
29+
"prepare": "npm run compile",
2930
"prepublish": "npm run compile",
3031
"publish": "git push origin --tags && npm run changelog && git push origin",
3132
"release:patch": "npm version patch && npm publish",
@@ -65,13 +66,14 @@
6566
"lib": "lib"
6667
},
6768
"dependencies": {
68-
"clone": "^2.1.1",
6969
"debug": "^2.6.3",
70-
"deep-assign": "^2.0.0",
7170
"feathers-commons": "^0.8.7",
7271
"feathers-errors": "^2.6.3",
7372
"feathers-query-filters": "^2.1.2",
73+
"lodash.clonedeep": "^4.5.0",
7474
"lodash.isobject": "^3.0.2",
75+
"lodash.merge": "^4.6.0",
76+
"lodash.trim": "^4.5.1",
7577
"rubberduck": "^1.1.1",
7678
"serialize-error": "^2.1.0"
7779
},

src/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import rubberduck from 'rubberduck/dist/rubberduck'
22
import setupServiceModule from './service-module/service-module'
33
import setupAuthModule from './auth-module/auth-module'
4-
import deepAssign from 'deep-assign'
5-
import clone from 'clone'
4+
import _merge from 'lodash.merge'
5+
import _cloneDeep from 'lodash.clonedeep'
66
import { normalizePath, makeConfig } from './utils'
77

88
const defaultOptions = {
@@ -21,8 +21,8 @@ const defaultOptions = {
2121
}
2222

2323
export default function (clientOrStore, options = {}, modules = {}) {
24-
var theClone = clone(defaultOptions)
25-
options = deepAssign(theClone, options)
24+
var theClone = _cloneDeep(defaultOptions)
25+
options = _merge(theClone, options)
2626

2727
return function feathersVuex (arg) {
2828
const asFeathersPlugin = !arg
@@ -36,9 +36,9 @@ export default function (clientOrStore, options = {}, modules = {}) {
3636
throw new Error('You must pass a Feathers Client instance to the Feathers-Vuex plugin.')
3737
}
3838

39-
// Normalize the modules into objects if they were provided as a string.
39+
// Normalize the modules into objects if they were provided as a string or an array
4040
Object.keys(modules).forEach(namespace => {
41-
if (typeof modules[namespace] === 'string') {
41+
if (typeof modules[namespace] === 'string' || Array.isArray(modules[namespace])) {
4242
modules[namespace] = { namespace: modules[namespace] }
4343
}
4444
})

src/service-module/mutations.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import deepAssign from 'deep-assign'
1+
import _merge from 'lodash.merge'
2+
import _cloneDeep from 'lodash.clonedeep'
23
import serializeError from 'serialize-error'
34
import isObject from 'lodash.isobject'
45

@@ -128,7 +129,7 @@ export default function makeServiceMutations (service) {
128129
setCurrent (state, item) {
129130
let id = isObject(item) ? item[idField] : item
130131
state.currentId = id
131-
state.copy = deepAssign({}, item)
132+
state.copy = _cloneDeep(item)
132133
},
133134

134135
clearCurrent (state) {
@@ -139,13 +140,13 @@ export default function makeServiceMutations (service) {
139140
// Deep assigns current to copy
140141
rejectCopy (state) {
141142
let current = state.keyedById[state.currentId]
142-
deepAssign(state.copy, current)
143+
_merge(state.copy, current)
143144
},
144145

145146
// Deep assigns copy to current
146147
commitCopy (state) {
147148
let current = state.keyedById[state.currentId]
148-
deepAssign(current, state.copy)
149+
_merge(current, state.copy)
149150
},
150151

151152
setFindPending (state) {

src/service-module/service-module.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { getShortName, getNameFromPath, getNameFromConfig } from '../utils'
2-
import deepAssign from 'deep-assign'
1+
import { getShortName, getNameFromPath, getNameFromExplicit } from '../utils'
2+
import _merge from 'lodash.merge'
33
import makeState from './state'
44
import makeGetters from './getters'
55
import makeMutations from './mutations'
@@ -11,7 +11,7 @@ export default function setupServiceModule (store) {
1111
const nameStyles = {
1212
short: getShortName,
1313
path: getNameFromPath,
14-
explicit: getNameFromConfig
14+
explicit: getNameFromExplicit
1515
}
1616
let namespace = nameStyles[vuexOptions.global.nameStyle](service)
1717
const existingName = service.vuexOptions.module.oldName
@@ -23,7 +23,7 @@ export default function setupServiceModule (store) {
2323
}
2424

2525
// update the name
26-
deepAssign(service.vuexOptions, { module: {namespace} })
26+
_merge(service.vuexOptions, { module: {namespace} })
2727
vuexOptions.modules[service.path] = vuexOptions.module
2828

2929
// Setup or re-setup the module if .vuex() was called manually.

src/utils.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import deepAssign from 'deep-assign'
1+
import _merge from 'lodash.merge'
2+
import _cloneDeep from 'lodash.clonedeep'
3+
import _trim from 'lodash.trim'
24

35
export function stripSlashes (location) {
4-
return location.replace(/^(\/*)|(\/*)$/g, '')
6+
return Array.isArray(location) ? location.map(l => _trim(l, '/')) : _trim(location, '/')
57
}
68

79
export function normalizePath (service, location) {
@@ -13,34 +15,40 @@ export function upperCaseFirst (string) {
1315
return string.charAt(0).toUpperCase() + string.slice(1)
1416
}
1517

18+
export function getNameFromConfig (service) {
19+
return service.vuexOptions.module && service.vuexOptions.module.namespace
20+
}
21+
1622
export function getShortName (service) {
1723
// If a name was manually provided, use it.
18-
let explicitName = service.vuexOptions.module && service.vuexOptions.module.namespace
19-
if (explicitName) {
20-
return stripSlashes(explicitName)
24+
let namespace = getNameFromConfig(service)
25+
if (namespace) {
26+
return stripSlashes(namespace)
2127
}
2228

2329
// Otherwise, create a short name.
24-
let location = stripSlashes(service.path)
25-
if (location.includes('/')) {
26-
location = location.slice(location.lastIndexOf('/') + 1)
30+
namespace = stripSlashes(service.path)
31+
if (Array.isArray(namespace)) {
32+
namespace = namespace.slice(-1);
33+
} else if (namespace.includes('/')) {
34+
namespace = namespace.slice(namespace.lastIndexOf('/') + 1)
2735
}
28-
return location
36+
return namespace
2937
}
3038

3139
export function getNameFromPath (service) {
3240
// If a name was manually provided, use it.
33-
let explicitName = service.vuexOptions.module && service.vuexOptions.module.namespace
34-
if (explicitName) {
35-
return stripSlashes(explicitName)
41+
let namespace = getNameFromConfig(service)
42+
if (namespace) {
43+
return stripSlashes(namespace)
3644
}
3745

3846
// Otherwise return the full service path.
3947
return service.path
4048
}
4149

42-
export function getNameFromConfig (service) {
43-
const namespace = service.vuexOptions.module && service.vuexOptions.module.namespace
50+
export function getNameFromExplicit (service) {
51+
const namespace = getNameFromConfig(service)
4452
if (!namespace) {
4553
throw new Error(`The feathers-vuex nameStyle attribute is set to explicit, but no name was provided for the ${service.path} service.`)
4654
}
@@ -57,7 +65,7 @@ export function makeConfig (options, modules) {
5765

5866
// moduleOptions (passed to the vuex method) will overwrite previous options.
5967
if (moduleOptions) {
60-
deepAssign(modules[service.path], moduleOptions)
68+
_merge(modules[service.path], moduleOptions)
6169
}
6270

6371
// Make the config available on the service.

0 commit comments

Comments
 (0)