@daybrush/utils
Advanced tools
Comparing version 1.6.0-beta.1 to 1.6.0
@@ -32,2 +32,3 @@ import { IArrayFormat, IObject, SplitOptions } from "./types"; | ||
export declare function findLastIndex<T>(arr: T[], callback: (element: T, index: number, arr: T[]) => any, defaultIndex?: number): number; | ||
export declare function findLast<T>(arr: T[], callback: (element: T, index: number, arr: T[]) => any, defalutValue?: T): T | undefined; | ||
export declare function find<T>(arr: T[], callback: (element: T, index: number, arr: T[]) => any, defalutValue?: T): T | undefined; | ||
@@ -51,1 +52,2 @@ export declare const requestAnimationFrame: (callback: FrameRequestCallback) => number; | ||
export declare function counter(num: number): number[]; | ||
export declare function replaceOnce(text: string, fromText: RegExp | string, toText: string | ((...args: any[]) => string)): string; |
@@ -7,3 +7,3 @@ /* | ||
repository: https://github.com/daybrush/utils | ||
@version 1.6.0-beta.1 | ||
@version 1.6.0 | ||
*/ | ||
@@ -813,2 +813,19 @@ /** | ||
/** | ||
* Returns the value of the reverse direction element in the array that satisfies the provided testing function. | ||
* @function | ||
* @memberof CrossBrowser | ||
* @param - The array `findLast` was called upon. | ||
* @param - A function to execute on each value in the array, | ||
* @param - Returns defalutValue if not found by the function. | ||
* @example | ||
import { find } from "@daybrush/utils"; | ||
find([{a: 1}, {a: 2}, {a: 3}, {a: 4}], ({ a }) => a === 2); // {a: 2} | ||
*/ | ||
function findLast(arr, callback, defalutValue) { | ||
var index = findLastIndex(arr, callback); | ||
return index > -1 ? arr[index] : defalutValue; | ||
} | ||
/** | ||
* Returns the value of the first element in the array that satisfies the provided testing function. | ||
@@ -1108,2 +1125,7 @@ * @function | ||
} | ||
/** | ||
* @function | ||
* @memberof Utils | ||
*/ | ||
function counter(num) { | ||
@@ -1118,3 +1140,25 @@ var nums = []; | ||
} | ||
/** | ||
* @function | ||
* @memberof Utils | ||
*/ | ||
function replaceOnce(text, fromText, toText) { | ||
var isOnce = false; | ||
return text.replace(fromText, function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
if (isOnce) { | ||
return args[0]; | ||
} | ||
isOnce = true; | ||
return isString(toText) ? toText : toText.apply(void 0, args); | ||
}); | ||
} | ||
/** | ||
@@ -1445,3 +1489,3 @@ * @namespace | ||
export { RGB, RGBA, HSL, HSLA, COLOR_MODELS, FUNCTION, PROPERTY, ARRAY, OBJECT, STRING, NUMBER, UNDEFINED, IS_WINDOW, doc as document, getCrossBrowserProperty, TRANSFORM, FILTER, ANIMATION, KEYFRAMES, OPEN_CLOSED_CHARACTERS, TINY_NUM, DEFAULT_UNIT_PRESETS, cutHex, hexToRGBA, toFullHex, hslToRGBA, stringToRGBA, dot, isUndefined, isObject, isArray, isString, isNumber, isFunction, splitText, splitSpace, splitComma, splitBracket, splitUnit, camelize, decamelize, toArray, now, findIndex, findLastIndex, find, requestAnimationFrame, cancelAnimationFrame, getKeys, sortOrders, convertUnitSize, between, checkBoundSize, calculateBoundSize, sum, average, getRad, getCenterPoint, getShapeDirection, getDist, throttle, throttleArray, counter, $, hasClass, addClass, removeClass, fromCSS, addEvent, removeEvent }; | ||
export { RGB, RGBA, HSL, HSLA, COLOR_MODELS, FUNCTION, PROPERTY, ARRAY, OBJECT, STRING, NUMBER, UNDEFINED, IS_WINDOW, doc as document, getCrossBrowserProperty, TRANSFORM, FILTER, ANIMATION, KEYFRAMES, OPEN_CLOSED_CHARACTERS, TINY_NUM, DEFAULT_UNIT_PRESETS, cutHex, hexToRGBA, toFullHex, hslToRGBA, stringToRGBA, dot, isUndefined, isObject, isArray, isString, isNumber, isFunction, splitText, splitSpace, splitComma, splitBracket, splitUnit, camelize, decamelize, toArray, now, findIndex, findLastIndex, findLast, find, requestAnimationFrame, cancelAnimationFrame, getKeys, sortOrders, convertUnitSize, between, checkBoundSize, calculateBoundSize, sum, average, getRad, getCenterPoint, getShapeDirection, getDist, throttle, throttleArray, counter, replaceOnce, $, hasClass, addClass, removeClass, fromCSS, addEvent, removeEvent }; | ||
//# sourceMappingURL=utils.esm.js.map |
@@ -7,3 +7,3 @@ /* | ||
repository: https://github.com/daybrush/utils | ||
@version 1.6.0-beta.1 | ||
@version 1.6.0 | ||
*/ | ||
@@ -819,2 +819,19 @@ (function (global, factory) { | ||
/** | ||
* Returns the value of the reverse direction element in the array that satisfies the provided testing function. | ||
* @function | ||
* @memberof CrossBrowser | ||
* @param - The array `findLast` was called upon. | ||
* @param - A function to execute on each value in the array, | ||
* @param - Returns defalutValue if not found by the function. | ||
* @example | ||
import { find } from "@daybrush/utils"; | ||
find([{a: 1}, {a: 2}, {a: 3}, {a: 4}], ({ a }) => a === 2); // {a: 2} | ||
*/ | ||
function findLast(arr, callback, defalutValue) { | ||
var index = findLastIndex(arr, callback); | ||
return index > -1 ? arr[index] : defalutValue; | ||
} | ||
/** | ||
* Returns the value of the first element in the array that satisfies the provided testing function. | ||
@@ -1114,2 +1131,7 @@ * @function | ||
} | ||
/** | ||
* @function | ||
* @memberof Utils | ||
*/ | ||
function counter(num) { | ||
@@ -1124,3 +1146,25 @@ var nums = []; | ||
} | ||
/** | ||
* @function | ||
* @memberof Utils | ||
*/ | ||
function replaceOnce(text, fromText, toText) { | ||
var isOnce = false; | ||
return text.replace(fromText, function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
if (isOnce) { | ||
return args[0]; | ||
} | ||
isOnce = true; | ||
return isString(toText) ? toText : toText.apply(void 0, args); | ||
}); | ||
} | ||
/** | ||
@@ -1499,2 +1543,3 @@ * @namespace | ||
findLastIndex: findLastIndex, | ||
findLast: findLast, | ||
find: find, | ||
@@ -1518,2 +1563,3 @@ requestAnimationFrame: requestAnimationFrame, | ||
counter: counter, | ||
replaceOnce: replaceOnce, | ||
$: $, | ||
@@ -1520,0 +1566,0 @@ hasClass: hasClass, |
@@ -7,5 +7,5 @@ /* | ||
repository: https://github.com/daybrush/utils | ||
@version 1.6.0-beta.1 | ||
@version 1.6.0 | ||
*/ | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):n.utils=e()}(this,function(){"use strict";function n(n){if(!d)return"";var e=(d.body||d.documentElement).style,r=p.length;if(typeof e[n]!=u)return n;for(var t=0;t<r;++t){var i="-"+p[t]+"-"+n;if(typeof e[i]!=u)return i}return""}var c="rgb",f="rgba",s="hsl",l="hsla",e=[c,f,s,l],r="function",t="object",i="string",o="number",u="undefined",a=typeof window!=u,d=typeof document!=u&&document,p=["webkit","ms","moz","o"],v=n("transform"),m=n("filter"),h=n("animation"),w=h.replace("animation","keyframes"),y=[{open:"(",close:")"},{open:'"',close:'"'},{open:"'",close:"'"},{open:'\\"',close:'\\"'},{open:"\\'",close:"\\'"}],g=1e-7,A={cm:function(n){return 96*n/2.54},mm:function(n){return 96*n/254},in:function(n){return 96*n},pt:function(n){return 96*n/72},pc:function(n){return 96*n/6},"%":function(n,e){return n*e/100},vw:function(n,e){return void 0===e&&(e=window.innerWidth),n/100*e},vh:function(n,e){return void 0===e&&(e=window.innerHeight),n/100*e},vmax:function(n,e){return void 0===e&&(e=Math.max(window.innerWidth,window.innerHeight)),n/100*e},vmin:function(n,e){return void 0===e&&(e=Math.min(window.innerWidth,window.innerHeight)),n/100*e}};function b(n){return n&&typeof n===t}function x(n){return typeof n===i}function S(n){return typeof n===r}function E(n,e,r,t,i){return R(n,e,r)?r:function(i,o,n,u,a){for(var c,e=n;e<u;++e){var r=function(n){var e=o[n].trim();if(e===i.close&&!R(i,o,n))return{value:n};var r=n,t=T(a,function(n){return n.open===e});if(t&&(r=E(t,o,n,u,a)),-1===r)return c=n,"break";c=n=r}(e);if(e=c,"object"==typeof r)return r.value;if("break"===r)break}return-1}(n,e,r+1,t,i)}function R(n,e,r){if(!n.ignore)return null;var t=e.slice(Math.max(r-3,0),r+3).join("");return new RegExp(n.ignore).exec(t)}function M(n,e){var r=x(e)?{separator:e}:e,t=r.separator,a=void 0===t?",":t,c=r.isSeparateFirst,f=r.isSeparateOnlyOpenClose,i=r.isSeparateOpenClose,s=void 0===i?f:i,o=r.openCloseCharacters,l=void 0===o?y:o,u=l.map(function(n){var e=n.open,r=n.close;return e===r?e:e+"|"+r}).join("|"),d=new RegExp("(\\s*"+a+"\\s*|"+u+"|\\s+)","g"),p=n.split(d).filter(Boolean),v=p.length,m=[],h=[];function w(){return h.length&&(m.push(h.join("")),h=[])}for(var g,A=0;A<v;++A){var b=function(n){var e,r,t=p[n].trim(),i=n,o=T(l,function(n){return n.open===t}),u=T(l,function(n){return n.close===t});if(o){if(-1!==(i=E(o,p,n,v,l))&&s)return w()&&c?(g=n,"break"):(m.push(p.slice(n,i+1).join("")),n=i,c?(g=n,"break"):(g=n,"continue"))}else{if(u&&!R(u,p,n))throw new Error("invalid format: "+u.close);if(e=t,!((""!==(r=a)&&" "!=r||""!==e&&" "!=e)&&e!==r||f))return w(),c?(g=n,"break"):(g=n,"continue")}-1===i&&(i=v-1),h.push(p.slice(n,i+1).join("")),g=n=i}(A),A=g;if("break"===b)break}return h.length&&m.push(h.join("")),m}function C(n){return M(n,",")}function F(n){var e=/([^(]*)\(([\s\S]*)\)([\s\S]*)/g.exec(n);return!e||e.length<4?{}:{prefix:e[1],value:e[2],suffix:e[3]}}function N(n){var e=/^([^\d|e|\-|\+]*)((?:\d|\.|-|e-|e\+)+)(\S*)$/g.exec(n);if(!e)return{prefix:"",unit:"",value:NaN};var r=e[1],t=e[2];return{prefix:r,unit:e[3],value:parseFloat(t)}}function O(){return Date.now?Date.now():(new Date).getTime()}function I(n,e,r){void 0===r&&(r=-1);for(var t=n.length,i=0;i<t;++i)if(e(n[i],i,n))return i;return r}function T(n,e,r){var t=I(n,e);return-1<t?n[t]:r}var L=function(){var r=O(),n=a&&(window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.msRequestAnimationFrame);return n?n.bind(window):function(n){var e=O();return window.setTimeout(function(){n(e-r)},1e3/60)}}();function k(n,e,r){return Math.max(e,Math.min(n,r))}function j(n,r,t){return[[U(r[0],g),U(r[0]*n[1]/n[0],g)],[U(r[1]*n[0]/n[1],g),U(r[1],g)]].filter(function(n){return n.every(function(n,e){return t?n<=r[e]:n>=r[e]})})[0]||n}function B(n){for(var e=n.length,r=0,t=e-1;0<=t;--t)r+=n[t];return e?r/e:0}function D(n,e){var r=e[0]-n[0],t=e[1]-n[1],i=Math.atan2(t,r);return 0<=i?i:i+2*Math.PI}function z(n){return[0,1].map(function(e){return B(n.map(function(n){return n[e]}))})}function U(n,e){return e?Math.round(n/e)*e:n}function P(n){return n.replace("#","")}function q(n){var e=P(n),r=parseInt(e.substring(0,2),16),t=parseInt(e.substring(2,4),16),i=parseInt(e.substring(4,6),16),o=parseInt(e.substring(6,8),16)/255;return isNaN(o)&&(o=1),[r,t,i,o]}function H(n){var e=n.charAt(1),r=n.charAt(2),t=n.charAt(3),i=n.charAt(4);return["#",e,e,r,r,t,t,i,i].join("")}function _(n){var e,r=n[0],t=n[1],i=n[2];r<0&&(r+=360*Math.floor((Math.abs(r)+360)/360)),r%=360;var o=(1-Math.abs(2*i-1))*t,u=o*(1-Math.abs(r/60%2-1)),a=i-o/2,c=r<60?[o,u,0]:r<120?[u,o,0]:r<180?[0,o,u]:r<240?[0,u,o]:r<300?[u,0,o]:r<360?[o,0,u]:[0,0,0];return[Math.round(255*(c[0]+a)),Math.round(255*(c[1]+a)),Math.round(255*(c[2]+a)),null!==(e=n[3])&&void 0!==e?e:1]}return{RGB:c,RGBA:f,HSL:s,HSLA:l,COLOR_MODELS:e,FUNCTION:r,PROPERTY:"property",ARRAY:"array",OBJECT:t,STRING:i,NUMBER:o,UNDEFINED:u,IS_WINDOW:a,document:d,getCrossBrowserProperty:n,TRANSFORM:v,FILTER:m,ANIMATION:h,KEYFRAMES:w,OPEN_CLOSED_CHARACTERS:y,TINY_NUM:g,DEFAULT_UNIT_PRESETS:A,cutHex:P,hexToRGBA:q,toFullHex:H,hslToRGBA:_,stringToRGBA:function(n){if("#"===n.charAt(0))return 4===n.length||5===n.length?q(H(n)):q(n);if(-1!==n.indexOf("(")){var e=F(n),r=e.prefix,t=e.value;if(!r||!t)return;var i=C(t),o=[0,0,0,1],u=i.length;switch(r){case c:case f:for(var a=0;a<u;++a)o[a]=parseFloat(i[a]);return o;case s:case l:for(a=0;a<u;++a)-1!==i[a].indexOf("%")?o[a]=parseFloat(i[a])/100:o[a]=parseFloat(i[a]);return _(o)}}},dot:function(n,e,r,t){return(n*t+e*r)/(r+t)},isUndefined:function(n){return typeof n==u},isObject:b,isArray:function(n){return Array.isArray(n)},isString:x,isNumber:function(n){return typeof n==o},isFunction:S,splitText:M,splitSpace:function(n){return M(n,"")},splitComma:C,splitBracket:F,splitUnit:N,camelize:function(n){return n.replace(/[\s-_]([a-z])/g,function(n,e){return e.toUpperCase()})},decamelize:function(n,t){return void 0===t&&(t="-"),n.replace(/([a-z])([A-Z])/g,function(n,e,r){return""+e+t+r.toLowerCase()})},toArray:function(n){return[].slice.call(n)},now:O,findIndex:I,findLastIndex:function(n,e,r){void 0===r&&(r=-1);for(var t=n.length-1;0<=t;--t)if(e(n[t],t,n))return t;return r},find:T,requestAnimationFrame:L,cancelAnimationFrame:function(){var n=a&&(window.cancelAnimationFrame||window.webkitCancelAnimationFrame||window.mozCancelAnimationFrame||window.msCancelAnimationFrame);return n?n.bind(window):function(n){clearTimeout(n)}}(),getKeys:function(n){if(Object.keys)return Object.keys(n);var e=[];for(var r in e)e.push(r);return e},sortOrders:function(n,i){void 0===i&&(i=[]),n.sort(function(n,e){var r=i.indexOf(n),t=i.indexOf(e);return-1===t&&-1===r?0:-1===r?1:-1===t?-1:r-t})},convertUnitSize:function(n,e){var r=N(n),t=r.value,i=r.unit;if(b(e)){var o=e[i];if(o){if(S(o))return o(t);if(A[i])return A[i](t,o)}}else if("%"===i)return t*e/100;return A[i]?A[i](t):t},between:k,checkBoundSize:j,calculateBoundSize:function(n,r,t,e){if(!e)return n.map(function(n,e){return k(n,r[e],t[e])});var i=n[0],o=n[1],u=j(n,r,!1),a=u[0],c=u[1],f=j(n,t,!0),s=f[0],l=f[1];return i<a||o<c?(i=a,o=c):(s<i||l<o)&&(i=s,o=l),[i,o]},sum:function(n){for(var e=0,r=n.length-1;0<=r;--r)e+=n[r];return e},average:B,getRad:D,getCenterPoint:z,getShapeDirection:function(n){var e=z(n),r=D(e,n[0]),t=D(e,n[1]);return r<t&&t-r<Math.PI||t<r&&t-r<-Math.PI?1:-1},getDist:function(n,e){return Math.sqrt(Math.pow((e?e[0]:0)-n[0],2)+Math.pow((e?e[1]:0)-n[1],2))},throttle:U,throttleArray:function(r,t){return r.forEach(function(n,e){r[e]=U(r[e],t)}),r},counter:function(n){for(var e=[],r=0;r<n;++r)e.push(r);return e},$:function(n,e){return e?d.querySelectorAll(n):d.querySelector(n)},hasClass:function(n,e){return n.classList?n.classList.contains(e):!!n.className.match(new RegExp("(\\s|^)"+e+"(\\s|$)"))},addClass:function(n,e){n.classList?n.classList.add(e):n.className+=" "+e},removeClass:function(n,e){var r;n.classList?n.classList.remove(e):(r=new RegExp("(\\s|^)"+e+"(\\s|$)"),n.className=n.className.replace(r," "))},fromCSS:function(n,e){if(!n||!e||!e.length)return{};var r;if(n instanceof Element)r=n;else{if(!n.length)return{};r=n[0]}for(var t={},i=window.getComputedStyle(r),o=e.length,u=0;u<o;++u)t[e[u]]=i[e[u]];return t},addEvent:function(n,e,r,t){n.addEventListener(e,r,t)},removeEvent:function(n,e,r,t){n.removeEventListener(e,r,t)}}}); | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):n.utils=e()}(this,function(){"use strict";function n(n){if(!d)return"";var e=(d.body||d.documentElement).style,r=p.length;if(typeof e[n]!=u)return n;for(var t=0;t<r;++t){var i="-"+p[t]+"-"+n;if(typeof e[i]!=u)return i}return""}var c="rgb",f="rgba",s="hsl",l="hsla",e=[c,f,s,l],r="function",t="object",i="string",o="number",u="undefined",a=typeof window!=u,d=typeof document!=u&&document,p=["webkit","ms","moz","o"],v=n("transform"),m=n("filter"),h=n("animation"),w=h.replace("animation","keyframes"),b=[{open:"(",close:")"},{open:'"',close:'"'},{open:"'",close:"'"},{open:'\\"',close:'\\"'},{open:"\\'",close:"\\'"}],g=1e-7,A={cm:function(n){return 96*n/2.54},mm:function(n){return 96*n/254},in:function(n){return 96*n},pt:function(n){return 96*n/72},pc:function(n){return 96*n/6},"%":function(n,e){return n*e/100},vw:function(n,e){return void 0===e&&(e=window.innerWidth),n/100*e},vh:function(n,e){return void 0===e&&(e=window.innerHeight),n/100*e},vmax:function(n,e){return void 0===e&&(e=Math.max(window.innerWidth,window.innerHeight)),n/100*e},vmin:function(n,e){return void 0===e&&(e=Math.min(window.innerWidth,window.innerHeight)),n/100*e}};function y(n){return n&&typeof n===t}function x(n){return typeof n===i}function S(n){return typeof n===r}function E(n,e,r,t,i){return R(n,e,r)?r:function(i,o,n,u,a){for(var c,e=n;e<u;++e){var r=function(n){var e=o[n].trim();if(e===i.close&&!R(i,o,n))return{value:n};var r=n,t=L(a,function(n){return n.open===e});if(t&&(r=E(t,o,n,u,a)),-1===r)return c=n,"break";c=n=r}(e);if(e=c,"object"==typeof r)return r.value;if("break"===r)break}return-1}(n,e,r+1,t,i)}function R(n,e,r){if(!n.ignore)return null;var t=e.slice(Math.max(r-3,0),r+3).join("");return new RegExp(n.ignore).exec(t)}function M(n,e){var r=x(e)?{separator:e}:e,t=r.separator,a=void 0===t?",":t,c=r.isSeparateFirst,f=r.isSeparateOnlyOpenClose,i=r.isSeparateOpenClose,s=void 0===i?f:i,o=r.openCloseCharacters,l=void 0===o?b:o,u=l.map(function(n){var e=n.open,r=n.close;return e===r?e:e+"|"+r}).join("|"),d=new RegExp("(\\s*"+a+"\\s*|"+u+"|\\s+)","g"),p=n.split(d).filter(Boolean),v=p.length,m=[],h=[];function w(){return h.length&&(m.push(h.join("")),h=[])}for(var g,A=0;A<v;++A){var y=function(n){var e,r,t=p[n].trim(),i=n,o=L(l,function(n){return n.open===t}),u=L(l,function(n){return n.close===t});if(o){if(-1!==(i=E(o,p,n,v,l))&&s)return w()&&c?(g=n,"break"):(m.push(p.slice(n,i+1).join("")),n=i,c?(g=n,"break"):(g=n,"continue"))}else{if(u&&!R(u,p,n))throw new Error("invalid format: "+u.close);if(e=t,!((""!==(r=a)&&" "!=r||""!==e&&" "!=e)&&e!==r||f))return w(),c?(g=n,"break"):(g=n,"continue")}-1===i&&(i=v-1),h.push(p.slice(n,i+1).join("")),g=n=i}(A),A=g;if("break"===y)break}return h.length&&m.push(h.join("")),m}function C(n){return M(n,",")}function F(n){var e=/([^(]*)\(([\s\S]*)\)([\s\S]*)/g.exec(n);return!e||e.length<4?{}:{prefix:e[1],value:e[2],suffix:e[3]}}function N(n){var e=/^([^\d|e|\-|\+]*)((?:\d|\.|-|e-|e\+)+)(\S*)$/g.exec(n);if(!e)return{prefix:"",unit:"",value:NaN};var r=e[1],t=e[2];return{prefix:r,unit:e[3],value:parseFloat(t)}}function O(){return Date.now?Date.now():(new Date).getTime()}function I(n,e,r){void 0===r&&(r=-1);for(var t=n.length,i=0;i<t;++i)if(e(n[i],i,n))return i;return r}function T(n,e,r){void 0===r&&(r=-1);for(var t=n.length-1;0<=t;--t)if(e(n[t],t,n))return t;return r}function L(n,e,r){var t=I(n,e);return-1<t?n[t]:r}var k=function(){var r=O(),n=a&&(window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.msRequestAnimationFrame);return n?n.bind(window):function(n){var e=O();return window.setTimeout(function(){n(e-r)},1e3/60)}}();function j(n,e,r){return Math.max(e,Math.min(n,r))}function B(n,r,t){return[[P(r[0],g),P(r[0]*n[1]/n[0],g)],[P(r[1]*n[0]/n[1],g),P(r[1],g)]].filter(function(n){return n.every(function(n,e){return t?n<=r[e]:n>=r[e]})})[0]||n}function D(n){for(var e=n.length,r=0,t=e-1;0<=t;--t)r+=n[t];return e?r/e:0}function z(n,e){var r=e[0]-n[0],t=e[1]-n[1],i=Math.atan2(t,r);return 0<=i?i:i+2*Math.PI}function U(n){return[0,1].map(function(e){return D(n.map(function(n){return n[e]}))})}function P(n,e){return e?Math.round(n/e)*e:n}function q(n){return n.replace("#","")}function H(n){var e=q(n),r=parseInt(e.substring(0,2),16),t=parseInt(e.substring(2,4),16),i=parseInt(e.substring(4,6),16),o=parseInt(e.substring(6,8),16)/255;return isNaN(o)&&(o=1),[r,t,i,o]}function _(n){var e=n.charAt(1),r=n.charAt(2),t=n.charAt(3),i=n.charAt(4);return["#",e,e,r,r,t,t,i,i].join("")}function G(n){var e,r=n[0],t=n[1],i=n[2];r<0&&(r+=360*Math.floor((Math.abs(r)+360)/360)),r%=360;var o=(1-Math.abs(2*i-1))*t,u=o*(1-Math.abs(r/60%2-1)),a=i-o/2,c=r<60?[o,u,0]:r<120?[u,o,0]:r<180?[0,o,u]:r<240?[0,u,o]:r<300?[u,0,o]:r<360?[o,0,u]:[0,0,0];return[Math.round(255*(c[0]+a)),Math.round(255*(c[1]+a)),Math.round(255*(c[2]+a)),null!==(e=n[3])&&void 0!==e?e:1]}return{RGB:c,RGBA:f,HSL:s,HSLA:l,COLOR_MODELS:e,FUNCTION:r,PROPERTY:"property",ARRAY:"array",OBJECT:t,STRING:i,NUMBER:o,UNDEFINED:u,IS_WINDOW:a,document:d,getCrossBrowserProperty:n,TRANSFORM:v,FILTER:m,ANIMATION:h,KEYFRAMES:w,OPEN_CLOSED_CHARACTERS:b,TINY_NUM:g,DEFAULT_UNIT_PRESETS:A,cutHex:q,hexToRGBA:H,toFullHex:_,hslToRGBA:G,stringToRGBA:function(n){if("#"===n.charAt(0))return 4===n.length||5===n.length?H(_(n)):H(n);if(-1!==n.indexOf("(")){var e=F(n),r=e.prefix,t=e.value;if(!r||!t)return;var i=C(t),o=[0,0,0,1],u=i.length;switch(r){case c:case f:for(var a=0;a<u;++a)o[a]=parseFloat(i[a]);return o;case s:case l:for(a=0;a<u;++a)-1!==i[a].indexOf("%")?o[a]=parseFloat(i[a])/100:o[a]=parseFloat(i[a]);return G(o)}}},dot:function(n,e,r,t){return(n*t+e*r)/(r+t)},isUndefined:function(n){return typeof n==u},isObject:y,isArray:function(n){return Array.isArray(n)},isString:x,isNumber:function(n){return typeof n==o},isFunction:S,splitText:M,splitSpace:function(n){return M(n,"")},splitComma:C,splitBracket:F,splitUnit:N,camelize:function(n){return n.replace(/[\s-_]([a-z])/g,function(n,e){return e.toUpperCase()})},decamelize:function(n,t){return void 0===t&&(t="-"),n.replace(/([a-z])([A-Z])/g,function(n,e,r){return""+e+t+r.toLowerCase()})},toArray:function(n){return[].slice.call(n)},now:O,findIndex:I,findLastIndex:T,findLast:function(n,e,r){var t=T(n,e);return-1<t?n[t]:r},find:L,requestAnimationFrame:k,cancelAnimationFrame:function(){var n=a&&(window.cancelAnimationFrame||window.webkitCancelAnimationFrame||window.mozCancelAnimationFrame||window.msCancelAnimationFrame);return n?n.bind(window):function(n){clearTimeout(n)}}(),getKeys:function(n){if(Object.keys)return Object.keys(n);var e=[];for(var r in e)e.push(r);return e},sortOrders:function(n,i){void 0===i&&(i=[]),n.sort(function(n,e){var r=i.indexOf(n),t=i.indexOf(e);return-1===t&&-1===r?0:-1===r?1:-1===t?-1:r-t})},convertUnitSize:function(n,e){var r=N(n),t=r.value,i=r.unit;if(y(e)){var o=e[i];if(o){if(S(o))return o(t);if(A[i])return A[i](t,o)}}else if("%"===i)return t*e/100;return A[i]?A[i](t):t},between:j,checkBoundSize:B,calculateBoundSize:function(n,r,t,e){if(!e)return n.map(function(n,e){return j(n,r[e],t[e])});var i=n[0],o=n[1],u=B(n,r,!1),a=u[0],c=u[1],f=B(n,t,!0),s=f[0],l=f[1];return i<a||o<c?(i=a,o=c):(s<i||l<o)&&(i=s,o=l),[i,o]},sum:function(n){for(var e=0,r=n.length-1;0<=r;--r)e+=n[r];return e},average:D,getRad:z,getCenterPoint:U,getShapeDirection:function(n){var e=U(n),r=z(e,n[0]),t=z(e,n[1]);return r<t&&t-r<Math.PI||t<r&&t-r<-Math.PI?1:-1},getDist:function(n,e){return Math.sqrt(Math.pow((e?e[0]:0)-n[0],2)+Math.pow((e?e[1]:0)-n[1],2))},throttle:P,throttleArray:function(r,t){return r.forEach(function(n,e){r[e]=P(r[e],t)}),r},counter:function(n){for(var e=[],r=0;r<n;++r)e.push(r);return e},replaceOnce:function(n,e,r){var t=!1;return n.replace(e,function(){for(var n=[],e=0;e<arguments.length;e++)n[e]=arguments[e];return t?n[0]:(t=!0,x(r)?r:r.apply(void 0,n))})},$:function(n,e){return e?d.querySelectorAll(n):d.querySelector(n)},hasClass:function(n,e){return n.classList?n.classList.contains(e):!!n.className.match(new RegExp("(\\s|^)"+e+"(\\s|$)"))},addClass:function(n,e){n.classList?n.classList.add(e):n.className+=" "+e},removeClass:function(n,e){var r;n.classList?n.classList.remove(e):(r=new RegExp("(\\s|^)"+e+"(\\s|$)"),n.className=n.className.replace(r," "))},fromCSS:function(n,e){if(!n||!e||!e.length)return{};var r;if(n instanceof Element)r=n;else{if(!n.length)return{};r=n[0]}for(var t={},i=window.getComputedStyle(r),o=e.length,u=0;u<o;++u)t[e[u]]=i[e[u]];return t},addEvent:function(n,e,r,t){n.addEventListener(e,r,t)},removeEvent:function(n,e,r,t){n.removeEventListener(e,r,t)}}}); | ||
//# sourceMappingURL=utils.min.js.map |
{ | ||
"name": "@daybrush/utils", | ||
"version": "1.6.0-beta.1", | ||
"version": "1.6.0", | ||
"description": "utils for daybrush", | ||
@@ -5,0 +5,0 @@ "main": "dist/utils.js", |
@@ -461,2 +461,23 @@ import { | ||
/** | ||
* Returns the value of the reverse direction element in the array that satisfies the provided testing function. | ||
* @function | ||
* @memberof CrossBrowser | ||
* @param - The array `findLast` was called upon. | ||
* @param - A function to execute on each value in the array, | ||
* @param - Returns defalutValue if not found by the function. | ||
* @example | ||
import { find } from "@daybrush/utils"; | ||
find([{a: 1}, {a: 2}, {a: 3}, {a: 4}], ({ a }) => a === 2); // {a: 2} | ||
*/ | ||
export function findLast<T>( | ||
arr: T[], | ||
callback: (element: T, index: number, arr: T[]) => any, | ||
defalutValue?: T, | ||
): T | undefined { | ||
const index = findLastIndex(arr, callback); | ||
return index > - 1 ? arr[index] : defalutValue; | ||
} | ||
/** | ||
* Returns the value of the first element in the array that satisfies the provided testing function. | ||
@@ -740,3 +761,7 @@ * @function | ||
export function counter(num: number) { | ||
/** | ||
* @function | ||
* @memberof Utils | ||
*/ | ||
export function counter(num: number): number[] { | ||
const nums: number[] = []; | ||
@@ -750,1 +775,16 @@ | ||
} | ||
/** | ||
* @function | ||
* @memberof Utils | ||
*/ | ||
export function replaceOnce(text: string, fromText: RegExp | string, toText: string | ((...args: any[]) => string)): string { | ||
let isOnce = false; | ||
return text.replace(fromText, (...args: any[]) => { | ||
if (isOnce) { | ||
return args[0]; | ||
} | ||
isOnce = true; | ||
return isString(toText) ? toText : toText(...args); | ||
}); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
318198
4223
0