@anjianshi/utils 1.0.0 → 1.0.3

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.
@@ -1,4 +1,3 @@
1
- import chalk from 'chalk';
2
1
  import { type Logger, type LogInfo, LogHandler } from '../logging/index.js';
3
2
  export * from '../logging/index.js';
4
3
  /**
@@ -25,14 +24,14 @@ export declare class ConsoleHandler extends LogHandler {
25
24
  };
26
25
  };
27
26
  static readonly levelColors: {
28
- 1: chalk.Chalk;
29
- 2: chalk.Chalk;
30
- 3: chalk.Chalk;
31
- 4: chalk.Chalk;
27
+ 1: import("chalk").ChalkInstance;
28
+ 2: import("chalk").ChalkInstance;
29
+ 3: import("chalk").ChalkInstance;
30
+ 4: import("chalk").ChalkInstance;
32
31
  };
33
32
  private static readonly loggerColors;
34
33
  private static readonly loggerColorMap;
35
- static getLoggerColor(logger: string): "green" | "yellow" | "blue" | "magenta" | "cyan" | "greenBright" | "yellowBright" | "blueBright" | "magentaBright" | "cyanBright";
34
+ static getLoggerColor(logger: string): "green" | "yellow" | "blue" | "cyan" | "magenta" | "greenBright" | "yellowBright" | "blueBright" | "cyanBright" | "magentaBright";
36
35
  }
37
36
  /**
38
37
  * 写入文件日志
package/lang/string.d.ts CHANGED
@@ -15,3 +15,11 @@ export declare function safeParseInt(value: string | number, fallback?: number,
15
15
  * 字符串解析成浮点数;若解析结果是 NaN,返回 fallback
16
16
  */
17
17
  export declare function safeParseFloat(value: string | number, fallback: number): number;
18
+ /**
19
+ * Return file size for human reading.
20
+ * From:https://stackoverflow.com/a/14919494
21
+ *
22
+ * si: true for radix of 1024;false for radix of 1000(default: false)
23
+ * dp: how many decimal places to keep(保留几位小数)
24
+ */
25
+ export declare function readableSize(bytes: number, si?: boolean, dp?: number): string;
package/lang/string.js CHANGED
@@ -57,3 +57,25 @@ export function safeParseFloat(value, fallback) {
57
57
  const raw = parseFloat(String(value));
58
58
  return isFinite(raw) ? raw : fallback;
59
59
  }
60
+ /**
61
+ * Return file size for human reading.
62
+ * From:https://stackoverflow.com/a/14919494
63
+ *
64
+ * si: true for radix of 1024;false for radix of 1000(default: false)
65
+ * dp: how many decimal places to keep(保留几位小数)
66
+ */
67
+ export function readableSize(bytes, si = false, dp = 1) {
68
+ const thresh = si ? 1000 : 1024;
69
+ if (Math.abs(bytes) < thresh)
70
+ return bytes.toString() + ' B';
71
+ const units = si
72
+ ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
73
+ : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
74
+ let u = -1;
75
+ const r = 10 ** dp;
76
+ do {
77
+ bytes /= thresh;
78
+ ++u;
79
+ } while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
80
+ return `${bytes.toFixed(dp)} ${units[u]}`;
81
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anjianshi/utils",
3
- "version": "1.0.0",
3
+ "version": "1.0.3",
4
4
  "description": "Common JavaScript Utils",
5
5
  "homepage": "https://github.com/anjianshi/js-utils",
6
6
  "bugs": {
@@ -25,6 +25,7 @@
25
25
  },
26
26
  "main": "index.js",
27
27
  "dependencies": {
28
+ "chalk": "^5.3.0",
28
29
  "dayjs": "^1.11.10",
29
30
  "lodash": "^4.17.21"
30
31
  },
@@ -60,3 +60,25 @@ export function safeParseFloat(value: string | number, fallback: number) {
60
60
  const raw = parseFloat(String(value))
61
61
  return isFinite(raw) ? raw : fallback
62
62
  }
63
+
64
+ /**
65
+ * Return file size for human reading.
66
+ * From:https://stackoverflow.com/a/14919494
67
+ *
68
+ * si: true for radix of 1024;false for radix of 1000(default: false)
69
+ * dp: how many decimal places to keep(保留几位小数)
70
+ */
71
+ export function readableSize(bytes: number, si = false, dp = 1) {
72
+ const thresh = si ? 1000 : 1024
73
+ if (Math.abs(bytes) < thresh) return bytes.toString() + ' B'
74
+ const units = si
75
+ ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
76
+ : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
77
+ let u = -1
78
+ const r = 10 ** dp
79
+ do {
80
+ bytes /= thresh
81
+ ++u
82
+ } while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1)
83
+ return `${bytes.toFixed(dp)} ${units[u]!}`
84
+ }