smart-unit 1.0.2 → 1.0.4

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/README.md CHANGED
@@ -5,6 +5,7 @@
5
5
  [![npm version](https://img.shields.io/npm/v/smart-unit)](https://www.npmjs.com/package/smart-unit)
6
6
  [![npm downloads](https://img.shields.io/npm/dm/smart-unit)](https://www.npmjs.com/package/smart-unit)
7
7
  [![bundle size](https://img.shields.io/bundlephobia/minzip/smart-unit)](https://bundlephobia.com/package/smart-unit)
8
+ [![test](https://github.com/flycran/smart-unit/workflows/Test/badge.svg)](https://github.com/flycran/smart-unit/actions)
8
9
  [![license](https://img.shields.io/npm/l/smart-unit)](./LICENSE)
9
10
 
10
11
  English | [中文](./README.zh-CN.md)
@@ -13,6 +14,16 @@ English | [中文](./README.zh-CN.md)
13
14
 
14
15
  **smart-unit** is a lightweight utility for automatic unit conversion with intelligent formatting.
15
16
 
17
+ ```ts
18
+ import SmartUnit from 'smart-unit';
19
+
20
+ const size = new SmartUnit(['B', 'KB', 'MB', 'GB'], { baseDigit: 1024 });
21
+
22
+ size.format(1024 * 1024 * 100); // "100MB"
23
+ size.format(1536); // "1.5KB"
24
+ size.parse('2.5GB'); // 2684354560
25
+ ```
26
+
16
27
  ## Features
17
28
 
18
29
  - 🎯 **Smart Formatting** — Automatically selects the optimal unit for display
@@ -21,6 +32,7 @@ English | [中文](./README.zh-CN.md)
21
32
  - 🧮 **High Precision** — Optional `decimal.js` integration for arbitrary precision
22
33
  - 📦 **TypeScript First** — Full type safety
23
34
  - 🪶 **Lightweight** — Core functionality with minimal footprint
35
+ - ✅ **Well Tested** — Comprehensive test suite with 100% coverage
24
36
 
25
37
  ## Install
26
38
 
@@ -102,9 +114,9 @@ Creates a unit converter instance.
102
114
  Formats a number to the optimal unit string.
103
115
 
104
116
  ```ts
105
- const size = new SmartUnit(['B', 'KB', 1024, 'MB'], { fractionDigits: 2 });
117
+ const size = new SmartUnit(['B', 'KB', 'MB'], { baseDigit: 1024, fractionDigits: 2 });
106
118
 
107
- size.format(1536); // => "1.5KB"
119
+ size.format(1536); // => "1.50KB"
108
120
  size.format(1536, 0); // => "2KB"
109
121
  size.format(1536, '1-3'); // => "1.5KB" (min 1, max 3 decimals)
110
122
  ```
@@ -114,7 +126,7 @@ size.format(1536, '1-3'); // => "1.5KB" (min 1, max 3 decimals)
114
126
  Parses a unit string back to base unit value.
115
127
 
116
128
  ```ts
117
- const size = new SmartUnit(['B', 'KB', 1024, 'MB']);
129
+ const size = new SmartUnit(['B', 'KB', 'MB'], { baseDigit: 1024 });
118
130
 
119
131
  size.parse('1.5KB'); // => 1536
120
132
  size.parse('2MB'); // => 2097152
@@ -125,7 +137,7 @@ size.parse('2MB'); // => 2097152
125
137
  Gets the optimal unit and converted value without formatting.
126
138
 
127
139
  ```ts
128
- const size = new SmartUnit(['B', 'KB', 1024, 'MB']);
140
+ const size = new SmartUnit(['B', 'KB', 'MB'], { baseDigit: 1024 });
129
141
 
130
142
  size.getUnit(1536);
131
143
  // => { num: 1.5, unit: 'KB' }
@@ -152,7 +164,7 @@ length.toBase(100, 'cm'); // => 1000 (mm)
152
164
  Extracts numeric value and unit from a formatted string.
153
165
 
154
166
  ```ts
155
- const size = new SmartUnit(['B', 'KB', 1024, 'MB']);
167
+ const size = new SmartUnit(['B', 'KB', 'MB'], { baseDigit: 1024 });
156
168
 
157
169
  size.splitUnit('1.5KB'); // => { num: 1.5, unit: 'KB' }
158
170
  ```
@@ -188,7 +200,7 @@ const freq = new SmartUnit(['Hz', 'kHz', 'MHz', 'GHz'], {
188
200
  fractionDigits: 2,
189
201
  });
190
202
 
191
- freq.format(2400000000); // => "2.4GHz"
203
+ freq.format(2400000000); // => "2.40GHz"
192
204
  ```
193
205
 
194
206
  ### Financial Amounts (High Precision)
@@ -203,6 +215,14 @@ const currency = new SmartUnit(['', 'K', 'M', 'B', 'T'], {
203
215
  currency.format('12345678901234567890'); // => "12345678.90T"
204
216
  ```
205
217
 
218
+ ## Testing
219
+
220
+ Run the test suite:
221
+
222
+ ```bash
223
+ npm test
224
+ ```
225
+
206
226
  ## TypeScript
207
227
 
208
228
  smart-unit is written in TypeScript and provides full type safety.
package/README.zh-CN.md CHANGED
@@ -5,6 +5,7 @@
5
5
  [![npm version](https://img.shields.io/npm/v/smart-unit)](https://www.npmjs.com/package/smart-unit)
6
6
  [![npm downloads](https://img.shields.io/npm/dm/smart-unit)](https://www.npmjs.com/package/smart-unit)
7
7
  [![bundle size](https://img.shields.io/bundlephobia/minzip/smart-unit)](https://bundlephobia.com/package/smart-unit)
8
+ [![test](https://github.com/flycran/smart-unit/workflows/Test/badge.svg)](https://github.com/flycran/smart-unit/actions)
8
9
  [![license](https://img.shields.io/npm/l/smart-unit)](./LICENSE)
9
10
 
10
11
  [English](./README.md) | 中文
@@ -13,6 +14,16 @@
13
14
 
14
15
  **smart-unit** 是一个轻量级的自动单位转换工具,支持智能格式化输出。
15
16
 
17
+ ```ts
18
+ import SmartUnit from 'smart-unit';
19
+
20
+ const size = new SmartUnit(['B', 'KB', 'MB', 'GB'], { baseDigit: 1024 });
21
+
22
+ size.format(1024 * 1024 * 100); // "100MB"
23
+ size.format(1536); // "1.5KB"
24
+ size.parse('2.5GB'); // 2684354560
25
+ ```
26
+
16
27
  ## 特性
17
28
 
18
29
  - 🎯 **智能格式化** — 自动选择最优单位进行展示
@@ -21,6 +32,7 @@
21
32
  - 🧮 **高精度** — 可选 `decimal.js` 集成,支持任意精度计算
22
33
  - 📦 **TypeScript 优先** — 完整的类型安全
23
34
  - 🪶 **轻量级** — 核心功能,体积小巧
35
+ - ✅ **测试完善** — 全面的测试套件,100% 覆盖率
24
36
 
25
37
  ## 安装
26
38
 
@@ -102,9 +114,9 @@ time.parse('2.5h'); // => 9000000 (ms)
102
114
  将数字格式化为最优单位字符串。
103
115
 
104
116
  ```ts
105
- const size = new SmartUnit(['B', 'KB', 1024, 'MB'], { fractionDigits: 2 });
117
+ const size = new SmartUnit(['B', 'KB', 'MB'], { baseDigit: 1024, fractionDigits: 2 });
106
118
 
107
- size.format(1536); // => "1.5KB"
119
+ size.format(1536); // => "1.50KB"
108
120
  size.format(1536, 0); // => "2KB"
109
121
  size.format(1536, '1-3'); // => "1.5KB"(最少1位,最多3位小数)
110
122
  ```
@@ -114,7 +126,7 @@ size.format(1536, '1-3'); // => "1.5KB"(最少1位,最多3位小数)
114
126
  将单位字符串解析回基本单位值。
115
127
 
116
128
  ```ts
117
- const size = new SmartUnit(['B', 'KB', 1024, 'MB']);
129
+ const size = new SmartUnit(['B', 'KB', 'MB'], { baseDigit: 1024 });
118
130
 
119
131
  size.parse('1.5KB'); // => 1536
120
132
  size.parse('2MB'); // => 2097152
@@ -125,7 +137,7 @@ size.parse('2MB'); // => 2097152
125
137
  获取最优单位和转换后的值(不进行格式化)。
126
138
 
127
139
  ```ts
128
- const size = new SmartUnit(['B', 'KB', 1024, 'MB']);
140
+ const size = new SmartUnit(['B', 'KB', 'MB'], { baseDigit: 1024 });
129
141
 
130
142
  size.getUnit(1536);
131
143
  // => { num: 1.5, unit: 'KB' }
@@ -152,7 +164,7 @@ length.toBase(100, 'cm'); // => 1000 (mm)
152
164
  从格式化字符串中提取数值和单位。
153
165
 
154
166
  ```ts
155
- const size = new SmartUnit(['B', 'KB', 1024, 'MB']);
167
+ const size = new SmartUnit(['B', 'KB', 'MB'], { baseDigit: 1024 });
156
168
 
157
169
  size.splitUnit('1.5KB'); // => { num: 1.5, unit: 'KB' }
158
170
  ```
@@ -188,7 +200,7 @@ const freq = new SmartUnit(['Hz', 'kHz', 'MHz', 'GHz'], {
188
200
  fractionDigits: 2,
189
201
  });
190
202
 
191
- freq.format(2400000000); // => "2.4GHz"
203
+ freq.format(2400000000); // => "2.40GHz"
192
204
  ```
193
205
 
194
206
  ### 金融金额(高精度)
@@ -203,6 +215,14 @@ const currency = new SmartUnit(['', 'K', 'M', 'B', 'T'], {
203
215
  currency.format('12345678901234567890'); // => "12345678.90T"
204
216
  ```
205
217
 
218
+ ## 测试
219
+
220
+ 运行测试套件:
221
+
222
+ ```bash
223
+ npm test
224
+ ```
225
+
206
226
  ## TypeScript
207
227
 
208
228
  smart-unit 使用 TypeScript 编写,提供完整的类型安全。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smart-unit",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Elegant unit conversion utility with automatic unit selection and high-precision support",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",
@@ -59,4 +59,4 @@
59
59
  "dependencies": {
60
60
  "decimal.js": "^10.5.0"
61
61
  }
62
- }
62
+ }