pubo-utils 1.0.159 → 1.0.161
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.
- package/dist/pubo-utils.js +1 -1
- package/es/index.d.ts +1 -0
- package/es/index.js +1 -0
- package/es/object/index.d.ts +1 -0
- package/es/object/index.js +29 -0
- package/es/throttle/index.js +1 -2
- package/lib/index.d.ts +1 -0
- package/lib/index.js +4 -0
- package/lib/object/index.d.ts +1 -0
- package/lib/object/index.js +39 -0
- package/lib/throttle/index.js +1 -2
- package/package.json +2 -2
package/dist/pubo-utils.js
CHANGED
|
@@ -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,ContinuousTrigger:()=>f,Emitter:()=>
|
|
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,ContinuousTrigger:()=>f,Emitter:()=>c,HistoryStack:()=>d,Level:()=>T,RegExpList:()=>I,SensorDataFilter:()=>N,StringSplit:()=>L,SyncQueue:()=>p,WatchDog:()=>x,callbackToPromise:()=>b,cloneDeep:()=>U,debounce:()=>s,degrees:()=>M,filterKeyPoints:()=>k,getAngle:()=>S,getBestPointIndex:()=>A,getCenter:()=>P,getDistance:()=>w,getPositionTheta:()=>j,getRotate:()=>v,getVectorTheta:()=>O,hex2rgb:()=>n,loop:()=>l,lower2camel:()=>R,orderByDistance:()=>E,radians:()=>_,random:()=>o,randomRangeNum:()=>r,sleep:()=>a,superFactory:()=>h,throttle:()=>m,waitFor:()=>u});var i={};t.r(i),t.d(i,{EncoderMap:()=>g,toUnit8Array:()=>y});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=>{let e;e="string"==typeof t?(t=>parseInt(t.replace("#",""),16))(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=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},r=t=>{const e=Math.abs(t[1]-t[0]);return Math.random()*e+Math.min(...t)};class c{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=o(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 h=t=>e=>{const i={};for(const s of Object.keys(e))i[s]=t(e[s],s);return i},a=async t=>{await new Promise((e=>{let i=setTimeout((()=>{e(),clearTimeout(i),i=null}),t)}))},l=(t,e)=>{let i=!0,s=()=>{i=!1},n=async()=>{try{await t()}catch(t){console.log(t)}await a(e),i?n():(n=null,t=null,s=null,i=null,e=null)};return n(),s},u=(t,{checkTime:e,timeout:i}={})=>new Promise(((s,n)=>{let o,r=l((async()=>{const e=await t();e&&("function"==typeof r&&r(),o&&(clearTimeout(o),o=null),s(e),r=null,t=null,s=null)}),e||100);i&&(o=setTimeout((()=>{"function"==typeof r&&r(),o&&(clearTimeout(o),o=null),n("timeout"),n=null,r=null,t=null}),i))}));class p{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}}function m(t,e){const i=new p;let s=[];return(...n)=>{if(s=n,!(i.length>0))return i.push((async()=>{await a(e),await t(...s)}))}}class f{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 g=function(){const t={};for(let e=0;e<64;e++)t["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(e)]=e;return t}();function y(t){const e=new Uint8Array(6*t.length/8);let i=0,s=0,n=0;for(let o=0;o<t.length&&n<e.length;o+=1)if(s=(s<<6)+g[t.charAt(o)],i+=6,i>=8){i-=8;let t=s>>>i;e[n++]=255&t,t<<=i,s-=t}return e}class d{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 x{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 T{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 b=t=>(...e)=>new Promise(((i,s)=>{t(...e,((t,...e)=>{t&&s(t),e.length<2?i(e[0]):i([...e])})),t=null})),w=(t,e)=>Math.sqrt(Math.pow(t.x-e.x,2)+Math.pow(t.y-e.y,2)),M=t=>180*t/Math.PI,_=t=>t*Math.PI/180,S=({w:t,h:e})=>M(Math.atan2(e,t));function k(t,e=.5){if(t.length<3||e<=0)return t;let i;return t.filter(((s,n)=>!(n>0&&w(i,s)<e||(i=t[n],0))))}function P(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 v(t,e){return[Math.cos(e)*t[0]-Math.sin(e)*t[1],Math.sin(e)*t[0]+Math.cos(e)*t[1]]}const j=(t,e)=>{const i={x:e.x-t.x,y:e.y-t.y};return Math.atan2(i.y,i.x)},A=(t,e)=>{if(t.length<2)return 0;const i=[];let s=1/0,n=0;for(const o of t){const t=w(o,e),r=j(e,o)-e.theta;s>t&&(s=t),i.push({...o,index:n,distance:t,theta:r}),n+=1}return i.filter((t=>t.distance-s<.1)).sort(((t,e)=>t.theta-e.theta))[0].index},E=(t,e={x:0,y:0,theta:0})=>{let i=e;const s=[],n=[...t];for(;n.length>0;){const t=A(n,i);s.push(n[t]),i=n[t],n.splice(t,1)}return s},O=(t,e)=>Math.atan2(e.y,e.x)-Math.atan2(t.y,t.x),R=t=>t.split("_").map(((t,e)=>e>0?t.slice(0,1).toUpperCase()+t.slice(1):t)).join("");class I{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 N{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 L{_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 U(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=>U(t,e))):n instanceof Set?i[s]=new Set([...n]):n instanceof Map?i[s]=new Map([...n]):(e.set(t,t),i[s]=U(n,e))})),i}return e})()));
|
package/es/index.d.ts
CHANGED
|
@@ -17,3 +17,4 @@ export { getAngle, getDistance, getCenter, degrees, radians, filterKeyPoints, ge
|
|
|
17
17
|
export { lower2camel } from './str';
|
|
18
18
|
export { RegExpList } from './regexp-list';
|
|
19
19
|
export { SensorDataFilter, StringSplit } from './filter/sensor';
|
|
20
|
+
export { cloneDeep } from './object';
|
package/es/index.js
CHANGED
|
@@ -17,3 +17,4 @@ export { getAngle, getDistance, getCenter, degrees, radians, filterKeyPoints, ge
|
|
|
17
17
|
export { lower2camel } from './str';
|
|
18
18
|
export { RegExpList } from './regexp-list';
|
|
19
19
|
export { SensorDataFilter, StringSplit } from './filter/sensor';
|
|
20
|
+
export { cloneDeep } from './object';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function cloneDeep(data: any, hash?: WeakMap<WeakKey, any>): any;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export function cloneDeep(data, hash = new WeakMap()) {
|
|
2
|
+
if (typeof data !== 'object' || data === null) {
|
|
3
|
+
return data;
|
|
4
|
+
}
|
|
5
|
+
if (hash.has(data)) {
|
|
6
|
+
return hash.get(data);
|
|
7
|
+
}
|
|
8
|
+
const tmp = {};
|
|
9
|
+
Object.keys(data).forEach((key)=>{
|
|
10
|
+
const value = data[key];
|
|
11
|
+
if (typeof value !== 'object' || value === null) {
|
|
12
|
+
tmp[key] = value;
|
|
13
|
+
} else if (Array.isArray(value)) {
|
|
14
|
+
tmp[key] = value.map((item)=>cloneDeep(item, hash));
|
|
15
|
+
} else if (value instanceof Set) {
|
|
16
|
+
tmp[key] = new Set([
|
|
17
|
+
...value
|
|
18
|
+
]);
|
|
19
|
+
} else if (value instanceof Map) {
|
|
20
|
+
tmp[key] = new Map([
|
|
21
|
+
...value
|
|
22
|
+
]);
|
|
23
|
+
} else {
|
|
24
|
+
hash.set(data, data);
|
|
25
|
+
tmp[key] = cloneDeep(value, hash);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
return tmp;
|
|
29
|
+
}
|
package/es/throttle/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { SyncQueue } from '../queue';
|
|
|
2
2
|
import { sleep } from '../sleep';
|
|
3
3
|
export function throttle(cb, time) {
|
|
4
4
|
const queue = new SyncQueue();
|
|
5
|
-
let payload;
|
|
5
|
+
let payload = [];
|
|
6
6
|
return (...args)=>{
|
|
7
7
|
payload = args;
|
|
8
8
|
if (queue.length > 0) {
|
|
@@ -11,7 +11,6 @@ export function throttle(cb, time) {
|
|
|
11
11
|
return queue.push(async ()=>{
|
|
12
12
|
await sleep(time);
|
|
13
13
|
await cb(...payload);
|
|
14
|
-
payload = null;
|
|
15
14
|
});
|
|
16
15
|
};
|
|
17
16
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -17,3 +17,4 @@ export { getAngle, getDistance, getCenter, degrees, radians, filterKeyPoints, ge
|
|
|
17
17
|
export { lower2camel } from './str';
|
|
18
18
|
export { RegExpList } from './regexp-list';
|
|
19
19
|
export { SensorDataFilter, StringSplit } from './filter/sensor';
|
|
20
|
+
export { cloneDeep } from './object';
|
package/lib/index.js
CHANGED
|
@@ -42,6 +42,9 @@ _export(exports, {
|
|
|
42
42
|
callbackToPromise: function() {
|
|
43
43
|
return _promise.callbackToPromise;
|
|
44
44
|
},
|
|
45
|
+
cloneDeep: function() {
|
|
46
|
+
return _object.cloneDeep;
|
|
47
|
+
},
|
|
45
48
|
debounce: function() {
|
|
46
49
|
return _debounce.debounce;
|
|
47
50
|
},
|
|
@@ -125,6 +128,7 @@ const _geometry = require("./math/geometry");
|
|
|
125
128
|
const _str = require("./str");
|
|
126
129
|
const _regexplist = require("./regexp-list");
|
|
127
130
|
const _sensor = require("./filter/sensor");
|
|
131
|
+
const _object = require("./object");
|
|
128
132
|
function _getRequireWildcardCache(nodeInterop) {
|
|
129
133
|
if (typeof WeakMap !== "function") return null;
|
|
130
134
|
var cacheBabelInterop = new WeakMap();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function cloneDeep(data: any, hash?: WeakMap<WeakKey, any>): any;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "cloneDeep", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return cloneDeep;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
function cloneDeep(data, hash = new WeakMap()) {
|
|
12
|
+
if (typeof data !== 'object' || data === null) {
|
|
13
|
+
return data;
|
|
14
|
+
}
|
|
15
|
+
if (hash.has(data)) {
|
|
16
|
+
return hash.get(data);
|
|
17
|
+
}
|
|
18
|
+
const tmp = {};
|
|
19
|
+
Object.keys(data).forEach((key)=>{
|
|
20
|
+
const value = data[key];
|
|
21
|
+
if (typeof value !== 'object' || value === null) {
|
|
22
|
+
tmp[key] = value;
|
|
23
|
+
} else if (Array.isArray(value)) {
|
|
24
|
+
tmp[key] = value.map((item)=>cloneDeep(item, hash));
|
|
25
|
+
} else if (value instanceof Set) {
|
|
26
|
+
tmp[key] = new Set([
|
|
27
|
+
...value
|
|
28
|
+
]);
|
|
29
|
+
} else if (value instanceof Map) {
|
|
30
|
+
tmp[key] = new Map([
|
|
31
|
+
...value
|
|
32
|
+
]);
|
|
33
|
+
} else {
|
|
34
|
+
hash.set(data, data);
|
|
35
|
+
tmp[key] = cloneDeep(value, hash);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return tmp;
|
|
39
|
+
}
|
package/lib/throttle/index.js
CHANGED
|
@@ -12,7 +12,7 @@ const _queue = require("../queue");
|
|
|
12
12
|
const _sleep = require("../sleep");
|
|
13
13
|
function throttle(cb, time) {
|
|
14
14
|
const queue = new _queue.SyncQueue();
|
|
15
|
-
let payload;
|
|
15
|
+
let payload = [];
|
|
16
16
|
return (...args)=>{
|
|
17
17
|
payload = args;
|
|
18
18
|
if (queue.length > 0) {
|
|
@@ -21,7 +21,6 @@ function throttle(cb, time) {
|
|
|
21
21
|
return queue.push(async ()=>{
|
|
22
22
|
await (0, _sleep.sleep)(time);
|
|
23
23
|
await cb(...payload);
|
|
24
|
-
payload = null;
|
|
25
24
|
});
|
|
26
25
|
};
|
|
27
26
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pubo-utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.161",
|
|
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": "
|
|
21
|
+
"gitHead": "bd8490cfef859238d4c4de7ebc4b7e3e820db956",
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"del": "^5.1.0",
|
|
24
24
|
"eslint": "^8.42.0",
|