Skip to content

Commit

Permalink
Add support for serialized payloads (#135)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
davestewart and Heziode authored Aug 3, 2021
1 parent 454d1ee commit 858a385
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.5.0] - 2021-08-03
### Added
- Support for serialized payloads - #125 / @Heziode

## [1.4.5] - 2020-12-17
### Fixed
- Setting values on objects using numeric keys is now supported
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vuex-pathify",
"version": "1.4.5",
"version": "1.5.0",
"description": "Ridiculously simple Vuex setup + wiring",
"main": "dist/vuex-pathify.js",
"module": "dist/vuex-pathify.esm.js",
Expand Down
11 changes: 10 additions & 1 deletion src/classes/Payload.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setValue } from '../utils/object'
import { isObject, setValue } from '../utils/object'
import options from '../plugin/options'

/**
Expand Down Expand Up @@ -37,3 +37,12 @@ export default class Payload {
: Object.assign({}, target)
}
}

/**
* Test if value is a serialized Payload
*
* @see https://github.com/davestewart/vuex-pathify/pull/125
*/
Payload.isSerialized = function (value) {
return isObject(value) && 'expr' in value && 'path' in value && 'value' in value
}
3 changes: 3 additions & 0 deletions src/helpers/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export function makeMutations (state) {
.reduce(function (obj, key) {
const mutation = resolveName('mutations', key)
obj[mutation] = function (state, value) {
if (Payload.isSerialized(value)) {
value = new Payload(value.expr, value.path, value.value)
}
state[key] = value instanceof Payload
? value.update(state[key])
: value
Expand Down
17 changes: 17 additions & 0 deletions tests/store-accessors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,20 @@ describe('special functionality', function () {
})
})
})

describe('serialized Payload', () => {
it('serialized Payload should be interpreted', function () {
const state = { name: { firstName: 'John', lastName: 'Doe' }, age: 28 }
const mutations = make.mutations(state)
const store = makeStore({
modules: {
people: { namespaced: true, state, mutations }
}
})

store.commit('people/name', { expr: 'people/name@firstname', value: 'Jane', path: 'firstname' })

expect(store.get('people/name@firstname')).toEqual('Jane')
expect(store.get('people/age')).toEqual(28)
})
})

0 comments on commit 858a385

Please sign in to comment.