Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@daybrush/utils

Package Overview
Dependencies
Maintainers
1
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@daybrush/utils - npm Package Compare versions

Comparing version 1.5.0 to 1.6.0-beta.0

4

declaration/consts.d.ts

@@ -1,2 +0,2 @@

import { IObject } from "./types";
import { IObject, OpenCloseCharacter } from "./types";
export declare const RGB = "rgb";

@@ -22,4 +22,4 @@ export declare const RGBA = "rgba";

export declare const KEYFRAMES: string;
export declare const OPEN_CLOSED_CHARACTER: string[];
export declare const OPEN_CLOSED_CHARACTERS: OpenCloseCharacter[];
export declare const TINY_NUM = 1e-7;
export declare const DEFAULT_UNIT_PRESETS: IObject<(pos: number, size?: number) => number>;

@@ -11,1 +11,13 @@ export interface IArrayFormat<T> {

}
export interface OpenCloseCharacter {
open: string;
close: string;
ignore?: RegExp;
}
export interface SplitOptions {
separator?: string;
isSeparateFirst?: boolean;
isSeparateOnlyOpenClose?: boolean;
isSeparateOpenClose?: boolean;
openCloseCharacters?: OpenCloseCharacter[];
}

@@ -1,2 +0,2 @@

import { IArrayFormat, IObject } from "./types";
import { IArrayFormat, IObject, SplitOptions } from "./types";
export declare function dot(a1: number, a2: number, b1: number, b2: number): number;

@@ -9,3 +9,3 @@ export declare function isUndefined(value: any): value is undefined;

export declare function isFunction(value: any): value is (...args: any[]) => any;
export declare function splitText(text: string, separator: string): string[];
export declare function splitText(text: string, splitOptions: string | SplitOptions): string[];
export declare function splitSpace(text: string): string[];

@@ -49,1 +49,2 @@ export declare function splitComma(text: string): string[];

export declare function throttleArray(nums: number[], unit?: number): number[];
export declare function counter(num: number): number[];

@@ -7,3 +7,3 @@ /*

repository: https://github.com/daybrush/utils
@version 1.5.0
@version 1.6.0-beta.0
*/

@@ -237,3 +237,18 @@ /**

var KEYFRAMES = /*#__PURE__*/ANIMATION.replace("animation", "keyframes");
var OPEN_CLOSED_CHARACTER = ["\"", "'", "\\\"", "\\'"];
var OPEN_CLOSED_CHARACTERS = [{
open: "(",
close: ")"
}, {
open: "\"",
close: "\""
}, {
open: "'",
close: "'"
}, {
open: "\\\"",
close: "\\\""
}, {
open: "\\'",
close: "\\'"
}];
var TINY_NUM = 0.0000001;

