pubo-utils 1.0.167 → 1.0.172

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.PuboUtils=e():t.PuboUtils=e()}(this,(()=>(()=>{"use strict";var t={d:(e,i)=>{for(var s in i)t.o(i,s)&&!t.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:i[s]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{Base64Utils:()=>i,ColorUtils:()=>h,ContinuousTrigger:()=>w,Emitter:()=>u,HistoryStack:()=>_,Level:()=>S,LinearColor:()=>a,RegExpList:()=>z,SensorDataFilter:()=>B,StringSplit:()=>F,SyncQueue:()=>d,WatchDog:()=>A,callbackToPromise:()=>k,cloneDeep:()=>W,debounce:()=>s,degrees:()=>P,filterKeyPoints:()=>R,getAngle:()=>E,getBestPointIndex:()=>N,getCenter:()=>O,getDistance:()=>v,getPositionTheta:()=>L,getRotate:()=>I,getVectorTheta:()=>C,hex2rgb:()=>r,loop:()=>f,lower2camel:()=>D,orderByDistance:()=>U,radians:()=>j,random:()=>c,randomRangeNum:()=>l,retry:()=>y,runAsyncTasks:()=>x,sleep:()=>p,superFactory:()=>m,throttle:()=>b,waitFor:()=>g});var i={};t.r(i),t.d(i,{EncoderMap:()=>T,toUnit8Array:()=>M});const s=(t,e,i=!1)=>{if(i){let i,s=!0;return(...n)=>{s&&(t(...n),s=!1),i&&clearTimeout(i),i=setTimeout((()=>{clearTimeout(i),s=!0,i=null}),e)}}{let i;return(...s)=>{i&&(clearTimeout(i),i=null),i=setTimeout((()=>{t(...s),clearTimeout(i),s=null,i=null}),e)}}},n=t=>parseInt(t.replace("#",""),16),r=t=>{let e;e="string"==typeof t?n(t):t;const i=e.toString(2).padStart(24,"0");return[parseInt(i.slice(0,8),2),parseInt(i.slice(8,16),2),parseInt(i.slice(16,24),2)]},o=t=>"#"+[0,1,2].map((e=>t[e].toString(16))).join("");class h{int;constructor(t){"number"==typeof t?this.int=t:"string"==typeof t?this.int=n(t):Array.isArray(t)&&(this.int=n(o(t)))}getRgbArray(){return r(this.int)}get rgb(){return`rba(${this.getRgbArray().join(", ")})`}get hex(){return o(this.getRgbArray())}toString(t="hex"){return"rgb"===t?this.rgb:this.hex}}class a{base=[255,0,0];intensity=1;min;max;constructor({base:t=[255,0,0],intensity:e=1}={}){this.base=t,this.intensity=e,this.intensity<1&&(this.intensity=1),this.min=this.base[1],this.max=this.base[0],Math.abs(this.min-this.max)<1&&(this.min=0,this.max=255)}getColor(t){if(t<0)return this.base;let e=this.base[0],i=this.base[1],s=this.base[2],n=t*this.intensity;return i=this.base[1]+n,i<this.max||(n=i-this.max,i=this.max,e=this.base[0]-n,e>this.min||(n=this.min-e,e=this.min,s=this.base[2]+n,s<this.max||(n=s-this.max,s=this.max,i-=n,i>this.min||(n=this.min-i,i=this.min,e+=n,e<this.max||(n=e-this.max,e=this.max,s-=n,s>this.min||(s=this.min)))))),[e,i,s]}}const c=(t=8)=>{const e=t=>Math.random().toString(32).slice(2,2+t);if(t<=8)return e(t);let i="";for(let s=0;s<=t;s+=8)i+=e(t-s>8?8:t-s);return i},l=t=>{const e=Math.abs(t[1]-t[0]);return Math.random()*e+Math.min(...t)};class u{state={};ids={};on(t,e){if(this.state[t]||(this.state[t]=[]),"function"!=typeof e)throw new Error("第二个参数必须为function!");const i=this.state[t].push(e)-1,s=c(40);return this.ids[s]={event:t,index:i},s}cancel(t){if(!t)return void this.clear();const{event:e,index:i}=this.ids[t]||{};e&&Array.isArray(this.state[e])&&(this.state[e].splice(i,1),delete this.ids[t],Object.keys(this.ids).forEach((t=>{this.ids[t].event===e&&this.ids[t].index>i&&(this.ids[t].index=this.ids[t].index-1)})))}clear(){this.state.length=0}emit(t,e){if(Array.isArray(this.state[t]))for(const i of this.state[t])"function"==typeof i&&i(e)}async emitSync(t,e){if(Array.isArray(this.state[t]))for(const i of this.state[t])"function"==typeof i&&await i(e)}clone(){return{state:{...this.state},ids:{...this.ids}}}restore(t){this.state=t.state,this.ids=t.ids}}const m=t=>e=>{const i={};for(const s of Object.keys(e))i[s]=t(e[s],s);return i},p=async t=>{await new Promise((e=>{let i=setTimeout((()=>{e(),clearTimeout(i),i=null}),t)}))},f=(t,e)=>{let i=!0,s=()=>{i=!1},n=async()=>{try{await t()}catch(t){console.log(t)}await p(e),i?n():(n=null,t=null,s=null,i=null,e=null)};return n(),s},g=(t,{checkTime:e,timeout:i}={})=>new Promise(((s,n)=>{let r,o=f((async()=>{const e=await t();e&&("function"==typeof o&&o(),r&&(clearTimeout(r),r=null),s(e),o=null,t=null,s=null)}),e||100);i&&(r=setTimeout((()=>{"function"==typeof o&&o(),r&&(clearTimeout(r),r=null),n("timeout"),n=null,o=null,t=null}),i))})),y=(t,{times:e=5,interval:i=1e3}={times:5,interval:1e3})=>{let s=1;const n=async(...r)=>{let o;if(s>e)throw new Error("retry times exceed");try{return o=await t(...r),o}catch(t){return console.log(`action error, times ${s}`),console.log(t),await p(i),s+=1,n(...r)}};return n};class d{cache=[];running=!1;len=0;async _run({fn:t,promise:e}){try{const i=await t();e.resolve(i)}catch(t){e.reject(t)}t=null,e=null}async run(){if(this.cache.length<1)return void(this.running=!1);this.running=!0;const t=this.cache.shift();this.len-=1,"function"==typeof t.fn&&await this._run(t),this.run()}push(t){return this.len+=1,new Promise(((e,i)=>{this.cache.push({fn:t,promise:{resolve:e,reject:i}}),this.running||this.run()}))}get length(){return this.len}}const x=async(t,e=4)=>{let i=-1;const s=[],n=async()=>{if(i+=1,t[i]){try{await t[i]()}catch(t){console.log(t)}await n()}};for(let t=0;t<e;t+=1)s.push(n());await Promise.all(s)};function b(t,e){const i=new d;let s=[];return(...n)=>{if(s=n,!(i.length>0))return i.push((async()=>{await p(e),await t(...s)}))}}class w{timeout;_count=0;props;constructor(t){this.props=t}get count(){return this._count}increment(){clearTimeout(this.timeout),this.timeout=setTimeout((()=>this.clear()),this.props.resetTime),this._count=this._count+1,this._count>this.props.count&&this.props.cb()}clear(){clearTimeout(this.timeout),this._count=0,this.timeout=0}}const T=function(){const t={};for(let e=0;e<64;e++)t["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(e)]=e;return t}();function M(t){const e=new Uint8Array(6*t.length/8);let i=0,s=0,n=0;for(let r=0;r<t.length&&n<e.length;r+=1)if(s=(s<<6)+T[t.charAt(r)],i+=6,i>=8){i-=8;let t=s>>>i;e[n++]=255&t,t<<=i,s-=t}return e}class _{stack=[];len=10;point=0;constructor(t=10){this.len=t}get current(){if(this.point<this.stack.length&&this.point>-1)return this.stack[this.point]}clear(){this.stack.length=0,this.point=0}backup(t){this.point>0&&this.stack.length>0&&(this.stack.splice(0,this.point),this.point=0),this.stack.unshift(t),this.stack.length>this.len&&this.stack.pop()}undo(){return this.point<this.stack.length-1&&(this.point+=1),this.current}redo(){return this.point>0&&(this.point-=1),this.current}}class A{onTimeout;timeout=null;_time;constructor({limit:t=10,onTimeout:e}){this._time=1e3*t,this.onTimeout=()=>{this.timeout&&(clearTimeout(this.timeout),this.timeout=null),e()}}feed(){this.init()}init(){clearTimeout(this.timeout),this.timeout=null,delete this.timeout,this.timeout=setTimeout(this.onTimeout,this._time)}stop(){this.timeout&&clearTimeout(this.timeout),delete this.timeout}}class S{config;step;constructor(t){this.config=t,this.step=(this.config.max-this.config.min)/(this.config.count-2)}get(t){if(t<=this.config.min)return 1;if(t>=this.config.max)return this.config.count;for(let e=2,i=this.config.min+this.step;i<this.config.max+this.step;i+=this.step,e+=1)if(t<i)return e;return this.config.count}}const k=t=>(...e)=>new Promise(((i,s)=>{t(...e,((t,...e)=>{t&&s(t),e.length<2?i(e[0]):i([...e])})),t=null})),v=(t,e)=>Math.sqrt(Math.pow(t.x-e.x,2)+Math.pow(t.y-e.y,2)),P=t=>180*t/Math.PI,j=t=>t*Math.PI/180,E=({w:t,h:e})=>P(Math.atan2(e,t));function R(t,e=.5){if(t.length<3||e<=0)return t;let i;return t.filter(((s,n)=>!(n>0&&v(i,s)<e||(i=t[n],0))))}function O(t){const e=[0,0];for(const i of t)Array.isArray(i)?(e[0]+=i[0],e[1]+=i[1]):(e[0]+=i.x,e[1]+=i.y);return{x:e[0]/t.length,y:e[1]/t.length}}function I(t,e,i){return i&&(e=j(e)),[Math.cos(e)*t[0]-Math.sin(e)*t[1],Math.sin(e)*t[0]+Math.cos(e)*t[1]]}const L=(t,e)=>{const i={x:e.x-t.x,y:e.y-t.y};return Math.atan2(i.y,i.x)},N=(t,e)=>{if(t.length<2)return 0;const i=[];let s=1/0,n=0;for(const r of t){const t=v(r,e),o=L(e,r)-e.theta;s>t&&(s=t),i.push({...r,index:n,distance:t,theta:o}),n+=1}return i.filter((t=>t.distance-s<.1)).sort(((t,e)=>t.theta-e.theta))[0].index},U=(t,e={x:0,y:0,theta:0})=>{let i=e;const s=[],n=[...t];for(;n.length>0;){const t=N(n,i);s.push(n[t]),i=n[t],n.splice(t,1)}return s},C=(t,e)=>Math.atan2(e.y,e.x)-Math.atan2(t.y,t.x),D=t=>t.split("_").map(((t,e)=>e>0?t.slice(0,1).toUpperCase()+t.slice(1):t)).join("");class z{list;_RegExpList=null;constructor(t){this.list=t}getRegEXP(t){const e=t.replace("/","\\/").replace("*",".*");return new RegExp(e)}include(t){return this._RegExpList||(this._RegExpList=this.list.map(this.getRegEXP)),this._RegExpList.some((e=>e.test(t)))}}class B{tmp=[];size;step;min;max;count=0;value=NaN;old;constructor({size:t=5,step:e=5,min:i=-1/0,max:s=1/0}={}){this.size=t,this.step=e,this.min=i,this.max=s}filter(t){return t<this.min||t>this.max||(this.tmp.push(t),this.old=this.calc(t)),this.old}calc(t){if(this.tmp.length<1)return t;this.tmp.length>this.size&&this.tmp.shift();const{res:e,dic:i}=this.getMostNumberOfTmp();return e!==t&&Math.abs(e-t)>this.step?(this.value!==t?this.count=1:this.count+=1,this.value=t,this.count>i[e]?(this.tmp.length=0,this.tmp.push(t),t):e):(this.count=0,this.value=NaN,t)}getMostNumberOfTmp(){const t={};let e,i=0;for(const s of this.tmp)t[s]?t[s]+=1:t[s]=1,t[s]>i&&(i=t[s],e=s);return{res:e,dic:t}}}class F{_splitSymbol;_cache="";constructor(t){this._splitSymbol=t}split(t){const e=(this._cache+t).split(this._splitSymbol);return this._cache=e.splice(e.length-1,1)[0],e}}function W(t,e=new WeakMap){if("object"!=typeof t||null===t)return t;if(e.has(t))return e.get(t);const i={};return Object.keys(t).forEach((s=>{const n=t[s];"object"!=typeof n||null===n?i[s]=n:Array.isArray(n)?i[s]=n.map((t=>W(t,e))):n instanceof Set?i[s]=new Set([...n]):n instanceof Map?i[s]=new Map([...n]):(e.set(t,t),i[s]=W(n,e))})),i}return e})()));
1
+ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.PuboUtils=e():t.PuboUtils=e()}(this,(()=>(()=>{"use strict";var t={d:(e,i)=>{for(var s in i)t.o(i,s)&&!t.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:i[s]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{Base64Utils:()=>i,ColorUtils:()=>a,ContinuousTrigger:()=>A,Emitter:()=>u,HistoryStack:()=>M,Level:()=>v,LinearColor:()=>h,RegExpList:()=>z,SensorDataFilter:()=>B,StringSplit:()=>W,SyncQueue:()=>d,WatchDog:()=>_,callbackToPromise:()=>S,cloneDeep:()=>X,debounce:()=>s,degrees:()=>P,filterKeyPoints:()=>N,filterTree:()=>Q,fixNum:()=>F,flatTree:()=>K,getAngle:()=>E,getBestPointIndex:()=>L,getCenter:()=>R,getDistance:()=>k,getPositionTheta:()=>I,getRotate:()=>O,getTreeItem:()=>q,getVectorTheta:()=>C,hex2rgb:()=>r,loop:()=>f,lower2camel:()=>D,orderByDistance:()=>U,radians:()=>j,random:()=>c,randomRangeNum:()=>l,retry:()=>g,runAsyncTasks:()=>x,searchTree:()=>H,sleep:()=>p,superFactory:()=>m,throttle:()=>b,waitFor:()=>y});var i={};t.r(i),t.d(i,{EncoderMap:()=>T,toUnit8Array:()=>w});const s=(t,e,i=!1)=>{if(i){let i,s=!0;return(...n)=>{s&&(t(...n),s=!1),i&&clearTimeout(i),i=setTimeout((()=>{clearTimeout(i),s=!0,i=null}),e)}}{let i;return(...s)=>{i&&(clearTimeout(i),i=null),i=setTimeout((()=>{t(...s),clearTimeout(i),s=null,i=null}),e)}}},n=t=>parseInt(t.replace("#",""),16),r=t=>{let e;e="string"==typeof t?n(t):t;const i=e.toString(2).padStart(24,"0");return[parseInt(i.slice(0,8),2),parseInt(i.slice(8,16),2),parseInt(i.slice(16,24),2)]},o=t=>"#"+[0,1,2].map((e=>t[e].toString(16))).join("");class a{int;constructor(t){"number"==typeof t?this.int=t:"string"==typeof t?this.int=n(t):Array.isArray(t)&&(this.int=n(o(t)))}getRgbArray(){return r(this.int)}get rgb(){return`rba(${this.getRgbArray().join(", ")})`}get hex(){return o(this.getRgbArray())}toString(t="hex"){return"rgb"===t?this.rgb:this.hex}}class h{base=[255,0,0];intensity=1;min;max;constructor({base:t=[255,0,0],intensity:e=1}={}){this.base=t,this.intensity=e,this.intensity<1&&(this.intensity=1),this.min=this.base[1],this.max=this.base[0],Math.abs(this.min-this.max)<1&&(this.min=0,this.max=255)}getColor(t){if(t<0)return this.base;let e=this.base[0],i=this.base[1],s=this.base[2],n=t*this.intensity;return i=this.base[1]+n,i<this.max||(n=i-this.max,i=this.max,e=this.base[0]-n,e>this.min||(n=this.min-e,e=this.min,s=this.base[2]+n,s<this.max||(n=s-this.max,s=this.max,i-=n,i>this.min||(n=this.min-i,i=this.min,e+=n,e<this.max||(n=e-this.max,e=this.max,s-=n,s>this.min||(s=this.min)))))),[e,i,s]}}const c=(t=8)=>{const e=t=>Math.random().toString(32).slice(2,2+t);if(t<=8)return e(t);let i="";for(let s=0;s<=t;s+=8)i+=e(t-s>8?8:t-s);return i},l=t=>{const e=Math.abs(t[1]-t[0]);return Math.random()*e+Math.min(...t)};class u{state={};ids={};on(t,e){if(this.state[t]||(this.state[t]=[]),"function"!=typeof e)throw new Error("第二个参数必须为function!");const i=this.state[t].push(e)-1,s=c(40);return this.ids[s]={event:t,index:i},s}cancel(t){if(!t)return void this.clear();const{event:e,index:i}=this.ids[t]||{};e&&Array.isArray(this.state[e])&&(this.state[e].splice(i,1),delete this.ids[t],Object.keys(this.ids).forEach((t=>{this.ids[t].event===e&&this.ids[t].index>i&&(this.ids[t].index=this.ids[t].index-1)})))}clear(){this.state.length=0}emit(t,e){if(Array.isArray(this.state[t]))for(const i of this.state[t])"function"==typeof i&&i(e)}async emitSync(t,e){if(Array.isArray(this.state[t]))for(const i of this.state[t])"function"==typeof i&&await i(e)}clone(){return{state:{...this.state},ids:{...this.ids}}}restore(t){this.state=t.state,this.ids=t.ids}}const m=t=>e=>{const i={};for(const s of Object.keys(e))i[s]=t(e[s],s);return i},p=async t=>{await new Promise((e=>{let i=setTimeout((()=>{e(),clearTimeout(i),i=null}),t)}))},f=(t,e)=>{let i=!0,s=()=>{i=!1},n=async()=>{try{await t()}catch(t){console.log(t)}await p(e),i?n():(n=null,t=null,s=null,i=null,e=null)};return n(),s},y=(t,{checkTime:e,timeout:i}={})=>new Promise(((s,n)=>{let r,o=f((async()=>{const e=await t();e&&("function"==typeof o&&o(),r&&(clearTimeout(r),r=null),s(e),o=null,t=null,s=null)}),e||100);i&&(r=setTimeout((()=>{"function"==typeof o&&o(),r&&(clearTimeout(r),r=null),n("timeout"),n=null,o=null,t=null}),i))})),g=(t,{times:e=5,interval:i=1e3}={times:5,interval:1e3})=>{let s=1;const n=async(...r)=>{let o;if(s>e)throw new Error("retry times exceed");try{return o=await t(...r),o}catch(t){return console.log(`action error, times ${s}`),console.log(t),await p(i),s+=1,n(...r)}};return n};class d{cache=[];running=!1;len=0;async _run({fn:t,promise:e}){try{const i=await t();e.resolve(i)}catch(t){e.reject(t)}t=null,e=null}async run(){if(this.cache.length<1)return void(this.running=!1);this.running=!0;const t=this.cache.shift();this.len-=1,"function"==typeof t.fn&&await this._run(t),this.run()}push(t){return this.len+=1,new Promise(((e,i)=>{this.cache.push({fn:t,promise:{resolve:e,reject:i}}),this.running||this.run()}))}get length(){return this.len}}const x=async(t,e=4)=>{let i=-1;const s=[],n=async()=>{if(i+=1,t[i]){try{await t[i]()}catch(t){console.log(t)}await n()}};for(let t=0;t<e;t+=1)s.push(n());await Promise.all(s)};function b(t,e){const i=new d;let s=[];return(...n)=>{if(s=n,!(i.length>0))return i.push((async()=>{await p(e),await t(...s)}))}}class A{timeout;_count=0;props;constructor(t){this.props=t}get count(){return this._count}increment(){clearTimeout(this.timeout),this.timeout=setTimeout((()=>this.clear()),this.props.resetTime),this._count=this._count+1,this._count>this.props.count&&this.props.cb()}clear(){clearTimeout(this.timeout),this._count=0,this.timeout=0}}const T=function(){const t={};for(let e=0;e<64;e++)t["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(e)]=e;return t}();function w(t){const e=new Uint8Array(6*t.length/8);let i=0,s=0,n=0;for(let r=0;r<t.length&&n<e.length;r+=1)if(s=(s<<6)+T[t.charAt(r)],i+=6,i>=8){i-=8;let t=s>>>i;e[n++]=255&t,t<<=i,s-=t}return e}class M{stack=[];len=10;point=0;constructor(t=10){this.len=t}get current(){if(this.point<this.stack.length&&this.point>-1)return this.stack[this.point]}clear(){this.stack.length=0,this.point=0}backup(t){this.point>0&&this.stack.length>0&&(this.stack.splice(0,this.point),this.point=0),this.stack.unshift(t),this.stack.length>this.len&&this.stack.pop()}undo(){return this.point<this.stack.length-1&&(this.point+=1),this.current}redo(){return this.point>0&&(this.point-=1),this.current}}class _{onTimeout;timeout=null;_time;constructor({limit:t=10,onTimeout:e}){this._time=1e3*t,this.onTimeout=()=>{this.timeout&&(clearTimeout(this.timeout),this.timeout=null),e()}}feed(){this.init()}init(){clearTimeout(this.timeout),this.timeout=null,delete this.timeout,this.timeout=setTimeout(this.onTimeout,this._time)}stop(){this.timeout&&clearTimeout(this.timeout),delete this.timeout}}class v{config;step;constructor(t){this.config=t,this.step=(this.config.max-this.config.min)/(this.config.count-2)}get(t){if(t<=this.config.min)return 1;if(t>=this.config.max)return this.config.count;for(let e=2,i=this.config.min+this.step;i<this.config.max+this.step;i+=this.step,e+=1)if(t<i)return e;return this.config.count}}const S=t=>(...e)=>new Promise(((i,s)=>{t(...e,((t,...e)=>{t&&s(t),e.length<2?i(e[0]):i([...e])})),t=null})),k=(t,e)=>Math.sqrt(Math.pow(t.x-e.x,2)+Math.pow(t.y-e.y,2)),P=t=>180*t/Math.PI,j=t=>t*Math.PI/180,E=({w:t,h:e})=>P(Math.atan2(e,t));function N(t,e=.5){if(t.length<3||e<=0)return t;let i;return t.filter(((s,n)=>!(n>0&&k(i,s)<e||(i=t[n],0))))}function R(t){const e=[0,0];for(const i of t)Array.isArray(i)?(e[0]+=i[0],e[1]+=i[1]):(e[0]+=i.x,e[1]+=i.y);return{x:e[0]/t.length,y:e[1]/t.length}}function O(t,e,i){return i&&(e=j(e)),[Math.cos(e)*t[0]-Math.sin(e)*t[1],Math.sin(e)*t[0]+Math.cos(e)*t[1]]}const I=(t,e)=>{const i={x:e.x-t.x,y:e.y-t.y};return Math.atan2(i.y,i.x)},L=(t,e)=>{if(t.length<2)return 0;const i=[];let s=1/0,n=0;for(const r of t){const t=k(r,e),o=I(e,r)-e.theta;s>t&&(s=t),i.push({...r,index:n,distance:t,theta:o}),n+=1}return i.filter((t=>t.distance-s<.1)).sort(((t,e)=>t.theta-e.theta))[0].index},U=(t,e={x:0,y:0,theta:0})=>{let i=e;const s=[],n=[...t];for(;n.length>0;){const t=L(n,i);s.push(n[t]),i=n[t],n.splice(t,1)}return s},C=(t,e)=>Math.atan2(e.y,e.x)-Math.atan2(t.y,t.x),D=t=>t.split("_").map(((t,e)=>e>0?t.slice(0,1).toUpperCase()+t.slice(1):t)).join(""),F=(t,e=2)=>null==t||"NaN"===t?"N/A":("number"!=typeof t&&(t=parseFloat(t)),isNaN(t)?"N/A":t.toFixed(e));class z{list;_RegExpList=null;constructor(t){this.list=t}getRegEXP(t){const e=t.replace("/","\\/").replace("*",".*");return new RegExp(e)}include(t){return this._RegExpList||(this._RegExpList=this.list.map(this.getRegEXP)),this._RegExpList.some((e=>e.test(t)))}}class B{tmp=[];size;step;min;max;count=0;value=NaN;old;constructor({size:t=5,step:e=5,min:i=-1/0,max:s=1/0}={}){this.size=t,this.step=e,this.min=i,this.max=s}filter(t){return t<this.min||t>this.max||(this.tmp.push(t),this.old=this.calc(t)),this.old}calc(t){if(this.tmp.length<1)return t;this.tmp.length>this.size&&this.tmp.shift();const{res:e,dic:i}=this.getMostNumberOfTmp();return e!==t&&Math.abs(e-t)>this.step?(this.value!==t?this.count=1:this.count+=1,this.value=t,this.count>i[e]?(this.tmp.length=0,this.tmp.push(t),t):e):(this.count=0,this.value=NaN,t)}getMostNumberOfTmp(){const t={};let e,i=0;for(const s of this.tmp)t[s]?t[s]+=1:t[s]=1,t[s]>i&&(i=t[s],e=s);return{res:e,dic:t}}}class W{_splitSymbol;_cache="";constructor(t){this._splitSymbol=t}split(t){const e=(this._cache+t).split(this._splitSymbol);return this._cache=e.splice(e.length-1,1)[0],e}}function X(t,e=new WeakMap){if("object"!=typeof t||null===t)return t;if(e.has(t))return e.get(t);const i={};return Object.keys(t).forEach((s=>{const n=t[s];"object"!=typeof n||null===n?i[s]=n:Array.isArray(n)?i[s]=n.map((t=>X(t,e))):n instanceof Set?i[s]=new Set([...n]):n instanceof Map?i[s]=new Map([...n]):(e.set(t,t),i[s]=X(n,e))})),i}function q(t,e){if(e.length<1)return null;let i;Array.isArray(t)&&(i={children:t});for(const t of e)i=i.children[t];return X(i)}function H(t,e,i="children"){const s=[];let n=!1;const r=t=>{for(let o=0;o<t.length;o+=1){const a=t[o];if(e(a)){n=!0,s.push(o);break}Array.isArray(a[i])&&(s.push(o),r(a[i]))}};return Array.isArray(t)?r(t):Array.isArray(t[i])&&r(t[i]),n?s:[]}const K=(t,e="children",i=[],s=[])=>{let n=[];return Array.isArray(t[e])?n=t[e]:Array.isArray(t)&&(n=t),n.forEach(((t,n)=>{const r={...t};delete r[e],s.push(r),r.__indexes=[...i,n]})),n.forEach(((t,n)=>K(t,e,[...i,n],s))),s},Q=(t,e,i="children")=>{const s=[];let n=[];return Array.isArray(t[i])?n=t[i]:Array.isArray(t)&&(n=t),n.forEach((t=>{var n;t[i]&&(t[i]=Q(t[i],e)),((null===(n=t[i])||void 0===n?void 0:n.length)>0||e(t))&&s.push(t)})),s};return e})()));
package/es/index.d.ts CHANGED
@@ -14,7 +14,7 @@ export { WatchDog } from './watch-dog';
14
14
  export { Level } from './level';
15
15
  export { callbackToPromise } from './promise';
16
16
  export { getAngle, getDistance, getCenter, degrees, radians, filterKeyPoints, getRotate, getPositionTheta, getBestPointIndex, orderByDistance, getVectorTheta, } from './math/geometry';
17
- export { lower2camel } from './str';
17
+ export { lower2camel, fixNum } from './str';
18
18
  export { RegExpList } from './regexp-list';
19
19
  export { SensorDataFilter, StringSplit } from './filter/sensor';
20
- export { cloneDeep } from './object';
20
+ export { cloneDeep, getTreeItem, searchTree, flatTree, filterTree } from './object';
package/es/index.js CHANGED
@@ -14,7 +14,7 @@ export { WatchDog } from './watch-dog';
14
14
  export { Level } from './level';
15
15
  export { callbackToPromise } from './promise';
16
16
  export { getAngle, getDistance, getCenter, degrees, radians, filterKeyPoints, getRotate, getPositionTheta, getBestPointIndex, orderByDistance, getVectorTheta } from './math/geometry';
17
- export { lower2camel } from './str';
17
+ export { lower2camel, fixNum } from './str';
18
18
  export { RegExpList } from './regexp-list';
19
19
  export { SensorDataFilter, StringSplit } from './filter/sensor';
20
- export { cloneDeep } from './object';
20
+ export { cloneDeep, getTreeItem, searchTree, flatTree, filterTree } from './object';
@@ -1 +1,5 @@
1
1
  export declare function cloneDeep(data: any, hash?: WeakMap<WeakKey, any>): any;
2
+ export declare function getTreeItem(tree: any, indexes: number[]): any;
3
+ export declare function searchTree(tree: any, cb: (item: any) => boolean, key?: string): number[];
4
+ export declare const flatTree: (tree: any, key?: string, indexes?: number[], tmp?: any[]) => any[];
5
+ export declare const filterTree: (tree: any, cb: (item: any) => boolean, key?: string) => any[];
@@ -27,3 +27,85 @@ export function cloneDeep(data, hash = new WeakMap()) {
27
27
  });
28
28
  return tmp;
29
29
  }
30
+ export function getTreeItem(tree, indexes) {
31
+ if (indexes.length < 1) {
32
+ return null;
33
+ }
34
+ let obj;
35
+ if (Array.isArray(tree)) {
36
+ obj = {
37
+ children: tree
38
+ };
39
+ }
40
+ for (const index of indexes){
41
+ obj = obj.children[index];
42
+ }
43
+ return cloneDeep(obj);
44
+ }
45
+ export function searchTree(tree, cb, key = 'children') {
46
+ const tmp = [];
47
+ let bool = false;
48
+ const each = (list)=>{
49
+ for(let i = 0; i < list.length; i += 1){
50
+ const item = list[i];
51
+ if (cb(item)) {
52
+ bool = true;
53
+ tmp.push(i);
54
+ break;
55
+ }
56
+ if (Array.isArray(item[key])) {
57
+ tmp.push(i);
58
+ each(item[key]);
59
+ }
60
+ }
61
+ };
62
+ if (Array.isArray(tree)) {
63
+ each(tree);
64
+ } else if (Array.isArray(tree[key])) {
65
+ each(tree[key]);
66
+ }
67
+ return bool ? tmp : [];
68
+ }
69
+ export const flatTree = (tree, key = 'children', indexes = [], tmp = [])=>{
70
+ let arr = [];
71
+ if (Array.isArray(tree[key])) {
72
+ arr = tree[key];
73
+ } else if (Array.isArray(tree)) {
74
+ arr = tree;
75
+ }
76
+ arr.forEach((item, i)=>{
77
+ const res = {
78
+ ...item
79
+ };
80
+ delete res[key];
81
+ tmp.push(res);
82
+ res.__indexes = [
83
+ ...indexes,
84
+ i
85
+ ];
86
+ });
87
+ arr.forEach((item, i)=>flatTree(item, key, [
88
+ ...indexes,
89
+ i
90
+ ], tmp));
91
+ return tmp;
92
+ };
93
+ export const filterTree = (tree, cb, key = 'children')=>{
94
+ const tmp = [];
95
+ let arr = [];
96
+ if (Array.isArray(tree[key])) {
97
+ arr = tree[key];
98
+ } else if (Array.isArray(tree)) {
99
+ arr = tree;
100
+ }
101
+ arr.forEach((item)=>{
102
+ var _item_key;
103
+ if (item[key]) {
104
+ item[key] = filterTree(item[key], cb);
105
+ }
106
+ if (((_item_key = item[key]) === null || _item_key === void 0 ? void 0 : _item_key.length) > 0 || cb(item)) {
107
+ tmp.push(item);
108
+ }
109
+ });
110
+ return tmp;
111
+ };
package/es/str.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export declare const lower2camel: (str: any) => any;
2
+ export declare const fixNum: (num: number | string | undefined | null, n?: number) => string;
package/es/str.js CHANGED
@@ -2,3 +2,15 @@
2
2
  export const lower2camel = (str)=>{
3
3
  return str.split('_').map((item, i)=>i > 0 ? item.slice(0, 1).toUpperCase() + item.slice(1) : item).join('');
4
4
  };
5
+ export const fixNum = (num, n = 2)=>{
6
+ if (num === undefined || num === null || num === 'NaN') {
7
+ return 'N/A';
8
+ }
9
+ if (typeof num !== 'number') {
10
+ num = parseFloat(num);
11
+ }
12
+ if (isNaN(num)) {
13
+ return 'N/A';
14
+ }
15
+ return num.toFixed(n);
16
+ };
package/lib/index.d.ts CHANGED
@@ -14,7 +14,7 @@ export { WatchDog } from './watch-dog';
14
14
  export { Level } from './level';
15
15
  export { callbackToPromise } from './promise';
16
16
  export { getAngle, getDistance, getCenter, degrees, radians, filterKeyPoints, getRotate, getPositionTheta, getBestPointIndex, orderByDistance, getVectorTheta, } from './math/geometry';
17
- export { lower2camel } from './str';
17
+ export { lower2camel, fixNum } from './str';
18
18
  export { RegExpList } from './regexp-list';
19
19
  export { SensorDataFilter, StringSplit } from './filter/sensor';
20
- export { cloneDeep } from './object';
20
+ export { cloneDeep, getTreeItem, searchTree, flatTree, filterTree } from './object';
package/lib/index.js CHANGED
@@ -60,6 +60,15 @@ _export(exports, {
60
60
  filterKeyPoints: function() {
61
61
  return _geometry.filterKeyPoints;
62
62
  },
63
+ filterTree: function() {
64
+ return _object.filterTree;
65
+ },
66
+ fixNum: function() {
67
+ return _str.fixNum;
68
+ },
69
+ flatTree: function() {
70
+ return _object.flatTree;
71
+ },
63
72
  getAngle: function() {
64
73
  return _geometry.getAngle;
65
74
  },
@@ -78,6 +87,9 @@ _export(exports, {
78
87
  getRotate: function() {
79
88
  return _geometry.getRotate;
80
89
  },
90
+ getTreeItem: function() {
91
+ return _object.getTreeItem;
92
+ },
81
93
  getVectorTheta: function() {
82
94
  return _geometry.getVectorTheta;
83
95
  },
@@ -108,6 +120,9 @@ _export(exports, {
108
120
  runAsyncTasks: function() {
109
121
  return _queue.runAsyncTasks;
110
122
  },
123
+ searchTree: function() {
124
+ return _object.searchTree;
125
+ },
111
126
  sleep: function() {
112
127
  return _sleep.sleep;
113
128
  },
@@ -1 +1,5 @@
1
1
  export declare function cloneDeep(data: any, hash?: WeakMap<WeakKey, any>): any;
2
+ export declare function getTreeItem(tree: any, indexes: number[]): any;
3
+ export declare function searchTree(tree: any, cb: (item: any) => boolean, key?: string): number[];
4
+ export declare const flatTree: (tree: any, key?: string, indexes?: number[], tmp?: any[]) => any[];
5
+ export declare const filterTree: (tree: any, cb: (item: any) => boolean, key?: string) => any[];
@@ -2,10 +2,27 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- Object.defineProperty(exports, "cloneDeep", {
6
- enumerable: true,
7
- get: function() {
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ cloneDeep: function() {
8
13
  return cloneDeep;
14
+ },
15
+ filterTree: function() {
16
+ return filterTree;
17
+ },
18
+ flatTree: function() {
19
+ return flatTree;
20
+ },
21
+ getTreeItem: function() {
22
+ return getTreeItem;
23
+ },
24
+ searchTree: function() {
25
+ return searchTree;
9
26
  }
10
27
  });
11
28
  function cloneDeep(data, hash = new WeakMap()) {
@@ -37,3 +54,85 @@ function cloneDeep(data, hash = new WeakMap()) {
37
54
  });
38
55
  return tmp;
39
56
  }
57
+ function getTreeItem(tree, indexes) {
58
+ if (indexes.length < 1) {
59
+ return null;
60
+ }
61
+ let obj;
62
+ if (Array.isArray(tree)) {
63
+ obj = {
64
+ children: tree
65
+ };
66
+ }
67
+ for (const index of indexes){
68
+ obj = obj.children[index];
69
+ }
70
+ return cloneDeep(obj);
71
+ }
72
+ function searchTree(tree, cb, key = 'children') {
73
+ const tmp = [];
74
+ let bool = false;
75
+ const each = (list)=>{
76
+ for(let i = 0; i < list.length; i += 1){
77
+ const item = list[i];
78
+ if (cb(item)) {
79
+ bool = true;
80
+ tmp.push(i);
81
+ break;
82
+ }
83
+ if (Array.isArray(item[key])) {
84
+ tmp.push(i);
85
+ each(item[key]);
86
+ }
87
+ }
88
+ };
89
+ if (Array.isArray(tree)) {
90
+ each(tree);
91
+ } else if (Array.isArray(tree[key])) {
92
+ each(tree[key]);
93
+ }
94
+ return bool ? tmp : [];
95
+ }
96
+ const flatTree = (tree, key = 'children', indexes = [], tmp = [])=>{
97
+ let arr = [];
98
+ if (Array.isArray(tree[key])) {
99
+ arr = tree[key];
100
+ } else if (Array.isArray(tree)) {
101
+ arr = tree;
102
+ }
103
+ arr.forEach((item, i)=>{
104
+ const res = {
105
+ ...item
106
+ };
107
+ delete res[key];
108
+ tmp.push(res);
109
+ res.__indexes = [
110
+ ...indexes,
111
+ i
112
+ ];
113
+ });
114
+ arr.forEach((item, i)=>flatTree(item, key, [
115
+ ...indexes,
116
+ i
117
+ ], tmp));
118
+ return tmp;
119
+ };
120
+ const filterTree = (tree, cb, key = 'children')=>{
121
+ const tmp = [];
122
+ let arr = [];
123
+ if (Array.isArray(tree[key])) {
124
+ arr = tree[key];
125
+ } else if (Array.isArray(tree)) {
126
+ arr = tree;
127
+ }
128
+ arr.forEach((item)=>{
129
+ var _item_key;
130
+ if (item[key]) {
131
+ item[key] = filterTree(item[key], cb);
132
+ }
133
+ if (((_item_key = item[key]) === null || _item_key === void 0 ? void 0 : _item_key.length) > 0 || cb(item)) {
134
+ tmp.push(item);
135
+ }
136
+ });
137
+ return tmp;
138
+ };
package/lib/str.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export declare const lower2camel: (str: any) => any;
2
+ export declare const fixNum: (num: number | string | undefined | null, n?: number) => string;
package/lib/str.js CHANGED
@@ -3,12 +3,32 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- Object.defineProperty(exports, "lower2camel", {
7
- enumerable: true,
8
- get: function() {
6
+ function _export(target, all) {
7
+ for(var name in all)Object.defineProperty(target, name, {
8
+ enumerable: true,
9
+ get: all[name]
10
+ });
11
+ }
12
+ _export(exports, {
13
+ fixNum: function() {
14
+ return fixNum;
15
+ },
16
+ lower2camel: function() {
9
17
  return lower2camel;
10
18
  }
11
19
  });
12
20
  const lower2camel = (str)=>{
13
21
  return str.split('_').map((item, i)=>i > 0 ? item.slice(0, 1).toUpperCase() + item.slice(1) : item).join('');
14
22
  };
23
+ const fixNum = (num, n = 2)=>{
24
+ if (num === undefined || num === null || num === 'NaN') {
25
+ return 'N/A';
26
+ }
27
+ if (typeof num !== 'number') {
28
+ num = parseFloat(num);
29
+ }
30
+ if (isNaN(num)) {
31
+ return 'N/A';
32
+ }
33
+ return num.toFixed(n);
34
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pubo-utils",
3
- "version": "1.0.167",
3
+ "version": "1.0.172",
4
4
  "main": "./lib/index.js",
5
5
  "module": "./es/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -18,7 +18,7 @@
18
18
  "engines": {
19
19
  "node": ">=8.0.0"
20
20
  },
21
- "gitHead": "3e19f954e9f9cf7e8c358012818b9578007df8a0",
21
+ "gitHead": "2caf39c9d826bfd2b30665bdff5c661f2d85e10f",
22
22
  "devDependencies": {
23
23
  "del": "^5.1.0",
24
24
  "eslint": "^8.42.0",