UNPKG

optix

Version:

Optix is a data manipulation library that provides optics-like capabilites with a simpler syntax. It features robust Typescript support with minimal type annotations and is smaller and faster than true optics libraries.

2 lines (1 loc) 1.95 kB
const e=(t,r)=>{if(!r.length)return t;const n=r.slice(1),s="function"==typeof r[0]?r[0](t):r[0];return Array.isArray(s)?s.map((r=>null!=t?e(t[r],n):void 0)):null!=t?e(t[s],n):void 0},t=(e,r,n)=>{if(!r.length)return n(e);const s=r.slice(1),o="function"==typeof r[0]?r[0](e):r[0],l="string"==typeof o||"number"==typeof o?[o]:Array.isArray(o)?o:[],f=typeof l[0];if("string"!==f&&"number"!==f||l[0]<0)return e;const i="number"===f,c=e&&"object"==typeof e?i?e.slice(0):Object.assign({},e):i?[]:{};for(let e=0;e<l.length;e++)c[l[e]]=t(c[l[e]],s,n);return c},r=(e,r,n)=>t(e,r,(()=>n)),n=(e,t)=>{if(!t.length||!e||"object"!=typeof e)return e;const r="function"==typeof t[0]?t[0](e):t[0],s="string"==typeof r||"number"==typeof r?[r]:Array.isArray(r)?r:[],o=typeof s[0];if("string"!==o&&"number"!==o||s[0]<0)return e;if(1===t.length){let t;if(Array.isArray(e)){const r=new Set(s);t=[];for(let n=0;n<e.length;n++)r.has(n)||t.push(e[n])}else{t=Object.assign({},e);for(let e=0;e<s.length;e++)delete t[s[e]]}return t}{const r=t.slice(1),o=Array.isArray(e)?e.slice(0):Object.assign({},e);for(let e=0;e<s.length;e++){const t=s[e];({}).hasOwnProperty.call(o,t)&&(o[t]=n(o[t],r))}return o}},s=e=>e.length-1,o=e=>e.map(((e,t)=>t)),l=Object.keys,f=e=>t=>{for(let r=0;r<t.length;r++)if(e(t[r]))return r;return-1},i=e=>t=>{for(let r=0,n=Object.keys(t);r<n.length;r++)if(e(n[r]))return n[r]},c=e=>t=>i((r=>e(t[r])))(t),u=e=>t=>{const r=[];for(let n=0;n<t.length;n++)e(t[n])&&r.push(n);return r},y=e=>t=>{const r=[],n=Object.keys(t);for(let t=0;t<n.length;t++)e(n[t])&&r.push(n[t]);return r},g=e=>t=>y((r=>e(t[r])))(t),a=(...t)=>r=>e(r,t),h=(...e)=>t=>n=>r(n,e,t),p=(...e)=>r=>n=>t(n,e,r),b=(...e)=>t=>n(t,e),A=h,j=p,m=b;export{e as _get,n as _remove,r as _set,t as _update,o as all,u as filter,y as filterByKey,g as filterByVal,f as find,i as findByKey,c as findByVal,a as get,l as keys,s as last,m as polyRemove,A as polySet,j as polyUpdate,b as remove,h as set,p as update};