UNPKG

@politie/sherlock-utils

Version:

Utility functions that are designed to work with Sherlock. His toolbelt.

2 lines 4.34 kB
!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@politie/sherlock")):"function"==typeof define&&define.amd?define(["exports","@politie/sherlock"],r):r((n="undefined"!=typeof globalThis?globalThis:n||self).SherlockUtils={},n.Sherlock)}(this,(function(n,r){"use strict";function t(){return new Map}var e="__cachedProxy",u=function(n,r){return u=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(n,r){n.__proto__=r}||function(n,r){for(var t in r)Object.prototype.hasOwnProperty.call(r,t)&&(n[t]=r[t])},u(n,r)};"function"==typeof SuppressedError&&SuppressedError;var o=function(n){function r(r){var t=n.call(this)||this;return t.fn=r,t}return function(n,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+r+" is not a constructor or null");function t(){this.constructor=n}u(n,r),n.prototype=null===r?Object.create(r):(t.prototype=r.prototype,new t)}(r,n),r.prototype.calculateCurrentValue=function(){return this.fn()},r.prototype.changed=function(){n.prototype.checkForChanges.call(this)},r}(r.PullDataSource);function i(n){return f(n.getState())}function f(n){return n===r.unresolved?{errored:!1,resolved:!1}:n instanceof r.ErrorWrapper?{error:n.error,errored:!0,resolved:!0}:{value:n,errored:!1,resolved:!0}}function c(n){return n.errored?new r.ErrorWrapper(n.error):n.resolved?n.value:r.unresolved}function a(n){return n.mapState(f)}function s(n,r){r.resolved?r.errored?n.setError(r.error):n.set(r.value):n.unset()}var l=p((function(n){return!n})),v=p((function(n){return!!n})),d=p((function(n){return null!=n}));function p(n){return function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return r.derive((function(){for(var e,u=0,o=t;u<o.length;u++){var i=o[u];if(e=r.unwrap(i),n(e))break}return e}))}}function h(n){if(r.isDerivable(n))return n.get();if(Array.isArray(n))return n.map(h);if(r.utils.isPlainObject(n)){for(var t={},e=0,u=Object.keys(n);e<u.length;e++){var o=u[e];t[o]=h(n[o])}return t}return n}n.FunctionDataSource=o,n.and=l,n.copyState=function(n,r){s(r,i(n))},n.dematerialize=function(n){return n.map(c)},n.derivableCache=function(n){var u=(n.mapFactory||t)(),o=n.delayedEviction,i=n.derivableFactory,f={get:function(n){var t=u.get(n);if(t)return t.getState();var f=r._internal.independentTracking((function(){return i(n)})),c=r.isSettableDerivable(f)?r.lens({get:function(){return f.get()},set:function(n){return f.set(n)}},r.atom(0)):r.atom(0).derive((function(){return f.get()}));o&&c.autoCache();var a=c.getState();return c.connected&&(c[e]=this,u.set(n,c),c.connected$.react((function(){return u.delete(n)}),{skipFirst:!0,once:!0})),a},set:function(n,t){var e=u.get(t)||i(t);if(!r.isSettableDerivable(e))throw r._internal.augmentStack(Error("Cached derivable is not settable"),e);e.set(n)}};return function(n){if(!r.isDerivable(n)){var t=u.get(n);if(t)return t[e]}return r.lens(f,n)}},n.firstNotNull=d,n.fromPromise=function(n){var t=r.atom.unresolved();return n.then((function(n){return t.setFinal(n)}),(function(n){return t.setFinal(new r.ErrorWrapper(n))})),t},n.fromStateObject=c,n.getStateObject=i,n.lift=function(n){return function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return new r._internal.Derivation(n,t)}},n.materialize=a,n.or=v,n.pairwise=function(n,r){var t=r;return function(r){var e=n.call(this,r,t);return t=r,e}},n.peek=function(n){return r._internal.independentTracking((function(){return n.get()}))},n.peekState=function(n){return r._internal.independentTracking((function(){return n.getState()}))},n.peekValue=function(n){return r._internal.independentTracking((function(){return n.value}))},n.scan=function(n,r){var t=r;return function(r){return t=n.call(this,t,r)}},n.setStateObject=s,n.struct=function(n){if(r.isDerivable(n))return n;if(!Array.isArray(n)&&!r.utils.isPlainObject(n))throw Error('"struct" only accepts Derivables, plain Objects and Arrays');return r.derive(h,n)},n.syncState=function(n,r,t){return a(n).react((function(n){return s(r,n)}),t)},n.template=function(n){for(var t=[],e=1;e<arguments.length;e++)t[e-1]=arguments[e];return r.derive((function(){for(var e="",u=0;u<n.length;u++)e+=n[u],u<t.length&&(e+=r.unwrap(t[u]));return e}))},n.toStateObject=f,Object.defineProperty(n,"__esModule",{value:!0})})); //# sourceMappingURL=sherlock-utils.umd.min.js.map