Skip to content

Commit 858a385

Browse files
davestewartHeziode
andauthored
Add support for serialized payloads (#135)
* Add support of serialized Payload The support of serialized Payload allows to use other vuex plugins like persisted and tab-sync to work with vuex-pathify. * Clarify serialized payload detection code * Update version and changelog Co-authored-by: Heziode <[email protected]>
1 parent 454d1ee commit 858a385

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## [1.5.0] - 2021-08-03
9+
### Added
10+
- Support for serialized payloads - #125 / @Heziode
11+
812
## [1.4.5] - 2020-12-17
913
### Fixed
1014
- Setting values on objects using numeric keys is now supported

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vuex-pathify",
3-
"version": "1.4.5",
3+
"version": "1.5.0",
44
"description": "Ridiculously simple Vuex setup + wiring",
55
"main": "dist/vuex-pathify.js",
66
"module": "dist/vuex-pathify.esm.js",

src/classes/Payload.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { setValue } from '../utils/object'
1+
import { isObject, setValue } from '../utils/object'
22
import options from '../plugin/options'
33

44
/**
@@ -37,3 +37,12 @@ export default class Payload {
3737
: Object.assign({}, target)
3838
}
3939
}
40+
41+
/**
42+
* Test if value is a serialized Payload
43+
*
44+
* @see https://github.com/davestewart/vuex-pathify/pull/125
45+
*/
46+
Payload.isSerialized = function (value) {
47+
return isObject(value) && 'expr' in value && 'path' in value && 'value' in value
48+
}

src/helpers/store.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export function makeMutations (state) {
4040
.reduce(function (obj, key) {
4141
const mutation = resolveName('mutations', key)
4242
obj[mutation] = function (state, value) {
43+
if (Payload.isSerialized(value)) {
44+
value = new Payload(value.expr, value.path, value.value)
45+
}
4346
state[key] = value instanceof Payload
4447
? value.update(state[key])
4548
: value

tests/store-accessors.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,20 @@ describe('special functionality', function () {
131131
})
132132
})
133133
})
134+
135+
describe('serialized Payload', () => {
136+
it('serialized Payload should be interpreted', function () {
137+
const state = { name: { firstName: 'John', lastName: 'Doe' }, age: 28 }
138+
const mutations = make.mutations(state)
139+
const store = makeStore({
140+
modules: {
141+
people: { namespaced: true, state, mutations }
142+
}
143+
})
144+
145+
store.commit('people/name', { expr: 'people/name@firstname', value: 'Jane', path: 'firstname' })
146+
147+
expect(store.get('people/name@firstname')).toEqual('Jane')
148+
expect(store.get('people/age')).toEqual(28)
149+
})
150+
})

0 commit comments

Comments
 (0)