diff --git a/cdn/radash.esm.js b/cdn/radash.esm.js index 53cde16c..d81bc385 100644 --- a/cdn/radash.esm.js +++ b/cdn/radash.esm.js @@ -644,13 +644,6 @@ const omit = (obj, keys) => { ); }; const get = (value, funcOrPath, defaultValue = null) => { - if (isFunction(funcOrPath)) { - try { - return funcOrPath(value) ?? defaultValue; - } catch { - return defaultValue; - } - } const segments = funcOrPath.split(/[\.\[\]]/g); let current = value; for (const key of segments) { diff --git a/cdn/radash.js b/cdn/radash.js index 6888723a..415e3d36 100644 --- a/cdn/radash.js +++ b/cdn/radash.js @@ -647,13 +647,6 @@ var radash = (function (exports) { ); }; const get = (value, funcOrPath, defaultValue = null) => { - if (isFunction(funcOrPath)) { - try { - return funcOrPath(value) ?? defaultValue; - } catch { - return defaultValue; - } - } const segments = funcOrPath.split(/[\.\[\]]/g); let current = value; for (const key of segments) { diff --git a/cdn/radash.min.js b/cdn/radash.min.js index c1ce6bc8..d053c364 100644 --- a/cdn/radash.min.js +++ b/cdn/radash.min.js @@ -1 +1 @@ -var radash=function(i){"use strict";const O=t=>!!t&&t.constructor===Symbol,C=t=>!!t&&t.constructor===Array,E=t=>!!t&&t.constructor===Object,N=t=>t==null||typeof t!="object"&&typeof t!="function",g=t=>!!(t&&t.constructor&&t.call&&t.apply),P=t=>typeof t=="string"||t instanceof String,D=t=>a(t)&&t%1===0,F=t=>a(t)&&t%1!==0,a=t=>{try{return Number(t)===t}catch{return!1}},_=t=>Object.prototype.toString.call(t)==="[object Date]",R=t=>{if(t===!0||t===!1||t==null)return!0;if(a(t))return t===0;if(_(t))return isNaN(t.getTime());if(g(t)||O(t))return!1;const e=t.length;if(a(e))return e===0;const n=t.size;return a(n)?n===0:Object.keys(t).length===0},$=(t,e)=>{if(Object.is(t,e))return!0;if(t instanceof Date&&e instanceof Date)return t.getTime()===e.getTime();if(t instanceof RegExp&&e instanceof RegExp)return t.toString()===e.toString();if(typeof t!="object"||t===null||typeof e!="object"||e===null)return!1;const n=Reflect.ownKeys(t),r=Reflect.ownKeys(e);if(n.length!==r.length)return!1;for(let c=0;ct.reduce((n,r)=>{const c=e(r);return n[c]||(n[c]=[]),n[c].push(r),n},{});function q(...t){return!t||!t.length?[]:new Array(Math.max(...t.map(({length:e})=>e))).fill([]).map((e,n)=>t.map(r=>r[n]))}function I(t,e){if(!t||!t.length)return{};const n=g(e)?e:C(e)?(r,c)=>e[c]:(r,c)=>e;return t.reduce((r,c,s)=>({...r,[c]:n(c,s)}),{})}const w=(t,e)=>!t||(t.length??0)===0?null:t.reduce(e),M=(t,e)=>(t||[]).reduce((n,r)=>n+(e?e(r):r),0),U=(t,e=void 0)=>t?.length>0?t[0]:e,K=(t,e=void 0)=>t?.length>0?t[t.length-1]:e,z=(t,e,n=!1)=>{if(!t)return[];const r=(s,u)=>e(s)-e(u),c=(s,u)=>e(u)-e(s);return t.slice().sort(n===!0?c:r)},V=(t,e,n="asc")=>{if(!t)return[];const r=(s,u)=>`${e(s)}`.localeCompare(e(u)),c=(s,u)=>`${e(u)}`.localeCompare(e(s));return t.slice().sort(n==="desc"?c:r)},J=(t,e)=>t.reduce((n,r)=>{const c=e(r);return{...n,[c]:(n[c]??0)+1}},{}),W=(t,e,n)=>{if(!t)return[];if(!e)return[...t];for(let r=0;rr)=>t.reduce((r,c)=>({...r,[e(c)]:n(c)}),{}),Y=(t,e,n)=>t.reduce((r,c)=>n(c)?[...r,e(c)]:r,[]),G=(t,e)=>{const n=e||(r=>r);return w(t,(r,c)=>n(r)>n(c)?r:c)},H=(t,e)=>{const n=e||(r=>r);return w(t,(r,c)=>n(r){const n=Math.ceil(t.length/e);return new Array(n).fill(null).map((r,c)=>t.slice(c*e,c*e+e))},v=(t,e)=>{const n=t.reduce((r,c)=>{const s=e?e(c):c;return r[s]?r:{...r,[s]:c}},{});return Object.values(n)};function*y(t,e,n=c=>c,r=1){const c=g(n)?n:()=>n,s=e?t:0,u=e??t;for(let o=s;o<=u&&(yield c(o),!(o+r>u));o+=r);}const S=(t,e,n,r)=>Array.from(y(t,e,n,r)),x=t=>t.reduce((e,n)=>[...e,...n],[]),tt=(t,e,n)=>{if(!t||!e)return!1;const r=n??(s=>s),c=e.reduce((s,u)=>({...s,[r(u)]:!0}),{});return t.some(s=>c[r(s)])},T=(t,e)=>t?t.reduce((n,r)=>{const[c,s]=n;return e(r)?[[...c,r],s]:[c,[...s,r]]},[[],[]]):[[],[]],et=(t,e,n)=>!e&&!t?[]:e?t?n?t.reduce((r,c)=>{const s=e.find(u=>n(c)===n(u));return s?[...r,s]:[...r,c]},[]):t:[]:t,nt=(t,e,n)=>{if(!t&&!e)return[];if(!e)return[...t];if(!t)return[e];for(let r=0;r{if(!t&&!e)return[];if(!t)return[e];if(!e)return[...t];const c=n?(o,l)=>n(o,l)===n(e,l):o=>o===e;return t.find(c)?t.filter((o,l)=>!c(o,l)):(r?.strategy??"append")==="append"?[...t,e]:[e,...t]},ct=t=>t?.filter(e=>!!e)??[],j=(t,e,n)=>{let r=n;for(let c=1;c<=t;c++)r=e(r,c);return r},it=(t,e,n=r=>r)=>{if(!t?.length&&!e?.length)return[];if(t?.length===void 0)return[...e];if(!e?.length)return[...t];const r=e.reduce((c,s)=>({...c,[n(s)]:!0}),{});return t.filter(c=>!r[n(c)])};function st(t,e){if(t.length===0)return t;const n=e%t.length;return n===0?t:[...t.slice(-n,t.length),...t.slice(0,-n)]}const ut=async(t,e,n)=>{const r=n!==void 0;if(!r&&t?.length<1)throw new Error("Cannot reduce empty array with no init value");const c=r?t:t.slice(1);let s=r?n:t[0];for(const u of c)s=await e(s,u);return s},ot=async(t,e)=>{if(!t)return[];let n=[],r=0;for(const c of t){const s=await e(c,r++);n.push(s)}return n},lt=async t=>{const e=[],n=(s,u)=>e.push({fn:s,rethrow:u?.rethrow??!1}),[r,c]=await d(t)(n);for(const{fn:s,rethrow:u}of e){const[o]=await d(s)(r);if(u)throw o}if(r)throw r;return c};class ft extends Error{constructor(e=[]){super();const n=e.find(r=>r.name)?.name??"";this.name=`AggregateError(${n}...)`,this.message=`AggregateError with ${e.length} errors`,this.stack=e.find(r=>r.stack)?.stack??this.stack,this.errors=e}}const at=async(t,e,n)=>{const r=e.map((f,h)=>({index:h,item:f})),c=async f=>{const h=[];for(;;){const A=r.pop();if(!A)return f(h);const[Wt,Xt]=await d(n)(A.item);h.push({error:Wt,result:Xt,index:A.index})}},s=S(1,t).map(()=>new Promise(c)),u=await Promise.all(s),[o,l]=T(z(u.flat(),f=>f.index),f=>!!f.error);if(o.length>0)throw new ft(o.map(f=>f.error));return l.map(f=>f.result)},dt=async(t,e)=>{const n=t?.times??3,r=t?.delay,c=t?.backoff??null;for(const s of y(1,n)){const[u,o]=await d(e)(l=>{throw{_exited:l}});if(!u)return o;if(u._exited)throw u._exited;if(s===n)throw u;r&&await p(r),c&&await p(c(s))}},p=t=>new Promise(e=>setTimeout(e,t)),d=t=>async(...e)=>{try{return[null,await t(...e)]}catch(n){return[n,null]}},gt=(...t)=>(...e)=>t.slice(1).reduce((n,r)=>r(n),t[0](...e)),mt=(...t)=>t.reverse().reduce((e,n)=>n(e)),ht=(t,...e)=>(...n)=>t(...e,...n),wt=(t,e)=>n=>t({...e,...n}),yt=t=>new Proxy({},{get:(e,n)=>t(n)}),pt=(t,e,n,r)=>function(...s){const u=n?n(...s):JSON.stringify({args:s}),o=t[u];if(o!==void 0&&o.exp>new Date().getTime())return o.value;const l=e(...s);return t[u]={exp:new Date().getTime()+r,value:l},l},bt=(t,{key:e=null,ttl:n=300}={})=>pt({},t,e,n),kt=({delay:t},e)=>{let n=null;return(...c)=>{clearTimeout(n),n=setTimeout(()=>e(...c),t)}},At=({interval:t},e)=>{let n=!0;return(...c)=>{!n||(e(...c),n=!1,setTimeout(()=>{n=!0},t))}},Ot=(t,e)=>{const n=()=>{};return new Proxy(Object.assign(n,t),{get:(r,c)=>r[c],set:(r,c,s)=>(r[c]=s,!0),apply:(r,c,s)=>e(Object.assign({},r))(...s)})},Ct=(t,e)=>{const n=e===void 0?0:e;if(t==null)return n;const r=parseFloat(t);return isNaN(r)?n:r},Et=(t,e)=>{const n=e===void 0?0:e;if(t==null)return n;const r=parseInt(t);return isNaN(r)?n:r},Nt=(t,e=n=>n===void 0)=>t?Object.keys(t).reduce((r,c)=>e(t[c])?r:{...r,[c]:t[c]},{}):{},b=(t,e)=>Object.keys(t).reduce((r,c)=>({...r,[e(c,t[c])]:t[c]}),{}),_t=(t,e)=>Object.keys(t).reduce((r,c)=>({...r,[c]:e(t[c],c)}),{}),$t=(t,e)=>t?Object.entries(t).reduce((n,[r,c])=>{const[s,u]=e(r,c);return{...n,[s]:u}},{}):{},zt=t=>t?Object.keys(t).reduce((n,r)=>({...n,[t[r]]:r}),{}):{},St=t=>b(t,e=>e.toLowerCase()),Tt=t=>b(t,e=>e.toUpperCase()),jt=t=>{if(N(t))return t;if(typeof t=="function")return t.bind({});const e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(n=>{e[n]=t[n]}),e},Bt=(t,e)=>{if(!t)return[];const n=Object.entries(t);return n.length===0?[]:n.reduce((r,c)=>[...r,e(c[0],c[1])],[])},Lt=(t,e)=>t?e.reduce((n,r)=>(t.hasOwnProperty(r)&&(n[r]=t[r]),n),{}):{},Pt=(t,e)=>t?!e||e.length===0?t:e.reduce((n,r)=>(delete n[r],n),{...t}):{},Dt=(t,e,n=null)=>{if(g(e))try{return e(t)??n}catch{return n}const r=e.split(/[\.\[\]]/g);let c=t;for(const s of r){if(c===null||c===void 0)return n;s.trim()!==""&&(c=c[s])}return c===void 0?n:c},B=(t,e)=>!t&&!e?{}:t?e?Object.entries(t).reduce((n,[r,c])=>({...n,[r]:(()=>E(c)?B(c,e[r]):e[r])()}),{}):t:e,k=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),Ft=t=>{const e=t.length;if(e===0)return null;const n=k(0,e-1);return t[n]},Rt=t=>t.map(e=>({rand:Math.random(),value:e})).sort((e,n)=>e.rand-n.rand).map(e=>e.value),Zt=(t,e="")=>{const n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"+e;return j(t,r=>r+n.charAt(k(0,n.length-1)),"")},qt=(...t)=>{const{itemsByValue:e,itemsByIndex:n}=t.reduce((r,c,s)=>({itemsByValue:{...r.itemsByValue,[c]:s},itemsByIndex:{...r.itemsByIndex,[s]:c}}),{itemsByValue:{},itemsByIndex:{}});return{min:(r,c)=>e[r]e[r]>e[c]?r:c,first:()=>n[0],last:()=>n[t.length-1],next:(r,c)=>n[e[r]+1]??c,previous:(r,c)=>n[e[r]-1]??c}},m=t=>{if(!t||t.length===0)return"";const e=t.toLowerCase();return e.substring(0,1).toUpperCase()+e.substring(1,e.length)},L=t=>{const e=t?.replace(/([A-Z])+/g,m)?.split(/(?=[A-Z])|[\.\-\s_]/).map(n=>n.toLowerCase())??[];return e.length===0?"":e.length===1?e[0]:e.reduce((n,r)=>`${n}${r.charAt(0).toUpperCase()}${r.slice(1)}`)},It=t=>{const e=t?.replace(/([A-Z])+/g,m).split(/(?=[A-Z])|[\.\-\s_]/).map(n=>n.toLowerCase())??[];return e.length===0?"":e.length===1?e[0]:e.reduce((n,r)=>`${n}_${r.toLowerCase()}`)},Mt=t=>{const e=t?.replace(/([A-Z])+/g,m)?.split(/(?=[A-Z])|[\.\-\s_]/).map(n=>n.toLowerCase())??[];return e.length===0?"":e.length===1?e[0]:e.reduce((n,r)=>`${n}-${r.toLowerCase()}`)},Ut=t=>{const e=t?.split(/[\.\-\s_]/).map(n=>n.toLowerCase())??[];return e.length===0?"":e.map(n=>n.charAt(0).toUpperCase()+n.slice(1)).join("")},Kt=t=>t?t.split(/(?=[A-Z])|[\.\-\s_]/).map(e=>e.trim()).filter(e=>!!e).map(e=>m(e.toLowerCase())).join(" "):"",Vt=(t,e,n=/\{\{(.+?)\}\}/g)=>Array.from(t.matchAll(n)).reduce((r,c)=>r.replace(c[0],e[c[1]]),t),Jt=(t,e=" ")=>{if(!t)return"";const n=new RegExp(`^[${e}]+|[${e}]+$`,"g");return t.replace(n,"")};return i.alphabetical=V,i.assign=B,i.boil=w,i.callable=Ot,i.camal=L,i.camel=L,i.capitalize=m,i.chain=gt,i.clone=jt,i.cluster=Q,i.compose=mt,i.counting=J,i.dash=Mt,i.debounce=kt,i.defer=lt,i.diff=it,i.draw=Ft,i.first=U,i.flat=x,i.fork=T,i.get=Dt,i.group=Z,i.intersects=tt,i.invert=zt,i.isArray=C,i.isDate=_,i.isEmpty=R,i.isEqual=$,i.isFloat=F,i.isFunction=g,i.isInt=D,i.isNumber=a,i.isObject=E,i.isPrimitive=N,i.isString=P,i.isSymbol=O,i.iterate=j,i.last=K,i.list=S,i.listify=Bt,i.lowerize=St,i.map=ot,i.mapEntries=$t,i.mapKeys=b,i.mapValues=_t,i.max=G,i.memo=bt,i.merge=et,i.min=H,i.objectify=X,i.omit=Pt,i.parallel=at,i.partial=ht,i.partob=wt,i.pascal=Ut,i.pick=Lt,i.proxied=yt,i.random=k,i.range=y,i.reduce=ut,i.replace=W,i.replaceOrAppend=nt,i.retry=dt,i.select=Y,i.series=qt,i.shake=Nt,i.shift=st,i.shuffle=Rt,i.sift=ct,i.sleep=p,i.snake=It,i.sort=z,i.sum=M,i.template=Vt,i.throttle=At,i.title=Kt,i.toFloat=Ct,i.toInt=Et,i.toggle=rt,i.trim=Jt,i.try=d,i.tryit=d,i.uid=Zt,i.unique=v,i.upperize=Tt,i.zip=q,i.zipToObject=I,i}({}); +var radash=function(i){"use strict";const O=t=>!!t&&t.constructor===Symbol,C=t=>!!t&&t.constructor===Array,E=t=>!!t&&t.constructor===Object,N=t=>t==null||typeof t!="object"&&typeof t!="function",m=t=>!!(t&&t.constructor&&t.call&&t.apply),L=t=>typeof t=="string"||t instanceof String,D=t=>a(t)&&t%1===0,F=t=>a(t)&&t%1!==0,a=t=>{try{return Number(t)===t}catch{return!1}},_=t=>Object.prototype.toString.call(t)==="[object Date]",R=t=>{if(t===!0||t===!1||t==null)return!0;if(a(t))return t===0;if(_(t))return isNaN(t.getTime());if(m(t)||O(t))return!1;const e=t.length;if(a(e))return e===0;const n=t.size;return a(n)?n===0:Object.keys(t).length===0},$=(t,e)=>{if(Object.is(t,e))return!0;if(t instanceof Date&&e instanceof Date)return t.getTime()===e.getTime();if(t instanceof RegExp&&e instanceof RegExp)return t.toString()===e.toString();if(typeof t!="object"||t===null||typeof e!="object"||e===null)return!1;const n=Reflect.ownKeys(t),r=Reflect.ownKeys(e);if(n.length!==r.length)return!1;for(let c=0;ct.reduce((n,r)=>{const c=e(r);return n[c]||(n[c]=[]),n[c].push(r),n},{});function q(...t){return!t||!t.length?[]:new Array(Math.max(...t.map(({length:e})=>e))).fill([]).map((e,n)=>t.map(r=>r[n]))}function I(t,e){if(!t||!t.length)return{};const n=m(e)?e:C(e)?(r,c)=>e[c]:(r,c)=>e;return t.reduce((r,c,s)=>({...r,[c]:n(c,s)}),{})}const w=(t,e)=>!t||(t.length??0)===0?null:t.reduce(e),M=(t,e)=>(t||[]).reduce((n,r)=>n+(e?e(r):r),0),U=(t,e=void 0)=>t?.length>0?t[0]:e,V=(t,e=void 0)=>t?.length>0?t[t.length-1]:e,z=(t,e,n=!1)=>{if(!t)return[];const r=(s,u)=>e(s)-e(u),c=(s,u)=>e(u)-e(s);return t.slice().sort(n===!0?c:r)},K=(t,e,n="asc")=>{if(!t)return[];const r=(s,u)=>`${e(s)}`.localeCompare(e(u)),c=(s,u)=>`${e(u)}`.localeCompare(e(s));return t.slice().sort(n==="desc"?c:r)},J=(t,e)=>t.reduce((n,r)=>{const c=e(r);return{...n,[c]:(n[c]??0)+1}},{}),W=(t,e,n)=>{if(!t)return[];if(!e)return[...t];for(let r=0;rr)=>t.reduce((r,c)=>({...r,[e(c)]:n(c)}),{}),Y=(t,e,n)=>t.reduce((r,c)=>n(c)?[...r,e(c)]:r,[]),G=(t,e)=>{const n=e||(r=>r);return w(t,(r,c)=>n(r)>n(c)?r:c)},H=(t,e)=>{const n=e||(r=>r);return w(t,(r,c)=>n(r){const n=Math.ceil(t.length/e);return new Array(n).fill(null).map((r,c)=>t.slice(c*e,c*e+e))},v=(t,e)=>{const n=t.reduce((r,c)=>{const s=e?e(c):c;return r[s]?r:{...r,[s]:c}},{});return Object.values(n)};function*y(t,e,n=c=>c,r=1){const c=m(n)?n:()=>n,s=e?t:0,u=e??t;for(let o=s;o<=u&&(yield c(o),!(o+r>u));o+=r);}const P=(t,e,n,r)=>Array.from(y(t,e,n,r)),x=t=>t.reduce((e,n)=>[...e,...n],[]),tt=(t,e,n)=>{if(!t||!e)return!1;const r=n??(s=>s),c=e.reduce((s,u)=>({...s,[r(u)]:!0}),{});return t.some(s=>c[r(s)])},S=(t,e)=>t?t.reduce((n,r)=>{const[c,s]=n;return e(r)?[[...c,r],s]:[c,[...s,r]]},[[],[]]):[[],[]],et=(t,e,n)=>!e&&!t?[]:e?t?n?t.reduce((r,c)=>{const s=e.find(u=>n(c)===n(u));return s?[...r,s]:[...r,c]},[]):t:[]:t,nt=(t,e,n)=>{if(!t&&!e)return[];if(!e)return[...t];if(!t)return[e];for(let r=0;r{if(!t&&!e)return[];if(!t)return[e];if(!e)return[...t];const c=n?(o,l)=>n(o,l)===n(e,l):o=>o===e;return t.find(c)?t.filter((o,l)=>!c(o,l)):(r?.strategy??"append")==="append"?[...t,e]:[e,...t]},ct=t=>t?.filter(e=>!!e)??[],T=(t,e,n)=>{let r=n;for(let c=1;c<=t;c++)r=e(r,c);return r},it=(t,e,n=r=>r)=>{if(!t?.length&&!e?.length)return[];if(t?.length===void 0)return[...e];if(!e?.length)return[...t];const r=e.reduce((c,s)=>({...c,[n(s)]:!0}),{});return t.filter(c=>!r[n(c)])};function st(t,e){if(t.length===0)return t;const n=e%t.length;return n===0?t:[...t.slice(-n,t.length),...t.slice(0,-n)]}const ut=async(t,e,n)=>{const r=n!==void 0;if(!r&&t?.length<1)throw new Error("Cannot reduce empty array with no init value");const c=r?t:t.slice(1);let s=r?n:t[0];for(const u of c)s=await e(s,u);return s},ot=async(t,e)=>{if(!t)return[];let n=[],r=0;for(const c of t){const s=await e(c,r++);n.push(s)}return n},lt=async t=>{const e=[],n=(s,u)=>e.push({fn:s,rethrow:u?.rethrow??!1}),[r,c]=await d(t)(n);for(const{fn:s,rethrow:u}of e){const[o]=await d(s)(r);if(u)throw o}if(r)throw r;return c};class ft extends Error{constructor(e=[]){super();const n=e.find(r=>r.name)?.name??"";this.name=`AggregateError(${n}...)`,this.message=`AggregateError with ${e.length} errors`,this.stack=e.find(r=>r.stack)?.stack??this.stack,this.errors=e}}const at=async(t,e,n)=>{const r=e.map((f,h)=>({index:h,item:f})),c=async f=>{const h=[];for(;;){const A=r.pop();if(!A)return f(h);const[Wt,Xt]=await d(n)(A.item);h.push({error:Wt,result:Xt,index:A.index})}},s=P(1,t).map(()=>new Promise(c)),u=await Promise.all(s),[o,l]=S(z(u.flat(),f=>f.index),f=>!!f.error);if(o.length>0)throw new ft(o.map(f=>f.error));return l.map(f=>f.result)},dt=async(t,e)=>{const n=t?.times??3,r=t?.delay,c=t?.backoff??null;for(const s of y(1,n)){const[u,o]=await d(e)(l=>{throw{_exited:l}});if(!u)return o;if(u._exited)throw u._exited;if(s===n)throw u;r&&await p(r),c&&await p(c(s))}},p=t=>new Promise(e=>setTimeout(e,t)),d=t=>async(...e)=>{try{return[null,await t(...e)]}catch(n){return[n,null]}},gt=(...t)=>(...e)=>t.slice(1).reduce((n,r)=>r(n),t[0](...e)),mt=(...t)=>t.reverse().reduce((e,n)=>n(e)),ht=(t,...e)=>(...n)=>t(...e,...n),wt=(t,e)=>n=>t({...e,...n}),yt=t=>new Proxy({},{get:(e,n)=>t(n)}),pt=(t,e,n,r)=>function(...s){const u=n?n(...s):JSON.stringify({args:s}),o=t[u];if(o!==void 0&&o.exp>new Date().getTime())return o.value;const l=e(...s);return t[u]={exp:new Date().getTime()+r,value:l},l},bt=(t,{key:e=null,ttl:n=300}={})=>pt({},t,e,n),kt=({delay:t},e)=>{let n=null;return(...c)=>{clearTimeout(n),n=setTimeout(()=>e(...c),t)}},At=({interval:t},e)=>{let n=!0;return(...c)=>{!n||(e(...c),n=!1,setTimeout(()=>{n=!0},t))}},Ot=(t,e)=>{const n=()=>{};return new Proxy(Object.assign(n,t),{get:(r,c)=>r[c],set:(r,c,s)=>(r[c]=s,!0),apply:(r,c,s)=>e(Object.assign({},r))(...s)})},Ct=(t,e)=>{const n=e===void 0?0:e;if(t==null)return n;const r=parseFloat(t);return isNaN(r)?n:r},Et=(t,e)=>{const n=e===void 0?0:e;if(t==null)return n;const r=parseInt(t);return isNaN(r)?n:r},Nt=(t,e=n=>n===void 0)=>t?Object.keys(t).reduce((r,c)=>e(t[c])?r:{...r,[c]:t[c]},{}):{},b=(t,e)=>Object.keys(t).reduce((r,c)=>({...r,[e(c,t[c])]:t[c]}),{}),_t=(t,e)=>Object.keys(t).reduce((r,c)=>({...r,[c]:e(t[c],c)}),{}),$t=(t,e)=>t?Object.entries(t).reduce((n,[r,c])=>{const[s,u]=e(r,c);return{...n,[s]:u}},{}):{},zt=t=>t?Object.keys(t).reduce((n,r)=>({...n,[t[r]]:r}),{}):{},Pt=t=>b(t,e=>e.toLowerCase()),St=t=>b(t,e=>e.toUpperCase()),Tt=t=>{if(N(t))return t;if(typeof t=="function")return t.bind({});const e=new t.constructor;return Object.getOwnPropertyNames(t).forEach(n=>{e[n]=t[n]}),e},jt=(t,e)=>{if(!t)return[];const n=Object.entries(t);return n.length===0?[]:n.reduce((r,c)=>[...r,e(c[0],c[1])],[])},Bt=(t,e)=>t?e.reduce((n,r)=>(t.hasOwnProperty(r)&&(n[r]=t[r]),n),{}):{},Lt=(t,e)=>t?!e||e.length===0?t:e.reduce((n,r)=>(delete n[r],n),{...t}):{},Dt=(t,e,n=null)=>{const r=e.split(/[\.\[\]]/g);let c=t;for(const s of r){if(c===null||c===void 0)return n;s.trim()!==""&&(c=c[s])}return c===void 0?n:c},j=(t,e)=>!t&&!e?{}:t?e?Object.entries(t).reduce((n,[r,c])=>({...n,[r]:(()=>E(c)?j(c,e[r]):e[r])()}),{}):t:e,k=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),Ft=t=>{const e=t.length;if(e===0)return null;const n=k(0,e-1);return t[n]},Rt=t=>t.map(e=>({rand:Math.random(),value:e})).sort((e,n)=>e.rand-n.rand).map(e=>e.value),Zt=(t,e="")=>{const n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"+e;return T(t,r=>r+n.charAt(k(0,n.length-1)),"")},qt=(...t)=>{const{itemsByValue:e,itemsByIndex:n}=t.reduce((r,c,s)=>({itemsByValue:{...r.itemsByValue,[c]:s},itemsByIndex:{...r.itemsByIndex,[s]:c}}),{itemsByValue:{},itemsByIndex:{}});return{min:(r,c)=>e[r]e[r]>e[c]?r:c,first:()=>n[0],last:()=>n[t.length-1],next:(r,c)=>n[e[r]+1]??c,previous:(r,c)=>n[e[r]-1]??c}},g=t=>{if(!t||t.length===0)return"";const e=t.toLowerCase();return e.substring(0,1).toUpperCase()+e.substring(1,e.length)},B=t=>{const e=t?.replace(/([A-Z])+/g,g)?.split(/(?=[A-Z])|[\.\-\s_]/).map(n=>n.toLowerCase())??[];return e.length===0?"":e.length===1?e[0]:e.reduce((n,r)=>`${n}${r.charAt(0).toUpperCase()}${r.slice(1)}`)},It=t=>{const e=t?.replace(/([A-Z])+/g,g).split(/(?=[A-Z])|[\.\-\s_]/).map(n=>n.toLowerCase())??[];return e.length===0?"":e.length===1?e[0]:e.reduce((n,r)=>`${n}_${r.toLowerCase()}`)},Mt=t=>{const e=t?.replace(/([A-Z])+/g,g)?.split(/(?=[A-Z])|[\.\-\s_]/).map(n=>n.toLowerCase())??[];return e.length===0?"":e.length===1?e[0]:e.reduce((n,r)=>`${n}-${r.toLowerCase()}`)},Ut=t=>{const e=t?.split(/[\.\-\s_]/).map(n=>n.toLowerCase())??[];return e.length===0?"":e.map(n=>n.charAt(0).toUpperCase()+n.slice(1)).join("")},Vt=t=>t?t.split(/(?=[A-Z])|[\.\-\s_]/).map(e=>e.trim()).filter(e=>!!e).map(e=>g(e.toLowerCase())).join(" "):"",Kt=(t,e,n=/\{\{(.+?)\}\}/g)=>Array.from(t.matchAll(n)).reduce((r,c)=>r.replace(c[0],e[c[1]]),t),Jt=(t,e=" ")=>{if(!t)return"";const n=new RegExp(`^[${e}]+|[${e}]+$`,"g");return t.replace(n,"")};return i.alphabetical=K,i.assign=j,i.boil=w,i.callable=Ot,i.camal=B,i.camel=B,i.capitalize=g,i.chain=gt,i.clone=Tt,i.cluster=Q,i.compose=mt,i.counting=J,i.dash=Mt,i.debounce=kt,i.defer=lt,i.diff=it,i.draw=Ft,i.first=U,i.flat=x,i.fork=S,i.get=Dt,i.group=Z,i.intersects=tt,i.invert=zt,i.isArray=C,i.isDate=_,i.isEmpty=R,i.isEqual=$,i.isFloat=F,i.isFunction=m,i.isInt=D,i.isNumber=a,i.isObject=E,i.isPrimitive=N,i.isString=L,i.isSymbol=O,i.iterate=T,i.last=V,i.list=P,i.listify=jt,i.lowerize=Pt,i.map=ot,i.mapEntries=$t,i.mapKeys=b,i.mapValues=_t,i.max=G,i.memo=bt,i.merge=et,i.min=H,i.objectify=X,i.omit=Lt,i.parallel=at,i.partial=ht,i.partob=wt,i.pascal=Ut,i.pick=Bt,i.proxied=yt,i.random=k,i.range=y,i.reduce=ut,i.replace=W,i.replaceOrAppend=nt,i.retry=dt,i.select=Y,i.series=qt,i.shake=Nt,i.shift=st,i.shuffle=Rt,i.sift=ct,i.sleep=p,i.snake=It,i.sort=z,i.sum=M,i.template=Kt,i.throttle=At,i.title=Vt,i.toFloat=Ct,i.toInt=Et,i.toggle=rt,i.trim=Jt,i.try=d,i.tryit=d,i.uid=Zt,i.unique=v,i.upperize=St,i.zip=q,i.zipToObject=I,i}({}); diff --git a/src/object.ts b/src/object.ts index 676a90fd..eb467860 100644 --- a/src/object.ts +++ b/src/object.ts @@ -1,4 +1,4 @@ -import { isFunction, isObject, isPrimitive } from './typed' +import { isObject, isPrimitive } from './typed' type LowercasedKeys> = { [P in keyof T & string as Lowercase

]: T[P] @@ -208,21 +208,16 @@ export const omit = ( } /** - * Warning: Passing a function has been @deprecated - * and will be removed in the next major version. + * Dynamically get a nested value from an array or + * object with a string. + * + * @example get(person, 'friends[0].name') */ export const get = ( value: T, - funcOrPath: ((t: T) => K) | string, + funcOrPath: string, defaultValue: K | null = null ): K | null => { - if (isFunction(funcOrPath)) { - try { - return (funcOrPath as Function)(value) ?? defaultValue - } catch { - return defaultValue - } - } const segments = (funcOrPath as string).split(/[\.\[\]]/g) let current: any = value for (const key of segments) { diff --git a/src/tests/object.test.ts b/src/tests/object.test.ts index a426ed58..0f109088 100644 --- a/src/tests/object.test.ts +++ b/src/tests/object.test.ts @@ -285,34 +285,6 @@ describe('object module', () => { assert.equal(_.get(null, 'name'), null) assert.equal(_.get(undefined, 'name'), null) }) - test('returns specified value or default using function', () => { - assert.equal( - _.get(jay, x => x.name), - 'jay' - ) - assert.equal( - _.get(jay, x => x.friends?.[0].age), - 17 - ) - assert.equal( - _.get( - jay, - x => { - throw 'error' - }, - 17 - ), - 17 - ) - assert.equal( - _.get({ age: undefined }, x => x.age, 22), - 22 - ) - assert.equal( - _.get(jay, x => x.friends?.[0].friends?.[0].friends?.[0].age, 22), - 22 - ) - }) test('returns specified value or default using path', () => { assert.equal(_.get({ age: undefined }, 'age', 22), 22) assert.equal(_.get(jay, 'friends[0].age'), 17)