lyb-js 1.1.11 → 1.2.1

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.
Files changed (44) hide show
  1. package/dist/Base/LibJsGetDataType/index.js +1 -1
  2. package/dist/Base/LibJsPromiseTimeout/index.js +9 -10
  3. package/dist/Browser/LibJsColorConsole/index.js +12 -22
  4. package/dist/Browser/LibJsIsMobile/index.js +2 -2
  5. package/dist/Browser/LibJsIsPad/index.js +8 -8
  6. package/dist/Browser/LibJsObjToUrlParams/index.js +2 -5
  7. package/dist/Browser/LibJsPathParams/index.js +5 -5
  8. package/dist/Browser/LibJsSetTitleIcon/index.js +3 -3
  9. package/dist/Browser/LibJsTagTitleTip/index.js +5 -5
  10. package/dist/Data/LibJsChunkArray/index.js +3 -3
  11. package/dist/Data/LibJsDeepJSONParse/index.js +4 -4
  12. package/dist/Data/LibJsGroupArrayByKey/index.js +2 -3
  13. package/dist/Data/LibJsMatchEmail/index.js +4 -4
  14. package/dist/Data/LibJsShuffleArray/index.js +5 -15
  15. package/dist/Data/LibJsStepArray/index.js +3 -3
  16. package/dist/Data/LibReverseArrayFromIndex/index.js +3 -12
  17. package/dist/File/LibJsDownloadImageLink/index.js +5 -5
  18. package/dist/File/LibJsImageOptimizer/index.js +36 -36
  19. package/dist/File/LibJsSaveJson/index.js +4 -4
  20. package/dist/Formatter/LibJsFormatterByte/index.js +6 -6
  21. package/dist/Formatter/LibJsMaskPhoneNumber/index.js +2 -2
  22. package/dist/Formatter/LibJsNumComma/index.js +3 -4
  23. package/dist/Formatter/LibJsNumberUnit/index.js +9 -15
  24. package/dist/Formatter/LibJsSecondsFormatterChinese/index.js +15 -15
  25. package/dist/Math/LibJsCalculateExpression/index.js +17 -19
  26. package/dist/Math/LibJsConvertAngle/index.js +1 -1
  27. package/dist/Math/LibJsCoordsAngle/index.js +5 -5
  28. package/dist/Math/LibJsCoordsDistance/index.js +4 -4
  29. package/dist/Math/LibJsDecimal/index.js +7 -8
  30. package/dist/Misc/LibJsRegFormValidate/index.js +6 -6
  31. package/dist/Misc/LibJsRetryRequest/index.js +6 -7
  32. package/dist/Misc/LibNumerStepper/index.js +17 -21
  33. package/dist/Random/LibJsProbabilityResult/index.js +1 -3
  34. package/dist/Random/LibJsRandom/index.js +1 -2
  35. package/dist/Random/LibJsRandomColor/index.js +5 -6
  36. package/dist/Random/LibJsUniqueRandomNumbers/index.js +5 -6
  37. package/dist/Time/LibJsSameTimeCheck/index.d.ts +1 -1
  38. package/dist/Time/LibJsSameTimeCheck/index.js +3 -3
  39. package/dist/Time/LibJsTimeAgo/index.js +7 -8
  40. package/dist/Time/LibJsTimeGreeting/index.js +3 -4
  41. package/dist/index.js +2 -1
  42. package/dist/libJs.d.ts +1 -1
  43. package/dist/libJs.js +48 -48
  44. package/package.json +3 -3
@@ -5,32 +5,31 @@ import Decimal from "decimal.js";
5
5
  * @returns 计算结果
6
6
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsCalculateExpression-表达式字符串
7
7
  */