@@ -402,23 +417,64 @@ var DEFAULT_UNIT_PRESETS = {

function findClosed(closedCharacter, texts, index, length) {
for (var i = index; i < length; ++i) {
function isEqualSeparator(character, separator) {
var isCharacterSpace = character === "" || character == " ";
var isSeparatorSpace = separator === "" || separator == " ";
return isSeparatorSpace && isCharacterSpace || character === separator;
}
function findOpen(openCharacter, texts, index, length, openCloseCharacters) {
var isIgnore = findIgnore(openCharacter, texts, index);
if (!isIgnore) {
return findClose(openCharacter, texts, index + 1, length, openCloseCharacters);
}
return index;
}
function findIgnore(character, texts, index) {
if (!character.ignore) {
return null;
}
var otherText = texts.slice(Math.max(index - 3, 0), index + 3).join("");
return new RegExp(character.ignore).exec(otherText);
}
function findClose(closeCharacter, texts, index, length, openCloseCharacters) {
var _loop_1 = function (i) {
var character = texts[i].trim();
if (character === closedCharacter) {
return i;
if (character === closeCharacter.close && !findIgnore(closeCharacter, texts, i)) {
return {
value: i
};
}
var nextIndex = i;
var nextIndex = i; // re open
if (character === "(") {
nextIndex = findClosed(")", texts, i + 1, length);
} else if (OPEN_CLOSED_CHARACTER.indexOf(character) > -1) {
nextIndex = findClosed(character, texts, i + 1, length);
var openCharacter = find(openCloseCharacters, function (_a) {
var open = _a.open;
return open === character;
});
if (openCharacter) {
nextIndex = findOpen(openCharacter, texts, i, length, openCloseCharacters);
}
if (nextIndex === -1) {
break;
return out_i_1 = i, "break";
}
i = nextIndex;
out_i_1 = i;
};
var out_i_1;
for (var i = index; i < length; ++i) {
var state_1 = _loop_1(i);
i = out_i_1;
if (typeof state_1 === "object") return state_1.value;
if (state_1 === "break") break;
}

@@ -429,4 +485,26 @@

function splitText(text, separator) {
var regexText = "(\\s*" + (separator || ",") + "\\s*|\\(|\\)|\"|'|\\\\\"|\\\\'|\\s+)";
function splitText(text, splitOptions) {
var _a = isString(splitOptions) ? {
separator: splitOptions
} : splitOptions,
_b = _a.separator,
separator = _b === void 0 ? "," : _b,
isSeparateFirst = _a.isSeparateFirst,
isSeparateOnlyOpenClose = _a.isSeparateOnlyOpenClose,
_c = _a.isSeparateOpenClose,
isSeparateOpenClose = _c === void 0 ? isSeparateOnlyOpenClose : _c,
_d = _a.openCloseCharacters,
openCloseCharacters = _d === void 0 ? OPEN_CLOSED_CHARACTERS : _d;
var openClosedText = openCloseCharacters.map(function (_a) {
var open = _a.open,
close = _a.close;
if (open === close) {
return open;
}
return open + "|" + close;
}).join("|");
var regexText = "(\\s*" + separator + "\\s*|" + openClosedText + "|\\s+)";
var regex = new RegExp(regexText, "g");

@@ -438,19 +516,51 @@ var texts = text.split(regex).filter(Boolean);

for (var i = 0; i < length; ++i) {
function resetTemp() {
if (tempValues.length) {
values.push(tempValues.join(""));
tempValues = [];
return true;
}
return false;
}
var _loop_2 = function (i) {
var character = texts[i].trim();
var nextIndex = i;
var openCharacter = find(openCloseCharacters, function (_a) {
var open = _a.open;
return open === character;
});
var closeCharacter = find(openCloseCharacters, function (_a) {
var close = _a.close;
return close === character;
});
if (character === "(") {
nextIndex = findClosed(")", texts, i + 1, length);
} else if (character === ")") {
throw new Error("invalid format");
} else if (OPEN_CLOSED_CHARACTER.indexOf(character) > -1) {
nextIndex = findClosed(character, texts, i + 1, length);
} else if (character === separator) {
if (tempValues.length) {
values.push(tempValues.join(""));
tempValues = [];
if (openCharacter) {
nextIndex = findOpen(openCharacter, texts, i, length, openCloseCharacters);
if (nextIndex !== -1 && isSeparateOpenClose) {
if (resetTemp() && isSeparateFirst) {
return out_i_2 = i, "break";
}
values.push(texts.slice(i, nextIndex + 1).join(""));
i = nextIndex;
if (isSeparateFirst) {
return out_i_2 = i, "break";
}
return out_i_2 = i, "continue";
}
} else if (closeCharacter && !findIgnore(closeCharacter, texts, i)) {
throw new Error("invalid format: " + closeCharacter.close);
} else if (isEqualSeparator(character, separator) && !isSeparateOnlyOpenClose) {
resetTemp();
continue;
if (isSeparateFirst) {
return out_i_2 = i, "break";
}
return out_i_2 = i, "continue";
}

@@ -464,2 +574,12 @@

i = nextIndex;
out_i_2 = i;
};
var out_i_2;
for (var i = 0; i < length; ++i) {
var state_2 = _loop_2(i);
i = out_i_2;
if (state_2 === "break") break;
}

@@ -488,3 +608,3 @@

function splitSpace(text) {
// divide comma(,)
// divide comma(space)
return splitText(text, "");

@@ -965,3 +1085,12 @@ }

}
function counter(num) {
var nums = [];
for (var i = 0; i < num; ++i) {
nums.push(i);
}
return nums;
}
/**

@@ -1292,3 +1421,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_CHARACTER, 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, find, requestAnimationFrame, cancelAnimationFrame, getKeys, sortOrders, convertUnitSize, between, checkBoundSize, calculateBoundSize, sum, average, getRad, getCenterPoint, getShapeDirection, getDist, throttle, throttleArray, $, 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, find, requestAnimationFrame, cancelAnimationFrame, getKeys, sortOrders, convertUnitSize, between, checkBoundSize, calculateBoundSize, sum, average, getRad, getCenterPoint, getShapeDirection, getDist, throttle, throttleArray, counter, $, hasClass, addClass, removeClass, fromCSS, addEvent, removeEvent };
//# sourceMappingURL=utils.esm.js.map

@@ -7,3 +7,3 @@ /*

repository: https://github.com/daybrush/utils
@version 1.5.0
@version 1.6.0-beta.0
*/

@@ -243,3 +243,18 @@ (function (global, factory) {

var KEYFRAMES = /*#__PURE__*/ANIMATION.replace("animation", "keyframes");
var OPEN_CLOSED_CHARACTER = ["\"", "'", "\\\"", "\\'"];
var OPEN_CLOSED_CHARACTERS = [{
open: "(",
close: ")"
}, {
open: "\"",
close: "\""
}, {
open: "'",
close: "'"
}, {
open: "\\\"",
close: "\\\""
}, {
open: "\\'",
close: "\\'"
}];
var TINY_NUM = 0.0000001;

@@ -408,23 +423,64 @@ var DEFAULT_UNIT_PRESETS = {

function findClosed(closedCharacter, texts, index, length) {
for (var i = index; i < length; ++i) {
function isEqualSeparator(character, separator) {
var isCharacterSpace = character === "" || character == " ";
var isSeparatorSpace = separator === "" || separator == " ";
return isSeparatorSpace && isCharacterSpace || character === separator;
}
function findOpen(openCharacter, texts, index, length, openCloseCharacters) {
var isIgnore = findIgnore(openCharacter, texts, index);
if (!isIgnore) {
return findClose(openCharacter, texts, index + 1, length, openCloseCharacters);
}
return index;
}
function findIgnore(character, texts, index) {
if (!character.ignore) {
return null;
}
var otherText = texts.slice(Math.max(index - 3, 0), index + 3).join("");
return new RegExp(character.ignore).exec(otherText);
}
function findClose(closeCharacter, texts, index, length, openCloseCharacters) {
var _loop_1 = function (i) {
var character = texts[i].trim();
if (character === closedCharacter) {
return i;
if (character === closeCharacter.close && !findIgnore(closeCharacter, texts, i)) {
return {
value: i
};
}
var nextIndex = i;
var nextIndex = i; // re open
if (character === "(") {
nextIndex = findClosed(")", texts, i + 1, length);
} else if (OPEN_CLOSED_CHARACTER.indexOf(character) > -1) {
nextIndex = findClosed(character, texts, i + 1, length);
var openCharacter = find(openCloseCharacters, function (_a) {
var open = _a.open;
return open === character;
});
if (openCharacter) {
nextIndex = findOpen(openCharacter, texts, i, length, openCloseCharacters);
}
if (nextIndex === -1) {
break;
return out_i_1 = i, "break";
}
i = nextIndex;
out_i_1 = i;
};
var out_i_1;
for (var i = index; i < length; ++i) {
var state_1 = _loop_1(i);
i = out_i_1;
if (typeof state_1 === "object") return state_1.value;
if (state_1 === "break") break;
}

@@ -435,4 +491,26 @@

function splitText(text, separator) {
var regexText = "(\\s*" + (separator || ",") + "\\s*|\\(|\\)|\"|'|\\\\\"|\\\\'|\\s+)";
function splitText(text, splitOptions) {
var _a = isString(splitOptions) ? {
separator: splitOptions
} : splitOptions,
_b = _a.separator,
separator = _b === void 0 ? "," : _b,
isSeparateFirst = _a.isSeparateFirst,
isSeparateOnlyOpenClose = _a.isSeparateOnlyOpenClose,
_c = _a.isSeparateOpenClose,
isSeparateOpenClose = _c === void 0 ? isSeparateOnlyOpenClose : _c,
_d = _a.openCloseCharacters,
openCloseCharacters = _d === void 0 ? OPEN_CLOSED_CHARACTERS : _d;
var openClosedText = openCloseCharacters.map(function (_a) {
var open = _a.open,
close = _a.close;
if (open === close) {
return open;
}
return open + "|" + close;
}).join("|");
var regexText = "(\\s*" + separator + "\\s*|" + openClosedText + "|\\s+)";
var regex = new RegExp(regexText, "g");

@@ -444,19 +522,51 @@ var texts = text.split(regex).filter(Boolean);

for (var i = 0; i < length; ++i) {
function resetTemp() {
if (tempValues.length) {
values.push(tempValues.join(""));
tempValues = [];
return true;
}
return false;
}
var _loop_2 = function (i) {
var character = texts[i].trim();
var nextIndex = i;
var openCharacter = find(openCloseCharacters, function (_a) {
var open = _a.open;
return open === character;
});
var closeCharacter = find(openCloseCharacters, function (_a) {
var close = _a.close;
return close === character;
});
if (character === "(") {
nextIndex = findClosed(")", texts, i + 1, length);
} else if (character === ")") {
throw new Error("invalid format");
} else if (OPEN_CLOSED_CHARACTER.indexOf(character) > -1) {
nextIndex = findClosed(character, texts, i + 1, length);
} else if (character === separator) {
if (tempValues.length) {
values.push(tempValues.join(""));
tempValues = [];
if (openCharacter) {
nextIndex = findOpen(openCharacter, texts, i, length, openCloseCharacters);
if (nextIndex !== -1 && isSeparateOpenClose) {
if (resetTemp() && isSeparateFirst) {
return out_i_2 = i, "break";
}
values.push(texts.slice(i, nextIndex + 1).join(""));
i = nextIndex;
if (isSeparateFirst) {
return out_i_2 = i, "break";
}
return out_i_2 = i, "continue";
}
} else if (closeCharacter && !findIgnore(closeCharacter, texts, i)) {
throw new Error("invalid format: " + closeCharacter.close);
} else if (isEqualSeparator(character, separator) && !isSeparateOnlyOpenClose) {
resetTemp();
continue;
if (isSeparateFirst) {
return out_i_2 = i, "break";
}
return out_i_2 = i, "continue";
}

@@ -470,2 +580,12 @@

i = nextIndex;
out_i_2 = i;
};
var out_i_2;
for (var i = 0; i < length; ++i) {
var state_2 = _loop_2(i);
i = out_i_2;
if (state_2 === "break") break;
}

@@ -494,3 +614,3 @@

function splitSpace(text) {
// divide comma(,)
// divide comma(space)
return splitText(text, "");

@@ -971,3 +1091,12 @@ }

}
function counter(num) {
var nums = [];
for (var i = 0; i < num; ++i) {
nums.push(i);
}
return nums;
}
/**

@@ -1320,3 +1449,3 @@ * @namespace

KEYFRAMES: KEYFRAMES,
OPEN_CLOSED_CHARACTER: OPEN_CLOSED_CHARACTER,
OPEN_CLOSED_CHARACTERS: OPEN_CLOSED_CHARACTERS,
TINY_NUM: TINY_NUM,

@@ -1363,2 +1492,3 @@ DEFAULT_UNIT_PRESETS: DEFAULT_UNIT_PRESETS,

throttleArray: throttleArray,
counter: counter,
$: $,

@@ -1365,0 +1495,0 @@ hasClass: hasClass,

@@ -7,5 +7,5 @@ /*

repository: https://github.com/daybrush/utils
@version 1.5.0
@version 1.6.0-beta.0
*/
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):n.utils=t()}(this,function(){"use strict";function n(n){if(!d)return"";var t=(d.body||d.documentElement).style,e=m.length;if(typeof t[n]!=u)return n;for(var r=0;r<e;++r){var i="-"+m[r]+"-"+n;if(typeof t[i]!=u)return i}return""}var f="rgb",c="rgba",s="hsl",l="hsla",t=[f,c,s,l],e="function",r="object",i="string",o="number",u="undefined",a=typeof window!=u,d=typeof document!=u&&document,m=["webkit","ms","moz","o"],v=n("transform"),h=n("filter"),p=n("animation"),w=p.replace("animation","keyframes"),g=['"',"'",'\\"',"\\'"],A=1e-7,y={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,t){return n*t/100},vw:function(n,t){return void 0===t&&(t=window.innerWidth),n/100*t},vh:function(n,t){return void 0===t&&(t=window.innerHeight),n/100*t},vmax:function(n,t){return void 0===t&&(t=Math.max(window.innerWidth,window.innerHeight)),n/100*t},vmin:function(n,t){return void 0===t&&(t=Math.min(window.innerWidth,window.innerHeight)),n/100*t}};function x(n){return n&&typeof n===r}function E(n){return typeof n===e}function R(n,t,e,r){for(var i=e;i<r;++i){var o=t[i].trim();if(o===n)return i;var u=i;if("("===o?u=R(")",t,i+1,r):-1<g.indexOf(o)&&(u=R(o,t,i+1,r)),-1===u)break;i=u}return-1}function M(n,t){for(var e=new RegExp("(\\s*"+(t||",")+"\\s*|\\(|\\)|\"|'|\\\\\"|\\\\'|\\s+)","g"),r=n.split(e).filter(Boolean),i=r.length,o=[],u=[],a=0;a<i;++a){var f=r[a].trim(),c=a;if("("===f)c=R(")",r,a+1,i);else{if(")"===f)throw new Error("invalid format");if(-1<g.indexOf(f))c=R(f,r,a+1,i);else if(f===t){u.length&&(o.push(u.join("")),u=[]);continue}}-1===c&&(c=i-1),u.push(r.slice(a,c+1).join("")),a=c}return u.length&&o.push(u.join("")),o}function b(n){return M(n,",")}function S(n){var t=/([^(]*)\(([\s\S]*)\)([\s\S]*)/g.exec(n);return!t||t.length<4?{}:{prefix:t[1],value:t[2],suffix:t[3]}}function N(n){var t=/^([^\d|e|\-|\+]*)((?:\d|\.|-|e-|e\+)+)(\S*)$/g.exec(n);if(!t)return{prefix:"",unit:"",value:NaN};var e=t[1],r=t[2];return{prefix:e,unit:t[3],value:parseFloat(r)}}function F(){return Date.now?Date.now():(new Date).getTime()}function O(n,t,e){void 0===e&&(e=-1);for(var r=n.length,i=0;i<r;++i)if(t(n[i],i,n))return i;return e}var C=function(){var e=F(),n=a&&(window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.msRequestAnimationFrame);return n?n.bind(window):function(n){var t=F();return window.setTimeout(function(){n(t-e)},1e3/60)}}();function T(n,t,e){return Math.max(t,Math.min(n,e))}function I(n,e,r){return[[z(e[0],A),z(e[0]*n[1]/n[0],A)],[z(e[1]*n[0]/n[1],A),z(e[1],A)]].filter(function(n){return n.every(function(n,t){return r?n<=e[t]:n>=e[t]})})[0]||n}function L(n){for(var t=n.length,e=0,r=t-1;0<=r;--r)e+=n[r];return t?e/t:0}function B(n,t){var e=t[0]-n[0],r=t[1]-n[1],i=Math.atan2(r,e);return 0<=i?i:i+2*Math.PI}function D(n){return[0,1].map(function(t){return L(n.map(function(n){return n[t]}))})}function z(n,t){return t?Math.round(n/t)*t:n}function U(n){return n.replace("#","")}function j(n){var t=U(n),e=parseInt(t.substring(0,2),16),r=parseInt(t.substring(2,4),16),i=parseInt(t.substring(4,6),16),o=parseInt(t.substring(6,8),16)/255;return isNaN(o)&&(o=1),[e,r,i,o]}function k(n){var t=n.charAt(1),e=n.charAt(2),r=n.charAt(3),i=n.charAt(4);return["#",t,t,e,e,r,r,i,i].join("")}function P(n){var t,e=n[0],r=n[1],i=n[2];e<0&&(e+=360*Math.floor((Math.abs(e)+360)/360)),e%=360;var o=(1-Math.abs(2*i-1))*r,u=o*(1-Math.abs(e/60%2-1)),a=i-o/2,f=e<60?[o,u,0]:e<120?[u,o,0]:e<180?[0,o,u]:e<240?[0,u,o]:e<300?[u,0,o]:e<360?[o,0,u]:[0,0,0];return[Math.round(255*(f[0]+a)),Math.round(255*(f[1]+a)),Math.round(255*(f[2]+a)),null!==(t=n[3])&&void 0!==t?t:1]}return{RGB:f,RGBA:c,HSL:s,HSLA:l,COLOR_MODELS:t,FUNCTION:e,PROPERTY:"property",ARRAY:"array",OBJECT:r,STRING:i,NUMBER:o,UNDEFINED:u,IS_WINDOW:a,document:d,getCrossBrowserProperty:n,TRANSFORM:v,FILTER:h,ANIMATION:p,KEYFRAMES:w,OPEN_CLOSED_CHARACTER:g,TINY_NUM:A,DEFAULT_UNIT_PRESETS:y,cutHex:U,hexToRGBA:j,toFullHex:k,hslToRGBA:P,stringToRGBA:function(n){if("#"===n.charAt(0))return 4===n.length||5===n.length?j(k(n)):j(n);if(-1!==n.indexOf("(")){var t=S(n),e=t.prefix,r=t.value;if(!e||!r)return;var i=b(r),o=[0,0,0,1],u=i.length;switch(e){case f:case c: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 P(o)}}},dot:function(n,t,e,r){return(n*r+t*e)/(e+r)},isUndefined:function(n){return typeof n==u},isObject:x,isArray:function(n){return Array.isArray(n)},isString:function(n){return typeof n==i},isNumber:function(n){return typeof n==o},isFunction:E,splitText:M,splitSpace:function(n){return M(n,"")},splitComma:b,splitBracket:S,splitUnit:N,camelize:function(n){return n.replace(/[\s-_]([a-z])/g,function(n,t){return t.toUpperCase()})},decamelize:function(n,r){return void 0===r&&(r="-"),n.replace(/([a-z])([A-Z])/g,function(n,t,e){return""+t+r+e.toLowerCase()})},toArray:function(n){return[].slice.call(n)},now:F,findIndex:O,find:function(n,t,e){var r=O(n,t);return-1<r?n[r]:e},requestAnimationFrame:C,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 t=[];for(var e in t)t.push(e);return t},sortOrders:function(n,i){void 0===i&&(i=[]),n.sort(function(n,t){var e=i.indexOf(n),r=i.indexOf(t);return-1===r&&-1===e?0:-1===e?1:-1===r?-1:e-r})},convertUnitSize:function(n,t){var e=N(n),r=e.value,i=e.unit;if(x(t)){var o=t[i];if(o){if(E(o))return o(r);if(y[i])return y[i](r,o)}}else if("%"===i)return r*t/100;return y[i]?y[i](r):r},between:T,checkBoundSize:I,calculateBoundSize:function(n,e,r,t){if(!t)return n.map(function(n,t){return T(n,e[t],r[t])});var i=n[0],o=n[1],u=I(n,e,!1),a=u[0],f=u[1],c=I(n,r,!0),s=c[0],l=c[1];return i<a||o<f?(i=a,o=f):(s<i||l<o)&&(i=s,o=l),[i,o]},sum:function(n){for(var t=0,e=n.length-1;0<=e;--e)t+=n[e];return t},average:L,getRad:B,getCenterPoint:D,getShapeDirection:function(n){var t=D(n),e=B(t,n[0]),r=B(t,n[1]);return e<r&&r-e<Math.PI||r<e&&r-e<-Math.PI?1:-1},getDist:function(n,t){return Math.sqrt(Math.pow((t?t[0]:0)-n[0],2)+Math.pow((t?t[1]:0)-n[1],2))},throttle:z,throttleArray:function(e,r){return e.forEach(function(n,t){e[t]=z(e[t],r)}),e},$:function(n,t){return t?d.querySelectorAll(n):d.querySelector(n)},hasClass:function(n,t){return n.classList?n.classList.contains(t):!!n.className.match(new RegExp("(\\s|^)"+t+"(\\s|$)"))},addClass:function(n,t){n.classList?n.classList.add(t):n.className+=" "+t},removeClass:function(n,t){var e;n.classList?n.classList.remove(t):(e=new RegExp("(\\s|^)"+t+"(\\s|$)"),n.className=n.className.replace(e," "))},fromCSS:function(n,t){if(!n||!t||!t.length)return{};var e;if(n instanceof Element)e=n;else{if(!n.length)return{};e=n[0]}for(var r={},i=window.getComputedStyle(e),o=t.length,u=0;u<o;++u)r[t[u]]=i[t[u]];return r},addEvent:function(n,t,e,r){n.addEventListener(t,e,r)},removeEvent:function(n,t,e,r){n.removeEventListener(t,e,r)}}});
!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,t=p.length;if(typeof e[n]!=u)return n;for(var r=0;r<t;++r){var i="-"+p[r]+"-"+n;if(typeof e[i]!=u)return i}return""}var c="rgb",f="rgba",s="hsl",l="hsla",e=[c,f,s,l],t="function",r="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===r}function S(n){return typeof n===i}function x(n){return typeof n===t}function E(n,e,t,r,i){return R(n,e,t)?t:function(i,o,n,u,a){for(var c,e=n;e<u;++e){var t=function(n){var e=o[n].trim();if(e===i.close&&!R(i,o,n))return{value:n};var t=n,r=I(a,function(n){return n.open===e});if(r&&(t=E(r,o,n,u,a)),-1===t)return c=n,"break";c=n=t}(e);if(e=c,"object"==typeof t)return t.value;if("break"===t)break}return-1}(n,e,t+1,r,i)}function R(n,e,t){if(!n.ignore)return null;var r=e.slice(Math.max(t-3,0),t+3).join("");return new RegExp(n.ignore).exec(r)}function M(n,e){var t=S(e)?{separator:e}:e,r=t.separator,a=void 0===r?",":r,c=t.isSeparateFirst,f=t.isSeparateOnlyOpenClose,i=t.isSeparateOpenClose,s=void 0===i?f:i,o=t.openCloseCharacters,l=void 0===o?y:o,u=l.map(function(n){var e=n.open,t=n.close;return e===t?e:e+"|"+t}).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,t,r=p[n].trim(),i=n,o=I(l,function(n){return n.open===r}),u=I(l,function(n){return n.close===r});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=r,!((""!==(t=a)&&" "!=t||""!==e&&" "!=e)&&e!==t||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 t=e[1],r=e[2];return{prefix:t,unit:e[3],value:parseFloat(r)}}function O(){return Date.now?Date.now():(new Date).getTime()}function T(n,e,t){void 0===t&&(t=-1);for(var r=n.length,i=0;i<r;++i)if(e(n[i],i,n))return i;return t}function I(n,e,t){var r=T(n,e);return-1<r?n[r]:t}var k=function(){var t=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-t)},1e3/60)}}();function L(n,e,t){return Math.max(e,Math.min(n,t))}function j(n,t,r){return[[U(t[0],g),U(t[0]*n[1]/n[0],g)],[U(t[1]*n[0]/n[1],g),U(t[1],g)]].filter(function(n){return n.every(function(n,e){return r?n<=t[e]:n>=t[e]})})[0]||n}function B(n){for(var e=n.length,t=0,r=e-1;0<=r;--r)t+=n[r];return e?t/e:0}function D(n,e){var t=e[0]-n[0],r=e[1]-n[1],i=Math.atan2(r,t);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),t=parseInt(e.substring(0,2),16),r=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),[t,r,i,o]}function H(n){var e=n.charAt(1),t=n.charAt(2),r=n.charAt(3),i=n.charAt(4);return["#",e,e,t,t,r,r,i,i].join("")}function _(n){var e,t=n[0],r=n[1],i=n[2];t<0&&(t+=360*Math.floor((Math.abs(t)+360)/360)),t%=360;var o=(1-Math.abs(2*i-1))*r,u=o*(1-Math.abs(t/60%2-1)),a=i-o/2,c=t<60?[o,u,0]:t<120?[u,o,0]:t<180?[0,o,u]:t<240?[0,u,o]:t<300?[u,0,o]:t<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:t,PROPERTY:"property",ARRAY:"array",OBJECT:r,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),t=e.prefix,r=e.value;if(!t||!r)return;var i=C(r),o=[0,0,0,1],u=i.length;switch(t){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,t,r){return(n*r+e*t)/(t+r)},isUndefined:function(n){return typeof n==u},isObject:b,isArray:function(n){return Array.isArray(n)},isString:S,isNumber:function(n){return typeof n==o},isFunction:x,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,r){return void 0===r&&(r="-"),n.replace(/([a-z])([A-Z])/g,function(n,e,t){return""+e+r+t.toLowerCase()})},toArray:function(n){return[].slice.call(n)},now:O,findIndex:T,find:I,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 t in e)e.push(t);return e},sortOrders:function(n,i){void 0===i&&(i=[]),n.sort(function(n,e){var t=i.indexOf(n),r=i.indexOf(e);return-1===r&&-1===t?0:-1===t?1:-1===r?-1:t-r})},convertUnitSize:function(n,e){var t=N(n),r=t.value,i=t.unit;if(b(e)){var o=e[i];if(o){if(x(o))return o(r);if(A[i])return A[i](r,o)}}else if("%"===i)return r*e/100;return A[i]?A[i](r):r},between:L,checkBoundSize:j,calculateBoundSize:function(n,t,r,e){if(!e)return n.map(function(n,e){return L(n,t[e],r[e])});var i=n[0],o=n[1],u=j(n,t,!1),a=u[0],c=u[1],f=j(n,r,!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,t=n.length-1;0<=t;--t)e+=n[t];return e},average:B,getRad:D,getCenterPoint:z,getShapeDirection:function(n){var e=z(n),t=D(e,n[0]),r=D(e,n[1]);return t<r&&r-t<Math.PI||r<t&&r-t<-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(t,r){return t.forEach(function(n,e){t[e]=U(t[e],r)}),t},counter:function(n){for(var e=[],t=0;t<n;++t)e.push(t);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 t;n.classList?n.classList.remove(e):(t=new RegExp("(\\s|^)"+e+"(\\s|$)"),n.className=n.className.replace(t," "))},fromCSS:function(n,e){if(!n||!e||!e.length)return{};var t;if(n instanceof Element)t=n;else{if(!n.length)return{};t=n[0]}for(var r={},i=window.getComputedStyle(t),o=e.length,u=0;u<o;++u)r[e[u]]=i[e[u]];return r},addEvent:function(n,e,t,r){n.addEventListener(e,t,r)},removeEvent:function(n,e,t,r){n.removeEventListener(e,t,r)}}});
//# sourceMappingURL=utils.min.js.map
{
"name": "@daybrush/utils",
"version": "1.5.0",
"version": "1.6.0-beta.0",
"description": "utils for daybrush",

@@ -5,0 +5,0 @@ "main": "dist/utils.js",

@@ -6,3 +6,3 @@ /**

import { IObject } from "./types";
import { IObject, OpenCloseCharacter } from "./types";

@@ -219,3 +219,9 @@ /**

export const OPEN_CLOSED_CHARACTER = [`"`, `'`, `\\"`, `\\'`];
export const OPEN_CLOSED_CHARACTERS: OpenCloseCharacter[] = [
{ open: "(", close: ")" },
{ open: `"`, close: `"`},
{ open: `'`, close: `'`},
{ open: `\\"`, close: `\\"`},
{ open: `\\'`, close: `\\'`},
];
export const TINY_NUM = 0.0000001;

@@ -222,0 +228,0 @@ export const DEFAULT_UNIT_PRESETS: IObject<(pos: number, size?: number) => number> = {

@@ -22,1 +22,17 @@

}
/**
* @typedef
*/
export interface OpenCloseCharacter {
open: string;
close: string;
ignore?: RegExp;
}
export interface SplitOptions {
separator?: string;
isSeparateFirst?: boolean;
isSeparateOnlyOpenClose?: boolean,
isSeparateOpenClose?: boolean,
openCloseCharacters?: OpenCloseCharacter[],
}
import {
UNDEFINED, STRING,
OBJECT, FUNCTION,
IS_WINDOW, OPEN_CLOSED_CHARACTER, NUMBER,
IS_WINDOW, OPEN_CLOSED_CHARACTERS, NUMBER,
DEFAULT_UNIT_PRESETS,
TINY_NUM
} from "./consts";
import { IArrayFormat, IObject } from "./types";
import { IArrayFormat, IObject, OpenCloseCharacter, SplitOptions } from "./types";
/**

@@ -117,15 +117,57 @@ * @namespace

}
function isEqualSeparator(
character: string,
separator: string,
) {
const isCharacterSpace = character === "" || character == " ";
const isSeparatorSpace = separator === "" || separator == " ";
function findClosed(closedCharacter: string, texts: string[], index: number, length: number) {
return (isSeparatorSpace && isCharacterSpace) || character === separator;
}
function findOpen(
openCharacter: OpenCloseCharacter,
texts: string[],
index: number,
length: number,
openCloseCharacters: OpenCloseCharacter[],
) {
const isIgnore = findIgnore(openCharacter, texts, index);
if (!isIgnore) {
return findClose(openCharacter, texts, index + 1, length, openCloseCharacters);
}
return index;
}
function findIgnore(
character: OpenCloseCharacter,
texts: string[],
index: number,
) {
if (!character.ignore) {
return null;
}
const otherText = texts.slice(Math.max(index - 3, 0), index + 3).join("");
return new RegExp(character.ignore).exec(otherText);
}
function findClose(
closeCharacter: OpenCloseCharacter,
texts: string[],
index: number,
length: number,
openCloseCharacters: OpenCloseCharacter[],
) {
for (let i = index; i < length; ++i) {
const character = texts[i].trim();
if (character === closedCharacter) {
if (character === closeCharacter.close && !findIgnore(closeCharacter, texts, i)) {
return i;
}
let nextIndex = i;
if (character === "(") {
nextIndex = findClosed(")", texts, i + 1, length);
} else if (OPEN_CLOSED_CHARACTER.indexOf(character) > -1) {
nextIndex = findClosed(character, texts, i + 1, length);
// re open
const openCharacter = find(openCloseCharacters, ({ open }) => open === character);
if (openCharacter) {
nextIndex = findOpen(openCharacter, texts, i, length, openCloseCharacters);
}

@@ -139,4 +181,23 @@ if (nextIndex === -1) {

}
export function splitText(text: string, separator: string) {
const regexText = `(\\s*${separator || ","}\\s*|\\(|\\)|"|'|\\\\"|\\\\'|\\s+)`;
export function splitText(
text: string,
splitOptions: string | SplitOptions,
) {
const {
separator = ",",
isSeparateFirst,
isSeparateOnlyOpenClose,
isSeparateOpenClose = isSeparateOnlyOpenClose,
openCloseCharacters = OPEN_CLOSED_CHARACTERS,
} = isString(splitOptions) ? {
separator: splitOptions,
} as SplitOptions : splitOptions;
const openClosedText = openCloseCharacters.map(({ open, close }) => {
if (open === close) {
return open;
}
return `${open}|${close}`;
}).join("|");
const regexText = `(\\s*${separator}\\s*|${openClosedText}|\\s+)`;
const regex = new RegExp(regexText, "g");

@@ -148,2 +209,11 @@ const texts = text.split(regex).filter(Boolean);

function resetTemp() {
if (tempValues.length) {
values.push(tempValues.join(""));
tempValues = [];
return true;
}
return false;
}
for (let i = 0; i < length; ++i) {

@@ -153,13 +223,28 @@ const character = texts[i].trim();

if (character === "(") {
nextIndex = findClosed(")", texts, i + 1, length);
} else if (character === ")") {
throw new Error("invalid format");
} else if (OPEN_CLOSED_CHARACTER.indexOf(character) > -1) {
nextIndex = findClosed(character, texts, i + 1, length);
} else if (character === separator) {
if (tempValues.length) {
values.push(tempValues.join(""));
tempValues = [];
const openCharacter = find(openCloseCharacters, ({ open }) => open === character);
const closeCharacter = find(openCloseCharacters, ({ close }) => close === character);
if (openCharacter) {
nextIndex = findOpen(openCharacter, texts, i, length, openCloseCharacters);
if (nextIndex !== -1 && isSeparateOpenClose) {
if (resetTemp() && isSeparateFirst) {
break;
}
values.push(texts.slice(i, nextIndex + 1).join(""));
i = nextIndex;
if (isSeparateFirst) {
break;
}
continue;
}
} else if (closeCharacter && !findIgnore(closeCharacter, texts, i)) {
throw new Error(`invalid format: ${closeCharacter.close}`);
} else if (isEqualSeparator(character, separator) && !isSeparateOnlyOpenClose) {
resetTemp();
if (isSeparateFirst) {
break;
}
continue;

@@ -194,3 +279,3 @@ }

export function splitSpace(text: string) {
// divide comma(,)
// divide comma(space)
return splitText(text, "");

@@ -631,1 +716,11 @@ }

}
export function counter(num: number) {
const nums: number[] = [];
for (let i = 0; i < num; ++i) {
nums.push(i);
}
return nums;
}

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc