UNPKG

@doeixd/make-with

Version:

Lightweight function application utilities

10 lines (9 loc) 13.7 kB
"use strict";var M=Object.defineProperty;var Q=Object.getOwnPropertyDescriptor;var X=Object.getOwnPropertyNames;var Y=Object.prototype.hasOwnProperty;var ee=(t,r)=>{for(var o in r)M(t,o,{get:r[o],enumerable:!0})},te=(t,r,o,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let e of X(r))!Y.call(t,e)&&e!==o&&M(t,e,{get:()=>r[e],enumerable:!(n=Q(r,e))||n.enumerable});return t};var ne=t=>te(M({},"__esModule",{value:!0}),t);var se={};ee(se,{LayeredError:()=>x,_with:()=>F,collectFns:()=>q,compose:()=>v,createLens:()=>N,createMerger:()=>B,createProxy:()=>$,default:()=>ae,enrich:()=>z,fallback:()=>E,getKeyDescriptions:()=>D,getSet:()=>W,ignoreCase:()=>U,make:()=>k,makeChainable:()=>J,makeLayered:()=>G,makeWith:()=>m,makeWithCompose:()=>V,merge:()=>K,noSpecialChars:()=>H,provide:()=>Z,provideTo:()=>j,rebind:()=>O,withFallback:()=>_});module.exports=ne(se);var h=Symbol("isChainable"),R=new WeakMap,L=new WeakSet,C=Symbol("isComposable"),d=Symbol("internalState");function p(t,r){if(!L.has(t)){for(let[o,n]of Object.entries(t))if(o!==h.toString()&&typeof n!="function")throw new TypeError(`Invalid method "${o}" in ${r}: expected function, got ${typeof n}`);L.add(t)}}function re(t){return typeof t!="function"?!1:t.length===1||t.length===0&&t.toString().includes("...")}var x=class extends Error{};function a(t,r,o){return new x(`[${t}] ${r}`,{cause:o})}function oe(t,r,o){let n=R.get(t);if(!n){let i=new WeakMap;n=R.get(t)||i,n===i&&R.set(t,n)}let e=n.get(r);if(!e){let i=o();e=n.get(r)||i,e===i&&n.set(r,e)}return e}function F(t){if(t==null)throw a("_with","Subject cannot be null or undefined");return function(...r){if(r.length===0)throw a("_with","At least one function must be provided");for(let o=0;o<r.length;o++)if(typeof r[o]!="function")throw a("_with",`Argument at index ${o} must be a function, got ${typeof r[o]}`);return r.map((o,n)=>(...e)=>{try{return o(t,...e)}catch(i){throw a("_with",`Function at index ${n} threw an error`,i)}})}}function k(...t){if(t.length===0)throw a("make","At least one argument must be provided");if(t.length===1&&typeof t[0]=="object"&&!Array.isArray(t[0])&&t[0]!==null){let n=t[0];try{return p(n,"make"),n}catch(e){throw a("make","Object validation failed",e)}}let r={},o=new Set;for(let n=0;n<t.length;n++){let e=t[n];if(typeof e!="function")throw a("make",`Argument at index ${n} must be a function, got ${typeof e}`);if(!e.name||e.name.trim()==="")throw a("make",`Function at index ${n} must have a non-empty name`);if(o.has(e.name))throw a("make",`Duplicate function name "${e.name}" found`);o.add(e.name),r[e.name]=e}return r}function O(...t){try{let r=k(...t);return r[h]=!0,r}catch(r){throw a("rebind","Failed to create chainable functions",r)}}function m(t){if(t==null)throw a("makeWith","Subject cannot be null or undefined");if(typeof t!="object")throw a("makeWith",`Subject must be an object, got ${typeof t}`);return function(r){if(!r||typeof r!="object")throw a("makeWith","Functions map must be a non-null object");return oe(t,r,()=>{try{p(r,"makeWith");let o={},n=r[h],e=Object.keys(r).filter(i=>i!==h.toString());for(let i of e){let s=r[i];n?o[i]=(...c)=>{try{let f=s(t,...c);if(f===void 0)throw a("makeWith",`Chainable method "${i}" returned undefined. Chainable methods must return a new state object.`);if(f===null||typeof f!="object")throw a("makeWith",`Chainable method "${i}" returned ${f===null?"null":typeof f}. Chainable methods must return a new state object.`);return m(f)(r)}catch(f){throw a("makeWith",`Chainable method "${i}" failed`,f)}}:o[i]=(...c)=>{try{return s(t,...c)}catch(f){throw a("makeWith",`Method "${i}" failed`,f)}}}return o[d]=t,o}catch(o){throw a("makeWith","API creation failed",o)}})}}function v(t){if(!t||typeof t!="object")throw a("compose","Methods must be a non-null object");for(let[r,o]of Object.entries(t))if(typeof o!="function")throw a("compose",`Method "${r}" must be a function, got ${typeof o}`);return Object.assign(t,{[C]:!0})}function K(...t){if(t.length===0)throw a("merge","At least one object must be provided");let r=t[0];if(!r||typeof r!="object")throw a("merge","First argument must be a non-null object");try{p(r,"merge first argument")}catch(o){throw a("merge","First object validation failed",o)}if(t.length===1)return function(...o){if(o.length===0)throw a("merge","At least one additional object must be provided to merge");for(let n=0;n<o.length;n++){let e=o[n];if(!e||typeof e!="object")throw a("merge",`Additional argument at index ${n} must be a non-null object`);try{p(e,`merge additional argument ${n}`)}catch(i){throw a("merge",`Additional object at index ${n} validation failed`,i)}}return Object.assign({},r,...o)};for(let o=1;o<t.length;o++){let n=t[o];if(!n||typeof n!="object")throw a("merge",`Argument at index ${o} must be a non-null object`);try{p(n,`merge argument ${o}`)}catch(e){throw a("merge",`Object at index ${o} validation failed`,e)}}return Object.assign({},...t)}function D(t,r){let o=new Set([...Object.getOwnPropertyNames(t),...Object.getOwnPropertySymbols(t),...Object.getOwnPropertyNames(r),...Object.getOwnPropertySymbols(r)]);return Array.from(o).map(n=>{let e=Object.getOwnPropertyDescriptor(t,n)||{enumerable:!1,configurable:!1,writable:!1,key:n},i=Object.getOwnPropertyDescriptor(r,n)||{enumerable:!1,configurable:!1,writable:!1,key:n};return[n,e,i]})}function B(t){if(!t)throw a("createMerger","Merge definition cannot be null or undefined");let r;if(Array.isArray(t)){r={};for(let[e,i]of t)if(typeof e=="object"&&"key"in e){let s=e.key;r[s]=i}else{let s=e;r[s]=i}}else r=t;for(let[e,i]of Object.entries(r))if(typeof i!="function")throw a("createMerger",`Merge function for key "${e}" must be a function, got ${typeof i}`);function o(e,i){if(!e||typeof e!="object")throw a("createMerger","First object must be a non-null object");if(!i||typeof i!="object")throw a("createMerger","Second object must be a non-null object");let s={},c=[],f=new Set([...Object.getOwnPropertyNames(e),...Object.getOwnPropertySymbols(e),...Object.getOwnPropertyNames(i),...Object.getOwnPropertySymbols(i)]);for(let u of f){let l=u,y=r[l];if(y)try{let g=e.hasOwnProperty(u)?{[u]:e[l]}:{},S=i.hasOwnProperty(u)?{[u]:i[l]}:{},b=y(g,S,l);b&&typeof b=="object"&&"error"in b?c.push([u,b.error]):s[l]=b}catch(g){c.push([u,g])}else i.hasOwnProperty(u)?s[l]=i[l]:e.hasOwnProperty(u)&&(s[l]=e[l])}return c.length>0?{success:!1,failures:c}:{success:!0,data:s}}function n(e,i){return i===void 0?s=>o(e,s):o(e,i)}return n}function W(t,r,...o){let n=String(r);if(n.startsWith("get")&&n.length>3){let e=n.charAt(3).toLowerCase()+n.slice(4);return t[e]}if(n.startsWith("set")&&n.length>3&&o.length>0){let e=n.charAt(3).toLowerCase()+n.slice(4);if(e in t)return{...t,[e]:o[0]}}}function U(t){return(r,o,...n)=>{let e=typeof o=="string"?o.toLowerCase():o;return t(r,e,...n)}}function H(t){return(r,o,...n)=>{let e=typeof o=="string"?o.replace(/[^a-zA-Z0-9]/g,""):o;return t(r,e,...n)}}function E(t){return(r,o,...n)=>{for(let e of t){let i=e(r,o,...n);if(i!==void 0)return i}}}function $(t){if(typeof t!="function")throw a("createProxy","Handler must be a function");return function(r){if(!r||typeof r!="object")throw a("createProxy","Initial state must be a non-null object");let o={[d]:r};return new Proxy(o,{get(n,e){return e===d?n[d]:typeof e=="symbol"||e==="constructor"||e==="toString"||e==="valueOf"?n[e]:function(...i){try{let s=n[d],c=t(s,e,...i);if(c&&typeof c=="object"&&"error"in c)throw a("createProxy",`Method "${String(e)}" failed: ${c.error}`);if(c===void 0)throw a("createProxy",`Method "${String(e)}" is not supported`);if(c&&typeof c=="object"&&typeof s=="object"){let f=Object.keys(s),u=Object.keys(c);if(f.some(y=>y in c)||u.length>0)return $(t)(c)}return c}catch(s){throw s instanceof x?s:a("createProxy",`Method "${String(e)}" execution failed`,s)}}},has(n,e){return e===d?!0:typeof e=="string"},ownKeys(n){let e=n[d];return[...Object.keys(e).flatMap(c=>[`get${c.charAt(0).toUpperCase()+c.slice(1)}`,`set${c.charAt(0).toUpperCase()+c.slice(1)}`]),d]},getOwnPropertyDescriptor(n,e){if(e===d)return{configurable:!0,enumerable:!1,writable:!0};if(typeof e=="string")return{configurable:!0,enumerable:!0,writable:!1}}})}}function N(t,r){if(typeof t!="function")throw a("createLens","Getter must be a function");if(typeof r!="function")throw a("createLens","Setter must be a function");return function(o){if(!o||typeof o!="object")throw a("createLens","Methods must be a non-null object");try{p(o,"createLens")}catch(i){throw a("createLens","Methods validation failed",i)}let n={},e=o[h]===!0;e&&(n[h]=!0);for(let[i,s]of Object.entries(o))if(i!==h.toString()){if(typeof s!="function")throw a("createLens",`Method "${i}" must be a function`);n[i]=(c,...f)=>{try{let u=t(c),l=s(u,...f);return e&&l!==void 0&&l!==null?r(c,l):l}catch(u){throw a("createLens",`Focused method "${i}" failed`,u)}}}return n}}var ie=t=>t!=null;function _(t,r=ie){if(!t||typeof t!="object")throw a("withFallback","Primary object must be a non-null object");if(typeof r!="function")throw a("withFallback","Validator must be a function");let o=[];function n(){function e(i){if(i===void 0)return I(t,o,r);if(!i||typeof i!="object")throw a("withFallback","Fallback object must be a non-null object");return o.push(i),n()}return e}return n()}function I(t,r,o){return new Proxy(t,{get(n,e){if(typeof e=="symbol"||e==="constructor"||e==="toString"||e==="valueOf")return n[e];let i=[n,...r];for(let s of i)if(s&&typeof s=="object"&&e in s){let c=s[e];try{if(o(c)){if(c&&typeof c=="object"&&!Array.isArray(c)){let f=[];for(let u of r)if(u&&typeof u=="object"&&e in u){let l=u[e];l&&typeof l=="object"&&!Array.isArray(l)&&f.push(l)}if(f.length>0)return I(c,f,o)}return c}}catch(f){continue}}},set(n,e,i){try{return n[e]=i,!0}catch(s){return!1}},has(n,e){let i=[n,...r];for(let s of i)if(s&&typeof s=="object"&&e in s)return!0;return!1},ownKeys(n){let e=new Set,i=[n,...r];for(let s of i)s&&typeof s=="object"&&(Object.getOwnPropertyNames(s).forEach(c=>e.add(c)),Object.getOwnPropertySymbols(s).forEach(c=>e.add(c)));return Array.from(e)},getOwnPropertyDescriptor(n,e){let i=Object.getOwnPropertyDescriptor(n,e);if(i)return i;for(let s of r)if(s&&typeof s=="object"){let c=Object.getOwnPropertyDescriptor(s,e);if(c)return{configurable:!0,enumerable:c.enumerable,writable:!0,value:void 0}}},deleteProperty(n,e){try{return delete n[e],!0}catch(i){return!1}},defineProperty(n,e,i){try{return Object.defineProperty(n,e,i),!0}catch(s){return!1}}})}function V(t){if(t==null)throw a("makeWithCompose","Subject cannot be null or undefined");if(typeof t!="object")throw a("makeWithCompose",`Subject must be an object, got ${typeof t}`);return function(...r){if(r.length===0)throw a("makeWithCompose","At least one method object must be provided");let o={};for(let n of r){if(!n||typeof n!="object")throw a("makeWithCompose","All arguments must be non-null objects");for(let[e,i]of Object.entries(n)){if(e===h.toString()){o[e]=i;continue}if(typeof i!="function")throw a("makeWithCompose",`Method "${e}" must be a function`);let s=o[e];s?o[e]=(c,...f)=>i(c,...f,s):o[e]=i}}return m(t)(o)}}function z(t,r){if(typeof t!="function")throw a("enrich","Primary factory must be a function");if(typeof r!="function")throw a("enrich","Secondary factory must be a function");return function(...o){try{let n=t(...o);if(!n||typeof n!="object")throw a("enrich",`Primary factory must return an object, got ${typeof n}`);let e=r(n);if(!e||typeof e!="object")throw a("enrich",`Secondary factory must return an object, got ${typeof e}`);return{...n,...e}}catch(n){throw a("enrich","Factory composition failed",n)}}}function G(t){if(t==null)throw a("makeLayered","Subject cannot be null or undefined");if(typeof t!="object")throw a("makeLayered",`Subject must be an object, got ${typeof t}`);return function(r){if(!r||typeof r!="object")throw a("makeLayered","Base functions must be a non-null object");try{let o=m(t)(r),n=0,e=i=>s=>{if(s===void 0)return i;n++;try{let c;if(typeof s=="function"){if(!re(s))throw a("makeLayered",`Layer function must accept exactly one parameter (the current API), got function with ${s.length} parameters`);if(c=s(i),!c||typeof c!="object")throw a("makeLayered",`Layer function must return an object of methods, got ${typeof c}`)}else{if(!s||typeof s!="object")throw a("makeLayered","Layer must be either a function or an object of methods");c=s}p(c,`makeLayered layer ${n}`);let f=c[C]===!0,u;if(f){u={};for(let[y,g]of Object.entries(c)){if(y===C.toString())continue;let S=g,b=i[y];b&&typeof b=="function"?u[y]=(...P)=>{let A=(...w)=>{let T=b(i,...w);return T&&typeof T=="object"&&Object.prototype.hasOwnProperty.call(T,d)?T[d]:T};try{return S(i,...P,A)}catch(w){throw a("compose",`Composed method "${y}" failed during execution`,w)}}:u[y]=(...P)=>{let A=()=>{throw a("compose",`No previous method "${y}" found to compose with`)};try{return S(i,...P,A)}catch(w){throw a("compose",`Composed method "${y}" failed during execution`,w)}}}u=j(i)(u)}else u=j(i)(c);let l=Object.assign(Object.create(Object.getPrototypeOf(i)),i,u);return e(l)}catch(c){throw a("makeLayered",`Layer ${n} creation failed`,c)}};return e(o)}catch(o){throw a("makeLayered","Layered API initialization failed",o)}}}var Z=F,q=k,j=m,J=O,ae={with:F,provide:Z,make:k,collectFns:q,makeWith:m,makeWithCompose:V,provideTo:j,rebind:O,makeChainable:J,enrich:z,makeLayered:G,compose:v,merge:K,createMerger:B,getKeyDescriptions:D,createProxy:$,getSet:W,ignoreCase:U,noSpecialChars:H,fallback:E,createLens:N,withFallback:_}; /** * @file A functional utility library for creating powerful, immutable, and chainable APIs. * It provides tools for partial application and function composition, enabling elegant state * management patterns. * * @version 0.0.5 * @license MIT */