pubo-utils 1.0.149 → 1.0.157
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/base64/index.js +33 -33
- package/es/color/utils.js +40 -55
- package/es/debounce/index.js +35 -40
- package/es/emitter/index.js +59 -279
- package/es/factory/index.js +9 -10
- package/es/filter/sensor.js +99 -126
- package/es/index.d.ts +1 -1
- package/es/index.js +3 -4
- package/es/level/index.js +20 -22
- package/es/loop/index.js +68 -123
- package/es/math/geometry.js +69 -98
- package/es/promise/index.js +15 -22
- package/es/queue/index.js +36 -115
- package/es/random/index.js +20 -22
- package/es/regexp-list/index.js +16 -20
- package/es/sleep/index.js +9 -28
- package/es/stack/index.js +34 -46
- package/es/str.js +6 -5
- package/es/throttle/index.js +17 -20
- package/es/trigger/index.js +22 -34
- package/es/watch-dog/index.js +29 -35
- package/lib/index.d.ts +1 -1
- package/lib/index.js +3 -3
- package/package.json +2 -2
package/es/emitter/index.js
CHANGED
|
@@ -1,295 +1,75 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var result = body();
|
|
9
|
-
if (result && result.then) {
|
|
10
|
-
return result.then(_empty);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
var _iteratorSymbol = /*#__PURE__*/typeof Symbol !== "undefined" ? Symbol.iterator || (Symbol.iterator = Symbol("Symbol.iterator")) : "@@iterator";
|
|
14
|
-
function _settle(pact, state, value) {
|
|
15
|
-
if (!pact.s) {
|
|
16
|
-
if (value instanceof _Pact) {
|
|
17
|
-
if (value.s) {
|
|
18
|
-
if (state & 1) {
|
|
19
|
-
state = value.s;
|
|
1
|
+
import { random } from '../random';
|
|
2
|
+
export class Emitter {
|
|
3
|
+
state = {};
|
|
4
|
+
ids = {};
|
|
5
|
+
on(event, func) {
|
|
6
|
+
if (!this.state[event]) {
|
|
7
|
+
this.state[event] = [];
|
|
20
8
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
value.o = _settle.bind(null, pact, state);
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
if (value && value.then) {
|
|
28
|
-
value.then(_settle.bind(null, pact, state), _settle.bind(null, pact, 2));
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
pact.s = state;
|
|
32
|
-
pact.v = value;
|
|
33
|
-
var observer = pact.o;
|
|
34
|
-
if (observer) {
|
|
35
|
-
observer(pact);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
var _Pact = /*#__PURE__*/function () {
|
|
40
|
-
function _Pact() {}
|
|
41
|
-
_Pact.prototype.then = function (onFulfilled, onRejected) {
|
|
42
|
-
var result = new _Pact();
|
|
43
|
-
var state = this.s;
|
|
44
|
-
if (state) {
|
|
45
|
-
var callback = state & 1 ? onFulfilled : onRejected;
|
|
46
|
-
if (callback) {
|
|
47
|
-
try {
|
|
48
|
-
_settle(result, 1, callback(this.v));
|
|
49
|
-
} catch (e) {
|
|
50
|
-
_settle(result, 2, e);
|
|
9
|
+
if (typeof func !== 'function') {
|
|
10
|
+
throw new Error('第二个参数必须为function!');
|
|
51
11
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
12
|
+
const index = this.state[event].push(func) - 1;
|
|
13
|
+
const key = random(40);
|
|
14
|
+
this.ids[key] = { event, index };
|
|
15
|
+
return key;
|
|
56
16
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (_this.s & 1) {
|
|
61
|
-
_settle(result, 1, onFulfilled ? onFulfilled(value) : value);
|
|
62
|
-
} else if (onRejected) {
|
|
63
|
-
_settle(result, 1, onRejected(value));
|
|
64
|
-
} else {
|
|
65
|
-
_settle(result, 2, value);
|
|
66
|
-
}
|
|
67
|
-
} catch (e) {
|
|
68
|
-
_settle(result, 2, e);
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
return result;
|
|
72
|
-
};
|
|
73
|
-
return _Pact;
|
|
74
|
-
}();
|
|
75
|
-
function _isSettledPact(thenable) {
|
|
76
|
-
return thenable instanceof _Pact && thenable.s & 1;
|
|
77
|
-
}
|
|
78
|
-
function _forTo(array, body, check) {
|
|
79
|
-
var i = -1,
|
|
80
|
-
pact,
|
|
81
|
-
reject;
|
|
82
|
-
function _cycle(result) {
|
|
83
|
-
try {
|
|
84
|
-
while (++i < array.length && (!check || !check())) {
|
|
85
|
-
result = body(i);
|
|
86
|
-
if (result && result.then) {
|
|
87
|
-
if (_isSettledPact(result)) {
|
|
88
|
-
result = result.v;
|
|
89
|
-
} else {
|
|
90
|
-
result.then(_cycle, reject || (reject = _settle.bind(null, pact = new _Pact(), 2)));
|
|
17
|
+
cancel(id) {
|
|
18
|
+
if (!id) {
|
|
19
|
+
this.clear();
|
|
91
20
|
return;
|
|
92
|
-
}
|
|
93
21
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
} else {
|
|
98
|
-
pact = result;
|
|
99
|
-
}
|
|
100
|
-
} catch (e) {
|
|
101
|
-
_settle(pact || (pact = new _Pact()), 2, e);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
_cycle();
|
|
105
|
-
return pact;
|
|
106
|
-
}
|
|
107
|
-
function _forOf(target, body, check) {
|
|
108
|
-
if (typeof target[_iteratorSymbol] === "function") {
|
|
109
|
-
var _cycle = function _cycle(result) {
|
|
110
|
-
try {
|
|
111
|
-
while (!(step = iterator.next()).done && (!check || !check())) {
|
|
112
|
-
result = body(step.value);
|
|
113
|
-
if (result && result.then) {
|
|
114
|
-
if (_isSettledPact(result)) {
|
|
115
|
-
result = result.v;
|
|
116
|
-
} else {
|
|
117
|
-
result.then(_cycle, reject || (reject = _settle.bind(null, pact = new _Pact(), 2)));
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
22
|
+
const { event, index } = this.ids[id] || {};
|
|
23
|
+
if (!event) {
|
|
24
|
+
return;
|
|
121
25
|
}
|
|
122
|
-
if (
|
|
123
|
-
|
|
124
|
-
} else {
|
|
125
|
-
pact = result;
|
|
26
|
+
if (!Array.isArray(this.state[event])) {
|
|
27
|
+
return;
|
|
126
28
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
pact,
|
|
134
|
-
reject;
|
|
135
|
-
_cycle();
|
|
136
|
-
if (iterator["return"]) {
|
|
137
|
-
var _fixup = function _fixup(value) {
|
|
138
|
-
try {
|
|
139
|
-
if (!step.done) {
|
|
140
|
-
iterator["return"]();
|
|
141
|
-
}
|
|
142
|
-
} catch (e) {}
|
|
143
|
-
return value;
|
|
144
|
-
};
|
|
145
|
-
if (pact && pact.then) {
|
|
146
|
-
return pact.then(_fixup, function (e) {
|
|
147
|
-
throw _fixup(e);
|
|
29
|
+
this.state[event].splice(index, 1);
|
|
30
|
+
delete this.ids[id];
|
|
31
|
+
Object.keys(this.ids).forEach((key) => {
|
|
32
|
+
if (this.ids[key].event === event && this.ids[key].index > index) {
|
|
33
|
+
this.ids[key].index = this.ids[key].index - 1;
|
|
34
|
+
}
|
|
148
35
|
});
|
|
149
|
-
}
|
|
150
|
-
_fixup();
|
|
151
36
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
// No support for Symbol.iterator
|
|
155
|
-
if (!("length" in target)) {
|
|
156
|
-
throw new TypeError("Object is not iterable");
|
|
157
|
-
}
|
|
158
|
-
// Handle live collections properly
|
|
159
|
-
var values = [];
|
|
160
|
-
for (var i = 0; i < target.length; i++) {
|
|
161
|
-
values.push(target[i]);
|
|
162
|
-
}
|
|
163
|
-
return _forTo(values, function (i) {
|
|
164
|
-
return body(values[i]);
|
|
165
|
-
}, check);
|
|
166
|
-
}
|
|
167
|
-
function _continueIgnored(value) {
|
|
168
|
-
if (value && value.then) {
|
|
169
|
-
return value.then(_empty);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
function _await(value, then, direct) {
|
|
173
|
-
if (direct) {
|
|
174
|
-
return then ? then(value) : value;
|
|
175
|
-
}
|
|
176
|
-
if (!value || !value.then) {
|
|
177
|
-
value = Promise.resolve(value);
|
|
178
|
-
}
|
|
179
|
-
return then ? value.then(then) : value;
|
|
180
|
-
}
|
|
181
|
-
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
|
|
182
|
-
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
183
|
-
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
184
|
-
function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
185
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
186
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
187
|
-
import { random } from '../random';
|
|
188
|
-
export var Emitter = /*#__PURE__*/function () {
|
|
189
|
-
function Emitter() {
|
|
190
|
-
this.state = {};
|
|
191
|
-
this.ids = {};
|
|
192
|
-
}
|
|
193
|
-
var _proto = Emitter.prototype;
|
|
194
|
-
_proto.on = function on(event, func) {
|
|
195
|
-
if (!this.state[event]) {
|
|
196
|
-
this.state[event] = [];
|
|
197
|
-
}
|
|
198
|
-
if (typeof func !== 'function') {
|
|
199
|
-
throw new Error('第二个参数必须为function!');
|
|
37
|
+
clear() {
|
|
38
|
+
this.state.length = 0;
|
|
200
39
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
_proto.cancel = function cancel(id) {
|
|
210
|
-
var _this = this;
|
|
211
|
-
if (!id) {
|
|
212
|
-
this.clear();
|
|
213
|
-
return;
|
|
40
|
+
emit(event, payload) {
|
|
41
|
+
if (Array.isArray(this.state[event])) {
|
|
42
|
+
for (const func of this.state[event]) {
|
|
43
|
+
if (typeof func === 'function') {
|
|
44
|
+
func(payload);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
214
48
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
49
|
+
async emitSync(event, payload) {
|
|
50
|
+
if (Array.isArray(this.state[event])) {
|
|
51
|
+
for (const func of this.state[event]) {
|
|
52
|
+
if (typeof func === 'function') {
|
|
53
|
+
await func(payload);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
220
57
|
}
|
|
221
|
-
|
|
222
|
-
|
|
58
|
+
clone() {
|
|
59
|
+
return { state: { ...this.state }, ids: { ...this.ids } };
|
|
223
60
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
if (_this.ids[key].event === event && _this.ids[key].index > index) {
|
|
228
|
-
_this.ids[key].index = _this.ids[key].index - 1;
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
};
|
|
232
|
-
_proto.clear = function clear() {
|
|
233
|
-
this.state.length = 0;
|
|
234
|
-
};
|
|
235
|
-
_proto.emit = function emit(event, payload) {
|
|
236
|
-
if (Array.isArray(this.state[event])) {
|
|
237
|
-
for (var _iterator = _createForOfIteratorHelperLoose(this.state[event]), _step; !(_step = _iterator()).done;) {
|
|
238
|
-
var func = _step.value;
|
|
239
|
-
if (typeof func === 'function') {
|
|
240
|
-
func(payload);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
61
|
+
restore(snapshot) {
|
|
62
|
+
this.state = snapshot.state;
|
|
63
|
+
this.ids = snapshot.ids;
|
|
243
64
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
return _continueIgnored(_forOf(_this2.state[event], function (func) {
|
|
251
|
-
return _invokeIgnored(function () {
|
|
252
|
-
if (typeof func === 'function') {
|
|
253
|
-
return _awaitIgnored(func(payload));
|
|
254
|
-
}
|
|
255
|
-
});
|
|
256
|
-
}));
|
|
257
|
-
}
|
|
258
|
-
}));
|
|
259
|
-
} catch (e) {
|
|
260
|
-
return Promise.reject(e);
|
|
65
|
+
}
|
|
66
|
+
export class CacheEmitter extends Emitter {
|
|
67
|
+
_cache = {};
|
|
68
|
+
emit(event, payload) {
|
|
69
|
+
this._cache[event] = payload;
|
|
70
|
+
super.emit(event, payload);
|
|
261
71
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
return {
|
|
265
|
-
state: _extends({}, this.state),
|
|
266
|
-
ids: _extends({}, this.ids)
|
|
267
|
-
};
|
|
268
|
-
};
|
|
269
|
-
_proto.restore = function restore(snapshot) {
|
|
270
|
-
this.state = snapshot.state;
|
|
271
|
-
this.ids = snapshot.ids;
|
|
272
|
-
};
|
|
273
|
-
return Emitter;
|
|
274
|
-
}();
|
|
275
|
-
export var CacheEmitter = /*#__PURE__*/function (_Emitter) {
|
|
276
|
-
_inheritsLoose(CacheEmitter, _Emitter);
|
|
277
|
-
function CacheEmitter() {
|
|
278
|
-
var _this3;
|
|
279
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
280
|
-
args[_key] = arguments[_key];
|
|
72
|
+
getState(event) {
|
|
73
|
+
return this._cache[event];
|
|
281
74
|
}
|
|
282
|
-
|
|
283
|
-
_this3._cache = {};
|
|
284
|
-
return _this3;
|
|
285
|
-
}
|
|
286
|
-
var _proto2 = CacheEmitter.prototype;
|
|
287
|
-
_proto2.emit = function emit(event, payload) {
|
|
288
|
-
this._cache[event] = payload;
|
|
289
|
-
_Emitter.prototype.emit.call(this, event, payload);
|
|
290
|
-
};
|
|
291
|
-
_proto2.getState = function getState(event) {
|
|
292
|
-
return this._cache[event];
|
|
293
|
-
};
|
|
294
|
-
return CacheEmitter;
|
|
295
|
-
}(Emitter);
|
|
75
|
+
}
|
package/es/factory/index.js
CHANGED
|
@@ -4,13 +4,12 @@
|
|
|
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
6
|
*/
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
};
|
|
7
|
+
export const superFactory = (factory) => {
|
|
8
|
+
return (options) => {
|
|
9
|
+
const product = {};
|
|
10
|
+
for (const key of Object.keys(options)) {
|
|
11
|
+
product[key] = factory(options[key], key);
|
|
12
|
+
}
|
|
13
|
+
return product;
|
|
14
|
+
};
|
|
15
|
+
};
|
package/es/filter/sensor.js
CHANGED
|
@@ -1,137 +1,110 @@
|
|
|
1
|
-
function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
2
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
3
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
4
1
|
// 过滤跳变数据
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
step = _ref$step === void 0 ? 5 : _ref$step,
|
|
28
|
-
_ref$min = _ref.min,
|
|
29
|
-
min = _ref$min === void 0 ? -Infinity : _ref$min,
|
|
30
|
-
_ref$max = _ref.max,
|
|
31
|
-
max = _ref$max === void 0 ? Infinity : _ref$max;
|
|
32
|
-
this.tmp = [];
|
|
33
|
-
this.size = void 0;
|
|
34
|
-
this.step = void 0;
|
|
35
|
-
this.min = void 0;
|
|
36
|
-
this.max = void 0;
|
|
37
|
-
this.count = 0;
|
|
38
|
-
this.value = NaN;
|
|
39
|
-
this.old = void 0;
|
|
40
|
-
this.size = size;
|
|
41
|
-
this.step = step;
|
|
42
|
-
this.min = min;
|
|
43
|
-
this.max = max;
|
|
44
|
-
}
|
|
45
|
-
var _proto = SensorDataFilter.prototype;
|
|
46
|
-
_proto.filter = function filter(n) {
|
|
47
|
-
// 溢出范围的值将被忽略
|
|
48
|
-
if (n < this.min || n > this.max) {
|
|
49
|
-
return this.old;
|
|
2
|
+
export class SensorDataFilter {
|
|
3
|
+
// 缓冲区
|
|
4
|
+
tmp = [];
|
|
5
|
+
// 最大缓存
|
|
6
|
+
size;
|
|
7
|
+
// 跳变步长
|
|
8
|
+
step;
|
|
9
|
+
// 最小值
|
|
10
|
+
min;
|
|
11
|
+
// 最大值
|
|
12
|
+
max;
|
|
13
|
+
// 与缓冲区不一致的值连续出现的次数 (不一致指的是与缓冲区中数量最多的数值不同且差值大于步长)
|
|
14
|
+
count = 0;
|
|
15
|
+
// 与缓冲区不一致的值
|
|
16
|
+
value = NaN;
|
|
17
|
+
// 上一次正确返回的值
|
|
18
|
+
old;
|
|
19
|
+
constructor({ size = 5, step = 5, min = -Infinity, max = Infinity, } = {}) {
|
|
20
|
+
this.size = size;
|
|
21
|
+
this.step = step;
|
|
22
|
+
this.min = min;
|
|
23
|
+
this.max = max;
|
|
50
24
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
// 缓冲区没有值时直接返回当前值
|
|
57
|
-
if (this.tmp.length < 1) {
|
|
58
|
-
return n;
|
|
59
|
-
}
|
|
60
|
-
if (this.tmp.length > this.size) {
|
|
61
|
-
this.tmp.shift();
|
|
62
|
-
}
|
|
63
|
-
var _this$getMostNumberOf = this.getMostNumberOfTmp(),
|
|
64
|
-
res = _this$getMostNumberOf.res,
|
|
65
|
-
dic = _this$getMostNumberOf.dic;
|
|
66
|
-
// 当前值与缓冲区数量最多的值不一致且差值大于步长
|
|
67
|
-
if (res !== n && Math.abs(res - n) > this.step) {
|
|
68
|
-
// 累计跳变数据连续出现的次数
|
|
69
|
-
if (this.value !== n) {
|
|
70
|
-
this.count = 1;
|
|
71
|
-
} else {
|
|
72
|
-
this.count += 1;
|
|
73
|
-
}
|
|
74
|
-
this.value = n;
|
|
75
|
-
// 跳变数据出现次数已经大于缓冲区最多的数量,表示数据确实已经改变
|
|
76
|
-
if (this.count > dic[res]) {
|
|
77
|
-
this.tmp.length = 0;
|
|
25
|
+
filter(n) {
|
|
26
|
+
// 溢出范围的值将被忽略
|
|
27
|
+
if (n < this.min || n > this.max) {
|
|
28
|
+
return this.old;
|
|
29
|
+
}
|
|
78
30
|
this.tmp.push(n);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
31
|
+
this.old = this.calc(n);
|
|
32
|
+
return this.old;
|
|
33
|
+
}
|
|
34
|
+
calc(n) {
|
|
35
|
+
// 缓冲区没有值时直接返回当前值
|
|
36
|
+
if (this.tmp.length < 1) {
|
|
37
|
+
return n;
|
|
38
|
+
}
|
|
39
|
+
if (this.tmp.length > this.size) {
|
|
40
|
+
this.tmp.shift();
|
|
41
|
+
}
|
|
42
|
+
const { res, dic } = this.getMostNumberOfTmp();
|
|
43
|
+
// 当前值与缓冲区数量最多的值不一致且差值大于步长
|
|
44
|
+
if (res !== n && Math.abs(res - n) > this.step) {
|
|
45
|
+
// 累计跳变数据连续出现的次数
|
|
46
|
+
if (this.value !== n) {
|
|
47
|
+
this.count = 1;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
this.count += 1;
|
|
51
|
+
}
|
|
52
|
+
this.value = n;
|
|
53
|
+
// 跳变数据出现次数已经大于缓冲区最多的数量,表示数据确实已经改变
|
|
54
|
+
if (this.count > dic[res]) {
|
|
55
|
+
this.tmp.length = 0;
|
|
56
|
+
this.tmp.push(n);
|
|
57
|
+
return n;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
// 清除跳变数据缓存
|
|
62
|
+
this.count = 0;
|
|
63
|
+
this.value = NaN;
|
|
64
|
+
return n;
|
|
65
|
+
}
|
|
66
|
+
return res;
|
|
86
67
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
68
|
+
/**
|
|
69
|
+
* A function to calculate the most frequent element in the 'tmp' array and its frequency.
|
|
70
|
+
*
|
|
71
|
+
* @return {object} An object containing the most frequent element and its frequency.
|
|
72
|
+
*/
|
|
73
|
+
getMostNumberOfTmp() {
|
|
74
|
+
const a = {};
|
|
75
|
+
let max = 0;
|
|
76
|
+
let res;
|
|
77
|
+
for (const item of this.tmp) {
|
|
78
|
+
if (!a[item]) {
|
|
79
|
+
a[item] = 1;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
a[item] += 1;
|
|
83
|
+
}
|
|
84
|
+
if (a[item] > max) {
|
|
85
|
+
max = a[item];
|
|
86
|
+
res = item;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return { res, dic: a };
|
|
109
90
|
}
|
|
110
|
-
|
|
111
|
-
res: res,
|
|
112
|
-
dic: a
|
|
113
|
-
};
|
|
114
|
-
};
|
|
115
|
-
return SensorDataFilter;
|
|
116
|
-
}();
|
|
91
|
+
}
|
|
117
92
|
/**
|
|
118
93
|
* Splits the input string using the specified split symbol and returns an array of substrings.
|
|
119
94
|
*
|
|
120
95
|
* @param {string} str - the input string to be split
|
|
121
96
|
* @return {string[]} an array of substrings
|
|
122
97
|
*/
|
|
123
|
-
export
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
return StringSplit;
|
|
137
|
-
}();
|
|
98
|
+
export class StringSplit {
|
|
99
|
+
_splitSymbol;
|
|
100
|
+
_cache = '';
|
|
101
|
+
constructor(splitSymbol) {
|
|
102
|
+
this._splitSymbol = splitSymbol;
|
|
103
|
+
}
|
|
104
|
+
split(str) {
|
|
105
|
+
const tmp = this._cache + str;
|
|
106
|
+
const arr = tmp.split(this._splitSymbol);
|
|
107
|
+
this._cache = arr.splice(arr.length - 1, 1)[0];
|
|
108
|
+
return arr;
|
|
109
|
+
}
|
|
110
|
+
}
|
package/es/index.d.ts
CHANGED
|
@@ -14,6 +14,6 @@ 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
18
|
export { RegExpList } from './regexp-list';
|
|
18
19
|
export { SensorDataFilter, StringSplit } from './filter/sensor';
|
|
19
|
-
export { lower2camel } from './str';
|
package/es/index.js
CHANGED
|
@@ -8,13 +8,12 @@ export { random, randomRangeNum } from './random';
|
|
|
8
8
|
export { sleep } from './sleep';
|
|
9
9
|
export { throttle } from './throttle';
|
|
10
10
|
export { ContinuousTrigger } from './trigger';
|
|
11
|
-
|
|
12
|
-
export { _Base64Utils as Base64Utils };
|
|
11
|
+
export * as Base64Utils from './base64';
|
|
13
12
|
export { HistoryStack } from './stack';
|
|
14
13
|
export { WatchDog } from './watch-dog';
|
|
15
14
|
export { Level } from './level';
|
|
16
15
|
export { callbackToPromise } from './promise';
|
|
17
|
-
export { getAngle, getDistance, getCenter, degrees, radians, filterKeyPoints, getRotate, getPositionTheta, getBestPointIndex, orderByDistance, getVectorTheta } from './math/geometry';
|
|
16
|
+
export { getAngle, getDistance, getCenter, degrees, radians, filterKeyPoints, getRotate, getPositionTheta, getBestPointIndex, orderByDistance, getVectorTheta, } from './math/geometry';
|
|
17
|
+
export { lower2camel } from './str';
|
|
18
18
|
export { RegExpList } from './regexp-list';
|
|
19
19
|
export { SensorDataFilter, StringSplit } from './filter/sensor';
|
|
20
|
-
export { lower2camel } from './str';
|