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/trigger/index.js
CHANGED
|
@@ -2,7 +2,7 @@ export class ContinuousTrigger {
|
|
|
2
2
|
timeout;
|
|
3
3
|
_count = 0;
|
|
4
4
|
props;
|
|
5
|
-
constructor(props)
|
|
5
|
+
constructor(props){
|
|
6
6
|
this.props = props;
|
|
7
7
|
}
|
|
8
8
|
get count() {
|
|
@@ -10,7 +10,7 @@ export class ContinuousTrigger {
|
|
|
10
10
|
}
|
|
11
11
|
increment() {
|
|
12
12
|
clearTimeout(this.timeout);
|
|
13
|
-
this.timeout = setTimeout(()
|
|
13
|
+
this.timeout = setTimeout(()=>this.clear(), this.props.resetTime);
|
|
14
14
|
this._count = this._count + 1;
|
|
15
15
|
if (this._count > this.props.count) {
|
|
16
16
|
this.props.cb();
|
package/es/watch-dog/index.js
CHANGED
|
@@ -2,9 +2,9 @@ export class WatchDog {
|
|
|
2
2
|
onTimeout;
|
|
3
3
|
timeout = null;
|
|
4
4
|
_time;
|
|
5
|
-
constructor({ limit = 10, onTimeout })
|
|
5
|
+
constructor({ limit = 10, onTimeout }){
|
|
6
6
|
this._time = limit * 1000;
|
|
7
|
-
this.onTimeout = ()
|
|
7
|
+
this.onTimeout = ()=>{
|
|
8
8
|
if (this.timeout) {
|
|
9
9
|
clearTimeout(this.timeout);
|
|
10
10
|
this.timeout = null;
|
package/lib/base64/index.js
CHANGED
|
@@ -1,31 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
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
|
+
EncoderMap: function() {
|
|
13
|
+
return EncoderMap;
|
|
14
|
+
},
|
|
15
|
+
toUnit8Array: function() {
|
|
16
|
+
return toUnit8Array;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
const EncoderMap = function() {
|
|
6
20
|
const S = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
7
21
|
const e = {};
|
|
8
|
-
for
|
|
22
|
+
for(let i = 0; i < 64; i++){
|
|
9
23
|
e[S.charAt(i)] = i;
|
|
10
24
|
}
|
|
11
25
|
return e;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* base64 转Uint8Array
|
|
15
|
-
* @param input base64 字符串
|
|
16
|
-
* @param output Uint8Array
|
|
17
|
-
* base64 6位的二进制, Uint8Array 8位的二进制,整体原理就是将 base64 的每个字符转成2进制然后拼接,再按每个八位为一个单位地取出
|
|
18
|
-
*/
|
|
26
|
+
}();
|
|
19
27
|
function toUnit8Array(input) {
|
|
20
|
-
const output = new Uint8Array(
|
|
28
|
+
const output = new Uint8Array(input.length * 6 / 8);
|
|
21
29
|
// 当前二进制字符长度
|
|
22
30
|
let l = 0;
|
|
23
31
|
// 二进制字符缓存值(格式为10进制)
|
|
24
32
|
let b = 0;
|
|
25
33
|
// Uint8Array 的指针
|
|
26
34
|
let j = 0;
|
|
27
|
-
for
|
|
28
|
-
b = (b << 6) +
|
|
35
|
+
for(let x = 0; x < input.length && j < output.length; x += 1){
|
|
36
|
+
b = (b << 6) + EncoderMap[input.charAt(x)];
|
|
29
37
|
// 为什么是6? base64 为64个字符,也就是 2^6,用二进制表示就是6位
|
|
30
38
|
l += 6;
|
|
31
39
|
// 为什么是8? Uint8Array 是八位的二进制
|
package/lib/color/utils.js
CHANGED
|
@@ -1,47 +1,68 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
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
|
+
ColorUtils: function() {
|
|
13
|
+
return ColorUtils;
|
|
14
|
+
},
|
|
15
|
+
hex2rgb: function() {
|
|
16
|
+
return hex2rgb;
|
|
17
|
+
},
|
|
18
|
+
rgb2hex: function() {
|
|
19
|
+
return rgb2hex;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
const str2int = (str)=>parseInt(str.replace('#', ''), 16);
|
|
23
|
+
const hex2rgb = (n)=>{
|
|
6
24
|
let v;
|
|
7
25
|
if (typeof n === 'string') {
|
|
8
26
|
v = str2int(n);
|
|
9
|
-
}
|
|
10
|
-
else {
|
|
27
|
+
} else {
|
|
11
28
|
v = n;
|
|
12
29
|
}
|
|
13
30
|
const str = v.toString(2).padStart(24, '0');
|
|
14
31
|
const r = parseInt(str.slice(0, 8), 2);
|
|
15
32
|
const g = parseInt(str.slice(8, 16), 2);
|
|
16
33
|
const b = parseInt(str.slice(16, 24), 2);
|
|
17
|
-
return [
|
|
34
|
+
return [
|
|
35
|
+
r,
|
|
36
|
+
g,
|
|
37
|
+
b
|
|
38
|
+
];
|
|
18
39
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
40
|
+
const rgb2hex = (color)=>{
|
|
41
|
+
return '#' + [
|
|
42
|
+
0,
|
|
43
|
+
1,
|
|
44
|
+
2
|
|
45
|
+
].map((i)=>color[i].toString(16)).join('');
|
|
22
46
|
};
|
|
23
|
-
exports.rgb2hex = rgb2hex;
|
|
24
47
|
class ColorUtils {
|
|
25
48
|
int;
|
|
26
|
-
constructor(n)
|
|
49
|
+
constructor(n){
|
|
27
50
|
if (typeof n === 'number') {
|
|
28
51
|
this.int = n;
|
|
29
|
-
}
|
|
30
|
-
else if (typeof n === 'string') {
|
|
52
|
+
} else if (typeof n === 'string') {
|
|
31
53
|
this.int = str2int(n);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
this.int = str2int((0, exports.rgb2hex)(n));
|
|
54
|
+
} else if (Array.isArray(n)) {
|
|
55
|
+
this.int = str2int(rgb2hex(n));
|
|
35
56
|
}
|
|
36
57
|
}
|
|
37
58
|
getRgbArray() {
|
|
38
|
-
return
|
|
59
|
+
return hex2rgb(this.int);
|
|
39
60
|
}
|
|
40
61
|
get rgb() {
|
|
41
62
|
return `rba(${this.getRgbArray().join(', ')})`;
|
|
42
63
|
}
|
|
43
64
|
get hex() {
|
|
44
|
-
return
|
|
65
|
+
return rgb2hex(this.getRgbArray());
|
|
45
66
|
}
|
|
46
67
|
toString(type = 'hex') {
|
|
47
68
|
if (type === 'rgb') {
|
|
@@ -50,4 +71,3 @@ class ColorUtils {
|
|
|
50
71
|
return this.hex;
|
|
51
72
|
}
|
|
52
73
|
}
|
|
53
|
-
exports.ColorUtils = ColorUtils;
|
package/lib/debounce/index.js
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "debounce", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return debounce;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const debounce = (cb, time, first = false)=>{
|
|
5
12
|
if (first) {
|
|
6
13
|
let shouldRun = true;
|
|
7
14
|
let t;
|
|
8
|
-
return (...args)
|
|
15
|
+
return (...args)=>{
|
|
9
16
|
if (shouldRun) {
|
|
10
17
|
cb(...args);
|
|
11
18
|
shouldRun = false;
|
|
@@ -13,21 +20,20 @@ const debounce = (cb, time, first = false) => {
|
|
|
13
20
|
if (t) {
|
|
14
21
|
clearTimeout(t);
|
|
15
22
|
}
|
|
16
|
-
t = setTimeout(()
|
|
23
|
+
t = setTimeout(()=>{
|
|
17
24
|
clearTimeout(t);
|
|
18
25
|
shouldRun = true;
|
|
19
26
|
t = null;
|
|
20
27
|
}, time);
|
|
21
28
|
};
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
29
|
+
} else {
|
|
24
30
|
let t;
|
|
25
|
-
return (...args)
|
|
31
|
+
return (...args)=>{
|
|
26
32
|
if (t) {
|
|
27
33
|
clearTimeout(t);
|
|
28
34
|
t = null;
|
|
29
35
|
}
|
|
30
|
-
t = setTimeout(()
|
|
36
|
+
t = setTimeout(()=>{
|
|
31
37
|
cb(...args);
|
|
32
38
|
clearTimeout(t);
|
|
33
39
|
args = null;
|
|
@@ -36,4 +42,3 @@ const debounce = (cb, time, first = false) => {
|
|
|
36
42
|
};
|
|
37
43
|
}
|
|
38
44
|
};
|
|
39
|
-
exports.debounce = debounce;
|
package/lib/emitter/index.js
CHANGED
|
@@ -1,7 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
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
|
+
CacheEmitter: function() {
|
|
13
|
+
return CacheEmitter;
|
|
14
|
+
},
|
|
15
|
+
Emitter: function() {
|
|
16
|
+
return Emitter;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
const _random = require("../random");
|
|
5
20
|
class Emitter {
|
|
6
21
|
state = {};
|
|
7
22
|
ids = {};
|
|
@@ -13,8 +28,11 @@ class Emitter {
|
|
|
13
28
|
throw new Error('第二个参数必须为function!');
|
|
14
29
|
}
|
|
15
30
|
const index = this.state[event].push(func) - 1;
|
|
16
|
-
const key = (0,
|
|
17
|
-
this.ids[key] = {
|
|
31
|
+
const key = (0, _random.random)(40);
|
|
32
|
+
this.ids[key] = {
|
|
33
|
+
event,
|
|
34
|
+
index
|
|
35
|
+
};
|
|
18
36
|
return key;
|
|
19
37
|
}
|
|
20
38
|
cancel(id) {
|
|
@@ -31,7 +49,7 @@ class Emitter {
|
|
|
31
49
|
}
|
|
32
50
|
this.state[event].splice(index, 1);
|
|
33
51
|
delete this.ids[id];
|
|
34
|
-
Object.keys(this.ids).forEach((key)
|
|
52
|
+
Object.keys(this.ids).forEach((key)=>{
|
|
35
53
|
if (this.ids[key].event === event && this.ids[key].index > index) {
|
|
36
54
|
this.ids[key].index = this.ids[key].index - 1;
|
|
37
55
|
}
|
|
@@ -42,7 +60,7 @@ class Emitter {
|
|
|
42
60
|
}
|
|
43
61
|
emit(event, payload) {
|
|
44
62
|
if (Array.isArray(this.state[event])) {
|
|
45
|
-
for (const func of this.state[event])
|
|
63
|
+
for (const func of this.state[event]){
|
|
46
64
|
if (typeof func === 'function') {
|
|
47
65
|
func(payload);
|
|
48
66
|
}
|
|
@@ -51,7 +69,7 @@ class Emitter {
|
|
|
51
69
|
}
|
|
52
70
|
async emitSync(event, payload) {
|
|
53
71
|
if (Array.isArray(this.state[event])) {
|
|
54
|
-
for (const func of this.state[event])
|
|
72
|
+
for (const func of this.state[event]){
|
|
55
73
|
if (typeof func === 'function') {
|
|
56
74
|
await func(payload);
|
|
57
75
|
}
|
|
@@ -59,14 +77,20 @@ class Emitter {
|
|
|
59
77
|
}
|
|
60
78
|
}
|
|
61
79
|
clone() {
|
|
62
|
-
return {
|
|
80
|
+
return {
|
|
81
|
+
state: {
|
|
82
|
+
...this.state
|
|
83
|
+
},
|
|
84
|
+
ids: {
|
|
85
|
+
...this.ids
|
|
86
|
+
}
|
|
87
|
+
};
|
|
63
88
|
}
|
|
64
89
|
restore(snapshot) {
|
|
65
90
|
this.state = snapshot.state;
|
|
66
91
|
this.ids = snapshot.ids;
|
|
67
92
|
}
|
|
68
93
|
}
|
|
69
|
-
exports.Emitter = Emitter;
|
|
70
94
|
class CacheEmitter extends Emitter {
|
|
71
95
|
_cache = {};
|
|
72
96
|
emit(event, payload) {
|
|
@@ -77,4 +101,3 @@ class CacheEmitter extends Emitter {
|
|
|
77
101
|
return this._cache[event];
|
|
78
102
|
}
|
|
79
103
|
}
|
|
80
|
-
exports.CacheEmitter = CacheEmitter;
|
package/lib/factory/index.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "superFactory", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return superFactory;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const superFactory = (factory)=>{
|
|
12
|
+
return (options)=>{
|
|
12
13
|
const product = {};
|
|
13
|
-
for (const key of Object.keys(options))
|
|
14
|
+
for (const key of Object.keys(options)){
|
|
14
15
|
product[key] = factory(options[key], key);
|
|
15
16
|
}
|
|
16
17
|
return product;
|
|
17
18
|
};
|
|
18
19
|
};
|
|
19
|
-
exports.superFactory = superFactory;
|
package/lib/filter/sensor.js
CHANGED
|
@@ -1,7 +1,22 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StringSplit = exports.SensorDataFilter = void 0;
|
|
4
1
|
// 过滤跳变数据
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
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
|
+
SensorDataFilter: function() {
|
|
14
|
+
return SensorDataFilter;
|
|
15
|
+
},
|
|
16
|
+
StringSplit: function() {
|
|
17
|
+
return StringSplit;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
5
20
|
class SensorDataFilter {
|
|
6
21
|
// 缓冲区
|
|
7
22
|
tmp = [];
|
|
@@ -19,7 +34,7 @@ class SensorDataFilter {
|
|
|
19
34
|
value = NaN;
|
|
20
35
|
// 上一次正确返回的值
|
|
21
36
|
old;
|
|
22
|
-
constructor({ size = 5, step = 5, min = -Infinity, max = Infinity
|
|
37
|
+
constructor({ size = 5, step = 5, min = -Infinity, max = Infinity } = {}){
|
|
23
38
|
this.size = size;
|
|
24
39
|
this.step = step;
|
|
25
40
|
this.min = min;
|
|
@@ -48,8 +63,7 @@ class SensorDataFilter {
|
|
|
48
63
|
// 累计跳变数据连续出现的次数
|
|
49
64
|
if (this.value !== n) {
|
|
50
65
|
this.count = 1;
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
66
|
+
} else {
|
|
53
67
|
this.count += 1;
|
|
54
68
|
}
|
|
55
69
|
this.value = n;
|
|
@@ -59,8 +73,7 @@ class SensorDataFilter {
|
|
|
59
73
|
this.tmp.push(n);
|
|
60
74
|
return n;
|
|
61
75
|
}
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
76
|
+
} else {
|
|
64
77
|
// 清除跳变数据缓存
|
|
65
78
|
this.count = 0;
|
|
66
79
|
this.value = NaN;
|
|
@@ -69,19 +82,17 @@ class SensorDataFilter {
|
|
|
69
82
|
return res;
|
|
70
83
|
}
|
|
71
84
|
/**
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
getMostNumberOfTmp() {
|
|
85
|
+
* A function to calculate the most frequent element in the 'tmp' array and its frequency.
|
|
86
|
+
*
|
|
87
|
+
* @return {object} An object containing the most frequent element and its frequency.
|
|
88
|
+
*/ getMostNumberOfTmp() {
|
|
77
89
|
const a = {};
|
|
78
90
|
let max = 0;
|
|
79
91
|
let res;
|
|
80
|
-
for (const item of this.tmp)
|
|
92
|
+
for (const item of this.tmp){
|
|
81
93
|
if (!a[item]) {
|
|
82
94
|
a[item] = 1;
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
95
|
+
} else {
|
|
85
96
|
a[item] += 1;
|
|
86
97
|
}
|
|
87
98
|
if (a[item] > max) {
|
|
@@ -89,20 +100,16 @@ class SensorDataFilter {
|
|
|
89
100
|
res = item;
|
|
90
101
|
}
|
|
91
102
|
}
|
|
92
|
-
return {
|
|
103
|
+
return {
|
|
104
|
+
res,
|
|
105
|
+
dic: a
|
|
106
|
+
};
|
|
93
107
|
}
|
|
94
108
|
}
|
|
95
|
-
exports.SensorDataFilter = SensorDataFilter;
|
|
96
|
-
/**
|
|
97
|
-
* Splits the input string using the specified split symbol and returns an array of substrings.
|
|
98
|
-
*
|
|
99
|
-
* @param {string} str - the input string to be split
|
|
100
|
-
* @return {string[]} an array of substrings
|
|
101
|
-
*/
|
|
102
109
|
class StringSplit {
|
|
103
110
|
_splitSymbol;
|
|
104
111
|
_cache = '';
|
|
105
|
-
constructor(splitSymbol)
|
|
112
|
+
constructor(splitSymbol){
|
|
106
113
|
this._splitSymbol = splitSymbol;
|
|
107
114
|
}
|
|
108
115
|
split(str) {
|
|
@@ -112,4 +119,3 @@ class StringSplit {
|
|
|
112
119
|
return arr;
|
|
113
120
|
}
|
|
114
121
|
}
|
|
115
|
-
exports.StringSplit = StringSplit;
|