diff --git a/cdn/radash.esm.js b/cdn/radash.esm.js index a0a63559..5575fdbc 100644 --- a/cdn/radash.esm.js +++ b/cdn/radash.esm.js @@ -85,6 +85,15 @@ const isEqual = (x, y) => { } return true; }; +const isNullish = (value) => { + return value === null || value === void 0; +}; +const isNonNullish = (value) => { + return value !== null && value !== void 0; +}; +const isKeyOf = (value, obj) => { + return value in obj; +}; const group = (array, getGroupId) => { return array.reduce((acc, item) => { @@ -937,4 +946,4 @@ const trim = (str, charsToTrim = " ") => { return str.replace(regex, ""); }; -export { all, alphabetical, assign, boil, callable, camel, capitalize, chain, clone, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, first, flat, fork, get, group, guard, inRange, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isNumber, isObject, isPrimitive, isPromise, isString, isSymbol, iterate, keys, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, set, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, upperize, zip, zipToObject }; +export { all, alphabetical, assign, boil, callable, camel, capitalize, chain, clone, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, first, flat, fork, get, group, guard, inRange, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isKeyOf, isNonNullish, isNullish, isNumber, isObject, isPrimitive, isPromise, isString, isSymbol, iterate, keys, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, set, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, upperize, zip, zipToObject }; diff --git a/cdn/radash.js b/cdn/radash.js index ca7ebea6..ffeaf401 100644 --- a/cdn/radash.js +++ b/cdn/radash.js @@ -88,6 +88,15 @@ var radash = (function (exports) { } return true; }; + const isNullish = (value) => { + return value === null || value === void 0; + }; + const isNonNullish = (value) => { + return value !== null && value !== void 0; + }; + const isKeyOf = (value, obj) => { + return value in obj; + }; const group = (array, getGroupId) => { return array.reduce((acc, item) => { @@ -975,6 +984,9 @@ var radash = (function (exports) { exports.isFloat = isFloat; exports.isFunction = isFunction; exports.isInt = isInt; + exports.isKeyOf = isKeyOf; + exports.isNonNullish = isNonNullish; + exports.isNullish = isNullish; exports.isNumber = isNumber; exports.isObject = isObject; exports.isPrimitive = isPrimitive; diff --git a/cdn/radash.min.js b/cdn/radash.min.js index f9363f50..714e8d06 100644 --- a/cdn/radash.min.js +++ b/cdn/radash.min.js @@ -1 +1 @@ -var radash=function(i){"use strict";const E=e=>!!e&&e.constructor===Symbol,w=Array.isArray,k=e=>!!e&&e.constructor===Object,N=e=>e==null||typeof e!="object"&&typeof e!="function",y=e=>!!(e&&e.constructor&&e.call&&e.apply),K=e=>typeof e=="string"||e instanceof String,W=e=>h(e)&&e%1===0,J=e=>h(e)&&e%1!==0,h=e=>{try{return Number(e)===e}catch{return!1}},T=e=>Object.prototype.toString.call(e)==="[object Date]",S=e=>!(!e||!e.then||!y(e.then)),X=e=>{if(e===!0||e===!1||e==null)return!0;if(h(e))return e===0;if(T(e))return isNaN(e.getTime());if(y(e)||E(e))return!1;const t=e.length;if(h(t))return t===0;const n=e.size;return h(n)?n===0:Object.keys(e).length===0},j=(e,t)=>{if(Object.is(e,t))return!0;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(e instanceof RegExp&&t instanceof RegExp)return e.toString()===t.toString();if(typeof e!="object"||e===null||typeof t!="object"||t===null)return!1;const n=Reflect.ownKeys(e),r=Reflect.ownKeys(t);if(n.length!==r.length)return!1;for(let s=0;se.reduce((n,r)=>{const s=t(r);return n[s]||(n[s]=[]),n[s].push(r),n},{});function H(...e){return!e||!e.length?[]:new Array(Math.max(...e.map(({length:t})=>t))).fill([]).map((t,n)=>e.map(r=>r[n]))}function Q(e,t){if(!e||!e.length)return{};const n=y(t)?t:w(t)?(r,s)=>t[s]:(r,s)=>t;return e.reduce((r,s,u)=>(r[s]=n(s,u),r),{})}const O=(e,t)=>!e||(e.length??0)===0?null:e.reduce(t);function V(e,t){return(e||[]).reduce((n,r)=>n+(t?t(r):r),0)}const G=(e,t=void 0)=>e?.length>0?e[0]:t,x=(e,t=void 0)=>e?.length>0?e[e.length-1]:t,z=(e,t,n=!1)=>{if(!e)return[];const r=(u,c)=>t(u)-t(c),s=(u,c)=>t(c)-t(u);return e.slice().sort(n===!0?s:r)},ee=(e,t,n="asc")=>{if(!e)return[];const r=(u,c)=>`${t(u)}`.localeCompare(t(c)),s=(u,c)=>`${t(c)}`.localeCompare(t(u));return e.slice().sort(n==="desc"?s:r)},te=(e,t)=>e?e.reduce((n,r)=>{const s=t(r);return n[s]=(n[s]??0)+1,n},{}):{},ne=(e,t,n)=>{if(!e)return[];if(t===void 0)return[...e];for(let r=0;rr)=>e.reduce((r,s)=>(r[t(s)]=n(s),r),{}),re=(e,t,n)=>e?e.reduce((r,s,u)=>(n(s,u)&&r.push(t(s,u)),r),[]):[];function se(e,t){const n=t??(r=>r);return O(e,(r,s)=>n(r)>n(s)?r:s)}function ie(e,t){const n=t??(r=>r);return O(e,(r,s)=>n(r){const n=Math.ceil(e.length/t);return new Array(n).fill(null).map((r,s)=>e.slice(s*t,s*t+t))},ce=(e,t)=>{const n=e.reduce((r,s)=>{const u=t?t(s):s;return r[u]||(r[u]=s),r},{});return Object.values(n)};function*A(e,t,n=s=>s,r=1){const s=y(n)?n:()=>n,u=t?e:0,c=t??e;for(let o=u;o<=c&&(yield s(o),!(o+r>c));o+=r);}const C=(e,t,n,r)=>Array.from(A(e,t,n,r)),oe=e=>e.reduce((t,n)=>(t.push(...n),t),[]),fe=(e,t,n)=>{if(!e||!t)return!1;const r=n??(u=>u),s=t.reduce((u,c)=>(u[r(c)]=!0,u),{});return e.some(u=>s[r(u)])},R=(e,t)=>e?e.reduce((n,r)=>{const[s,u]=n;return t(r)?[[...s,r],u]:[s,[...u,r]]},[[],[]]):[[],[]],le=(e,t,n)=>!t&&!e?[]:t?e?n?e.reduce((r,s)=>{const u=t.find(c=>n(s)===n(c));return u?r.push(u):r.push(s),r},[]):e:[]:e,ae=(e,t,n)=>{if(!e&&!t)return[];if(!t)return[...e];if(!e)return[t];for(let r=0;r{if(!e&&!t)return[];if(!e)return[t];if(!t)return[...e];const s=n?(o,a)=>n(o,a)===n(t,a):o=>o===t;return e.find(s)?e.filter((o,a)=>!s(o,a)):(r?.strategy??"append")==="append"?[...e,t]:[t,...e]},ge=e=>e?.filter(t=>!!t)??[],B=(e,t,n)=>{let r=n;for(let s=1;s<=e;s++)r=t(r,s);return r},he=(e,t,n=r=>r)=>{if(!e?.length&&!t?.length)return[];if(e?.length===void 0)return[...t];if(!t?.length)return[...e];const r=t.reduce((s,u)=>(s[n(u)]=!0,s),{});return e.filter(s=>!r[n(s)])};function me(e,t){if(e.length===0)return e;const n=t%e.length;return n===0?e:[...e.slice(-n,e.length),...e.slice(0,-n)]}const we=async(e,t,n)=>{const r=n!==void 0;if(!r&&e?.length<1)throw new Error("Cannot reduce empty array with no init value");const s=r?e:e.slice(1);let u=r?n:e[0];for(const[c,o]of s.entries())u=await t(u,o,c);return u},ye=async(e,t)=>{if(!e)return[];let n=[],r=0;for(const s of e){const u=await t(s,r++);n.push(u)}return n},pe=async e=>{const t=[],n=(u,c)=>t.push({fn:u,rethrow:c?.rethrow??!1}),[r,s]=await m(e)(n);for(const{fn:u,rethrow:c}of t){const[o]=await m(u)(r);if(o&&c)throw o}if(r)throw r;return s};class L extends Error{constructor(t=[]){super();const n=t.find(r=>r.name)?.name??"";this.name=`AggregateError(${n}...)`,this.message=`AggregateError with ${t.length} errors`,this.stack=t.find(r=>r.stack)?.stack??this.stack,this.errors=t}}const be=async(e,t,n)=>{const r=t.map((d,b)=>({index:b,item:d})),s=async d=>{const b=[];for(;;){const f=r.pop();if(!f)return d(b);const[l,g]=await m(n)(f.item);b.push({error:l,result:g,index:f.index})}},u=C(1,e).map(()=>new Promise(s)),c=await Promise.all(u),[o,a]=R(z(c.flat(),d=>d.index),d=>!!d.error);if(o.length>0)throw new L(o.map(d=>d.error));return a.map(d=>d.result)};async function ke(e){const t=w(e)?e.map(s=>[null,s]):Object.entries(e),n=await Promise.all(t.map(([s,u])=>u.then(c=>({result:c,exc:null,key:s})).catch(c=>({result:null,exc:c,key:s})))),r=n.filter(s=>s.exc);if(r.length>0)throw new L(r.map(s=>s.exc));return w(e)?n.map(s=>s.result):n.reduce((s,u)=>({...s,[u.key]:u.result}),{})}const Oe=async(e,t)=>{const n=e?.times??3,r=e?.delay,s=e?.backoff??null;for(const u of A(1,n)){const[c,o]=await m(t)(a=>{throw{_exited:a}});if(!c)return o;if(c._exited)throw c._exited;if(u===n)throw c;r&&await $(r),s&&await $(s(u))}},$=e=>new Promise(t=>setTimeout(t,e)),m=e=>(...t)=>{try{const n=e(...t);return S(n)?n.then(r=>[void 0,r]).catch(r=>[r,void 0]):[void 0,n]}catch(n){return[n,void 0]}},Ae=(e,t)=>{const n=s=>{if(t&&!t(s))throw s},r=s=>s instanceof Promise;try{const s=e();return r(s)?s.catch(n):s}catch(s){return n(s)}};function Ce(...e){return(...t)=>e.slice(1).reduce((n,r)=>r(n),e[0](...t))}function $e(...e){return e.reverse().reduce((t,n)=>n(t))}const Pe=(e,...t)=>(...n)=>e(...t,...n),_e=(e,t)=>n=>e({...t,...n}),Ee=e=>new Proxy({},{get:(t,n)=>e(n)}),Ne=(e,t,n,r)=>function(...u){const c=n?n(...u):JSON.stringify({args:u}),o=e[c];if(o!==void 0&&(!o.exp||o.exp>new Date().getTime()))return o.value;const a=t(...u);return e[c]={exp:r?new Date().getTime()+r:null,value:a},a},Te=(e,t={})=>Ne({},e,t.key??null,t.ttl??null),Se=({delay:e},t)=>{let n,r=!0;const s=(...u)=>{r?(clearTimeout(n),n=setTimeout(()=>{r&&t(...u),n=void 0},e)):t(...u)};return s.isPending=()=>n!==void 0,s.cancel=()=>{r=!1},s.flush=(...u)=>t(...u),s},je=({interval:e},t)=>{let n=!0,r;const s=(...u)=>{n&&(t(...u),n=!1,r=setTimeout(()=>{n=!0,r=void 0},e))};return s.isThrottled=()=>r!==void 0,s},ze=(e,t)=>{const n=()=>{};return new Proxy(Object.assign(n,e),{get:(r,s)=>r[s],set:(r,s,u)=>(r[s]=u,!0),apply:(r,s,u)=>t(Object.assign({},r))(...u)})};function Me(e,t,n){return typeof e=="number"&&typeof t=="number"&&(typeof n>"u"||typeof n=="number")?(typeof n>"u"&&(n=t,t=0),e>=Math.min(t,n)&&e{const n=t===void 0?0:t;if(e==null)return n;const r=parseFloat(e);return isNaN(r)?n:r},Z=(e,t)=>{const n=t===void 0?0:t;if(e==null)return n;const r=parseInt(e);return isNaN(r)?n:r},Be=(e,t=n=>n===void 0)=>e?Object.keys(e).reduce((r,s)=>(t(e[s])||(r[s]=e[s]),r),{}):{},P=(e,t)=>Object.keys(e).reduce((r,s)=>(r[t(s,e[s])]=e[s],r),{}),Le=(e,t)=>Object.keys(e).reduce((r,s)=>(r[s]=t(e[s],s),r),{}),Ze=(e,t)=>e?Object.entries(e).reduce((n,[r,s])=>{const[u,c]=t(r,s);return n[u]=c,n},{}):{},De=e=>e?Object.keys(e).reduce((n,r)=>(n[e[r]]=r,n),{}):{},Fe=e=>P(e,t=>t.toLowerCase()),qe=e=>P(e,t=>t.toUpperCase()),D=e=>{if(N(e))return e;if(typeof e=="function")return e.bind({});const t=new e.constructor;return Object.getOwnPropertyNames(e).forEach(n=>{t[n]=e[n]}),t},Ie=(e,t)=>{if(!e)return[];const n=Object.entries(e);return n.length===0?[]:n.reduce((r,s)=>(r.push(t(s[0],s[1])),r),[])},ve=(e,t)=>e?t.reduce((n,r)=>(Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=e[r]),n),{}):{},Ue=(e,t)=>e?!t||t.length===0?e:t.reduce((n,r)=>(delete n[r],n),{...e}):{},F=(e,t,n)=>{const r=t.split(/[\.\[\]]/g);let s=e;for(const u of r){if(s===null||s===void 0)return n;const c=u.replace(/['"]/g,"");c.trim()!==""&&(s=s[c])}return s===void 0?n:s},q=(e,t,n)=>{if(!e)return{};if(!t||n===void 0)return e;const r=t.split(/[\.\[\]]/g).filter(c=>!!c.trim()),s=c=>{if(r.length>1){const o=r.shift(),a=Z(r[0],null)!==null;c[o]=c[o]===void 0?a?[]:{}:c[o],s(c[o])}else c[r[0]]=n},u=D(e);return s(u),u},I=(e,t)=>!e||!t?e??t??{}:Object.entries({...e,...t}).reduce((n,[r,s])=>({...n,[r]:(()=>k(e[r])?I(e[r],s):s)()}),{}),v=e=>{if(!e)return[];const t=(n,r)=>k(n)?Object.entries(n).flatMap(([s,u])=>t(u,[...r,s])):w(n)?n.flatMap((s,u)=>t(s,[...r,`${u}`])):[r.join(".")];return t(e,[])},Ke=e=>e?M(v(e),t=>t,t=>F(e,t)):{},We=e=>e?Object.keys(e).reduce((t,n)=>q(t,n,e[n]),{}):{},_=(e,t)=>Math.floor(Math.random()*(t-e+1)+e),Je=e=>{const t=e.length;if(t===0)return null;const n=_(0,t-1);return e[n]},Xe=e=>e.map(t=>({rand:Math.random(),value:t})).sort((t,n)=>t.rand-n.rand).map(t=>t.value),Ye=(e,t="")=>{const n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"+t;return B(e,r=>r+n.charAt(_(0,n.length-1)),"")},He=(e,t=n=>`${n}`)=>{const{indexesByKey:n,itemsByIndex:r}=e.reduce((f,l,g)=>({indexesByKey:{...f.indexesByKey,[t(l)]:g},itemsByIndex:{...f.itemsByIndex,[g]:l}}),{indexesByKey:{},itemsByIndex:{}}),s=(f,l)=>n[t(f)]n[t(f)]>n[t(l)]?f:l,c=()=>r[0],o=()=>r[e.length-1],a=(f,l)=>r[n[t(f)]+1]??l??c(),d=(f,l)=>r[n[t(f)]-1]??l??o();return{min:s,max:u,first:c,last:o,next:a,previous:d,spin:(f,l)=>{if(l===0)return f;const g=Math.abs(l),rt=g>e.length?g%e.length:g;return C(0,rt-1).reduce(U=>l>0?a(U):d(U),f)}}},p=e=>{if(!e||e.length===0)return"";const t=e.toLowerCase();return t.substring(0,1).toUpperCase()+t.substring(1,t.length)},Qe=e=>{const t=e?.replace(/([A-Z])+/g,p)?.split(/(?=[A-Z])|[\.\-\s_]/).map(n=>n.toLowerCase())??[];return t.length===0?"":t.length===1?t[0]:t.reduce((n,r)=>`${n}${r.charAt(0).toUpperCase()}${r.slice(1)}`)},Ve=(e,t)=>{const n=e?.replace(/([A-Z])+/g,p).split(/(?=[A-Z])|[\.\-\s_]/).map(s=>s.toLowerCase())??[];if(n.length===0)return"";if(n.length===1)return n[0];const r=n.reduce((s,u)=>`${s}_${u.toLowerCase()}`);return t?.splitOnNumber===!1?r:r.replace(/([A-Za-z]{1}[0-9]{1})/,s=>`${s[0]}_${s[1]}`)},Ge=e=>{const t=e?.replace(/([A-Z])+/g,p)?.split(/(?=[A-Z])|[\.\-\s_]/).map(n=>n.toLowerCase())??[];return t.length===0?"":t.length===1?t[0]:t.reduce((n,r)=>`${n}-${r.toLowerCase()}`)},xe=e=>{const t=e?.split(/[\.\-\s_]/).map(n=>n.toLowerCase())??[];return t.length===0?"":t.map(n=>n.charAt(0).toUpperCase()+n.slice(1)).join("")},et=e=>e?e.split(/(?=[A-Z])|[\.\-\s_]/).map(t=>t.trim()).filter(t=>!!t).map(t=>p(t.toLowerCase())).join(" "):"",tt=(e,t,n=/\{\{(.+?)\}\}/g)=>Array.from(e.matchAll(n)).reduce((r,s)=>r.replace(s[0],t[s[1]]),e),nt=(e,t=" ")=>{if(!e)return"";const n=t.replace(/[\W]{1}/g,"\\$&"),r=new RegExp(`^[${n}]+|[${n}]+$`,"g");return e.replace(r,"")};return i.all=ke,i.alphabetical=ee,i.assign=I,i.boil=O,i.callable=ze,i.camel=Qe,i.capitalize=p,i.chain=Ce,i.clone=D,i.cluster=ue,i.compose=$e,i.construct=We,i.counting=te,i.crush=Ke,i.dash=Ge,i.debounce=Se,i.defer=pe,i.diff=he,i.draw=Je,i.first=G,i.flat=oe,i.fork=R,i.get=F,i.group=Y,i.guard=Ae,i.inRange=Me,i.intersects=fe,i.invert=De,i.isArray=w,i.isDate=T,i.isEmpty=X,i.isEqual=j,i.isFloat=J,i.isFunction=y,i.isInt=W,i.isNumber=h,i.isObject=k,i.isPrimitive=N,i.isPromise=S,i.isString=K,i.isSymbol=E,i.iterate=B,i.keys=v,i.last=x,i.list=C,i.listify=Ie,i.lowerize=Fe,i.map=ye,i.mapEntries=Ze,i.mapKeys=P,i.mapValues=Le,i.max=se,i.memo=Te,i.merge=le,i.min=ie,i.objectify=M,i.omit=Ue,i.parallel=be,i.partial=Pe,i.partob=_e,i.pascal=xe,i.pick=ve,i.proxied=Ee,i.random=_,i.range=A,i.reduce=we,i.replace=ne,i.replaceOrAppend=ae,i.retry=Oe,i.select=re,i.series=He,i.set=q,i.shake=Be,i.shift=me,i.shuffle=Xe,i.sift=ge,i.sleep=$,i.snake=Ve,i.sort=z,i.sum=V,i.template=tt,i.throttle=je,i.title=et,i.toFloat=Re,i.toInt=Z,i.toggle=de,i.trim=nt,i.try=m,i.tryit=m,i.uid=Ye,i.unique=ce,i.upperize=qe,i.zip=H,i.zipToObject=Q,i}({}); +var radash=function(s){"use strict";const _=t=>!!t&&t.constructor===Symbol,w=Array.isArray,O=t=>!!t&&t.constructor===Object,E=t=>t==null||typeof t!="object"&&typeof t!="function",y=t=>!!(t&&t.constructor&&t.call&&t.apply),K=t=>typeof t=="string"||t instanceof String,W=t=>h(t)&&t%1===0,J=t=>h(t)&&t%1!==0,h=t=>{try{return Number(t)===t}catch{return!1}},T=t=>Object.prototype.toString.call(t)==="[object Date]",S=t=>!(!t||!t.then||!y(t.then)),X=t=>{if(t===!0||t===!1||t==null)return!0;if(h(t))return t===0;if(T(t))return isNaN(t.getTime());if(y(t)||_(t))return!1;const e=t.length;if(h(e))return e===0;const n=t.size;return h(n)?n===0:Object.keys(t).length===0},j=(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 i=0;it==null,H=t=>t!=null,Q=(t,e)=>t in e,V=(t,e)=>t.reduce((n,r)=>{const i=e(r);return n[i]||(n[i]=[]),n[i].push(r),n},{});function G(...t){return!t||!t.length?[]:new Array(Math.max(...t.map(({length:e})=>e))).fill([]).map((e,n)=>t.map(r=>r[n]))}function x(t,e){if(!t||!t.length)return{};const n=y(e)?e:w(e)?(r,i)=>e[i]:(r,i)=>e;return t.reduce((r,i,u)=>(r[i]=n(i,u),r),{})}const k=(t,e)=>!t||(t.length??0)===0?null:t.reduce(e);function tt(t,e){return(t||[]).reduce((n,r)=>n+(e?e(r):r),0)}const et=(t,e=void 0)=>t?.length>0?t[0]:e,nt=(t,e=void 0)=>t?.length>0?t[t.length-1]:e,z=(t,e,n=!1)=>{if(!t)return[];const r=(u,c)=>e(u)-e(c),i=(u,c)=>e(c)-e(u);return t.slice().sort(n===!0?i:r)},rt=(t,e,n="asc")=>{if(!t)return[];const r=(u,c)=>`${e(u)}`.localeCompare(e(c)),i=(u,c)=>`${e(c)}`.localeCompare(e(u));return t.slice().sort(n==="desc"?i:r)},it=(t,e)=>t?t.reduce((n,r)=>{const i=e(r);return n[i]=(n[i]??0)+1,n},{}):{},st=(t,e,n)=>{if(!t)return[];if(e===void 0)return[...t];for(let r=0;rr)=>t.reduce((r,i)=>(r[e(i)]=n(i),r),{}),ut=(t,e,n)=>t?t.reduce((r,i,u)=>(n(i,u)&&r.push(e(i,u)),r),[]):[];function ct(t,e){const n=e??(r=>r);return k(t,(r,i)=>n(r)>n(i)?r:i)}function ot(t,e){const n=e??(r=>r);return k(t,(r,i)=>n(r){const n=Math.ceil(t.length/e);return new Array(n).fill(null).map((r,i)=>t.slice(i*e,i*e+e))},lt=(t,e)=>{const n=t.reduce((r,i)=>{const u=e?e(i):i;return r[u]||(r[u]=i),r},{});return Object.values(n)};function*A(t,e,n=i=>i,r=1){const i=y(n)?n:()=>n,u=e?t:0,c=e??t;for(let o=u;o<=c&&(yield i(o),!(o+r>c));o+=r);}const N=(t,e,n,r)=>Array.from(A(t,e,n,r)),at=t=>t.reduce((e,n)=>(e.push(...n),e),[]),dt=(t,e,n)=>{if(!t||!e)return!1;const r=n??(u=>u),i=e.reduce((u,c)=>(u[r(c)]=!0,u),{});return t.some(u=>i[r(u)])},R=(t,e)=>t?t.reduce((n,r)=>{const[i,u]=n;return e(r)?[[...i,r],u]:[i,[...u,r]]},[[],[]]):[[],[]],gt=(t,e,n)=>!e&&!t?[]:e?t?n?t.reduce((r,i)=>{const u=e.find(c=>n(i)===n(c));return u?r.push(u):r.push(i),r},[]):t:[]:t,ht=(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 i=n?(o,a)=>n(o,a)===n(e,a):o=>o===e;return t.find(i)?t.filter((o,a)=>!i(o,a)):(r?.strategy??"append")==="append"?[...t,e]:[e,...t]},wt=t=>t?.filter(e=>!!e)??[],B=(t,e,n)=>{let r=n;for(let i=1;i<=t;i++)r=e(r,i);return r},yt=(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((i,u)=>(i[n(u)]=!0,i),{});return t.filter(i=>!r[n(i)])};function pt(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 bt=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 i=r?t:t.slice(1);let u=r?n:t[0];for(const[c,o]of i.entries())u=await e(u,o,c);return u},Ot=async(t,e)=>{if(!t)return[];let n=[],r=0;for(const i of t){const u=await e(i,r++);n.push(u)}return n},kt=async t=>{const e=[],n=(u,c)=>e.push({fn:u,rethrow:c?.rethrow??!1}),[r,i]=await m(t)(n);for(const{fn:u,rethrow:c}of e){const[o]=await m(u)(r);if(o&&c)throw o}if(r)throw r;return i};class L 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((d,b)=>({index:b,item:d})),i=async d=>{const b=[];for(;;){const f=r.pop();if(!f)return d(b);const[l,g]=await m(n)(f.item);b.push({error:l,result:g,index:f.index})}},u=N(1,t).map(()=>new Promise(i)),c=await Promise.all(u),[o,a]=R(z(c.flat(),d=>d.index),d=>!!d.error);if(o.length>0)throw new L(o.map(d=>d.error));return a.map(d=>d.result)};async function Nt(t){const e=w(t)?t.map(i=>[null,i]):Object.entries(t),n=await Promise.all(e.map(([i,u])=>u.then(c=>({result:c,exc:null,key:i})).catch(c=>({result:null,exc:c,key:i})))),r=n.filter(i=>i.exc);if(r.length>0)throw new L(r.map(i=>i.exc));return w(t)?n.map(i=>i.result):n.reduce((i,u)=>({...i,[u.key]:u.result}),{})}const Ct=async(t,e)=>{const n=t?.times??3,r=t?.delay,i=t?.backoff??null;for(const u of A(1,n)){const[c,o]=await m(e)(a=>{throw{_exited:a}});if(!c)return o;if(c._exited)throw c._exited;if(u===n)throw c;r&&await C(r),i&&await C(i(u))}},C=t=>new Promise(e=>setTimeout(e,t)),m=t=>(...e)=>{try{const n=t(...e);return S(n)?n.then(r=>[void 0,r]).catch(r=>[r,void 0]):[void 0,n]}catch(n){return[n,void 0]}},$t=(t,e)=>{const n=i=>{if(e&&!e(i))throw i},r=i=>i instanceof Promise;try{const i=t();return r(i)?i.catch(n):i}catch(i){return n(i)}};function Pt(...t){return(...e)=>t.slice(1).reduce((n,r)=>r(n),t[0](...e))}function _t(...t){return t.reverse().reduce((e,n)=>n(e))}const Et=(t,...e)=>(...n)=>t(...e,...n),Tt=(t,e)=>n=>t({...e,...n}),St=t=>new Proxy({},{get:(e,n)=>t(n)}),jt=(t,e,n,r)=>function(...u){const c=n?n(...u):JSON.stringify({args:u}),o=t[c];if(o!==void 0&&(!o.exp||o.exp>new Date().getTime()))return o.value;const a=e(...u);return t[c]={exp:r?new Date().getTime()+r:null,value:a},a},zt=(t,e={})=>jt({},t,e.key??null,e.ttl??null),Mt=({delay:t},e)=>{let n,r=!0;const i=(...u)=>{r?(clearTimeout(n),n=setTimeout(()=>{r&&e(...u),n=void 0},t)):e(...u)};return i.isPending=()=>n!==void 0,i.cancel=()=>{r=!1},i.flush=(...u)=>e(...u),i},Rt=({interval:t},e)=>{let n=!0,r;const i=(...u)=>{n&&(e(...u),n=!1,r=setTimeout(()=>{n=!0,r=void 0},t))};return i.isThrottled=()=>r!==void 0,i},Bt=(t,e)=>{const n=()=>{};return new Proxy(Object.assign(n,t),{get:(r,i)=>r[i],set:(r,i,u)=>(r[i]=u,!0),apply:(r,i,u)=>e(Object.assign({},r))(...u)})};function Lt(t,e,n){return typeof t=="number"&&typeof e=="number"&&(typeof n>"u"||typeof n=="number")?(typeof n>"u"&&(n=e,e=0),t>=Math.min(e,n)&&t{const n=e===void 0?0:e;if(t==null)return n;const r=parseFloat(t);return isNaN(r)?n:r},Z=(t,e)=>{const n=e===void 0?0:e;if(t==null)return n;const r=parseInt(t);return isNaN(r)?n:r},Dt=(t,e=n=>n===void 0)=>t?Object.keys(t).reduce((r,i)=>(e(t[i])||(r[i]=t[i]),r),{}):{},$=(t,e)=>Object.keys(t).reduce((r,i)=>(r[e(i,t[i])]=t[i],r),{}),Ft=(t,e)=>Object.keys(t).reduce((r,i)=>(r[i]=e(t[i],i),r),{}),qt=(t,e)=>t?Object.entries(t).reduce((n,[r,i])=>{const[u,c]=e(r,i);return n[u]=c,n},{}):{},It=t=>t?Object.keys(t).reduce((n,r)=>(n[t[r]]=r,n),{}):{},vt=t=>$(t,e=>e.toLowerCase()),Ut=t=>$(t,e=>e.toUpperCase()),D=t=>{if(E(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},Kt=(t,e)=>{if(!t)return[];const n=Object.entries(t);return n.length===0?[]:n.reduce((r,i)=>(r.push(e(i[0],i[1])),r),[])},Wt=(t,e)=>t?e.reduce((n,r)=>(Object.prototype.hasOwnProperty.call(t,r)&&(n[r]=t[r]),n),{}):{},Jt=(t,e)=>t?!e||e.length===0?t:e.reduce((n,r)=>(delete n[r],n),{...t}):{},F=(t,e,n)=>{const r=e.split(/[\.\[\]]/g);let i=t;for(const u of r){if(i===null||i===void 0)return n;const c=u.replace(/['"]/g,"");c.trim()!==""&&(i=i[c])}return i===void 0?n:i},q=(t,e,n)=>{if(!t)return{};if(!e||n===void 0)return t;const r=e.split(/[\.\[\]]/g).filter(c=>!!c.trim()),i=c=>{if(r.length>1){const o=r.shift(),a=Z(r[0],null)!==null;c[o]=c[o]===void 0?a?[]:{}:c[o],i(c[o])}else c[r[0]]=n},u=D(t);return i(u),u},I=(t,e)=>!t||!e?t??e??{}:Object.entries({...t,...e}).reduce((n,[r,i])=>({...n,[r]:(()=>O(t[r])?I(t[r],i):i)()}),{}),v=t=>{if(!t)return[];const e=(n,r)=>O(n)?Object.entries(n).flatMap(([i,u])=>e(u,[...r,i])):w(n)?n.flatMap((i,u)=>e(i,[...r,`${u}`])):[r.join(".")];return e(t,[])},Xt=t=>t?M(v(t),e=>e,e=>F(t,e)):{},Yt=t=>t?Object.keys(t).reduce((e,n)=>q(e,n,t[n]),{}):{},P=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),Ht=t=>{const e=t.length;if(e===0)return null;const n=P(0,e-1);return t[n]},Qt=t=>t.map(e=>({rand:Math.random(),value:e})).sort((e,n)=>e.rand-n.rand).map(e=>e.value),Vt=(t,e="")=>{const n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"+e;return B(t,r=>r+n.charAt(P(0,n.length-1)),"")},Gt=(t,e=n=>`${n}`)=>{const{indexesByKey:n,itemsByIndex:r}=t.reduce((f,l,g)=>({indexesByKey:{...f.indexesByKey,[e(l)]:g},itemsByIndex:{...f.itemsByIndex,[g]:l}}),{indexesByKey:{},itemsByIndex:{}}),i=(f,l)=>n[e(f)]n[e(f)]>n[e(l)]?f:l,c=()=>r[0],o=()=>r[t.length-1],a=(f,l)=>r[n[e(f)]+1]??l??c(),d=(f,l)=>r[n[e(f)]-1]??l??o();return{min:i,max:u,first:c,last:o,next:a,previous:d,spin:(f,l)=>{if(l===0)return f;const g=Math.abs(l),ue=g>t.length?g%t.length:g;return N(0,ue-1).reduce(U=>l>0?a(U):d(U),f)}}},p=t=>{if(!t||t.length===0)return"";const e=t.toLowerCase();return e.substring(0,1).toUpperCase()+e.substring(1,e.length)},xt=t=>{const e=t?.replace(/([A-Z])+/g,p)?.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)}`)},te=(t,e)=>{const n=t?.replace(/([A-Z])+/g,p).split(/(?=[A-Z])|[\.\-\s_]/).map(i=>i.toLowerCase())??[];if(n.length===0)return"";if(n.length===1)return n[0];const r=n.reduce((i,u)=>`${i}_${u.toLowerCase()}`);return e?.splitOnNumber===!1?r:r.replace(/([A-Za-z]{1}[0-9]{1})/,i=>`${i[0]}_${i[1]}`)},ee=t=>{const e=t?.replace(/([A-Z])+/g,p)?.split(/(?=[A-Z])|[\.\-\s_]/).map(n=>n.toLowerCase())??[];return e.length===0?"":e.length===1?e[0]:e.reduce((n,r)=>`${n}-${r.toLowerCase()}`)},ne=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("")},re=t=>t?t.split(/(?=[A-Z])|[\.\-\s_]/).map(e=>e.trim()).filter(e=>!!e).map(e=>p(e.toLowerCase())).join(" "):"",ie=(t,e,n=/\{\{(.+?)\}\}/g)=>Array.from(t.matchAll(n)).reduce((r,i)=>r.replace(i[0],e[i[1]]),t),se=(t,e=" ")=>{if(!t)return"";const n=e.replace(/[\W]{1}/g,"\\$&"),r=new RegExp(`^[${n}]+|[${n}]+$`,"g");return t.replace(r,"")};return s.all=Nt,s.alphabetical=rt,s.assign=I,s.boil=k,s.callable=Bt,s.camel=xt,s.capitalize=p,s.chain=Pt,s.clone=D,s.cluster=ft,s.compose=_t,s.construct=Yt,s.counting=it,s.crush=Xt,s.dash=ee,s.debounce=Mt,s.defer=kt,s.diff=yt,s.draw=Ht,s.first=et,s.flat=at,s.fork=R,s.get=F,s.group=V,s.guard=$t,s.inRange=Lt,s.intersects=dt,s.invert=It,s.isArray=w,s.isDate=T,s.isEmpty=X,s.isEqual=j,s.isFloat=J,s.isFunction=y,s.isInt=W,s.isKeyOf=Q,s.isNonNullish=H,s.isNullish=Y,s.isNumber=h,s.isObject=O,s.isPrimitive=E,s.isPromise=S,s.isString=K,s.isSymbol=_,s.iterate=B,s.keys=v,s.last=nt,s.list=N,s.listify=Kt,s.lowerize=vt,s.map=Ot,s.mapEntries=qt,s.mapKeys=$,s.mapValues=Ft,s.max=ct,s.memo=zt,s.merge=gt,s.min=ot,s.objectify=M,s.omit=Jt,s.parallel=At,s.partial=Et,s.partob=Tt,s.pascal=ne,s.pick=Wt,s.proxied=St,s.random=P,s.range=A,s.reduce=bt,s.replace=st,s.replaceOrAppend=ht,s.retry=Ct,s.select=ut,s.series=Gt,s.set=q,s.shake=Dt,s.shift=pt,s.shuffle=Qt,s.sift=wt,s.sleep=C,s.snake=te,s.sort=z,s.sum=tt,s.template=ie,s.throttle=Rt,s.title=re,s.toFloat=Zt,s.toInt=Z,s.toggle=mt,s.trim=se,s.try=m,s.tryit=m,s.uid=Vt,s.unique=lt,s.upperize=Ut,s.zip=G,s.zipToObject=x,s}({}); diff --git a/docs/typed/is-key-of.mdx b/docs/typed/is-key-of.mdx new file mode 100644 index 00000000..d7466e5d --- /dev/null +++ b/docs/typed/is-key-of.mdx @@ -0,0 +1,21 @@ +--- +title: isKeyOf +description: 'Checks if the given value is a key of the given object. It is useful for narrowing down the type of a value.' +group: Typed +--- + +## Basic usage + +Pass the object and the value to check. It will return a boolean indicating if the value is a key of the object. + +```ts +import { isKeyOf } from 'radash' + +const obj = { + a: 1, + b: 2, + c: 3, +} +isKeyOf(obj, 'a') // true +isKeyOf(obj, 'd') // false +``` diff --git a/docs/typed/is-non-nullish.mdx b/docs/typed/is-non-nullish.mdx new file mode 100644 index 00000000..a31b0309 --- /dev/null +++ b/docs/typed/is-non-nullish.mdx @@ -0,0 +1,23 @@ +--- +title: isNonNullish +description: 'Checks if the given value is not null or undefined.' +group: Typed +--- + +## Basic usage + +Pass in a value and get a boolean telling you if the value is not a Nullish. + +```ts +import { isNonNullish } from 'radash' + +isNonNullish(null) // false +isNonNullish(undefined) // false +isNonNullish('') // true +isNonNullish(0) // true +isNonNullish(false) // true +isNonNullish(NaN) // true +isNonNullish([]) // true +isNonNullish({}) // true + +``` diff --git a/docs/typed/is-nullish.mdx b/docs/typed/is-nullish.mdx new file mode 100644 index 00000000..281740c2 --- /dev/null +++ b/docs/typed/is-nullish.mdx @@ -0,0 +1,22 @@ +--- +title: isNullish +description: 'Checks if the given value is null or undefined.' +group: Typed +--- + +## Basic usage + +Pass in a value and get a boolean telling you if the value is a Nullish. + +```ts +import { isNullish } from 'radash' + +isNullish(null) // true +isNullish(undefined) // true +isNullish(0) // false +isNullish('') // false +isNullish(false) // false +isNullish(NaN) // false +isNullish([]) // false +isNullish({}) // false +``` diff --git a/src/index.ts b/src/index.ts index 9dff2e21..eafec6e1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -93,6 +93,9 @@ export { isFloat, isFunction, isInt, + isKeyOf, + isNonNullish, + isNullish, isNumber, isObject, isPrimitive, diff --git a/src/tests/typed.test.ts b/src/tests/typed.test.ts index 2263cc1f..1f38b0f2 100644 --- a/src/tests/typed.test.ts +++ b/src/tests/typed.test.ts @@ -541,4 +541,51 @@ describe('typed module', () => { assert.isFalse(_.isEqual([complex], [{ ...complex, num: 222 }])) }) }) + + describe('isNullish function', () => { + test('returns true for null and undefined', () => { + assert.isTrue(_.isNullish(null)) + assert.isTrue(_.isNullish(undefined)) + }) + test('returns false for non-nullish values', () => { + assert.isFalse(_.isNullish(0)) + assert.isFalse(_.isNullish('')) + assert.isFalse(_.isNullish(false)) + assert.isFalse(_.isNullish([])) + assert.isFalse(_.isNullish({})) + assert.isFalse(_.isNullish(() => {})) + assert.isFalse(_.isNullish(Symbol(''))) + assert.isFalse(_.isNullish(new Date())) + }) + }) + + describe('isNonNullish function', () => { + test('returns false for null and undefined', () => { + assert.isFalse(_.isNonNullish(null)) + assert.isFalse(_.isNonNullish(undefined)) + }) + test('returns true for non-nullish values', () => { + assert.isTrue(_.isNonNullish(0)) + assert.isTrue(_.isNonNullish('')) + assert.isTrue(_.isNonNullish(false)) + assert.isTrue(_.isNonNullish([])) + assert.isTrue(_.isNonNullish({})) + assert.isTrue(_.isNonNullish(() => {})) + assert.isTrue(_.isNonNullish(Symbol(''))) + assert.isTrue(_.isNonNullish(new Date())) + }) + }) + + describe('isKeyOf function', () => { + test('returns true for keys of the object', () => { + const obj = { name: 'ray', age: 22 } + assert.isTrue(_.isKeyOf('name', obj)) + assert.isTrue(_.isKeyOf('age', obj)) + }) + test('returns false for keys not in the object', () => { + const obj = { name: 'ray', age: 22 } + assert.isFalse(_.isKeyOf('height', obj)) + assert.isFalse(_.isKeyOf('weight', obj)) + }) + }) }) diff --git a/src/typed.ts b/src/typed.ts index e6600171..d4c6920f 100644 --- a/src/typed.ts +++ b/src/typed.ts @@ -104,3 +104,32 @@ export const isEqual = (x: TType, y: TType): boolean => { } return true } + +/** + * Checks if the given value is null or undefined. + */ +export const isNullish = (value: any): value is null | undefined => { + return value === null || value === undefined +} + +/** + * Checks if the given value is not null or undefined. + */ +export const isNonNullish = ( + value: TType +): value is Exclude => { + return value !== null && value !== undefined +} + +/** + * Checks if the given value is a key of the given object. It is useful for narrowing down the type of a value. + * @param value key to check + * @param obj object to check + * @returns true if the value is a key of the object + */ +export const isKeyOf = >( + value: string | number | symbol, + obj: TType +): value is keyof TType => { + return value in obj +}