Skip to content

Commit

Permalink
feat: added pinia plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kouts committed May 22, 2021
1 parent 265ec35 commit def484c
Show file tree
Hide file tree
Showing 16 changed files with 787 additions and 84 deletions.
22 changes: 11 additions & 11 deletions dist/cjs/pathStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,19 @@ var deleteMany = function deleteMany(obj, path) {

var ARRAY_METHODS = ['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'];

var createPathStore = function createPathStore(state) {
var store = Vue__default['default'].observable(state);

var methods = _objectSpread2({
function createPathStoreMethods() {
return _objectSpread2({
set: function set(path, value) {
setMany(store, path, value);
setMany(this, path, value);
},
toggle: function toggle(path) {
setOne(store, path, !getByPath(store, path));
setOne(this, path, !getByPath(this, path));
},
get: function get(path) {
return path ? getByPath(store, path) : store;
return path ? getByPath(this, path) : this;
},
del: function del(path) {
deleteMany(store, path);
deleteMany(this, path);
}
}, ARRAY_METHODS.reduce(function (acc, method) {
var fn = function fn() {
Expand All @@ -217,19 +215,21 @@ var createPathStore = function createPathStore(state) {
}

var path = args.shift();
var arr = getByPath(store, path);
var arr = getByPath(this, path);

if (!isArray(arr)) {
throw Error('Argument must be an array.');
}

arr[method].apply(arr, args);
return arr[method].apply(arr, args);
};

return Object.assign(acc, _defineProperty({}, method, fn));
}, {}));
}

return Object.assign(store, methods);
var createPathStore = function createPathStore(state) {
return Object.assign(Vue__default['default'].observable(state), createPathStoreMethods());
};

exports.createPathStore = createPathStore;
235 changes: 235 additions & 0 deletions dist/cjs/pathStorePiniaPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var Vue = require('vue');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);

function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);

if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);

if (enumerableOnly) {
symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
}

keys.push.apply(keys, symbols);
}

return keys;
}

function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};

if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}

return target;
}

function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}

return obj;
}

function _typeof(obj) {
"@babel/helpers - typeof";

if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function _typeof(obj) {
return typeof obj;
};
} else {
_typeof = function _typeof(obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}

return _typeof(obj);
}

function isObject(obj) {
return _typeof(obj) === 'object' && !Array.isArray(obj) && obj !== null;
}

function isNumeric(str) {
return !isNaN(str) && !isNaN(parseFloat(str));
}

function isArray(arr) {
return Array.isArray(arr);
}

function splitPath(str) {
var regex = /(\w+)|\[([^\]]+)\]/g;
var result = [];
var path;

while (path = regex.exec(str || '')) {
if (str[path.index] === '[') {
result.push(path[2]);
} else {
result.push(path[1]);
}
}

return result;
}

function getByPath(obj, path) {
var parts = isArray(path) ? path : splitPath(path);
var length = parts.length;

for (var i = 0; i < length; i++) {
if (typeof obj[parts[i]] === 'undefined') {
return undefined;
}

obj = obj[parts[i]];
}

return obj;
}

var setOne = function setOne(obj, pathStr, value) {
var path = splitPath(pathStr);
var length = path.length;
var lastIndex = length - 1;

for (var index = 0; index < length; index++) {
var prop = path[index]; // If we are not on the last index
// we start building the data object from the path

if (index !== lastIndex) {
var objValue = obj[prop]; // If objValue exists, is not primitive and is not observable, then make it so using Vue.set

if (objValue && _typeof(objValue) === 'object') {
// eslint-disable-next-line no-prototype-builtins
if (!objValue.hasOwnProperty('__ob__')) {
Vue__default['default'].set(obj, prop, objValue);
} // Array to object transformation
// Check if parent path is an array, we are not on the last item
// and the next key in the path is not a number


if (isArray(objValue) && !isNumeric(path[index + 1])) {
Vue__default['default'].set(obj, prop, {});
}
} else {
// Create an empty object or an empty array based on the next path entry
if (isNumeric(path[index + 1])) {
Vue__default['default'].set(obj, prop, []);
} else {
Vue__default['default'].set(obj, prop, {});
}
}
} else {
// If we are on the last index then we just assign the the value to the data object
// Note: If we used obj[prop] = value; arrays wouldn't be updated.
Vue__default['default'].set(obj, prop, value);
}

obj = obj[prop];
}
};

