UNPKG

@dvo/raven

Version:

A simple store and databinding tool for vanilla applications.

1 lines 6.25 kB
"use strict";function ownKeys(r,t){var e=Object.keys(r);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(r);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable}))),e.push.apply(e,a)}return e}function _objectSpread(r){for(var t=1;t<arguments.length;t++){var e=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(e),!0).forEach((function(t){_defineProperty(r,t,e[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(e)):ownKeys(Object(e)).forEach((function(t){Object.defineProperty(r,t,Object.getOwnPropertyDescriptor(e,t))}))}return r}function _defineProperty(r,t,e){return t in r?Object.defineProperty(r,t,{value:e,enumerable:!0,configurable:!0,writable:!0}):r[t]=e,r}Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var prefix="_",pipe=function(){for(var r=arguments.length,t=new Array(r),e=0;e<r;e++)t[e]=arguments[e];return r=>t.reduce((r,t)=>t(r),r)},isArray=Array.isArray,isObject=r=>"object"==typeof r&&!isArray(r)&&null!==r,isString=r=>"string"==typeof r,identity=r=>r,fromStringToArray=r=>r.split("."),fromStringListToArrayList=r=>r.map(fromStringToArray),fromArrayToString=r=>r.join("."),fromArrayListToStringList=r=>r.map(fromArrayToString),fromArrayToObject=(r,t)=>r.reverse().reduce((r,e,a)=>0===a?{[e]:t}:{[e]:r},0),fromArrayListToObject=r=>r.map(r=>{var t=[...r],e=t.pop();return fromArrayToObject(t,e)}).reduce((r,t)=>mergeObjects(r,t)),fromStringToObject=(r,t)=>fromArrayToObject(fromStringToArray(r),t),fromObjectToArrayList=r=>{var t=[],e=(r,a)=>{Object.entries(r).forEach(r=>{var[o,i]=r;isObject(i)?e(i,[...a,o]):t.push([...a,o])})};return e(r,[]),t},fromObjectToStringList=r=>fromObjectToArrayList(r).map(fromArrayToString),parseToObject=(r,t)=>"string"==typeof r?fromStringToObject(r,t):r,reach=r=>t=>isArray(t)?reachArray(r,t):reachString(r,t),reachArray=(r,t)=>{if(1===t.length)return{parentObject:r,propertyName:t[0],propertyValue:r[t[0]]};var e=[...t],a=r[e.shift()];return reach(a)(e)},reachString=(r,t)=>reachArray(r,fromStringToArray(t)),addPartialPaths=r=>isArray(r[0])?addPartialArrayPaths(r):addPartialStringPaths(r),addPartialArrayPaths=r=>{var t=[],e=r=>{if(t.push(r),r.length>1){var a=[...r];a.pop(),e(a)}};return r.forEach(e),t},addPartialStringPaths=r=>pipe(fromStringListToArrayList,addPartialArrayPaths,fromArrayListToStringList)(r),mergeObjects=(r,t)=>{var e=_objectSpread({},r);return Object.entries(t).forEach(r=>{var[t,a]=r,o=e[t];isObject(o)&&isObject(a)?e[t]=mergeObjects(o,a):e[t]=a}),e},executeCallbacks=function(r){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:raven.funcs.subscriptions,e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:raven.store,a=pipe(fromObjectToStringList,addPartialPaths)(r),o=reach(e);a.filter(r=>t[r]).flatMap(r=>t[r].map(t=>({path:r,callback:t}))).forEach(r=>r.callback(o(r.path).propertyValue))},setValueToObject=r=>t=>{var e=fromObjectToArrayList(r).map(r=>[...r,t]);return fromArrayListToObject(e)},copyValueToStore=(r,t,e)=>{var a=setValueToObject(t)(e(r.target[r.prop]));set(a)},pushFromElement=function(r,t){var e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:identity,a="string"==typeof r.event?[r.event]:r.event||["change","input"];a.forEach(a=>r.target.addEventListener(a,r=>{var a=r.target.value||r.target.innerHTML,o=e(a);set(setValueToObject(t)(o))}))},pushFromObject=function(r,t){var e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:identity;r.target["".concat(prefix).concat(r.prop)]=r.target[r.prop],copyValueToStore(r,t,e),Object.defineProperty(r.target,r.prop,{set:a=>{var o=e(a);fromObjectToStringList(t).forEach(r=>raven.set(r,o)),r.target["".concat(prefix).concat(r.prop)]=a},get:()=>r.target["".concat(prefix).concat(r.prop)]})},pullToElement=function(r,t){var e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:identity,a=reach(raven.store)(r).propertyValue;t.target[t.prop]=e(a),subscribe(r,r=>{t.target[t.prop]=e(r)})},load=r=>{raven.store=r},set=(r,t)=>{var e="string"==typeof r?fromStringToObject(r,t):r;load(mergeObjects(raven.store,e)),executeCallbacks(e)},subscribe=(r,t)=>{(isObject(r)?fromObjectToStringList(r):isArray(r)?[fromArrayToString(r)]:[r]).forEach(r=>{raven.funcs.subscriptions[r]=[...raven.funcs.subscriptions[r]||[],t]})},clear=r=>{r?(isArray(r)?r:[r]).forEach(r=>{isString(r)?delete raven.funcs.subscriptions[r]:Object.keys(raven.store).forEach(t=>{raven.funcs.subscriptions[t]&&(raven.funcs.subscriptions[t]=raven.funcs.subscriptions[t].filter(t=>t!==r))})}):raven.funcs.subscriptions={}},push=function(r,t){var e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:identity,a=arguments.length>3&&void 0!==arguments[3]?arguments[3]:pushFromElement,o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:pushFromObject,i=parseToObject(t,!0),n=r.target instanceof window.HTMLElement?a:o;n(r,i,e)},pull=function(r,t){var e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:identity,a=arguments.length>3&&void 0!==arguments[3]?arguments[3]:pullToElement,o=r;if(isObject(r)){var i=fromObjectToArrayList(r);if(i.length>1)throw new Error("Raven.pull cannot be passed an object with multiple properties.");o=i[0]}a(o,t,e)},sync=function(r,t){var e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:identity,a=arguments.length>3&&void 0!==arguments[3]?arguments[3]:identity;pull(r,t,a),push(t,r,e)},raven={clear:clear,load:load,pull:pull,push:push,set:set,store:{},subscribe:subscribe,sync:sync,funcs:{addPartialPaths:addPartialPaths,addPartialArrayPaths:addPartialArrayPaths,addPartialStringPaths:addPartialStringPaths,copyValueToStore:copyValueToStore,fromArrayListToObject:fromArrayListToObject,fromArrayToString:fromArrayToString,fromStringToObject:fromStringToObject,fromObjectToStringList:fromObjectToStringList,fromObjectToArrayList:fromObjectToArrayList,fromStringToArray:fromStringToArray,executeCallbacks:executeCallbacks,isArray:isArray,isObject:isObject,isString:isString,mergeObjects:mergeObjects,pullToElement:pullToElement,pushFromObject:pushFromObject,pushFromElement:pushFromElement,reach:reach,reachArray:reachArray,reachString:reachString,setValueToObject:setValueToObject,subscriptions:{}}},_default=raven;exports.default=_default;