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/es/level/index.js CHANGED
@@ -1,24 +1,22 @@
1
- export var Level = /*#__PURE__*/function () {
2
- function Level(props) {
3
- this.config = void 0;
4
- this.step = void 0;
5
- this.config = props;
6
- this.step = (this.config.max - this.config.min) / (this.config.count - 2);
7
- }
8
- var _proto = Level.prototype;
9
- _proto.get = function get(value) {
10
- if (value <= this.config.min) {
11
- return 1;
1
+ export class Level {
2
+ config;
3
+ step;
4
+ constructor(props) {
5
+ this.config = props;
6
+ this.step = (this.config.max - this.config.min) / (this.config.count - 2);
12
7
  }
13
- if (value >= this.config.max) {
14
- return this.config.count;
8
+ get(value) {
9
+ if (value <= this.config.min) {
10
+ return 1;
11
+ }
12
+ if (value >= this.config.max) {
13
+ return this.config.count;
14
+ }
15
+ for (let i = 2, v = this.config.min + this.step; v < this.config.max + this.step; v += this.step, i += 1) {
16
+ if (value < v) {
17
+ return i;
18
+ }
19
+ }
20
+ return this.config.count;
15
21
  }
16
- for (var i = 2, v = this.config.min + this.step; v < this.config.max + this.step; v += this.step, i += 1) {
17
- if (value < v) {
18
- return i;
19
- }
20
- }
21
- return this.config.count;
22
- };
23
- return Level;
24
- }();
22
+ }
package/es/loop/index.js CHANGED
@@ -6,130 +6,75 @@ import { sleep } from '../sleep';
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
8
  */
9
-
10
- function _empty() {} /**
11
- * Waits for a boolean condition to be true, with optional timeouts for the check and the overall wait.
12
- *
13
- * @param {WaitForBool} bool - the boolean condition to wait for
14
- * @param {Object} options - optional parameters for checkTime and timeout
15
- * @param {number} options.checkTime - the time interval for checking the boolean condition (default is 100)
16
- * @param {number} options.timeout - the maximum time to wait for the boolean condition to be true
17
- * @return {Promise<any>} a promise that resolves when the boolean condition is true or rejects with 'timeout' if the timeout is reached
18
- */
19
-
20
- function _call(body, then, direct) {
21
- if (direct) {
22
- return then ? then(body()) : body();
23
- }
24
- try {
25
- var result = Promise.resolve(body());
26
- return then ? result.then(then) : result;
27
- } catch (e) {
28
- return Promise.reject(e);
29
- }
30
- }
31
- function _callIgnored(body, direct) {
32
- return _call(body, _empty, direct);
33
- }
34
- function _catch(body, recover) {
35
- try {
36
- var result = body();
37
- } catch (e) {
38
- return recover(e);
39
- }
40
- if (result && result.then) {
41
- return result.then(void 0, recover);
42
- }
43
- return result;
44
- }
45
- function _await(value, then, direct) {
46
- if (direct) {
47
- return then ? then(value) : value;
48
- }
49
- if (!value || !value.then) {
50
- value = Promise.resolve(value);
51
- }
52
- return then ? value.then(then) : value;
53
- }
54
- function _continue(value, then) {
55
- return value && value.then ? value.then(then) : then(value);
56
- }
57
- function _async(f) {
58
- return function () {
59
- for (var args = [], i = 0; i < arguments.length; i++) {
60
- args[i] = arguments[i];
61
- }
62
- try {
63
- return Promise.resolve(f.apply(this, args));
64
- } catch (e) {
65
- return Promise.reject(e);
66
- }
67
- };
68
- }
69
- export var loop = function loop(cb, time) {
70
- var onOff = true;
71
- var stop = function stop() {
72
- onOff = false;
73
- };
74
- var fn = _async(function () {
75
- return _continue(_catch(function () {
76
- return _callIgnored(cb);
77
- }, function (err) {
78
- console.log(err);
79
- }), function () {
80
- return _await(sleep(time), function () {
81
- if (onOff) {
82
- fn();
83
- } else {
84
- fn = null;
85
- cb = null;
86
- stop = null;
87
- onOff = null;
88
- time = null;
9
+ export const loop = (cb, time) => {
10
+ let onOff = true;
11
+ let stop = () => {
12
+ onOff = false;
13
+ };
14
+ let fn = async () => {
15
+ try {
16
+ await cb();
89
17
  }
90
- });
91
- });
92
- });
93
- fn();
94
- return stop;
95
- };
96
- export var waitFor = function waitFor(bool, _temp) {
97
- var _ref = _temp === void 0 ? {} : _temp,
98
- checkTime = _ref.checkTime,
99
- timeout = _ref.timeout;
100
- return new Promise(function (resolve, reject) {
101
- var _timeout;
102
- var stop = loop(function () {
103
- return _call(bool, function (res) {
104
- if (res) {
105
- if (typeof stop === 'function') {
106
- stop();
107
- }
108
- if (_timeout) {
109
- clearTimeout(_timeout);
110
- _timeout = null;
111
- }
112
- resolve(res);
113
- stop = null;
114
- bool = null;
115
- resolve = null;
18
+ catch (err) {
19
+ console.log(err);
116
20
  }
117
- });
118
- }, checkTime || 100);
119
- if (timeout) {
120
- _timeout = setTimeout(function () {
121
- if (typeof stop === 'function') {
122
- stop();
21
+ await sleep(time);
22
+ if (onOff) {
23
+ fn();
123
24
  }
124
- if (_timeout) {
125
- clearTimeout(_timeout);
126
- _timeout = null;
25
+ else {
26
+ fn = null;
27
+ cb = null;
28
+ stop = null;
29
+ onOff = null;
30
+ time = null;
127
31
  }
128
- reject('timeout');
129
- reject = null;
130
- stop = null;
131
- bool = null;
132
- }, timeout);
133
- }
134
- });
135
- };
32
+ };
33
+ fn();
34
+ return stop;
35
+ };
36
+ /**
37
+ * Waits for a boolean condition to be true, with optional timeouts for the check and the overall wait.
38
+ *
39
+ * @param {WaitForBool} bool - the boolean condition to wait for
40
+ * @param {Object} options - optional parameters for checkTime and timeout
41
+ * @param {number} options.checkTime - the time interval for checking the boolean condition (default is 100)
42
+ * @param {number} options.timeout - the maximum time to wait for the boolean condition to be true
43
+ * @return {Promise<any>} a promise that resolves when the boolean condition is true or rejects with 'timeout' if the timeout is reached
44
+ */
45
+ export const waitFor = (bool, { checkTime, timeout } = {}) => {
46
+ return new Promise((resolve, reject) => {
47
+ let _timeout;
48
+ let stop = loop(async () => {
49
+ const res = await bool();
50
+ if (res) {
51
+ if (typeof stop === 'function') {
52
+ stop();
53
+ }
54
+ if (_timeout) {
55
+ clearTimeout(_timeout);
56
+ _timeout = null;
57
+ }
58
+ resolve(res);
59
+ stop = null;
60
+ bool = null;
61
+ resolve = null;
62
+ }
63
+ }, checkTime || 100);
64
+ if (timeout) {
65
+ _timeout = setTimeout(() => {
66
+ if (typeof stop === 'function') {
67
+ stop();
68
+ }
69
+ if (_timeout) {
70
+ clearTimeout(_timeout);
71
+ _timeout = null;
72
+ }
73
+ reject('timeout');
74
+ reject = null;
75
+ stop = null;
76
+ bool = null;
77
+ }, timeout);
78
+ }
79
+ });
80
+ };
@@ -1,124 +1,95 @@
1
- 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); }
2
- 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."); }
3
- 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); }
4
- 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; }
5
1
  // 获取两点的距离
6
- export var getDistance = function getDistance(a, b) {
7
- return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
2
+ export const getDistance = (a, b) => {
3
+ return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
8
4
  };
9
5
  // 弧度转角度
10
- export var degrees = function degrees(rad) {
11
- return rad * 180 / Math.PI;
6
+ export const degrees = (rad) => {
7
+ return (rad * 180) / Math.PI;
12
8
  };
13
9
  // 角度转弧度
14
- export var radians = function radians(deg) {
15
- return deg * Math.PI / 180;
10
+ export const radians = (deg) => {
11
+ return (deg * Math.PI) / 180;
16
12
  };
17
13
  // 获取三角形角度
18
- export var getAngle = function getAngle(_ref) {
19
- var w = _ref.w,
20
- h = _ref.h;
21
- return degrees(Math.atan2(h, w));
14
+ export const getAngle = ({ w, h }) => {
15
+ return degrees(Math.atan2(h, w));
22
16
  };
23
17
  // 关键点过滤
24
- export function filterKeyPoints(list, len) {
25
- if (len === void 0) {
26
- len = 0.5;
27
- }
28
- if (list.length < 3 || len <= 0) {
29
- return list;
30
- }
31
- var last;
32
- return list.filter(function (item, i) {
33
- if (i > 0 && getDistance(last, item) < len) {
34
- return false;
18
+ export function filterKeyPoints(list, len = 0.5) {
19
+ if (list.length < 3 || len <= 0) {
20
+ return list;
35
21
  }
36
- last = list[i];
37
- return true;
38
- });
22
+ let last;
23
+ return list.filter((item, i) => {
24
+ if (i > 0 && getDistance(last, item) < len) {
25
+ return false;
26
+ }
27
+ last = list[i];
28
+ return true;
29
+ });
39
30
  }
40
31
  // 获取中心点坐标
41
32
  export function getCenter(list) {
42
- var tmp = [0, 0];
43
- for (var _iterator = _createForOfIteratorHelperLoose(list), _step; !(_step = _iterator()).done;) {
44
- var item = _step.value;
45
- if (Array.isArray(item)) {
46
- tmp[0] += item[0];
47
- tmp[1] += item[1];
48
- } else {
49
- tmp[0] += item.x;
50
- tmp[1] += item.y;
33
+ const tmp = [0, 0];
34
+ for (const item of list) {
35
+ if (Array.isArray(item)) {
36
+ tmp[0] += item[0];
37
+ tmp[1] += item[1];
38
+ }
39
+ else {
40
+ tmp[0] += item.x;
41
+ tmp[1] += item.y;
42
+ }
51
43
  }
52
- }
53
- return {
54
- x: tmp[0] / list.length,
55
- y: tmp[1] / list.length
56
- };
44
+ return { x: tmp[0] / list.length, y: tmp[1] / list.length };
57
45
  }
58
46
  // 2D旋转
59
47
  export function getRotate(data, theta) {
60
- var x = Math.cos(theta) * data[0] - Math.sin(theta) * data[1];
61
- var y = Math.sin(theta) * data[0] + Math.cos(theta) * data[1];
62
- return [x, y];
48
+ const x = Math.cos(theta) * data[0] - Math.sin(theta) * data[1];
49
+ const y = Math.sin(theta) * data[0] + Math.cos(theta) * data[1];
50
+ return [x, y];
63
51
  }
64
52
  // 获取A点到B点的方向角
65
- export var getPositionTheta = function getPositionTheta(a, b) {
66
- var vector = {
67
- x: b.x - a.x,
68
- y: b.y - a.y
69
- };
70
- return Math.atan2(vector.y, vector.x);
53
+ export const getPositionTheta = (a, b) => {
54
+ const vector = { x: b.x - a.x, y: b.y - a.y };
55
+ return Math.atan2(vector.y, vector.x);
71
56
  };
72
57
  // 获取距离和方向最佳的位置点
73
- export var getBestPointIndex = function getBestPointIndex(points, pose) {
74
- if (points.length < 2) {
75
- return 0;
76
- }
77
- var temp = [];
78
- var minDistance = Infinity;
79
- var index = 0;
80
- for (var _iterator2 = _createForOfIteratorHelperLoose(points), _step2; !(_step2 = _iterator2()).done;) {
81
- var item = _step2.value;
82
- var distance = getDistance(item, pose);
83
- var theta = getPositionTheta(pose, item) - pose.theta;
84
- if (minDistance > distance) {
85
- minDistance = distance;
58
+ export const getBestPointIndex = (points, pose) => {
59
+ if (points.length < 2) {
60
+ return 0;
86
61
  }
87
- temp.push(_extends({}, item, {
88
- index: index,
89
- distance: distance,
90
- theta: theta
91
- }));
92
- index += 1;
93
- }
94
- var results = temp.filter(function (item) {
95
- return item.distance - minDistance < 0.1;
96
- }).sort(function (a, b) {
97
- return a.theta - b.theta;
98
- });
99
- return results[0].index;
62
+ const temp = [];
63
+ let minDistance = Infinity;
64
+ let index = 0;
65
+ for (const item of points) {
66
+ const distance = getDistance(item, pose);
67
+ const theta = getPositionTheta(pose, item) - pose.theta;
68
+ if (minDistance > distance) {
69
+ minDistance = distance;
70
+ }
71
+ temp.push({ ...item, index, distance, theta });
72
+ index += 1;
73
+ }
74
+ const results = temp
75
+ .filter((item) => item.distance - minDistance < 0.1)
76
+ .sort((a, b) => a.theta - b.theta);
77
+ return results[0].index;
100
78
  };
101
79
  // 按照距离和方向排序
102
- export var orderByDistance = function orderByDistance(points, pose) {
103
- if (pose === void 0) {
104
- pose = {
105
- x: 0,
106
- y: 0,
107
- theta: 0
108
- };
109
- }
110
- var current = pose;
111
- var results = [];
112
- var arr = [].concat(points);
113
- while (arr.length > 0) {
114
- var index = getBestPointIndex(arr, current);
115
- results.push(arr[index]);
116
- current = arr[index];
117
- arr.splice(index, 1);
118
- }
119
- return results;
80
+ export const orderByDistance = (points, pose = { x: 0, y: 0, theta: 0 }) => {
81
+ let current = pose;
82
+ const results = [];
83
+ const arr = [...points];
84
+ while (arr.length > 0) {
85
+ const index = getBestPointIndex(arr, current);
86
+ results.push(arr[index]);
87
+ current = arr[index];
88
+ arr.splice(index, 1);
89
+ }
90
+ return results;
120
91
  };
121
92
  // 获取向量a到向量b的夹角
122
- export var getVectorTheta = function getVectorTheta(a, b) {
123
- return Math.atan2(b.y, b.x) - Math.atan2(a.y, a.x);
124
- };
93
+ export const getVectorTheta = (a, b) => {
94
+ return Math.atan2(b.y, b.x) - Math.atan2(a.y, a.x);
95
+ };
@@ -1,24 +1,17 @@
1
1
  // 回调函数转异步函数
2
- export var callbackToPromise = function callbackToPromise(fn) {
3
- return function () {
4
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
5
- args[_key] = arguments[_key];
6
- }
7
- return new Promise(function (resolve, reject) {
8
- fn.apply(void 0, args.concat([function (err) {
9
- if (err) {
10
- reject(err);
11
- }
12
- for (var _len2 = arguments.length, rest = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
13
- rest[_key2 - 1] = arguments[_key2];
14
- }
15
- if (rest.length < 2) {
16
- resolve(rest[0]);
17
- } else {
18
- resolve([].concat(rest));
19
- }
20
- }]));
21
- fn = null;
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
+ }
11
+ else {
12
+ resolve([...rest]);
13
+ }
14
+ });
15
+ fn = null;
22
16
  });
23
- };
24
- };
17
+ };
package/es/queue/index.js CHANGED
@@ -1,122 +1,43 @@
1
- function _call(body, then, direct) {
2
- if (direct) {
3
- return then ? then(body()) : body();
4
- }
5
- try {
6
- var result = Promise.resolve(body());
7
- return then ? result.then(then) : result;
8
- } catch (e) {
9
- return Promise.reject(e);
10
- }
11
- }
12
- function _catch(body, recover) {
13
- try {
14
- var result = body();
15
- } catch (e) {
16
- return recover(e);
17
- }
18
- if (result && result.then) {
19
- return result.then(void 0, recover);
20
- }
21
- return result;
22
- }
23
- function _continue(value, then) {
24
- return value && value.then ? value.then(then) : then(value);
25
- }
26
- function _await(value, then, direct) {
27
- if (direct) {
28
- return then ? then(value) : value;
29
- }
30
- if (!value || !value.then) {
31
- value = Promise.resolve(value);
32
- }
33
- return then ? value.then(then) : value;
34
- }
35
- function _empty() {}
36
- function _awaitIgnored(value, direct) {
37
- if (!direct) {
38
- return value && value.then ? value.then(_empty) : Promise.resolve();
39
- }
40
- }
41
- function _invoke(body, then) {
42
- var result = body();
43
- if (result && result.then) {
44
- return result.then(then);
45
- }
46
- return then(result);
47
- }
48
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
49
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
50
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
51
- function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
52
- export var SyncQueue = /*#__PURE__*/function () {
53
- function SyncQueue() {
54
- this.cache = [];
55
- this.running = false;
56
- this.len = 0;
57
- }
58
- var _proto = SyncQueue.prototype;
59
- _proto._run = function _run(_ref) {
60
- var fn = _ref.fn,
61
- promise = _ref.promise;
62
- try {
63
- return _await(_continue(_catch(function () {
64
- return _call(fn, function (res) {
65
- promise.resolve(res);
66
- });
67
- }, function (err) {
68
- promise.reject(err);
69
- }), function () {
1
+ export class SyncQueue {
2
+ cache = [];
3
+ running = false;
4
+ len = 0;
5
+ async _run({ fn, promise }) {
6
+ try {
7
+ const res = await fn();
8
+ promise.resolve(res);
9
+ }
10
+ catch (err) {
11
+ promise.reject(err);
12
+ }
70
13
  fn = null;
71
14
  promise = null;
72
- }));
73
- } catch (e) {
74
- return Promise.reject(e);
75
15
  }
76
- };
77
- _proto.run = function run() {
78
- try {
79
- var _this = this;
80
- if (_this.cache.length < 1) {
81
- _this.running = false;
82
- return _await();
83
- } else {
84
- _this.running = true;
85
- }
86
- var item = _this.cache.shift();
87
- _this.len -= 1;
88
- return _await(_invoke(function () {
16
+ async run() {
17
+ if (this.cache.length < 1) {
18
+ this.running = false;
19
+ return;
20
+ }
21
+ else {
22
+ this.running = true;
23
+ }
24
+ const item = this.cache.shift();
25
+ this.len -= 1;
89
26
  if (typeof item.fn === 'function') {
90
- return _awaitIgnored(_this._run(item));
27
+ await this._run(item);
91
28
  }
92
- }, function () {
93
- _this.run();
94
- }));
95
- } catch (e) {
96
- return Promise.reject(e);
29
+ this.run();
97
30
  }
98
- };
99
- _proto.push = function push(fn) {
100
- var _this2 = this;
101
- this.len += 1;
102
- return new Promise(function (resolve, reject) {
103
- _this2.cache.push({
104
- fn: fn,
105
- promise: {
106
- resolve: resolve,
107
- reject: reject
108
- }
109
- });
110
- if (!_this2.running) {
111
- _this2.run();
112
- }
113
- });
114
- };
115
- _createClass(SyncQueue, [{
116
- key: "length",
117
- get: function get() {
118
- return this.len;
31
+ push(fn) {
32
+ this.len += 1;
33
+ return new Promise((resolve, reject) => {
34
+ this.cache.push({ fn, promise: { resolve, reject } });
35
+ if (!this.running) {
36
+ this.run();
37
+ }
38
+ });
39
+ }
40
+ get length() {
41
+ return this.len;
119
42
  }
120
- }]);
121
- return SyncQueue;
122
- }();
43
+ }