UNPKG

@tienedev/datype

Version:

Modern TypeScript utility library with pragmatic typing and zero dependencies

1 lines 18.9 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).Datype={})}(this,function(t){"use strict";function e(t){if("object"!=typeof t||null===t)return!1;if(Array.isArray(t))return!1;if(t instanceof Date||t instanceof RegExp)return!1;const e=Object.getPrototypeOf(t);return null===e||e===Object.prototype}const r={arrayMergeStrategy:"concat",maxDepth:50};function n(t,r,o,i=0,f=new WeakSet){if(i>o.maxDepth)throw new Error(`Maximum merge depth (${o.maxDepth}) exceeded`);if(f.has(t))return t;f.add(t);const u=Object.assign({},t);for(const t of r)if(e(t))for(const r of Object.keys(t)){const c=t[r],a=u[r];Array.isArray(a)&&Array.isArray(c)?u[r]="concat"===o.arrayMergeStrategy?[...a,...c]:c:e(a)&&e(c)?u[r]=n(a,[c],o,i+1,f):u[r]=c}return f.delete(t),u}function o(t,e,r){if(Object.is(t,e))return!0;if(null==t||null==e)return t===e;if(typeof t!=typeof e)return!1;if("object"!=typeof t)return!1;if(r.has(t))return r.get(t)===e;const n=t,i=e;if(t instanceof Date&&e instanceof Date)return Object.is(t.getTime(),e.getTime());if(t instanceof RegExp&&e instanceof RegExp)return t.source===e.source&&t.flags===e.flags;if(t instanceof Set&&e instanceof Set){if(t.size!==e.size)return!1;r.set(t,e);for(const n of Array.from(t)){let i=!1;for(const t of Array.from(e))if(o(n,t,r)){i=!0;break}if(!i)return r.delete(t),!1}return r.delete(t),!0}if(t instanceof Map&&e instanceof Map){if(t.size!==e.size)return!1;r.set(t,e);for(const[n,i]of Array.from(t)){let f=!1;for(const[t,u]of Array.from(e))if(o(n,t,r)&&o(i,u,r)){f=!0;break}if(!f)return r.delete(t),!1}return r.delete(t),!0}if(Array.isArray(t)&&Array.isArray(e)){if(t.length!==e.length)return!1;r.set(t,e);for(let n=0;n<t.length;n++)if(!o(t[n],e[n],r))return r.delete(t),!1;return r.delete(t),!0}if(Array.isArray(t)||Array.isArray(e))return!1;if("function"==typeof t&&"function"==typeof e)return t===e;r.set(t,e);const f=Object.keys(n),u=Object.keys(i);if(f.length!==u.length)return r.delete(t),!1;for(const e of f){if(!Object.prototype.hasOwnProperty.call(i,e))return r.delete(t),!1;if(!o(n[e],i[e],r))return r.delete(t),!1}return r.delete(t),!0}function i(t){if(!Array.isArray(t))throw new TypeError("Expected first argument to be an array");const e=[];return function t(r){for(let n=0;n<r.length;n++){const o=r[n];Array.isArray(o)?t(o):e.push(o)}}(t),e}function f(t,e){if(!Array.isArray(t))throw new TypeError("Expected first argument to be an array");if("function"!=typeof e)throw new TypeError("Expected iteratee to be a function");if(0===t.length)return[];const r=new Set,n=[];for(let o=0;o<t.length;o++){const i=t[o],f=e(i,o,t);r.has(f)||(r.add(f),n.push(i))}return n}const u={"à":"a","á":"a","â":"a","ã":"a","ä":"a","å":"a","æ":"ae","ç":"c","è":"e","é":"e","ê":"e","ë":"e","ì":"i","í":"i","î":"i","ï":"i","ñ":"n","ò":"o","ó":"o","ô":"o","õ":"o","ö":"o","ø":"o","ù":"u","ú":"u","û":"u","ü":"u","ý":"y","ÿ":"y","þ":"th","ß":"ss","À":"A","Á":"A","Â":"A","Ã":"A","Ä":"A","Å":"A","Æ":"AE","Ç":"C","È":"E","É":"E","Ê":"E","Ë":"E","Ì":"I","Í":"I","Î":"I","Ï":"I","Ñ":"N","Ò":"O","Ó":"O","Ô":"O","Õ":"O","Ö":"O","Ø":"O","Ù":"U","Ú":"U","Û":"U","Ü":"U","Ý":"Y","Þ":"TH","ă":"a","ą":"a","ć":"c","č":"c","ď":"d","đ":"d","ę":"e","ě":"e","ğ":"g","ı":"i","ł":"l","ľ":"l","ń":"n","ň":"n","ő":"o","œ":"oe","ř":"r","ś":"s","š":"s","ť":"t","ů":"u","ű":"u","ź":"z","ż":"z","ž":"z","Ă":"A","Ą":"A","Ć":"C","Č":"C","Ď":"D","Đ":"D","Ę":"E","Ě":"E","Ğ":"G","İ":"I","Ł":"L","Ľ":"L","Ń":"N","Ň":"N","Ő":"O","Œ":"OE","Ř":"R","Ś":"S","Š":"S","Ť":"T","Ů":"U","Ű":"U","Ź":"Z","Ż":"Z","Ž":"Z","а":"a","б":"b","в":"v","г":"g","д":"d","е":"e","ё":"yo","ж":"zh","з":"z","и":"i","й":"y","к":"k","л":"l","м":"m","н":"n","о":"o","п":"p","р":"r","с":"s","т":"t","у":"u","ф":"f","х":"h","ц":"ts","ч":"ch","ш":"sh","щ":"sch","ъ":"","ы":"y","ь":"","э":"e","ю":"yu","я":"ya","А":"A","Б":"B","В":"V","Г":"G","Д":"D","Е":"E","Ё":"YO","Ж":"ZH","З":"Z","И":"I","Й":"Y","К":"K","Л":"L","М":"M","Н":"N","О":"O","П":"P","Р":"R","С":"S","Т":"T","У":"U","Ф":"F","Х":"H","Ц":"TS","Ч":"CH","Ш":"SH","Щ":"SCH","Ъ":"","Ы":"Y","Ь":"","Э":"E","Ю":"YU","Я":"YA","α":"a","β":"b","γ":"g","δ":"d","ε":"e","ζ":"z","η":"h","θ":"th","ι":"i","κ":"k","λ":"l","μ":"m","ν":"n","ξ":"x","ο":"o","π":"p","ρ":"r","σ":"s","ς":"s","τ":"t","υ":"y","φ":"f","χ":"ch","ψ":"ps","ω":"w","Α":"A","Β":"B","Γ":"G","Δ":"D","Ε":"E","Ζ":"Z","Η":"H","Θ":"TH","Ι":"I","Κ":"K","Λ":"L","Μ":"M","Ν":"N","Ξ":"X","Ο":"O","Π":"P","Ρ":"R","Σ":"S","Τ":"T","Υ":"Y","Φ":"F","Χ":"CH","Ψ":"PS","Ω":"W","٠":"0","١":"1","٢":"2","٣":"3","٤":"4","٥":"5","٦":"6","٧":"7","٨":"8","٩":"9","‘":"","’":"","“":"","”":"","…":"...","–":"-","—":"-","•":"","‚":"","„":"","‹":"","›":"","«":"","»":"","€":"euro","£":"pound","¥":"yen","₽":"ruble",$:"dollar","¢":"cent","©":"c","®":"r","™":"tm","&":"and"};function c(t){if("string"!=typeof t)throw new TypeError("Expected first argument to be a string");return 0===t.length?t:t.replace(/([a-z0-9])([A-Z])/g,"$1_$2").replace(/([A-Z])([A-Z][a-z])/g,"$1_$2").replace(/[^a-zA-Z0-9]+/g,"_").toLowerCase().replace(/^_+|_+$/g,"")}function a(t,e=30,r="..."){if("string"!=typeof t)throw new TypeError("Expected first argument to be a string");if("number"!=typeof e||!Number.isInteger(e)||e<0)throw new TypeError("Expected length to be a non-negative integer");if("string"!=typeof r)throw new TypeError("Expected omission to be a string");if(t.length<=e)return t;if(r.length>=e)return r.slice(0,e);const n=e-r.length;return n<=0?r.slice(0,e):t.slice(0,n)+r}t.camelCase=function(t){if("string"!=typeof t)throw new TypeError("Expected first argument to be a string");return 0===t.length?t:t.replace(/([a-z0-9])([A-Z])/g,"$1 $2").replace(/[^a-zA-Z0-9]+/g," ").trim().split(/\s+/).filter(t=>t.length>0).map((t,e)=>0===e?t.toLowerCase():t.charAt(0).toUpperCase()+t.slice(1).toLowerCase()).join("")},t.capitalize=function(t){if("string"!=typeof t)throw new TypeError("Expected first argument to be a string");return 0===t.length?t:t.charAt(0).toUpperCase()+t.slice(1).toLowerCase()},t.chunk=function(t,e){if(!Array.isArray(t))throw new TypeError("Expected first argument to be an array");if("number"!=typeof e||!Number.isInteger(e)||e<=0)throw new TypeError("Expected size to be a positive integer");if(0===t.length)return[];const r=[];for(let n=0;n<t.length;n+=e){const o=t.slice(n,n+e);r.push(o)}return r},t.cloneDeep=function t(e,r=new WeakMap){if(null==e)return e;if("object"!=typeof e)return e;if(r.has(e))return r.get(e);if(e instanceof Date)return new Date(e.getTime());if(e instanceof RegExp)return new RegExp(e.source,e.flags);if(Array.isArray(e)){const n=[];r.set(e,n);for(let o=0;o<e.length;o++)n[o]=t(e[o],r);return n}if(e instanceof Set){const n=new Set;r.set(e,n);for(const o of Array.from(e))n.add(t(o,r));return n}if(e instanceof Map){const n=new Map;r.set(e,n);for(const[o,i]of Array.from(e))n.set(t(o,r),t(i,r));return n}if("object"==typeof e){const n={};r.set(e,n);for(const o in e)Object.prototype.hasOwnProperty.call(e,o)&&(n[o]=t(e[o],r));return n}return e},t.compact=function(t){if(!Array.isArray(t))throw new TypeError("Expected first argument to be an array");const e=[];for(let r=0;r<t.length;r++){const n=t[r];n&&e.push(n)}return e},t.compactBy=function(t,e){if(!Array.isArray(t))throw new TypeError("Expected first argument to be an array");if(!Array.isArray(e))throw new TypeError("Expected second argument to be an array");const r=new Set(e),n=[];for(let e=0;e<t.length;e++){const o=t[e];r.has(o)||n.push(o)}return n},t.compactWith=function(t,e){if(!Array.isArray(t))throw new TypeError("Expected first argument to be an array");if("function"!=typeof e)throw new TypeError("Expected second argument to be a function");const r=[];for(let n=0;n<t.length;n++){const o=t[n];e(o,n,t)||r.push(o)}return r},t.compose=function(...t){if(0===t.length)throw new Error("compose requires at least one function");return 1===t.length?t[0]:(...e)=>{let r=t[t.length-1](...e);for(let e=t.length-2;e>=0;e--)r=t[e](r);return r}},t.constantCase=function(t){return c(t).toUpperCase()},t.curry=function(t){const e=(...r)=>r.length>=t.length?t(...r):(...t)=>e(...r,...t);return e},t.debounce=function(t,e,r={}){const{leading:n=!1,trailing:o=!0,maxWait:i}=r;let f,u,c,a,s,p=0;if("function"!=typeof t)throw new TypeError("Expected a function");if(e<0)throw new RangeError("Wait time must be non-negative");if(void 0!==i&&i<e)throw new RangeError("maxWait must be greater than or equal to wait");function y(e){const r=c;return c=void 0,p=e,s=t(...r),s}function g(t){return void 0===a||t-(a||0)>=e||void 0!==i&&t-p>=i}function d(){const t=Date.now();g(t)?b(t):f=setTimeout(d,function(t){const r=t-p,n=e-(t-(a||0));return void 0!==i?Math.min(n,i-r):n}(t))}function w(){const t=Date.now();void 0!==c&&(void 0!==f&&(clearTimeout(f),f=void 0),b(t))}function b(t){return f=void 0,u=void 0,o&&void 0!==c?y(t):(c=void 0,s)}function l(...t){const r=Date.now(),o=g(r);if(c=t,a=r,o){if(void 0===f&&void 0===u)return function(t){return p=t,f=setTimeout(d,e),void 0!==i&&(u=setTimeout(w,i)),n?y(t):s}(r);if(void 0!==u)return clearTimeout(f),clearTimeout(u),f=void 0,u=void 0,y(r)}return void 0===f&&(f=setTimeout(d,e)),void 0!==i&&void 0===u&&(u=setTimeout(w,i)),s}return l.cancel=function(){void 0!==f&&(clearTimeout(f),f=void 0),void 0!==u&&(clearTimeout(u),u=void 0),p=0,c=void 0,a=void 0},l.flush=function(){if(void 0===f&&void 0===u)return s;void 0!==f&&(clearTimeout(f),f=void 0),void 0!==u&&(clearTimeout(u),u=void 0);const t=Date.now();return void 0!==c?y(t):s},l.pending=function(){return void 0!==f||void 0!==u},l},t.deepMerge=function(t,...o){if(!e(t))throw new TypeError("Target must be a plain object");if(0===o.length)return Object.assign({},t);let i={},f=[];const u=o[o.length-1];return u&&"object"==typeof u&&("arrayMergeStrategy"in u||"maxDepth"in u)?(i=u,f=o.slice(0,-1)):f=o,n(t,f,Object.assign(Object.assign({},r),i))},t.dotCase=function(t){if("string"!=typeof t)throw new TypeError("Expected first argument to be a string");return 0===t.length?t:t.replace(/([a-z0-9])([A-Z])/g,"$1.$2").replace(/[^a-zA-Z0-9]+/g,".").toLowerCase().replace(/^\.+|\.+$/g,"")},t.flatten=function(t){if(!Array.isArray(t))throw new TypeError("Expected first argument to be an array");const e=[];for(let r=0;r<t.length;r++){const n=t[r];Array.isArray(n)?e.push(...n):e.push(n)}return e},t.flattenDeep=i,t.flattenDepth=function(t,e=1){if(!Array.isArray(t))throw new TypeError("Expected first argument to be an array");if("number"!=typeof e||e<0)throw new TypeError("Expected depth to be a non-negative number");if(0===e)return t.slice();if(e===1/0)return i(t);const r=[];return function t(e,n){for(let o=0;o<e.length;o++){const i=e[o];Array.isArray(i)&&n>0?t(i,n-1):r.push(i)}}(t,e),r},t.get=function(t,e,r){if(null==t)return r;if(!e||"string"!=typeof e)return r;const n=e.split(".");let o=t;for(let t=0;t<n.length;t++){const e=n[t];if(null==o)return r;if(Array.isArray(o)&&/^\d+$/.test(e))o=o[parseInt(e,10)];else if("object"==typeof o)o=o[e];else try{o=o[e]}catch(t){return r}}return void 0!==o?o:r},t.groupBy=function(t,e){if(!Array.isArray(t))throw new TypeError("Expected first argument to be an array");if("string"!=typeof e&&"number"!=typeof e&&"symbol"!=typeof e&&"function"!=typeof e)throw new TypeError("Expected iteratee to be a property key or function");const r={};for(let n=0;n<t.length;n++){const o=t[n];let i;i="function"==typeof e?e(o):null==o?void 0:o[e];const f=String(null==i?String(i):i);r[f]||(r[f]=[]),r[f].push(o)}return r},t.isArray=function(t){return Array.isArray(t)},t.isEmpty=function(t){return null==t||("string"==typeof t||Array.isArray(t)?0===t.length:t instanceof Set||t instanceof Map?0===t.size:!(t instanceof Date||t instanceof RegExp||"function"==typeof t||t.constructor&&t.constructor!==Object||"object"!=typeof t||0!==Object.keys(t).length))},t.isEqual=function(t,e){return o(t,e,new Map)},t.isFunction=function(t){return"function"==typeof t},t.isPlainObject=function(t){if(null===t||"object"!=typeof t)return!1;if(Array.isArray(t))return!1;if(t instanceof Date)return!1;if(t instanceof RegExp)return!1;if(t instanceof Error)return!1;if("function"==typeof t)return!1;if(t instanceof Map||t instanceof Set||t instanceof WeakMap||t instanceof WeakSet)return!1;if(ArrayBuffer.isView(t)||t instanceof ArrayBuffer)return!1;if(t instanceof Promise)return!1;const e=Object.getPrototypeOf(t);return null===e||e===Object.prototype},t.kebabCase=function(t){if("string"!=typeof t)throw new TypeError("Expected first argument to be a string");return 0===t.length?t:t.replace(/([a-z0-9])([A-Z])/g,"$1-$2").replace(/([A-Z])([A-Z][a-z])/g,"$1-$2").replace(/[^a-zA-Z0-9]+/g,"-").toLowerCase().replace(/^-+|-+$/g,"")},t.keyTransformers={toCamelCase:t=>t.replace(/_([a-z])/g,(t,e)=>e.toUpperCase()),toSnakeCase:t=>t.replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`),toKebabCase:t=>t.replace(/[A-Z]/g,t=>`-${t.toLowerCase()}`),toLowerCase:t=>t.toLowerCase(),toUpperCase:t=>t.toUpperCase(),addPrefix:t=>e=>`${t}${e}`,addSuffix:t=>e=>`${e}${t}`,normalize:t=>t.toLowerCase().replace(/[-\s]+/g,"_").replace(/[^a-z0-9_]/g,"_").replace(/_+/g,"_").replace(/^_|_$/g,"")},t.mapKeys=function(t,e){if(null==t)throw new TypeError("Expected object to be non-null");if("function"!=typeof e)throw new TypeError("Expected iteratee to be a function");const r={},n=[...Object.keys(t),...Object.getOwnPropertySymbols(t).filter(e=>Object.prototype.propertyIsEnumerable.call(t,e))];for(const o of n)r[e(o,t[o],t)]=t[o];return r},t.mapValues=function(t,e){if(null==t)throw new TypeError("Expected object to be non-null");if("function"!=typeof e)throw new TypeError("Expected iteratee to be a function");const r={},n=[...Object.keys(t),...Object.getOwnPropertySymbols(t).filter(e=>Object.prototype.propertyIsEnumerable.call(t,e))];for(const o of n)r[o]=e(t[o],o,t);return r},t.merge=function(...t){const e={};for(const r of t)if(null!=r&&"object"==typeof r){const t=Object.getOwnPropertyDescriptors(r);for(const n in t){const o=t[n];(null==o?void 0:o.enumerable)&&(e[n]=r[n])}const n=Object.getOwnPropertySymbols(r);for(const t of n){const n=Object.getOwnPropertyDescriptor(r,t);n&&n.enumerable&&(e[t]=r[t])}}return e},t.omit=function(t,e){if("object"!=typeof t||null===t)throw new TypeError("First argument must be an object");if(!Array.isArray(e))throw new TypeError("Second argument must be an array of keys");const r=new Set(e),n={};for(const e in t)Object.prototype.hasOwnProperty.call(t,e)&&!r.has(e)&&(n[e]=t[e]);return n},t.once=function(t){let e,r,n=!1,o=!1;return function(...i){if(!n){n=!0;try{e=t(...i)}catch(t){throw o=!0,r=t,t}}if(o)throw r;return e}},t.pascalCase=function(t){if("string"!=typeof t)throw new TypeError("Expected first argument to be a string");return 0===t.length?t:t.replace(/([a-z0-9])([A-Z])/g,"$1 $2").replace(/[^a-zA-Z0-9]+/g," ").trim().split(/\s+/).filter(t=>t.length>0).map(t=>t.charAt(0).toUpperCase()+t.slice(1).toLowerCase()).join("")},t.pick=function(t,e){if("object"!=typeof t||null===t)throw new TypeError("First argument must be an object");if(!Array.isArray(e))throw new TypeError("Second argument must be an array of keys");const r={};for(const n of e)n in t&&(r[n]=t[n]);return r},t.pipe=function(...t){if(0===t.length)throw new Error("pipe requires at least one function");return 1===t.length?t[0]:(...e)=>{let r=t[0](...e);for(let e=1;e<t.length;e++)r=t[e](r);return r}},t.set=function(t,e,r){if(null==t)throw new TypeError("Cannot set property on null or undefined");if(!e||"string"!=typeof e)throw new TypeError("Path must be a non-empty string");const n=e.split("."),o=Array.isArray(t)?[...t]:Object.assign({},t);let i=o;for(let t=0;t<n.length-1;t++){const e=n[t],r=n[t+1],o=/^\d+$/.test(r);null===i[e]||void 0===i[e]?i[e]=o?[]:{}:i[e]=Array.isArray(i[e])?[...i[e]]:Object.assign({},i[e]),i=i[e]}return i[n[n.length-1]]=r,o},t.slugify=function(t,e={}){const{separator:r="-",lowercase:n=!0,trim:o=!0,replacements:i={},remove:f=!0,strict:c=!1}=e;if("string"!=typeof t)throw new TypeError("Expected input to be a string");if(0===t.length)return"";let a=t;a=a.normalize("NFD").replace(/[\u0300-\u036f]/g,""),n&&(a=a.toLowerCase());for(const[t,e]of Object.entries(i))a.includes(t)&&(a=a.replace(new RegExp(t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),"g"),e));if(c)a=a.replace(/[^a-zA-Z0-9\s]/g," ");else{for(const[t,e]of Object.entries(u))a.includes(t)&&(a=a.replace(new RegExp(t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),"g"),e));f&&(a=a.replace(/[^\w\s-]/g," "))}if(a=a.replace(/\s+/g,r).replace(new RegExp(`\\${r}+`,"g"),r),o&&r.length>0){const t=r.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),e=new RegExp(`^${t}+|${t}+$`,"g");a=a.replace(e,"")}return a},t.snakeCase=c,t.throttle=function(t,e,r={}){const{leading:n=!0,trailing:o=!0}=r;let i,f,u,c,a=0;if("function"!=typeof t)throw new TypeError("Expected a function");if(e<0)throw new RangeError("Wait time must be non-negative");function s(e){const r=f;return f=void 0,a=e,c=t(...r),c}function p(){y(Date.now())}function y(t){return i=void 0,o&&void 0!==f?s(t):(f=void 0,c)}function g(...t){const r=Date.now(),y=function(t){return void 0===u||t-a>=e}(r);return f=t,u=r,y&&void 0===i?function(t){return a=t,o&&(i=setTimeout(p,e)),n?s(t):c}(r):(void 0===i&&o&&(i=setTimeout(p,e)),c)}return g.cancel=function(){void 0!==i&&(clearTimeout(i),i=void 0),a=0,f=void 0,u=void 0},g.flush=function(){return void 0===i?c:y(Date.now())},g.pending=function(){return void 0!==i},g},t.truncate=a,t.truncateMiddle=function(t,e,r="..."){if("string"!=typeof t)throw new TypeError("Expected first argument to be a string");if("number"!=typeof e||!Number.isInteger(e)||e<0)throw new TypeError("Expected length to be a non-negative integer");if("string"!=typeof r)throw new TypeError("Expected omission to be a string");if(t.length<=e)return t;if(r.length>=e)return r.slice(0,e);const n=e-r.length;if(n<=0)return r.slice(0,e);const o=Math.ceil(n/2),i=n-o;return t.slice(0,o)+r+(i>0?t.slice(-i):"")},t.truncateWords=function(t,e,r="..."){if("string"!=typeof t)throw new TypeError("Expected first argument to be a string");if("number"!=typeof e||!Number.isInteger(e)||e<0)throw new TypeError("Expected length to be a non-negative integer");if("string"!=typeof r)throw new TypeError("Expected omission to be a string");if(t.length<=e)return t;const n=t.slice(0,e-r.length).lastIndexOf(" ");return-1===n?a(t,e,r):t.slice(0,n)+r},t.uniq=function(t){if(!Array.isArray(t))throw new TypeError("Expected first argument to be an array");return 0===t.length?[]:Array.from(new Set(t))},t.uniqBy=f,t.uniqByProperty=function(t,e){return f(t,t=>t[e])}});