diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b00ea2..47a2bd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package.json b/package.json index 6b5c8b2..7fae0fa 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/classes/Payload.js b/src/classes/Payload.js index 8ae3f31..f841ae3 100644 --- a/src/classes/Payload.js +++ b/src/classes/Payload.js @@ -1,4 +1,4 @@ -import { setValue } from '../utils/object' +import { isObject, setValue } from '../utils/object' import options from '../plugin/options' /** @@ -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 +} diff --git a/src/helpers/store.js b/src/helpers/store.js index 98cafe8..6e19bfc 100644 --- a/src/helpers/store.js +++ b/src/helpers/store.js @@ -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 diff --git a/tests/store-accessors.test.js b/tests/store-accessors.test.js index ae80dde..16f0c10 100644 --- a/tests/store-accessors.test.js +++ b/tests/store-accessors.test.js @@ -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) + }) +})