pubo-utils 1.0.158 → 1.0.159
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/es/base64/index.js +6 -7
- package/es/color/utils.js +17 -12
- package/es/debounce/index.js +6 -7
- package/es/emitter/index.js +15 -5
- package/es/factory/index.js +3 -4
- package/es/filter/sensor.js +15 -17
- package/es/index.js +1 -1
- package/es/level/index.js +2 -2
- package/es/loop/index.js +9 -13
- package/es/math/geometry.js +45 -25
- package/es/promise/index.js +15 -14
- package/es/queue/index.js +10 -6
- package/es/random/index.js +5 -10
- package/es/regexp-list/index.js +2 -2
- package/es/sleep/index.js +3 -4
- package/es/stack/index.js +1 -1
- package/es/str.js +2 -5
- package/es/throttle/index.js +2 -2
- package/es/trigger/index.js +2 -2
- package/es/watch-dog/index.js +2 -2
- package/lib/base64/index.js +23 -15
- package/lib/color/utils.js +40 -20
- package/lib/debounce/index.js +15 -10
- package/lib/emitter/index.js +34 -11
- package/lib/factory/index.js +12 -12
- package/lib/filter/sensor.js +32 -26
- package/lib/index.js +165 -73
- package/lib/level/index.js +11 -5
- package/lib/loop/index.js +28 -33
- package/lib/math/geometry.js +94 -54
- package/lib/promise/index.js +25 -18
- package/lib/queue/index.js +19 -9
- package/lib/random/index.js +23 -20
- package/lib/regexp-list/index.js +11 -5
- package/lib/sleep/index.js +13 -8
- package/lib/stack/index.js +10 -4
- package/lib/str.js +12 -9
- package/lib/throttle/index.js +15 -8
- package/lib/trigger/index.js +11 -5
- package/lib/watch-dog/index.js +11 -5
- package/package.json +2 -14
package/es/base64/index.js
CHANGED
|
@@ -1,26 +1,25 @@
|
|
|
1
|
-
export const EncoderMap =
|
|
1
|
+
export const EncoderMap = function() {
|
|
2
2
|
const S = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
3
3
|
const e = {};
|
|
4
|
-
for
|
|
4
|
+
for(let i = 0; i < 64; i++){
|
|
5
5
|
e[S.charAt(i)] = i;
|
|
6
6
|
}
|
|
7
7
|
return e;
|
|
8
|
-
}
|
|
8
|
+
}();
|
|
9
9
|
/**
|
|
10
10
|
* base64 转Uint8Array
|
|
11
11
|
* @param input base64 字符串
|
|
12
12
|
* @param output Uint8Array
|
|
13
13
|
* base64 6位的二进制, Uint8Array 8位的二进制,整体原理就是将 base64 的每个字符转成2进制然后拼接,再按每个八位为一个单位地取出
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
const output = new Uint8Array((input.length * 6) / 8);
|
|
14
|
+
*/ export function toUnit8Array(input) {
|
|
15
|
+
const output = new Uint8Array(input.length * 6 / 8);
|
|
17
16
|
// 当前二进制字符长度
|
|
18
17
|
let l = 0;
|
|
19
18
|
// 二进制字符缓存值(格式为10进制)
|
|
20
19
|
let b = 0;
|
|
21
20
|
// Uint8Array 的指针
|
|
22
21
|
let j = 0;
|
|
23
|
-
for
|
|
22
|
+
for(let x = 0; x < input.length && j < output.length; x += 1){
|
|
24
23
|
b = (b << 6) + EncoderMap[input.charAt(x)];
|
|
25
24
|
// 为什么是6? base64 为64个字符,也就是 2^6,用二进制表示就是6位
|
|
26
25
|
l += 6;
|
package/es/color/utils.js
CHANGED
|
@@ -1,31 +1,36 @@
|
|
|
1
|
-
const str2int = (str)
|
|
2
|
-
export const hex2rgb = (n)
|
|
1
|
+
const str2int = (str)=>parseInt(str.replace('#', ''), 16);
|
|
2
|
+
export const hex2rgb = (n)=>{
|
|
3
3
|
let v;
|
|
4
4
|
if (typeof n === 'string') {
|
|
5
5
|
v = str2int(n);
|
|
6
|
-
}
|
|
7
|
-
else {
|
|
6
|
+
} else {
|
|
8
7
|
v = n;
|
|
9
8
|
}
|
|
10
9
|
const str = v.toString(2).padStart(24, '0');
|
|
11
10
|
const r = parseInt(str.slice(0, 8), 2);
|
|
12
11
|
const g = parseInt(str.slice(8, 16), 2);
|
|
13
12
|
const b = parseInt(str.slice(16, 24), 2);
|
|
14
|
-
return [
|
|
13
|
+
return [
|
|
14
|
+
r,
|
|
15
|
+
g,
|
|
16
|
+
b
|
|
17
|
+
];
|
|
15
18
|
};
|
|
16
|
-
export const rgb2hex = (color)
|
|
17
|
-
return '#' + [
|
|
19
|
+
export const rgb2hex = (color)=>{
|
|
20
|
+
return '#' + [
|
|
21
|
+
0,
|
|
22
|
+
1,
|
|
23
|
+
2
|
|
24
|
+
].map((i)=>color[i].toString(16)).join('');
|
|
18
25
|
};
|
|
19
26
|
export class ColorUtils {
|
|
20
27
|
int;
|
|
21
|
-
constructor(n)
|
|
28
|
+
constructor(n){
|
|
22
29
|
if (typeof n === 'number') {
|
|
23
30
|
this.int = n;
|
|
24
|
-
}
|
|
25
|
-
else if (typeof n === 'string') {
|
|
31
|
+
} else if (typeof n === 'string') {
|
|
26
32
|
this.int = str2int(n);
|
|
27
|
-
}
|
|
28
|
-
else if (Array.isArray(n)) {
|
|
33
|
+
} else if (Array.isArray(n)) {
|
|
29
34
|
this.int = str2int(rgb2hex(n));
|
|
30
35
|
}
|
|
31
36
|
}
|
package/es/debounce/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export const debounce = (cb, time, first = false)
|
|
1
|
+
export const debounce = (cb, time, first = false)=>{
|
|
2
2
|
if (first) {
|
|
3
3
|
let shouldRun = true;
|
|
4
4
|
let t;
|
|
5
|
-
return (...args)
|
|
5
|
+
return (...args)=>{
|
|
6
6
|
if (shouldRun) {
|
|
7
7
|
cb(...args);
|
|
8
8
|
shouldRun = false;
|
|
@@ -10,21 +10,20 @@ export const debounce = (cb, time, first = false) => {
|
|
|
10
10
|
if (t) {
|
|
11
11
|
clearTimeout(t);
|
|
12
12
|
}
|
|
13
|
-
t = setTimeout(()
|
|
13
|
+
t = setTimeout(()=>{
|
|
14
14
|
clearTimeout(t);
|
|
15
15
|
shouldRun = true;
|
|
16
16
|
t = null;
|
|
17
17
|
}, time);
|
|
18
18
|
};
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
19
|
+
} else {
|
|
21
20
|
let t;
|
|
22
|
-
return (...args)
|
|
21
|
+
return (...args)=>{
|
|
23
22
|
if (t) {
|
|
24
23
|
clearTimeout(t);
|
|
25
24
|
t = null;
|
|
26
25
|
}
|
|
27
|
-
t = setTimeout(()
|
|
26
|
+
t = setTimeout(()=>{
|
|
28
27
|
cb(...args);
|
|
29
28
|
clearTimeout(t);
|
|
30
29
|
args = null;
|
package/es/emitter/index.js
CHANGED
|
@@ -11,7 +11,10 @@ export class Emitter {
|
|
|
11
11
|
}
|
|
12
12
|
const index = this.state[event].push(func) - 1;
|
|
13
13
|
const key = random(40);
|
|
14
|
-
this.ids[key] = {
|
|
14
|
+
this.ids[key] = {
|
|
15
|
+
event,
|
|
16
|
+
index
|
|
17
|
+
};
|
|
15
18
|
return key;
|
|
16
19
|
}
|
|
17
20
|
cancel(id) {
|
|
@@ -28,7 +31,7 @@ export class Emitter {
|
|
|
28
31
|
}
|
|
29
32
|
this.state[event].splice(index, 1);
|
|
30
33
|
delete this.ids[id];
|
|
31
|
-
Object.keys(this.ids).forEach((key)
|
|
34
|
+
Object.keys(this.ids).forEach((key)=>{
|
|
32
35
|
if (this.ids[key].event === event && this.ids[key].index > index) {
|
|
33
36
|
this.ids[key].index = this.ids[key].index - 1;
|
|
34
37
|
}
|
|
@@ -39,7 +42,7 @@ export class Emitter {
|
|
|
39
42
|
}
|
|
40
43
|
emit(event, payload) {
|
|
41
44
|
if (Array.isArray(this.state[event])) {
|
|
42
|
-
for (const func of this.state[event])
|
|
45
|
+
for (const func of this.state[event]){
|
|
43
46
|
if (typeof func === 'function') {
|
|
44
47
|
func(payload);
|
|
45
48
|
}
|
|
@@ -48,7 +51,7 @@ export class Emitter {
|
|
|
48
51
|
}
|
|
49
52
|
async emitSync(event, payload) {
|
|
50
53
|
if (Array.isArray(this.state[event])) {
|
|
51
|
-
for (const func of this.state[event])
|
|
54
|
+
for (const func of this.state[event]){
|
|
52
55
|
if (typeof func === 'function') {
|
|
53
56
|
await func(payload);
|
|
54
57
|
}
|
|
@@ -56,7 +59,14 @@ export class Emitter {
|
|
|
56
59
|
}
|
|
57
60
|
}
|
|
58
61
|
clone() {
|
|
59
|
-
return {
|
|
62
|
+
return {
|
|
63
|
+
state: {
|
|
64
|
+
...this.state
|
|
65
|
+
},
|
|
66
|
+
ids: {
|
|
67
|
+
...this.ids
|
|
68
|
+
}
|
|
69
|
+
};
|
|
60
70
|
}
|
|
61
71
|
restore(snapshot) {
|
|
62
72
|
this.state = snapshot.state;
|
package/es/factory/index.js
CHANGED
|
@@ -3,11 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @param {SuperFactory} factory - the factory function to be used in creating the product object
|
|
5
5
|
* @return {(options: any) => any} the new factory function that creates the product object
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
return (options) => {
|
|
6
|
+
*/ export const superFactory = (factory)=>{
|
|
7
|
+
return (options)=>{
|
|
9
8
|
const product = {};
|
|
10
|
-
for (const key of Object.keys(options))
|
|
9
|
+
for (const key of Object.keys(options)){
|
|
11
10
|
product[key] = factory(options[key], key);
|
|
12
11
|
}
|
|
13
12
|
return product;
|
package/es/filter/sensor.js
CHANGED
|
@@ -16,7 +16,7 @@ export class SensorDataFilter {
|
|
|
16
16
|
value = NaN;
|
|
17
17
|
// 上一次正确返回的值
|
|
18
18
|
old;
|
|
19
|
-
constructor({ size = 5, step = 5, min = -Infinity, max = Infinity
|
|
19
|
+
constructor({ size = 5, step = 5, min = -Infinity, max = Infinity } = {}){
|
|
20
20
|
this.size = size;
|
|
21
21
|
this.step = step;
|
|
22
22
|
this.min = min;
|
|
@@ -45,8 +45,7 @@ export class SensorDataFilter {
|
|
|
45
45
|
// 累计跳变数据连续出现的次数
|
|
46
46
|
if (this.value !== n) {
|
|
47
47
|
this.count = 1;
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
48
|
+
} else {
|
|
50
49
|
this.count += 1;
|
|
51
50
|
}
|
|
52
51
|
this.value = n;
|
|
@@ -56,8 +55,7 @@ export class SensorDataFilter {
|
|
|
56
55
|
this.tmp.push(n);
|
|
57
56
|
return n;
|
|
58
57
|
}
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
58
|
+
} else {
|
|
61
59
|
// 清除跳变数据缓存
|
|
62
60
|
this.count = 0;
|
|
63
61
|
this.value = NaN;
|
|
@@ -66,19 +64,17 @@ export class SensorDataFilter {
|
|
|
66
64
|
return res;
|
|
67
65
|
}
|
|
68
66
|
/**
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
getMostNumberOfTmp() {
|
|
67
|
+
* A function to calculate the most frequent element in the 'tmp' array and its frequency.
|
|
68
|
+
*
|
|
69
|
+
* @return {object} An object containing the most frequent element and its frequency.
|
|
70
|
+
*/ getMostNumberOfTmp() {
|
|
74
71
|
const a = {};
|
|
75
72
|
let max = 0;
|
|
76
73
|
let res;
|
|
77
|
-
for (const item of this.tmp)
|
|
74
|
+
for (const item of this.tmp){
|
|
78
75
|
if (!a[item]) {
|
|
79
76
|
a[item] = 1;
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
77
|
+
} else {
|
|
82
78
|
a[item] += 1;
|
|
83
79
|
}
|
|
84
80
|
if (a[item] > max) {
|
|
@@ -86,7 +82,10 @@ export class SensorDataFilter {
|
|
|
86
82
|
res = item;
|
|
87
83
|
}
|
|
88
84
|
}
|
|
89
|
-
return {
|
|
85
|
+
return {
|
|
86
|
+
res,
|
|
87
|
+
dic: a
|
|
88
|
+
};
|
|
90
89
|
}
|
|
91
90
|
}
|
|
92
91
|
/**
|
|
@@ -94,11 +93,10 @@ export class SensorDataFilter {
|
|
|
94
93
|
*
|
|
95
94
|
* @param {string} str - the input string to be split
|
|
96
95
|
* @return {string[]} an array of substrings
|
|
97
|
-
*/
|
|
98
|
-
export class StringSplit {
|
|
96
|
+
*/ export class StringSplit {
|
|
99
97
|
_splitSymbol;
|
|
100
98
|
_cache = '';
|
|
101
|
-
constructor(splitSymbol)
|
|
99
|
+
constructor(splitSymbol){
|
|
102
100
|
this._splitSymbol = splitSymbol;
|
|
103
101
|
}
|
|
104
102
|
split(str) {
|
package/es/index.js
CHANGED
|
@@ -13,7 +13,7 @@ export { HistoryStack } from './stack';
|
|
|
13
13
|
export { WatchDog } from './watch-dog';
|
|
14
14
|
export { Level } from './level';
|
|
15
15
|
export { callbackToPromise } from './promise';
|
|
16
|
-
export { getAngle, getDistance, getCenter, degrees, radians, filterKeyPoints, getRotate, getPositionTheta, getBestPointIndex, orderByDistance, getVectorTheta
|
|
16
|
+
export { getAngle, getDistance, getCenter, degrees, radians, filterKeyPoints, getRotate, getPositionTheta, getBestPointIndex, orderByDistance, getVectorTheta } from './math/geometry';
|
|
17
17
|
export { lower2camel } from './str';
|
|
18
18
|
export { RegExpList } from './regexp-list';
|
|
19
19
|
export { SensorDataFilter, StringSplit } from './filter/sensor';
|
package/es/level/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export class Level {
|
|
2
2
|
config;
|
|
3
3
|
step;
|
|
4
|
-
constructor(props)
|
|
4
|
+
constructor(props){
|
|
5
5
|
this.config = props;
|
|
6
6
|
this.step = (this.config.max - this.config.min) / (this.config.count - 2);
|
|
7
7
|
}
|
|
@@ -12,7 +12,7 @@ export class Level {
|
|
|
12
12
|
if (value >= this.config.max) {
|
|
13
13
|
return this.config.count;
|
|
14
14
|
}
|
|
15
|
-
for
|
|
15
|
+
for(let i = 2, v = this.config.min + this.step; v < this.config.max + this.step; v += this.step, i += 1){
|
|
16
16
|
if (value < v) {
|
|
17
17
|
return i;
|
|
18
18
|
}
|
package/es/loop/index.js
CHANGED
|
@@ -5,24 +5,21 @@ import { sleep } from '../sleep';
|
|
|
5
5
|
* @param {Function} cb - The callback function to be executed in the loop. It takes a stop function as a parameter.
|
|
6
6
|
* @param {number} time - The time interval in milliseconds for the loop.
|
|
7
7
|
* @return {Function} The stop function that can be used to stop the loop.
|
|
8
|
-
*/
|
|
9
|
-
export const loop = (cb, time) => {
|
|
8
|
+
*/ export const loop = (cb, time)=>{
|
|
10
9
|
let onOff = true;
|
|
11
|
-
let stop = ()
|
|
10
|
+
let stop = ()=>{
|
|
12
11
|
onOff = false;
|
|
13
12
|
};
|
|
14
|
-
let fn = async ()
|
|
13
|
+
let fn = async ()=>{
|
|
15
14
|
try {
|
|
16
15
|
await cb();
|
|
17
|
-
}
|
|
18
|
-
catch (err) {
|
|
16
|
+
} catch (err) {
|
|
19
17
|
console.log(err);
|
|
20
18
|
}
|
|
21
19
|
await sleep(time);
|
|
22
20
|
if (onOff) {
|
|
23
21
|
fn();
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
22
|
+
} else {
|
|
26
23
|
fn = null;
|
|
27
24
|
cb = null;
|
|
28
25
|
stop = null;
|
|
@@ -41,11 +38,10 @@ export const loop = (cb, time) => {
|
|
|
41
38
|
* @param {number} options.checkTime - the time interval for checking the boolean condition (default is 100)
|
|
42
39
|
* @param {number} options.timeout - the maximum time to wait for the boolean condition to be true
|
|
43
40
|
* @return {Promise<any>} a promise that resolves when the boolean condition is true or rejects with 'timeout' if the timeout is reached
|
|
44
|
-
*/
|
|
45
|
-
|
|
46
|
-
return new Promise((resolve, reject) => {
|
|
41
|
+
*/ export const waitFor = (bool, { checkTime, timeout } = {})=>{
|
|
42
|
+
return new Promise((resolve, reject)=>{
|
|
47
43
|
let _timeout;
|
|
48
|
-
let stop = loop(async ()
|
|
44
|
+
let stop = loop(async ()=>{
|
|
49
45
|
const res = await bool();
|
|
50
46
|
if (res) {
|
|
51
47
|
if (typeof stop === 'function') {
|
|
@@ -62,7 +58,7 @@ export const waitFor = (bool, { checkTime, timeout } = {}) => {
|
|
|
62
58
|
}
|
|
63
59
|
}, checkTime || 100);
|
|
64
60
|
if (timeout) {
|
|
65
|
-
_timeout = setTimeout(()
|
|
61
|
+
_timeout = setTimeout(()=>{
|
|
66
62
|
if (typeof stop === 'function') {
|
|
67
63
|
stop();
|
|
68
64
|
}
|
package/es/math/geometry.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
// 获取两点的距离
|
|
2
|
-
export const getDistance = (a, b)
|
|
2
|
+
export const getDistance = (a, b)=>{
|
|
3
3
|
return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
|
|
4
4
|
};
|
|
5
5
|
// 弧度转角度
|
|
6
|
-
export const degrees = (rad)
|
|
7
|
-
return
|
|
6
|
+
export const degrees = (rad)=>{
|
|
7
|
+
return rad * 180 / Math.PI;
|
|
8
8
|
};
|
|
9
9
|
// 角度转弧度
|
|
10
|
-
export const radians = (deg)
|
|
11
|
-
return
|
|
10
|
+
export const radians = (deg)=>{
|
|
11
|
+
return deg * Math.PI / 180;
|
|
12
12
|
};
|
|
13
13
|
// 获取三角形角度
|
|
14
|
-
export const getAngle = ({ w, h })
|
|
14
|
+
export const getAngle = ({ w, h })=>{
|
|
15
15
|
return degrees(Math.atan2(h, w));
|
|
16
16
|
};
|
|
17
17
|
// 关键点过滤
|
|
@@ -20,7 +20,7 @@ export function filterKeyPoints(list, len = 0.5) {
|
|
|
20
20
|
return list;
|
|
21
21
|
}
|
|
22
22
|
let last;
|
|
23
|
-
return list.filter((item, i)
|
|
23
|
+
return list.filter((item, i)=>{
|
|
24
24
|
if (i > 0 && getDistance(last, item) < len) {
|
|
25
25
|
return false;
|
|
26
26
|
}
|
|
@@ -30,58 +30,78 @@ export function filterKeyPoints(list, len = 0.5) {
|
|
|
30
30
|
}
|
|
31
31
|
// 获取中心点坐标
|
|
32
32
|
export function getCenter(list) {
|
|
33
|
-
const tmp = [
|
|
34
|
-
|
|
33
|
+
const tmp = [
|
|
34
|
+
0,
|
|
35
|
+
0
|
|
36
|
+
];
|
|
37
|
+
for (const item of list){
|
|
35
38
|
if (Array.isArray(item)) {
|
|
36
39
|
tmp[0] += item[0];
|
|
37
40
|
tmp[1] += item[1];
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
41
|
+
} else {
|
|
40
42
|
tmp[0] += item.x;
|
|
41
43
|
tmp[1] += item.y;
|
|
42
44
|
}
|
|
43
45
|
}
|
|
44
|
-
return {
|
|
46
|
+
return {
|
|
47
|
+
x: tmp[0] / list.length,
|
|
48
|
+
y: tmp[1] / list.length
|
|
49
|
+
};
|
|
45
50
|
}
|
|
46
51
|
// 2D旋转
|
|
47
52
|
export function getRotate(data, theta) {
|
|
48
53
|
const x = Math.cos(theta) * data[0] - Math.sin(theta) * data[1];
|
|
49
54
|
const y = Math.sin(theta) * data[0] + Math.cos(theta) * data[1];
|
|
50
|
-
return [
|
|
55
|
+
return [
|
|
56
|
+
x,
|
|
57
|
+
y
|
|
58
|
+
];
|
|
51
59
|
}
|
|
52
60
|
// 获取A点到B点的方向角
|
|
53
|
-
export const getPositionTheta = (a, b)
|
|
54
|
-
const vector = {
|
|
61
|
+
export const getPositionTheta = (a, b)=>{
|
|
62
|
+
const vector = {
|
|
63
|
+
x: b.x - a.x,
|
|
64
|
+
y: b.y - a.y
|
|
65
|
+
};
|
|
55
66
|
return Math.atan2(vector.y, vector.x);
|
|
56
67
|
};
|
|
57
68
|
// 获取距离和方向最佳的位置点
|
|
58
|
-
export const getBestPointIndex = (points, pose)
|
|
69
|
+
export const getBestPointIndex = (points, pose)=>{
|
|
59
70
|
if (points.length < 2) {
|
|
60
71
|
return 0;
|
|
61
72
|
}
|
|
62
73
|
const temp = [];
|
|
63
74
|
let minDistance = Infinity;
|
|
64
75
|
let index = 0;
|
|
65
|
-
for (const item of points)
|
|
76
|
+
for (const item of points){
|
|
66
77
|
const distance = getDistance(item, pose);
|
|
67
78
|
const theta = getPositionTheta(pose, item) - pose.theta;
|
|
68
79
|
if (minDistance > distance) {
|
|
69
80
|
minDistance = distance;
|
|
70
81
|
}
|
|
71
|
-
temp.push({
|
|
82
|
+
temp.push({
|
|
83
|
+
...item,
|
|
84
|
+
index,
|
|
85
|
+
distance,
|
|
86
|
+
theta
|
|
87
|
+
});
|
|
72
88
|
index += 1;
|
|
73
89
|
}
|
|
74
|
-
const results = temp
|
|
75
|
-
.filter((item) => item.distance - minDistance < 0.1)
|
|
76
|
-
.sort((a, b) => a.theta - b.theta);
|
|
90
|
+
const results = temp.filter((item)=>item.distance - minDistance < 0.1).sort((a, b)=>a.theta - b.theta);
|
|
77
91
|
return results[0].index;
|
|
78
92
|
};
|
|
79
93
|
// 按照距离和方向排序
|
|
80
|
-
export const orderByDistance = (points, pose = {
|
|
94
|
+
export const orderByDistance = (points, pose = {
|
|
95
|
+
x: 0,
|
|
96
|
+
y: 0,
|
|
97
|
+
theta: 0
|
|
98
|
+
})=>{
|
|
81
99
|
let current = pose;
|
|
82
100
|
const results = [];
|
|
83
|
-
const arr = [
|
|
84
|
-
|
|
101
|
+
const arr = [
|
|
102
|
+
...points
|
|
103
|
+
];
|
|
104
|
+
while(arr.length > 0){
|
|
85
105
|
const index = getBestPointIndex(arr, current);
|
|
86
106
|
results.push(arr[index]);
|
|
87
107
|
current = arr[index];
|
|
@@ -90,6 +110,6 @@ export const orderByDistance = (points, pose = { x: 0, y: 0, theta: 0 }) => {
|
|
|
90
110
|
return results;
|
|
91
111
|
};
|
|
92
112
|
// 获取向量a到向量b的夹角
|
|
93
|
-
export const getVectorTheta = (a, b)
|
|
113
|
+
export const getVectorTheta = (a, b)=>{
|
|
94
114
|
return Math.atan2(b.y, b.x) - Math.atan2(a.y, a.x);
|
|
95
115
|
};
|
package/es/promise/index.js
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
// 回调函数转异步函数
|
|
2
|
-
export const callbackToPromise = (fn)
|
|
3
|
-
return (...args)
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
2
|
+
export const callbackToPromise = (fn)=>{
|
|
3
|
+
return (...args)=>new Promise((resolve, reject)=>{
|
|
4
|
+
fn(...args, (err, ...rest)=>{
|
|
5
|
+
if (err) {
|
|
6
|
+
reject(err);
|
|
7
|
+
}
|
|
8
|
+
if (rest.length < 2) {
|
|
9
|
+
resolve(rest[0]);
|
|
10
|
+
} else {
|
|
11
|
+
resolve([
|
|
12
|
+
...rest
|
|
13
|
+
]);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
fn = null;
|
|
14
17
|
});
|
|
15
|
-
fn = null;
|
|
16
|
-
});
|
|
17
18
|
};
|
package/es/queue/index.js
CHANGED
|
@@ -6,8 +6,7 @@ export class SyncQueue {
|
|
|
6
6
|
try {
|
|
7
7
|
const res = await fn();
|
|
8
8
|
promise.resolve(res);
|
|
9
|
-
}
|
|
10
|
-
catch (err) {
|
|
9
|
+
} catch (err) {
|
|
11
10
|
promise.reject(err);
|
|
12
11
|
}
|
|
13
12
|
fn = null;
|
|
@@ -17,8 +16,7 @@ export class SyncQueue {
|
|
|
17
16
|
if (this.cache.length < 1) {
|
|
18
17
|
this.running = false;
|
|
19
18
|
return;
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
19
|
+
} else {
|
|
22
20
|
this.running = true;
|
|
23
21
|
}
|
|
24
22
|
const item = this.cache.shift();
|
|
@@ -30,8 +28,14 @@ export class SyncQueue {
|
|
|
30
28
|
}
|
|
31
29
|
push(fn) {
|
|
32
30
|
this.len += 1;
|
|
33
|
-
return new Promise((resolve, reject)
|
|
34
|
-
this.cache.push({
|
|
31
|
+
return new Promise((resolve, reject)=>{
|
|
32
|
+
this.cache.push({
|
|
33
|
+
fn,
|
|
34
|
+
promise: {
|
|
35
|
+
resolve,
|
|
36
|
+
reject
|
|
37
|
+
}
|
|
38
|
+
});
|
|
35
39
|
if (!this.running) {
|
|
36
40
|
this.run();
|
|
37
41
|
}
|
package/es/random/index.js
CHANGED
|
@@ -3,20 +3,16 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @param {number} n - The length of the random string to generate
|
|
5
5
|
* @return {string} The randomly generated string
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const ra = (x) => Math.random()
|
|
9
|
-
.toString(32)
|
|
10
|
-
.slice(2, 2 + x);
|
|
6
|
+
*/ export const random = (n = 8)=>{
|
|
7
|
+
const ra = (x)=>Math.random().toString(32).slice(2, 2 + x);
|
|
11
8
|
if (n <= 8) {
|
|
12
9
|
return ra(n);
|
|
13
10
|
}
|
|
14
11
|
let res = '';
|
|
15
|
-
for
|
|
12
|
+
for(let a = 0; a <= n; a += 8){
|
|
16
13
|
if (n - a > 8) {
|
|
17
14
|
res += ra(8);
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
15
|
+
} else {
|
|
20
16
|
res += ra(n - a);
|
|
21
17
|
}
|
|
22
18
|
}
|
|
@@ -27,8 +23,7 @@ export const random = (n = 8) => {
|
|
|
27
23
|
*
|
|
28
24
|
* @param {number[]} range - The range within which to generate the random number.
|
|
29
25
|
* @return {number} The randomly generated number within the specified range.
|
|
30
|
-
*/
|
|
31
|
-
export const randomRangeNum = (range) => {
|
|
26
|
+
*/ export const randomRangeNum = (range)=>{
|
|
32
27
|
const size = Math.abs(range[1] - range[0]);
|
|
33
28
|
return Math.random() * size + Math.min(...range);
|
|
34
29
|
};
|
package/es/regexp-list/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export class RegExpList {
|
|
2
2
|
list;
|
|
3
3
|
_RegExpList = null;
|
|
4
|
-
constructor(list)
|
|
4
|
+
constructor(list){
|
|
5
5
|
this.list = list;
|
|
6
6
|
}
|
|
7
7
|
getRegEXP(item) {
|
|
@@ -12,6 +12,6 @@ export class RegExpList {
|
|
|
12
12
|
if (!this._RegExpList) {
|
|
13
13
|
this._RegExpList = this.list.map(this.getRegEXP);
|
|
14
14
|
}
|
|
15
|
-
return this._RegExpList.some((item)
|
|
15
|
+
return this._RegExpList.some((item)=>item.test(value));
|
|
16
16
|
}
|
|
17
17
|
}
|
package/es/sleep/index.js
CHANGED
|
@@ -3,10 +3,9 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @param {number} time - The duration in milliseconds to sleep
|
|
5
5
|
* @return {Promise<void>} A promise that resolves after the specified time
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
let timeout = setTimeout(() => {
|
|
6
|
+
*/ export const sleep = async (time)=>{
|
|
7
|
+
await new Promise((resolve)=>{
|
|
8
|
+
let timeout = setTimeout(()=>{
|
|
10
9
|
resolve();
|
|
11
10
|
clearTimeout(timeout);
|
|
12
11
|
timeout = null;
|
package/es/stack/index.js
CHANGED
package/es/str.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
// 下划线转驼峰
|
|
2
|
-
export const lower2camel = (str)
|
|
3
|
-
return str
|
|
4
|
-
.split('_')
|
|
5
|
-
.map((item, i) => (i > 0 ? item.slice(0, 1).toUpperCase() + item.slice(1) : item))
|
|
6
|
-
.join('');
|
|
2
|
+
export const lower2camel = (str)=>{
|
|
3
|
+
return str.split('_').map((item, i)=>i > 0 ? item.slice(0, 1).toUpperCase() + item.slice(1) : item).join('');
|
|
7
4
|
};
|
package/es/throttle/index.js
CHANGED
|
@@ -3,12 +3,12 @@ import { sleep } from '../sleep';
|
|
|
3
3
|
export function throttle(cb, time) {
|
|
4
4
|
const queue = new SyncQueue();
|
|
5
5
|
let payload;
|
|
6
|
-
return (...args)
|
|
6
|
+
return (...args)=>{
|
|
7
7
|
payload = args;
|
|
8
8
|
if (queue.length > 0) {
|
|
9
9
|
return;
|
|
10
10
|
}
|
|
11
|
-
return queue.push(async ()
|
|
11
|
+
return queue.push(async ()=>{
|
|
12
12
|
await sleep(time);
|
|
13
13
|
await cb(...payload);
|
|
14
14
|
payload = null;
|