8
- export var libJsCalculateExpression = function (expression, point) {
9
- if (point === void 0) { point = 2; }
8
+ export const libJsCalculateExpression = (expression, point = 2) => {
10
9
  //清除所有空格
11
10
  expression = expression.replace(/\s+/g, "");
12
11
  //支持的运算符和优先级
13
- var operators = {
12
+ const operators = {
14
13
  "+": 1,
15
14
  "-": 1,
16
15
  "*": 2,
17
16
  "/": 2,
18
17
  };
19
18
  //支持的小数点精度
20
- var toDecimal = function (value) { return new Decimal(value); };
19
+ const toDecimal = (value) => new Decimal(value);
21
20
  //判断字符是否是运算符
22
- var isOperator = function (char) { return ["+", "-", "*", "/"].includes(char); };
21
+ const isOperator = (char) => ["+", "-", "*", "/"].includes(char);
23
22
  //判断字符是否是数字(包括小数点)
24
- var isNumber = function (char) { return /[0-9.]/.test(char); };
23
+ const isNumber = (char) => /[0-9.]/.test(char);
25
24
  //解析表达式并计算
26
- var evaluate = function (expression) {
27
- var outputQueue = []; //输出队列
28
- var operatorStack = []; //操作符栈
29
- var i = 0;
25
+ const evaluate = (expression) => {
26
+ const outputQueue = []; //输出队列
27
+ const operatorStack = []; //操作符栈
28
+ let i = 0;
30
29
  while (i < expression.length) {
31
- var char = expression[i];
30
+ const char = expression[i];
32
31
  if (isNumber(char)) {
33
- var numStr = "";
32
+ let numStr = "";
34
33
  //处理多位数字(支持小数)
35
34
  while (i < expression.length && isNumber(expression[i])) {
36
35
  numStr += expression[i];
@@ -61,7 +60,7 @@ export var libJsCalculateExpression = function (expression, point) {
61
60
  i++;
62
61
  }
63
62
  else {
64
- throw new Error("\u65E0\u6548\u5B57\u7B26: ".concat(char));
63
+ throw new Error(`无效字符: ${char}`);
65
64
  }
66
65
  }
67
66
  //把所有剩余的操作符添加到输出队列
@@ -69,12 +68,11 @@ export var libJsCalculateExpression = function (expression, point) {
69
68
  outputQueue.push(operatorStack.pop());
70
69
  }
71
70
  //执行运算
72
- var calcStack = [];
73
- for (var _i = 0, outputQueue_1 = outputQueue; _i < outputQueue_1.length; _i++) {
74
- var token = outputQueue_1[_i];
71
+ const calcStack = [];
72
+ for (let token of outputQueue) {
75
73
  if (typeof token === "string") {
76
- var b = calcStack.pop();
77
- var a = calcStack.pop();
74
+ const b = calcStack.pop();
75
+ const a = calcStack.pop();
78
76
  switch (token) {
79
77
  case "+":
80
78
  calcStack.push(a.add(b));
@@ -100,7 +98,7 @@ export var libJsCalculateExpression = function (expression, point) {
100
98
  };
101
99
  try {
102
100
  //调用计算器并返回结果
103
- var result = evaluate(expression);
101
+ const result = evaluate(expression);
104
102
  return Number(result.toFixed(point)); //保留指定的小数位数
105
103
  }
106
104
  catch (error) {
@@ -4,7 +4,7 @@
4
4
  * @param type 角度类型或弧度类型
5
5
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsConvertAngle-角弧度互转
6
6
  */
7
- export var libJsConvertAngle = function (value, type) {
7
+ export const libJsConvertAngle = (value, type) => {
8
8
  if (type === "rad") {
9
9
  return value * (Math.PI / 180);
10
10
  }
@@ -3,14 +3,14 @@
3
3
  * @param coord2 终点坐标
4
4
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsCoordsAngle-两点角度
5
5
  */
6
- export var libJsCoordsAngle = function (coord1, coord2) {
6
+ export const libJsCoordsAngle = (coord1, coord2) => {
7
7
  //计算相对于第一个坐标的水平和垂直距离
8
- var deltaX = coord2.x - coord1.x;
9
- var deltaY = coord2.y - coord1.y;
8
+ const deltaX = coord2.x - coord1.x;
9
+ const deltaY = coord2.y - coord1.y;
10
10
  //使用反三角函数计算角度(以弧度为单位)
11
- var angleRad = Math.atan2(deltaY, deltaX);
11
+ const angleRad = Math.atan2(deltaY, deltaX);
12
12
  //将弧度转换为角度
13
- var angleDeg = angleRad * (180 / Math.PI);
13
+ let angleDeg = angleRad * (180 / Math.PI);
14
14
  //将角度转换为顺时针方向为正方向的角度
15
15
  angleDeg = -angleDeg + 90;
16
16
  //调整角度使得右边成为 360 度的位置变为 0 度
@@ -3,11 +3,11 @@
3
3
  * @param coord2 终点坐标
4
4
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsCoordsDistance-两点距离
5
5
  */
6
- export var libJsCoordsDistance = function (coord1, coord2) {
6
+ export const libJsCoordsDistance = (coord1, coord2) => {
7
7
  //计算两个坐标之间的水平和垂直距离
8
- var deltaX = coord2.x - coord1.x;
9
- var deltaY = coord2.y - coord1.y;
8
+ const deltaX = coord2.x - coord1.x;
9
+ const deltaY = coord2.y - coord1.y;
10
10
  //使用勾股定理计算两点之间的距离
11
- var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
11
+ const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
12
12
  return distance;
13
13
  };
@@ -5,19 +5,18 @@ import { Decimal } from "decimal.js";
5
5
  * @param operator 运算符,支持加减乘除
6
6
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsDecimal-高精度计算
7
7
  */
8
- export var libJsDecimal = function (num1, num2, operator, point) {
9
- if (point === void 0) { point = 2; }
10
- var calc = {
11
- "+": function (a, b) { return a.add(b); },
12
- "-": function (a, b) { return a.sub(b); },
13
- "*": function (a, b) { return a.mul(b); },
14
- "/": function (a, b) {
8
+ export const libJsDecimal = (num1, num2, operator, point = 2) => {
9
+ const calc = {
10
+ "+": (a, b) => a.add(b),
11
+ "-": (a, b) => a.sub(b),
12
+ "*": (a, b) => a.mul(b),
13
+ "/": (a, b) => {
15
14
  if (b.eq(0)) {
16
15
  throw new Error("除数不能为0");
17
16
  }
18
17
  return a.div(b);
19
18
  },
20
19
  };
21
- var result = calc[operator](new Decimal(num1), new Decimal(num2));
20
+ const result = calc[operator](new Decimal(num1), new Decimal(num2));
22
21
  return Number(result.toFixed(point));
23
22
  };
@@ -5,15 +5,15 @@
5
5
  * @returns 验证结果数组,包含未通过验证的项
6
6
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsRegFormValidate-表单验证
7
7
  */
8
- export var libJsRegFormValidate = function (form, rules) {
9
- return rules.reduce(function (result, rule) {
10
- var key = rule.key, verify = rule.verify, msg = rule.msg, name = rule.name;
11
- var value = form[key];
8
+ export const libJsRegFormValidate = (form, rules) => {
9
+ return rules.reduce((result, rule) => {
10
+ const { key, verify, msg, name } = rule;
11
+ const value = form[key];
12
12
  if (value === "" || value === undefined || value === null) {
13
- result.push({ key: key, msg: "必填项", name: name });
13
+ result.push({ key, msg: "必填项", name });
14
14
  }
15
15
  else if (typeof verify === "function" ? !verify(value) : !verify.test(value)) {
16
- result.push({ key: key, msg: msg, name: name });
16
+ result.push({ key, msg, name });
17
17
  }
18
18
  return result;
19
19
  }, []);
@@ -5,16 +5,15 @@
5
5
  * @param params 请求参数
6
6
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsRetryRequest-请求重连
7
7
  */
8
- export var libJsRetryRequest = function (_a) {
9
- var promiseFn = _a.promiseFn, _b = _a.maxRetries, maxRetries = _b === void 0 ? 3 : _b, _c = _a.retryDelay, retryDelay = _c === void 0 ? 2000 : _c, _d = _a.params, params = _d === void 0 ? undefined : _d;
10
- return new Promise(function (resolve, reject) {
11
- var count = 0;
12
- var makeRequest = function () {
8
+ export const libJsRetryRequest = ({ promiseFn, maxRetries = 3, retryDelay = 2000, params = undefined, }) => {
9
+ return new Promise((resolve, reject) => {
10
+ let count = 0;
11
+ const makeRequest = () => {
13
12
  promiseFn(params)
14
- .then(function (res) {
13
+ .then((res) => {
15
14
  resolve(res);
16
15
  })
17
- .catch(function (err) {
16
+ .catch((err) => {
18
17
  count++;
19
18
  if (count >= maxRetries) {
20
19
  reject(err);
@@ -1,13 +1,12 @@
1
1
  /** @description 数字步进器
2
2
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibNumerStepper-数字步进器
3
3
  */
4
- var libNumerStepper = /** @class */ (function () {
4
+ export class libNumerStepper {
5
5
  /**
6
6
  * @param numsLength 数字长度
7
7
  * @param onChange 数字变动时触发
8
8
  */
9
- function libNumerStepper(numsLength, onChange) {
10
- var _this = this;
9
+ constructor(numsLength, onChange) {
11
10
  /** 当前按下状态 */
12
11
  this._isDown = false;
13
12
  /** 当前数字索引 */
@@ -16,35 +15,34 @@ var libNumerStepper = /** @class */ (function () {
16
15
  this._numsLength = 0;
17
16
  this._onChange = onChange;
18
17
  this._numsLength = numsLength;
19
- window.addEventListener("pointerup", function () {
20
- _this._isDown && _this._up();
18
+ window.addEventListener("pointerup", () => {
19
+ this._isDown && this._up();
21
20
  });
22
21
  }
23
22
  /** @description 按下 */
24
- libNumerStepper.prototype.down = function (type) {
25
- var _this = this;
23
+ down(type) {
26
24
  this._isDown = true;
27
25
  this._handleChange(type);
28
- this._timerId = setTimeout(function () {
29
- if (_this._isDown) {
30
- _this._intervalId = setInterval(function () {
31
- _this._handleChange(type);
26
+ this._timerId = setTimeout(() => {
27
+ if (this._isDown) {
28
+ this._intervalId = setInterval(() => {
29
+ this._handleChange(type);
32
30
  }, 100);
33
31
  }
34
32
  }, 100);
35
- };
33
+ }
36
34
  /** @description 更新索引 */
37
- libNumerStepper.prototype.updateIndex = function (index) {
35
+ updateIndex(index) {
38
36
  this._currentIndex = index;
39
- };
37
+ }
40
38
  /** @description 抬起 */
41
- libNumerStepper.prototype._up = function () {
39
+ _up() {
42
40
  this._isDown = false;
43
41
  clearTimeout(this._timerId);
44
42
  clearInterval(this._intervalId);
45
- };
43
+ }
46
44
  /** @description 处理数字变化 */
47
- libNumerStepper.prototype._handleChange = function (type) {
45
+ _handleChange(type) {
48
46
  if (type === "add") {
49
47
  if (this._currentIndex < this._numsLength - 1) {
50
48
  this._currentIndex++;
@@ -57,7 +55,5 @@ var libNumerStepper = /** @class */ (function () {
57
55
  this._onChange(this._currentIndex);
58
56
  }
59
57
  }
60
- };
61
- return libNumerStepper;
62
- }());
63
- export { libNumerStepper };
58
+ }
59
+ }
@@ -2,6 +2,4 @@
2
2
  * @param probability 触发概率,百分比,0-100
3
3
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsProbabilityResult-概率触发
4
4
  */
5
- export var libJsProbabilityResult = function (probability) {
6
- return Math.random() * 100 < probability;
7
- };
5
+ export const libJsProbabilityResult = (probability) => Math.random() * 100 < probability;
@@ -4,7 +4,6 @@
4
4
  * @param num 保留小数位数
5
5
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsRandom-随机数
6
6
  */
7
- export var libJsRandom = function (min, max, num) {
8
- if (num === void 0) { num = 0; }
7
+ export const libJsRandom = (min, max, num = 0) => {
9
8
  return parseFloat((Math.random() * (max - min) + min).toFixed(num));
10
9
  };
@@ -2,10 +2,9 @@
2
2
  * @param alpha 透明度
3
3
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsRandomColor-随机色
4
4
  */
5
- export var libJsRandomColor = function (alpha) {
6
- if (alpha === void 0) { alpha = 1; }
7
- var r = Math.floor(Math.random() * 256);
8
- var g = Math.floor(Math.random() * 256);
9
- var b = Math.floor(Math.random() * 256);
10
- return "rgba(".concat(r, ", ").concat(g, ", ").concat(b, ", ").concat(alpha, ")");
5
+ export const libJsRandomColor = (alpha = 1) => {
6
+ const r = Math.floor(Math.random() * 256);
7
+ const g = Math.floor(Math.random() * 256);
8
+ const b = Math.floor(Math.random() * 256);
9
+ return `rgba(${r}, ${g}, ${b}, ${alpha})`;
11
10
  };
@@ -4,12 +4,11 @@
4
4
  * @param count 数组长度
5
5
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsUniqueRandomNumbers-随机数数组
6
6
  */
7
- export var libJsUniqueRandomNumbers = function (min, max, count) {
8
- var _a;
9
- var numbers = Array.from({ length: max - min + 1 }, function (_, i) { return i + min; });
10
- for (var i = numbers.length - 1; i > 0; i--) {
11
- var j = Math.floor(Math.random() * (i + 1));
12
- _a = [numbers[j], numbers[i]], numbers[i] = _a[0], numbers[j] = _a[1];
7
+ export const libJsUniqueRandomNumbers = (min, max, count) => {
8
+ const numbers = Array.from({ length: max - min + 1 }, (_, i) => i + min);
9
+ for (let i = numbers.length - 1; i > 0; i--) {
10
+ const j = Math.floor(Math.random() * (i + 1));
11
+ [numbers[i], numbers[j]] = [numbers[j], numbers[i]];
13
12
  }
14
13
  return numbers.slice(0, count);
15
14
  };
@@ -6,4 +6,4 @@ import dayjs from "dayjs";
6
6
  * @returns 0-同一单位时间 1-新单位时间 -1时间戳大于当前时间
7
7
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsSameTimeCheck-时间比对
8
8
  */
9
- export declare const libJsSameTimeCheck: (timestamp: number, unit: dayjs.OpUnitType) => 0 | 1 | -1;
9
+ export declare const libJsSameTimeCheck: (timestamp: number, unit: dayjs.OpUnitType) => 1 | -1 | 0;
@@ -6,9 +6,9 @@ import dayjs from "dayjs";
6
6
  * @returns 0-同一单位时间 1-新单位时间 -1时间戳大于当前时间
7
7
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsSameTimeCheck-时间比对
8
8
  */
9
- export var libJsSameTimeCheck = function (timestamp, unit) {
10
- var inputTime = dayjs(timestamp).startOf(unit);
11
- var currentTime = dayjs().startOf(unit);
9
+ export const libJsSameTimeCheck = (timestamp, unit) => {
10
+ const inputTime = dayjs(timestamp).startOf(unit);
11
+ const currentTime = dayjs().startOf(unit);
12
12
  if (inputTime.isSame(currentTime)) {
13
13
  return 0;
14
14
  }
@@ -2,8 +2,8 @@
2
2
  * @param timestamp 毫秒时间戳
3
3
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsTimeAgo-中文时间差
4
4
  */
5
- export var libJsTimeAgo = function (timestamp) {
6
- var timeUnits = [
5
+ export const libJsTimeAgo = (timestamp) => {
6
+ const timeUnits = [
7
7
  { unit: "年", milliseconds: 365 * 24 * 60 * 60 * 1000 },
8
8
  { unit: "月", milliseconds: 30 * 24 * 60 * 60 * 1000 },
9
9
  { unit: "周", milliseconds: 7 * 24 * 60 * 60 * 1000 },
@@ -11,13 +11,12 @@ export var libJsTimeAgo = function (timestamp) {
11
11
  { unit: "小时", milliseconds: 60 * 60 * 1000 },
12
12
  { unit: "分钟", milliseconds: 60 * 1000 },
13
13
  ];
14
- var currentTime = Date.now();
15
- var timeDifference = currentTime - timestamp;
16
- for (var _i = 0, timeUnits_1 = timeUnits; _i < timeUnits_1.length; _i++) {
17
- var _a = timeUnits_1[_i], unit = _a.unit, milliseconds = _a.milliseconds;
14
+ const currentTime = Date.now();
15
+ const timeDifference = currentTime - timestamp;
16
+ for (const { unit, milliseconds } of timeUnits) {
18
17
  if (timeDifference >= milliseconds) {
19
- var count = Math.floor(timeDifference / milliseconds);
20
- return "".concat(count, " ").concat(unit, "\u524D");
18
+ const count = Math.floor(timeDifference / milliseconds);
19
+ return `${count} ${unit}前`;
21
20
  }
22
21
  }
23
22
  return "刚刚";
@@ -3,10 +3,9 @@
3
3
  * @param greet 自定义问候语对象
4
4
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsTimeGreeting-时间问候
5
5
  */
6
- export var libJsTimeGreeting = function (greet) {
7
- if (greet === void 0) { greet = {}; }
8
- var _a = greet.midnight, midnight = _a === void 0 ? "午夜好" : _a, _b = greet.morning, morning = _b === void 0 ? "早上好" : _b, _c = greet.forenoon, forenoon = _c === void 0 ? "上午好" : _c, _d = greet.noon, noon = _d === void 0 ? "中午好" : _d, _e = greet.afternoon, afternoon = _e === void 0 ? "下午好" : _e, _f = greet.evening, evening = _f === void 0 ? "晚上好" : _f;
9
- var now = new Date().getHours();
6
+ export const libJsTimeGreeting = (greet = {}) => {
7
+ const { midnight = "午夜好", morning = "早上好", forenoon = "上午好", noon = "中午好", afternoon = "下午好", evening = "晚上好", } = greet;
8
+ const now = new Date().getHours();
10
9
  return now < 4
11
10
  ? midnight
12
11
  : now < 10
package/dist/index.js CHANGED
@@ -1 +1,2 @@
1
- export * as LibJs from "./libJs";
1
+ import * as LibJs_1 from "./libJs";
2
+ export { LibJs_1 as LibJs };
package/dist/libJs.d.ts CHANGED
@@ -280,7 +280,7 @@ export declare const Time: {
280
280
  * @returns 0-同一单位时间 1-新单位时间 -1时间戳大于当前时间
281
281
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsSameTimeCheck-时间比对
282
282
  */
283
- libJsSameTimeCheck: (timestamp: number, unit: import("dayjs").OpUnitType) => 0 | 1 | -1;
283
+ libJsSameTimeCheck: (timestamp: number, unit: import("dayjs").OpUnitType) => 1 | -1 | 0;
284
284
  /** @description 时间差计算
285
285
  * @param timestamp 毫秒时间戳
286
286
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsTimeAgo-中文时间差