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