zustand-lite
Version:
Zustand Lite builds upon zustand, by auto-generating selectors and simplifying API even more.
2 lines • 3.49 kB
JavaScript
function O(e){return e}import{devtools as C,persist as z}from"zustand/middleware";import{createStore as _}from"zustand/vanilla";import{shallow as j}from"zustand/shallow";import{shallow as R}from"zustand/shallow";import w from"error-stack-parser";function p(){let e=w.parse(new Error),r=e.findIndex(t=>t.functionName?.includes("_zustandLiteInferName_"));return r-1>=0?e[r-1].functionName:null}function u(e,r){return(t,o,n)=>{let i=e.getState(),s=typeof t=="function"?t(i):t;R(i,s)||e.setState(s,o,r?{type:p()??n??"setState",payload:s}:void 0)}}function d(e,r,t){let o=u(e,t);return r.forEach(n=>{o[n]=i=>{j(e.getState()[n],i)||e.setState(s=>({...s,[n]:i}),!1,t?{type:p()??n,payload:{[n]:i}}:void 0)}}),o}var x=e=>e,c=(e,r)=>r.reduce((t,o)=>(o in e&&(t[o]=e[o]),t),{});import{shallow as N}from"zustand/shallow";import{useStoreWithEqualityFn as E}from"zustand/traditional";function y(e,r,t,o){typeof e=="object"&&e!==null&&Object.keys(e).forEach(n=>{let i=[...t,n];Object.defineProperty(r,n,{value:(s=N)=>E(o,a=>P(a,i),s),writable:!0,configurable:!0,enumerable:!0}),y(e[n],r[n],i,o)})}function P(e,r){let t=e;for(let o of r)if(t=t[o],!t)return t;return t}import{shallow as U}from"zustand/shallow";import{useStoreWithEqualityFn as W}from"zustand/traditional";function m(e){return(r=x,t=U)=>W(e,Array.isArray(r)?o=>c(o,r):r??x,t)}function g(e,r){let t=m(e);return y(c(e.getState(),r),t,[],e),t}function b(e,r,t,o){let n=typeof e=="function"?e(r):e;return t.setState(n),r.use={...r.use,...g(t,Object.keys(n))},r.set={...r.set,...d(t,Object.keys(n),o)},r}import{shallow as D}from"zustand/shallow";import{useStoreWithEqualityFn as I}from"zustand/traditional";function l(e){return()=>e.getState()}function G(e,r,t){let o=e({get:r.get}),n={};return Object.keys(o).forEach(i=>{n[i]=(...s)=>I(t,()=>o[i](...s),D)}),r.use=Object.assign(m(t),r.use,n),r.get=Object.assign(l(t),r.get,o),r}function A(e,r,t,o){let n=u(t,o),i=Object.entries(e(r)).reduce((s,[a,S])=>(s[a]=function(...h){return S(...h)},s),{});return r.set=Object.assign(n,r.set,i),r}function F(e,r){return{getInitialState:e.getInitialState,getState:e.getState,persist:M(e,r),setState:e.setState,subscribe:e.subscribe}}function M(e,r){if("persist"in e){let t=e.persist;return t.read=()=>{try{return JSON.parse(localStorage?.getItem(r)??"")?.state}catch{return}},t}}function B(e){return l(e)}function L(e,r,t,o){return{api:t.api,set:t.set,use:e?(()=>{let n=Object.keys(t.use).reduce((i,s)=>r[s]&&e.includes(s)?i:{...i,[s]:t.use[s]},{});return Object.assign(m(o),n)})():t.use,get:e?Object.assign(()=>Object.entries(t.get()).reduce((i,[s,a])=>r[s]&&e.includes(s)?i:{...i,[s]:a},{}),t.get):t.get}}var f={appName:"zustand-lite",logging:!1};function q(e){f={...f,...e}}function K(e,r){let{name:t="zustand-lite",middlewares:o={}}=r??{},n=()=>e,i=`${f.appName.replace(/\s/,"-")}.${t}}`,s=f.logging||!!o.devtools;s&&(n=C(n,{name:f.appName,store:t,...typeof o.devtools=="object"?o.devtools:{}})),o.persist&&(n=z(n,{name:i,...typeof o.persist=="object"?o.persist:{}}));let a=_(n);return{api:F(a,i),get:B(a),use:g(a,Object.keys(e)),set:d(a,Object.keys(e),s),composePlugin(S){return S(this)},extendGetters(S){return G(S,this,a)},extendSetters(S){return A(S,this,a,s)},extendByState(S){return b(S,this,a,s)},restrictState(S=[]){return L(S,e,this,a)}}}var $=e=>e.extendSetters(({api:r,set:t})=>({reset:()=>{t(r.getInitialState?.()??{},!0)}}));export{K as createStore,O as definePlugin,q as setGlobalConfig,$ as withReset};
//# sourceMappingURL=index.js.map