var setMany = function setMany(obj, path, value) {
if (typeof path === 'string') {
setOne(obj, path, value);
} else if (isObject(path)) {
for (var key in path) {
setOne(obj, key, path[key]);
}
} else {
throw Error('Arguments must be either string or object.');
}
};

var deleteOne = function deleteOne(obj, pathStr) {
var path = splitPath(pathStr);
var prop = path.pop();
Vue__default['default']["delete"](getByPath(obj, path), prop);
};

var deleteMany = function deleteMany(obj, path) {
if (typeof path === 'string') {
deleteOne(obj, path);
} else if (isArray(path)) {
path.forEach(function (item) {
deleteOne(obj, item);
});
} else {
throw Error('Arguments must be either string or array.');
}
};

var ARRAY_METHODS = ['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'];

function createPathStoreMethods() {
return _objectSpread2({
set: function set(path, value) {
setMany(this, path, value);
},
toggle: function toggle(path) {
setOne(this, path, !getByPath(this, path));
},
get: function get(path) {
return path ? getByPath(this, path) : this;
},
del: function del(path) {
deleteMany(this, path);
}
}, ARRAY_METHODS.reduce(function (acc, method) {
var fn = function fn() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

var path = args.shift();
var arr = getByPath(this, path);

if (!isArray(arr)) {
throw Error('Argument must be an array.');
}

return arr[method].apply(arr, args);
};

return Object.assign(acc, _defineProperty({}, method, fn));
}, {}));
}

var pathStorePiniaPlugin = function pathStorePiniaPlugin(ctx) {
return Object.assign(ctx.store.actions = ctx.store.actions || {}, createPathStoreMethods());
};

exports.pathStorePiniaPlugin = pathStorePiniaPlugin;
8 changes: 4 additions & 4 deletions dist/cjs/pathStoreVuexPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,14 @@ var pathStoreVuexPlugin = function pathStoreVuexPlugin(store) {
}

var path = args.shift();
store.commit(method, {
return store.commit(method, {
path: path,
args: args
});
};

return Object.assign(acc, _defineProperty({}, method, fn));
}));
}, {}));

var mutations = _objectSpread2({
set: function set(state, info) {
Expand All @@ -288,11 +288,11 @@ var pathStoreVuexPlugin = function pathStoreVuexPlugin(store) {
throw Error('Argument must be an array');
}

arr[method].apply(arr, _toConsumableArray(args));
return arr[method].apply(arr, _toConsumableArray(args));
};

return Object.assign(acc, _defineProperty({}, method, fn));
}));
}, {}));

var _loop = function _loop(type) {
var entry = store._mutations[type] || (store._mutations[type] = []);
Expand Down
22 changes: 11 additions & 11 deletions dist/es/pathStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,19 @@ var deleteMany = function deleteMany(obj, path) {

var ARRAY_METHODS = ['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'];

var createPathStore = function createPathStore(state) {
var store = Vue.observable(state);

var methods = _objectSpread2({
function createPathStoreMethods() {
return _objectSpread2({
set: function set(path, value) {
setMany(store, path, value);
setMany(this, path, value);
},
toggle: function toggle(path) {
setOne(store, path, !getByPath(store, path));
setOne(this, path, !getByPath(this, path));
},
get: function get(path) {
return path ? getByPath(store, path) : store;
return path ? getByPath(this, path) : this;
},
del: function del(path) {
deleteMany(store, path);
deleteMany(this, path);
}
}, ARRAY_METHODS.reduce(function (acc, method) {
var fn = function fn() {
Expand All @@ -209,19 +207,21 @@ var createPathStore = function createPathStore(state) {
}

var path = args.shift();
var arr = getByPath(store, path);
var arr = getByPath(this, path);

if (!isArray(arr)) {
throw Error('Argument must be an array.');
}

arr[method].apply(arr, args);
return arr[method].apply(arr, args);
};

return Object.assign(acc, _defineProperty({}, method, fn));
}, {}));
}

return Object.assign(store, methods);
var createPathStore = function createPathStore(state) {
return Object.assign(Vue.observable(state), createPathStoreMethods());
};

export { createPathStore };
Loading

0 comments on commit def484c

Please sign in to comment.