@daybrush/utils
Advanced tools
Comparing version 0.10.1 to 0.10.2
@@ -21,1 +21,2 @@ export declare const RGB = "rgb"; | ||
export declare const KEYFRAMES: string; | ||
export declare const OPEN_CLOSED_CHARACTER: string[]; |
@@ -8,3 +8,3 @@ import { IArrayFormat, IObject } from "./types"; | ||
export declare function isFunction(value: any): value is (...args: any[]) => any; | ||
export declare function splitSpace(text: string): RegExpMatchArray; | ||
export declare function splitSpace(text: string): string[]; | ||
export declare function splitComma(text: string): string[]; | ||
@@ -11,0 +11,0 @@ export declare function splitBracket(text: string): { |
@@ -7,3 +7,3 @@ /* | ||
repository: https://github.com/daybrush/utils | ||
@version 0.10.1 | ||
@version 0.10.2 | ||
*/ | ||
@@ -247,2 +247,3 @@ /** | ||
ANIMATION.replace("animation", "keyframes"); | ||
var OPEN_CLOSED_CHARACTER = ["\"", "'", "\\\"", "\\'"]; | ||
@@ -358,2 +359,73 @@ /** | ||
} | ||
function throwInvalidFormatError() { | ||
throw new Error("Invalid Format"); | ||
} | ||
function findClosed(closedCharacter, texts, index, length) { | ||
for (var i = index; i < length; ++i) { | ||
var character = texts[i].trim(); | ||
if (character === closedCharacter) { | ||
return i; | ||
} | ||
var 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); | ||
} | ||
if (nextIndex === -1) { | ||
throwInvalidFormatError(); | ||
} | ||
i = nextIndex; | ||
} | ||
return -1; | ||
} | ||
function splitText(text, separator) { | ||
var texts = text.split(/(\s*,\s*|\(|\)|"|'|\\"|\\'|\s+)/g).filter(Boolean); | ||
var length = texts.length; | ||
var values = []; | ||
var tempValues = []; | ||
for (var i = 0; i < length; ++i) { | ||
var character = texts[i].trim(); | ||
var nextIndex = i; | ||
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 = []; | ||
} | ||
continue; | ||
} | ||
if (nextIndex === -1) { | ||
throwInvalidFormatError(); | ||
} else { | ||
tempValues.push(texts.slice(i, nextIndex + 1).join("")); | ||
} | ||
i = nextIndex; | ||
} | ||
if (tempValues.length) { | ||
values.push(tempValues.join("")); | ||
} | ||
return values; | ||
} | ||
/** | ||
@@ -373,6 +445,6 @@ * divide text by space. | ||
function splitSpace(text) { | ||
// divide comma(,) | ||
var matches = text.match(/("[^"]*")|('[^']*')|([^\s()]*(?:\((?:[^()]*|\([^()]*\))*\))[^\s()]*)|\S+/g); | ||
return matches || []; | ||
return splitText(text, ""); | ||
} | ||
@@ -396,6 +468,3 @@ /** | ||
// "[^"]*"|'[^']*' | ||
var matches = text.match(/("[^"]*"|'[^']*'|[^,\s()]*\((?:[^()]*|\([^()]*\))*\)[^,\s()]*|[^,])+/g); | ||
return matches ? matches.map(function (str) { | ||
return str.trim(); | ||
}) : []; | ||
return splitText(text, ","); | ||
} | ||
@@ -951,3 +1020,3 @@ /** | ||
export { RGB, RGBA, HSL, HSLA, COLOR_MODELS, FUNCTION, PROPERTY, ARRAY, OBJECT, STRING, NUMBER, UNDEFINED, IS_WINDOW, doc as document, getCrossBrowserProperty, TRANSFORM, FILTER, ANIMATION, KEYFRAMES, cutHex, hexToRGBA, toFullHex, hslToRGBA, stringToRGBA, dot, isUndefined, isObject, isArray, isString, isFunction, splitSpace, splitComma, splitBracket, splitUnit, camelize, decamelize, toArray, now, findIndex, find, requestAnimationFrame, cancelAnimationFrame, $, 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_CHARACTER, cutHex, hexToRGBA, toFullHex, hslToRGBA, stringToRGBA, dot, isUndefined, isObject, isArray, isString, isFunction, splitSpace, splitComma, splitBracket, splitUnit, camelize, decamelize, toArray, now, findIndex, find, requestAnimationFrame, cancelAnimationFrame, $, hasClass, addClass, removeClass, fromCSS, addEvent, removeEvent }; | ||
//# sourceMappingURL=utils.esm.js.map |
@@ -7,3 +7,3 @@ /* | ||
repository: https://github.com/daybrush/utils | ||
@version 0.10.1 | ||
@version 0.10.2 | ||
*/ | ||
@@ -253,2 +253,3 @@ (function (global, factory) { | ||
ANIMATION.replace("animation", "keyframes"); | ||
var OPEN_CLOSED_CHARACTER = ["\"", "'", "\\\"", "\\'"]; | ||
@@ -364,2 +365,73 @@ /** | ||
} | ||
function throwInvalidFormatError() { | ||
throw new Error("Invalid Format"); | ||
} | ||
function findClosed(closedCharacter, texts, index, length) { | ||
for (var i = index; i < length; ++i) { | ||
var character = texts[i].trim(); | ||
if (character === closedCharacter) { | ||
return i; | ||
} | ||
var 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); | ||
} | ||
if (nextIndex === -1) { | ||
throwInvalidFormatError(); | ||
} | ||
i = nextIndex; | ||
} | ||
return -1; | ||
} | ||
function splitText(text, separator) { | ||
var texts = text.split(/(\s*,\s*|\(|\)|"|'|\\"|\\'|\s+)/g).filter(Boolean); | ||
var length = texts.length; | ||
var values = []; | ||
var tempValues = []; | ||
for (var i = 0; i < length; ++i) { | ||
var character = texts[i].trim(); | ||
var nextIndex = i; | ||
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 = []; | ||
} | ||
continue; | ||
} | ||
if (nextIndex === -1) { | ||
throwInvalidFormatError(); | ||
} else { | ||
tempValues.push(texts.slice(i, nextIndex + 1).join("")); | ||
} | ||
i = nextIndex; | ||
} | ||
if (tempValues.length) { | ||
values.push(tempValues.join("")); | ||
} | ||
return values; | ||
} | ||
/** | ||
@@ -379,6 +451,6 @@ * divide text by space. | ||
function splitSpace(text) { | ||
// divide comma(,) | ||
var matches = text.match(/("[^"]*")|('[^']*')|([^\s()]*(?:\((?:[^()]*|\([^()]*\))*\))[^\s()]*)|\S+/g); | ||
return matches || []; | ||
return splitText(text, ""); | ||
} | ||
@@ -402,6 +474,3 @@ /** | ||
// "[^"]*"|'[^']*' | ||
var matches = text.match(/("[^"]*"|'[^']*'|[^,\s()]*\((?:[^()]*|\([^()]*\))*\)[^,\s()]*|[^,])+/g); | ||
return matches ? matches.map(function (str) { | ||
return str.trim(); | ||
}) : []; | ||
return splitText(text, ","); | ||
} | ||
@@ -979,2 +1048,3 @@ /** | ||
KEYFRAMES: KEYFRAMES, | ||
OPEN_CLOSED_CHARACTER: OPEN_CLOSED_CHARACTER, | ||
cutHex: cutHex, | ||
@@ -981,0 +1051,0 @@ hexToRGBA: hexToRGBA, |
@@ -7,5 +7,5 @@ /* | ||
repository: https://github.com/daybrush/utils | ||
@version 0.10.1 | ||
@version 0.10.2 | ||
*/ | ||
!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";var s="rgb",c="rgba",f="hsl",l="hsla",n=[s,c,f,l],e="function",t="object",r="string",a="undefined",i=typeof window!==a,o=typeof document!==a&&document,u=["webkit","ms","moz","o"],m=function(n){if(!o)return"";var e=(o.body||o.documentElement).style,t=u.length;if(typeof e[n]!==a)return n;for(var r=0;r<t;++r){var i="-"+u[r]+"-"+n;if(typeof e[i]!==a)return i}return""},d=m("transform"),p=m("filter"),v=m("animation"),w=v.replace("animation","keyframes");function h(n){var e=n.match(/("[^"]*"|'[^']*'|[^,\s()]*\((?:[^()]*|\([^()]*\))*\)[^,\s()]*|[^,])+/g);return e?e.map(function(n){return n.trim()}):[]}function A(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 g(){return Date.now?Date.now():(new Date).getTime()}function y(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}var F=function(){var t=g(),n=i&&(window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.msRequestAnimationFrame);return n?n.bind(window):function(n){var e=g();return window.setTimeout(function(){n(e-t)},1e3/60)}}();function R(n){return n.replace("#","")}function b(n){var e=R(n),t=parseInt(e.substring(0,2),16),r=parseInt(e.substring(2,4),16),i=parseInt(e.substring(4,6),16),a=parseInt(e.substring(6,8),16)/255;return isNaN(a)&&(a=1),[t,r,i,a]}function N(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 S(n){var e=n[0],t=n[1],r=n[2];e<0&&(e+=360*Math.floor((Math.abs(e)+360)/360)),e%=360;var i,a=(1-Math.abs(2*r-1))*t,o=a*(1-Math.abs(e/60%2-1)),u=r-a/2;return e<60?i=[a,o,0]:e<120?i=[o,a,0]:e<180?i=[0,a,o]:e<240?i=[0,o,a]:e<300?i=[o,0,a]:e<360&&(i=[a,0,o]),[Math.round(255*(i[0]+u)),Math.round(255*(i[1]+u)),Math.round(255*(i[2]+u)),3<n.length?n[3]:1]}return{RGB:s,RGBA:c,HSL:f,HSLA:l,COLOR_MODELS:n,FUNCTION:e,PROPERTY:"property",ARRAY:"array",OBJECT:t,STRING:r,NUMBER:"number",UNDEFINED:a,IS_WINDOW:i,document:o,getCrossBrowserProperty:m,TRANSFORM:d,FILTER:p,ANIMATION:v,KEYFRAMES:w,cutHex:R,hexToRGBA:b,toFullHex:N,hslToRGBA:S,stringToRGBA:function(n){if("#"===n.charAt(0))return 4===n.length||5===n.length?b(N(n)):b(n);if(-1!==n.indexOf("(")){var e=A(n),t=e.prefix,r=e.value;if(!t||!r)return;var i=h(r),a=[],o=i.length;switch(t){case s:case c:for(var u=0;u<o;++u)a[u]=parseFloat(i[u]);return a;case f:case l:for(u=0;u<o;++u)-1!==i[u].indexOf("%")?a[u]=parseFloat(i[u])/100:a[u]=parseFloat(i[u]);return S(a)}}},dot:function(n,e,t,r){return(n*r+e*t)/(t+r)},isUndefined:function(n){return typeof n===a},isObject:function(n){return n&&typeof n===t},isArray:function(n){return Array.isArray(n)},isString:function(n){return typeof n===r},isFunction:function(n){return typeof n===e},splitSpace:function(n){return n.match(/("[^"]*")|('[^']*')|([^\s()]*(?:\((?:[^()]*|\([^()]*\))*\))[^\s()]*)|\S+/g)||[]},splitComma:h,splitBracket:A,splitUnit:function(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)}},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:g,findIndex:y,find:function(n,e,t){var r=y(n,e);return-1<r?n[r]:t},requestAnimationFrame:F,cancelAnimationFrame:function(){var n=i&&(window.cancelAnimationFrame||window.webkitCancelAnimationFrame||window.mozCancelAnimationFrame||window.msCancelAnimationFrame);return n?n.bind(window):function(n){clearTimeout(n)}}(),$:function(n,e){return e?o.querySelectorAll(n):o.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){if(n.classList)n.classList.remove(e);else{var 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),a=e.length,o=0;o<a;++o)r[e[o]]=i[e[o]];return r},addEvent:function(n,e,t,r){n.addEventListener(e,t,r)},removeEvent:function(n,e,t){n.removeEventListener(e,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";var s="rgb",f="rgba",c="hsl",l="hsla",n=[s,f,c,l],e="function",t="object",r="string",o="undefined",i=typeof window!==o,a=typeof document!==o&&document,u=["webkit","ms","moz","o"],m=function(n){if(!a)return"";var e=(a.body||a.documentElement).style,t=u.length;if(typeof e[n]!==o)return n;for(var r=0;r<t;++r){var i="-"+u[r]+"-"+n;if(typeof e[i]!==o)return i}return""},d=m("transform"),p=m("filter"),v=m("animation"),w=v.replace("animation","keyframes"),h=['"',"'",'\\"',"\\'"];function A(){throw new Error("Invalid Format")}function g(n,e,t,r){for(var i=t;i<r;++i){var o=e[i].trim();if(o===n)return i;var a=i;"("===o?a=g(")",e,i+1,r):-1<h.indexOf(o)&&(a=g(o,e,i+1,r)),-1===a&&A(),i=a}return-1}function y(n,e){for(var t=n.split(/(\s*,\s*|\(|\)|"|'|\\"|\\'|\s+)/g).filter(Boolean),r=t.length,i=[],o=[],a=0;a<r;++a){var u=t[a].trim(),s=a;if("("===u)s=g(")",t,a+1,r);else{if(")"===u)throw new Error("invalid format");if(-1<h.indexOf(u))s=g(u,t,a+1,r);else if(u===e){o.length&&(i.push(o.join("")),o=[]);continue}}-1===s?A():o.push(t.slice(a,s+1).join("")),a=s}return o.length&&i.push(o.join("")),i}function R(n){return y(n,",")}function E(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 F(){return Date.now?Date.now():(new Date).getTime()}function b(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}var x=function(){var t=F(),n=i&&(window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.msRequestAnimationFrame);return n?n.bind(window):function(n){var e=F();return window.setTimeout(function(){n(e-t)},1e3/60)}}();function N(n){return n.replace("#","")}function C(n){var e=N(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 S(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 O(n){var e=n[0],t=n[1],r=n[2];e<0&&(e+=360*Math.floor((Math.abs(e)+360)/360)),e%=360;var i,o=(1-Math.abs(2*r-1))*t,a=o*(1-Math.abs(e/60%2-1)),u=r-o/2;return e<60?i=[o,a,0]:e<120?i=[a,o,0]:e<180?i=[0,o,a]:e<240?i=[0,a,o]:e<300?i=[a,0,o]:e<360&&(i=[o,0,a]),[Math.round(255*(i[0]+u)),Math.round(255*(i[1]+u)),Math.round(255*(i[2]+u)),3<n.length?n[3]:1]}return{RGB:s,RGBA:f,HSL:c,HSLA:l,COLOR_MODELS:n,FUNCTION:e,PROPERTY:"property",ARRAY:"array",OBJECT:t,STRING:r,NUMBER:"number",UNDEFINED:o,IS_WINDOW:i,document:a,getCrossBrowserProperty:m,TRANSFORM:d,FILTER:p,ANIMATION:v,KEYFRAMES:w,OPEN_CLOSED_CHARACTER:h,cutHex:N,hexToRGBA:C,toFullHex:S,hslToRGBA:O,stringToRGBA:function(n){if("#"===n.charAt(0))return 4===n.length||5===n.length?C(S(n)):C(n);if(-1!==n.indexOf("(")){var e=E(n),t=e.prefix,r=e.value;if(!t||!r)return;var i=R(r),o=[],a=i.length;switch(t){case s:case f:for(var u=0;u<a;++u)o[u]=parseFloat(i[u]);return o;case c:case l:for(u=0;u<a;++u)-1!==i[u].indexOf("%")?o[u]=parseFloat(i[u])/100:o[u]=parseFloat(i[u]);return O(o)}}},dot:function(n,e,t,r){return(n*r+e*t)/(t+r)},isUndefined:function(n){return typeof n===o},isObject:function(n){return n&&typeof n===t},isArray:function(n){return Array.isArray(n)},isString:function(n){return typeof n===r},isFunction:function(n){return typeof n===e},splitSpace:function(n){return y(n,"")},splitComma:R,splitBracket:E,splitUnit:function(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)}},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:F,findIndex:b,find:function(n,e,t){var r=b(n,e);return-1<r?n[r]:t},requestAnimationFrame:x,cancelAnimationFrame:function(){var n=i&&(window.cancelAnimationFrame||window.webkitCancelAnimationFrame||window.mozCancelAnimationFrame||window.msCancelAnimationFrame);return n?n.bind(window):function(n){clearTimeout(n)}}(),$:function(n,e){return e?a.querySelectorAll(n):a.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){if(n.classList)n.classList.remove(e);else{var 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,a=0;a<o;++a)r[e[a]]=i[e[a]];return r},addEvent:function(n,e,t,r){n.addEventListener(e,t,r)},removeEvent:function(n,e,t){n.removeEventListener(e,t)}}}); | ||
//# sourceMappingURL=utils.min.js.map |
{ | ||
"name": "@daybrush/utils", | ||
"version": "0.10.1", | ||
"version": "0.10.2", | ||
"description": "utils for daybrush", | ||
@@ -41,5 +41,8 @@ "main": "dist/utils.js", | ||
"scripts": { | ||
"coverage": "karma start --coverage && print-coveralls --sort=desc", | ||
"test": "karma start", | ||
"build": "npm run build:rollup && npm run declaration", | ||
"declaration": "rm -rf declaration && tsc -p tsconfig.declaration.json", | ||
"build:rollup": "rollup -c", | ||
"coveralls": "cat ./coverage/lcov.info | coveralls", | ||
"doc": "rm -rf ./doc && jsdoc -c jsdoc.json", | ||
@@ -61,4 +64,17 @@ "release": "npm run build && npm run doc && release --dirs=dist,doc" | ||
"@daybrush/release": "^0.2.4", | ||
"@types/karma-chai": "^0.1.2", | ||
"@types/mocha": "^7.0.2", | ||
"@types/sinon": "^9.0.0", | ||
"chai": "^4.2.0", | ||
"coveralls": "^3.1.0", | ||
"daybrush-jsdoc-template": "^1.5.0", | ||
"gh-pages": "^2.0.1", | ||
"karma": "^5.0.2", | ||
"karma-chai": "^0.1.0", | ||
"karma-chrome-launcher": "^3.1.0", | ||
"karma-mocha": "^2.0.0", | ||
"karma-mocha-reporter": "^2.2.5", | ||
"karma-typescript": "^5.0.2", | ||
"mocha": "^7.1.2", | ||
"print-coveralls": "^1.2.2", | ||
"rollup": "^0.66.6", | ||
@@ -65,0 +81,0 @@ "rollup-plugin-es3": "^1.1.0", |
@@ -215,1 +215,3 @@ /** | ||
export const KEYFRAMES = /*#__PURE__*/ANIMATION.replace("animation", "keyframes"); | ||
export const OPEN_CLOSED_CHARACTER = [`"`, `'`, `\\"`, `\\'`]; |
@@ -1,2 +0,2 @@ | ||
import { UNDEFINED, STRING, OBJECT, FUNCTION, IS_WINDOW } from "./consts"; | ||
import { UNDEFINED, STRING, OBJECT, FUNCTION, IS_WINDOW, OPEN_CLOSED_CHARACTER } from "./consts"; | ||
import { IArrayFormat, IObject } from "./types"; | ||
@@ -106,2 +106,63 @@ /** | ||
} | ||
function throwInvalidFormatError() { | ||
throw new Error("Invalid Format"); | ||
} | ||
function findClosed(closedCharacter: string, texts: string[], index: number, length: number) { | ||
for (let i = index; i < length; ++i) { | ||
const character = texts[i].trim(); | ||
if (character === closedCharacter) { | ||
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); | ||
} | ||
if (nextIndex === -1) { | ||
throwInvalidFormatError(); | ||
} | ||
i = nextIndex; | ||
} | ||
return -1; | ||
} | ||
function splitText(text: string, separator: string) { | ||
const texts = text.split(/(\s*,\s*|\(|\)|"|'|\\"|\\'|\s+)/g).filter(Boolean); | ||
const length = texts.length; | ||
const values = []; | ||
let tempValues = []; | ||
for (let i = 0; i < length; ++i) { | ||
const character = texts[i].trim(); | ||
let nextIndex = i; | ||
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 = []; | ||
} | ||
continue; | ||
} | ||
if (nextIndex === -1) { | ||
throwInvalidFormatError(); | ||
} else { | ||
tempValues.push(texts.slice(i, nextIndex + 1).join("")); | ||
} | ||
i = nextIndex; | ||
} | ||
if (tempValues.length) { | ||
values.push(tempValues.join("")); | ||
} | ||
return values; | ||
} | ||
/** | ||
@@ -122,6 +183,5 @@ * divide text by space. | ||
// divide comma(,) | ||
const matches = text.match(/("[^"]*")|('[^']*')|([^\s()]*(?:\((?:[^()]*|\([^()]*\))*\))[^\s()]*)|\S+/g); | ||
return splitText(text, ""); | ||
} | ||
return matches || []; | ||
} | ||
/** | ||
@@ -143,5 +203,3 @@ * divide text by comma. | ||
// "[^"]*"|'[^']*' | ||
const matches = text.match(/("[^"]*"|'[^']*'|[^,\s()]*\((?:[^()]*|\([^()]*\))*\)[^,\s()]*|[^,])+/g); | ||
return matches ? matches.map(str => str.trim()) : []; | ||
return splitText(text, ","); | ||
} | ||
@@ -185,3 +243,3 @@ /** | ||
*/ | ||
export function splitUnit(text: string): {prefix: string, unit: string, value: number} { | ||
export function splitUnit(text: string): { prefix: string, unit: string, value: number } { | ||
const matches = /^([^\d|e|\-|\+]*)((?:\d|\.|-|e-|e\+)+)(\S*)$/g.exec(text); | ||
@@ -327,8 +385,8 @@ | ||
return raf ? (raf.bind(window) as (callback: FrameRequestCallback) => number) : ((callback: FrameRequestCallback) => { | ||
const currTime = now(); | ||
const id = window.setTimeout(() => { | ||
callback(currTime - firstTime); | ||
}, 1000 / 60); | ||
return id; | ||
}); | ||
const currTime = now(); | ||
const id = window.setTimeout(() => { | ||
callback(currTime - firstTime); | ||
}, 1000 / 60); | ||
return id; | ||
}); | ||
})(); | ||
@@ -357,4 +415,4 @@ | ||
return caf | ||
? caf.bind(window) as (handle: number) => void | ||
: ((handle: number) => { clearTimeout(handle); }); | ||
? caf.bind(window) as (handle: number) => void | ||
: ((handle: number) => { clearTimeout(handle); }); | ||
})(); |
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
218744
31
2983
26