@pisell/utils 1.0.13 → 1.0.15

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/format.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @title: 格式化金额
3
+ * @description:
4
+ * @param {number} amount 原金额
5
+ * @param {number} precision 小数位数
6
+ * @param {string} symbol 货币符号
7
+ * @return {*}
8
+ * @Author: zhiwei.Wang
9
+ * @Date: 2023-11-06 16:51
10
+ */
11
+ export declare const formatAmount: (amount?: number | string, precision?: number, symbol?: string) => string | number;
package/es/format.js ADDED
@@ -0,0 +1,50 @@
1
+ import { isString, isNumber } from "./typeUtils";
2
+
3
+ /**
4
+ * @title: 格式化金额
5
+ * @description:
6
+ * @param {number} amount 原金额
7
+ * @param {number} precision 小数位数
8
+ * @param {string} symbol 货币符号
9
+ * @return {*}
10
+ * @Author: zhiwei.Wang
11
+ * @Date: 2023-11-06 16:51
12
+ */
13
+ export var formatAmount = function formatAmount() {
14
+ var amount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
15
+ var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
16
+ var symbol = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
17
+ // 校验格式
18
+ if (!isString(amount) || !isNumber(amount)) {
19
+ console.error('amount is not a number');
20
+ return amount;
21
+ }
22
+ try {
23
+ // 四舍五入
24
+ var roundedAmount = Math.round(amount * Math.pow(10, precision)) / Math.pow(10, precision);
25
+ // 处理负数情况
26
+ var absoluteAmount = Math.abs(roundedAmount);
27
+ // 截取整数
28
+ var integerPart = Math.floor(absoluteAmount);
29
+ // 截取小数部分并补零
30
+ var decimalPart = (absoluteAmount - integerPart).toFixed(precision).substring(2);
31
+
32
+ // 拼接货币符号
33
+ var formattedAmount = symbol + integerPart.toString();
34
+
35
+ // 添加小数点
36
+ if (precision > 0) {
37
+ formattedAmount += '.' + decimalPart;
38
+ }
39
+
40
+ // 添加负号
41
+ if (roundedAmount < 0) {
42
+ formattedAmount = '-' + formattedAmount;
43
+ }
44
+ return formattedAmount;
45
+ } catch (err) {
46
+ // 计算错误则返回原金额
47
+ console.error(err);
48
+ return amount;
49
+ }
50
+ };
package/es/index.d.ts CHANGED
@@ -4,3 +4,4 @@ export * from './document';
4
4
  export * from './date';
5
5
  export * from './platform';
6
6
  export * from './log';
7
+ export * from './format';
package/es/index.js CHANGED
@@ -4,4 +4,5 @@ export * from "./document";
4
4
  export * from "./date";
5
5
  export * from "./platform";
6
6
  export * from "./log";
7
+ export * from "./format";
7
8
  // export { default as firebase } from './firebase';
package/es/platform.js CHANGED
@@ -32,5 +32,5 @@ export var isAndroid = function isAndroid() {
32
32
 
33
33
  // 是否是ios平台
34
34
  export var isIos = function isIos() {
35
- return /iphone|ipad|ipod|safari/.test(navigator.userAgent.toLowerCase());
35
+ return /iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase());
36
36
  };
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @title: 格式化金额
3
+ * @description:
4
+ * @param {number} amount 原金额
5
+ * @param {number} precision 小数位数
6
+ * @param {string} symbol 货币符号
7
+ * @return {*}
8
+ * @Author: zhiwei.Wang
9
+ * @Date: 2023-11-06 16:51
10
+ */
11
+ export declare const formatAmount: (amount?: number | string, precision?: number, symbol?: string) => string | number;
package/lib/format.js ADDED
@@ -0,0 +1,52 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/format.ts
20
+ var format_exports = {};
21
+ __export(format_exports, {
22
+ formatAmount: () => formatAmount
23
+ });
24
+ module.exports = __toCommonJS(format_exports);
25
+ var import_typeUtils = require("./typeUtils");
26
+ var formatAmount = (amount = 0, precision = 2, symbol = "") => {
27
+ if (!(0, import_typeUtils.isString)(amount) || !(0, import_typeUtils.isNumber)(amount)) {
28
+ console.error("amount is not a number");
29
+ return amount;
30
+ }
31
+ try {
32
+ const roundedAmount = Math.round(amount * Math.pow(10, precision)) / Math.pow(10, precision);
33
+ const absoluteAmount = Math.abs(roundedAmount);
34
+ const integerPart = Math.floor(absoluteAmount);
35
+ const decimalPart = (absoluteAmount - integerPart).toFixed(precision).substring(2);
36
+ let formattedAmount = symbol + integerPart.toString();
37
+ if (precision > 0) {
38
+ formattedAmount += "." + decimalPart;
39
+ }
40
+ if (roundedAmount < 0) {
41
+ formattedAmount = "-" + formattedAmount;
42
+ }
43
+ return formattedAmount;
44
+ } catch (err) {
45
+ console.error(err);
46
+ return amount;
47
+ }
48
+ };
49
+ // Annotate the CommonJS export names for ESM import in node:
50
+ 0 && (module.exports = {
51
+ formatAmount
52
+ });
package/lib/index.d.ts CHANGED
@@ -4,3 +4,4 @@ export * from './document';
4
4
  export * from './date';
5
5
  export * from './platform';
6
6
  export * from './log';
7
+ export * from './format';
package/lib/index.js CHANGED
@@ -22,6 +22,7 @@ __reExport(src_exports, require("./document"), module.exports);
22
22
  __reExport(src_exports, require("./date"), module.exports);
23
23
  __reExport(src_exports, require("./platform"), module.exports);
24
24
  __reExport(src_exports, require("./log"), module.exports);
25
+ __reExport(src_exports, require("./format"), module.exports);
25
26
  // Annotate the CommonJS export names for ESM import in node:
26
27
  0 && (module.exports = {
27
28
  ...require("./otherUtils"),
@@ -29,5 +30,6 @@ __reExport(src_exports, require("./log"), module.exports);
29
30
  ...require("./document"),
30
31
  ...require("./date"),
31
32
  ...require("./platform"),
32
- ...require("./log")
33
+ ...require("./log"),
34
+ ...require("./format")
33
35
  });
package/lib/platform.js CHANGED
@@ -57,7 +57,7 @@ var isAndroid = () => {
57
57
  return navigator.userAgent.toLowerCase().indexOf("android") !== -1;
58
58
  };
59
59
  var isIos = () => {
60
- return /iphone|ipad|ipod|safari/.test(navigator.userAgent.toLowerCase());
60
+ return /iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase());
61
61
  };
62
62
  // Annotate the CommonJS export names for ESM import in node:
63
63
  0 && (module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pisell/utils",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "sideEffects": false,
5
5
  "main": "./lib/index.js",
6
6
  "module": "./es/index.js",