smart-unit 2.0.0 → 2.1.0

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/dist/index.cjs CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var utils = require('./utils-DqEXot5a.js');
5
+ var utils = require('./utils-4mK2MBi1.js');
6
6
 
7
7
  class SmartUnit extends utils.SmartUnitBase {
8
8
  constructor(units, option = {}) {
@@ -129,7 +129,7 @@ class SmartUnit extends utils.SmartUnitBase {
129
129
  }
130
130
  throw new Error(`Undefined unit: "${unit}".`);
131
131
  }
132
- // 将带单位的值转换为基础单位的数值
132
+ // 将带单位的字符串转换为基础单位的数值
133
133
  /**
134
134
  * Parses a string with unit into a base unit numeric value
135
135
  *
@@ -140,6 +140,21 @@ class SmartUnit extends utils.SmartUnitBase {
140
140
  const { num, unit } = this.splitUnit(str);
141
141
  return this.toBase(num, unit);
142
142
  }
143
+ // 将带链式单位的字符串转换为基础单位的数值
144
+ /**
145
+ * Parses a string with chain units into a base unit numeric value
146
+ *
147
+ * @param str - Input string containing a number and unit
148
+ * @returns The value converted to base unit
149
+ */
150
+ parseChain(str, separator = this.separator) {
151
+ const units = this.splitChainUnit(str, separator);
152
+ let sum = 0;
153
+ for (const unit of units) {
154
+ sum += this.toBase(unit.num, unit.unit);
155
+ }
156
+ return sum;
157
+ }
143
158
  // 将给定数值从原单位转换为最佳单位,并可指定小数精度
144
159
  /**
145
160
  * Converts a value from the original unit to the optimal unit with optional decimal precision
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { S as SmartUnitBase, E as ERROR_NAN_INPUT } from './utils-C1byK7Uk.js';
1
+ import { S as SmartUnitBase, E as ERROR_NAN_INPUT } from './utils-biRhOw5Y.js';
2
2
 
3
3
  class SmartUnit extends SmartUnitBase {
4
4
  constructor(units, option = {}) {
@@ -125,7 +125,7 @@ class SmartUnit extends SmartUnitBase {
125
125
  }
126
126
  throw new Error(`Undefined unit: "${unit}".`);
127
127
  }
128
- // 将带单位的值转换为基础单位的数值
128
+ // 将带单位的字符串转换为基础单位的数值
129
129
  /**
130
130
  * Parses a string with unit into a base unit numeric value
131
131
  *
@@ -136,6 +136,21 @@ class SmartUnit extends SmartUnitBase {
136
136
  const { num, unit } = this.splitUnit(str);
137
137
  return this.toBase(num, unit);
138
138
  }
139
+ // 将带链式单位的字符串转换为基础单位的数值
140
+ /**
141
+ * Parses a string with chain units into a base unit numeric value
142
+ *
143
+ * @param str - Input string containing a number and unit
144
+ * @returns The value converted to base unit
145
+ */
146
+ parseChain(str, separator = this.separator) {
147
+ const units = this.splitChainUnit(str, separator);
148
+ let sum = 0;
149
+ for (const unit of units) {
150
+ sum += this.toBase(unit.num, unit.unit);
151
+ }
152
+ return sum;
153
+ }
139
154
  // 将给定数值从原单位转换为最佳单位,并可指定小数精度
140
155
  /**
141
156
  * Converts a value from the original unit to the optimal unit with optional decimal precision
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var DecimalConstructor = require('decimal.js');
6
- var utils = require('./utils-DqEXot5a.js');
6
+ var utils = require('./utils-4mK2MBi1.js');
7
7
 
8
8
  class SmartUnitPrecision extends utils.SmartUnitBase {
9
9
  constructor(units, option = {}) {
@@ -158,6 +158,17 @@ class SmartUnitPrecision extends utils.SmartUnitBase {
158
158
  numStr,
159
159
  };
160
160
  }
161
+ // 从给定链式单位字符串中分离出数字部分和单位
162
+ /**
163
+ * Splits a string into its numeric part and unit
164
+ *
165
+ * @param str - Input string containing a number followed by a unit
166
+ * @returns An object containing the numeric value, unit, and Decimal instance
167
+ * @throws An error if no predefined unit is matched
168
+ */
169
+ splitChainUnit(str, separator = this.separator) {
170
+ return super.splitChainUnit(str, separator).map((unit) => (Object.assign(Object.assign({}, unit), { decimal: new this.DecimalClass(unit.numStr) })));
171
+ }
161
172
  // 将带单位的值转换为基础单位的数值
162
173
  /**
163
174
  * Parses a string with unit into a base unit numeric value
@@ -169,6 +180,21 @@ class SmartUnitPrecision extends utils.SmartUnitBase {
169
180
  const { decimal, unit } = this.splitUnit(str);
170
181
  return this.toBase(decimal, unit);
171
182
  }
183
+ // 将带链式单位的字符串转换为基础单位的数值
184
+ /**
185
+ * Parses a string with chain units into a base unit numeric value
186
+ *
187
+ * @param str - Input string containing a number and unit
188
+ * @returns The value converted to base unit
189
+ */
190
+ parseChain(str) {
191
+ const units = this.splitChainUnit(str);
192
+ let sum = new this.DecimalClass(0);
193
+ for (const unit of units) {
194
+ sum = sum.plus(this.toBase(unit.num, unit.unit));
195
+ }
196
+ return sum;
197
+ }
172
198
  // 将给定数值从原单位转换为最佳单位,并可指定小数精度
173
199
  /**
174
200
  * Converts a value from the original unit to the optimal unit with optional decimal precision
@@ -1,5 +1,5 @@
1
1
  import DecimalConstructor from 'decimal.js';
2
- import { S as SmartUnitBase, E as ERROR_NAN_INPUT } from './utils-C1byK7Uk.js';
2
+ import { S as SmartUnitBase, E as ERROR_NAN_INPUT } from './utils-biRhOw5Y.js';
3
3
 
4
4
  class SmartUnitPrecision extends SmartUnitBase {
5
5
  constructor(units, option = {}) {
@@ -154,6 +154,17 @@ class SmartUnitPrecision extends SmartUnitBase {
154
154
  numStr,
155
155
  };
156
156
  }
157
+ // 从给定链式单位字符串中分离出数字部分和单位
158
+ /**
159
+ * Splits a string into its numeric part and unit
160
+ *
161
+ * @param str - Input string containing a number followed by a unit
162
+ * @returns An object containing the numeric value, unit, and Decimal instance
163
+ * @throws An error if no predefined unit is matched
164
+ */
165
+ splitChainUnit(str, separator = this.separator) {
166
+ return super.splitChainUnit(str, separator).map((unit) => (Object.assign(Object.assign({}, unit), { decimal: new this.DecimalClass(unit.numStr) })));
167
+ }
157
168
  // 将带单位的值转换为基础单位的数值
158
169
  /**
159
170
  * Parses a string with unit into a base unit numeric value
@@ -165,6 +176,21 @@ class SmartUnitPrecision extends SmartUnitBase {
165
176
  const { decimal, unit } = this.splitUnit(str);
166
177
  return this.toBase(decimal, unit);
167
178
  }
179
+ // 将带链式单位的字符串转换为基础单位的数值
180
+ /**
181
+ * Parses a string with chain units into a base unit numeric value
182
+ *
183
+ * @param str - Input string containing a number and unit
184
+ * @returns The value converted to base unit
185
+ */
186
+ parseChain(str) {
187
+ const units = this.splitChainUnit(str);
188
+ let sum = new this.DecimalClass(0);
189
+ for (const unit of units) {
190
+ sum = sum.plus(this.toBase(unit.num, unit.unit));
191
+ }
192
+ return sum;
193
+ }
168
194
  // 将给定数值从原单位转换为最佳单位,并可指定小数精度
169
195
  /**
170
196
  * Converts a value from the original unit to the optimal unit with optional decimal precision
@@ -48,6 +48,13 @@ export declare class SmartUnit<U extends string = string> extends SmartUnitBase<
48
48
  * @returns The value converted to base unit
49
49
  */
50
50
  parse(str: string): number;
51
+ /**
52
+ * Parses a string with chain units into a base unit numeric value
53
+ *
54
+ * @param str - Input string containing a number and unit
55
+ * @returns The value converted to base unit
56
+ */
57
+ parseChain(str: string, separator?: string): number;
51
58
  /**
52
59
  * Converts a value from the original unit to the optimal unit with optional decimal precision
53
60
  *
@@ -12,6 +12,7 @@ export declare abstract class SmartUnitBase<U extends string = string, D extends
12
12
  _sortedUnitNames?: U[];
13
13
  /** Sorted unit names by length for efficient lookup */
14
14
  get sortedUnitNames(): U[];
15
+ /** Unit conversion function */
15
16
  convert?: (str: U) => string;
16
17
  constructor(units: (U | number)[], option?: SmartUnitOptions);
17
18
  withConvert(convert: (str: U) => string): this;
@@ -28,27 +29,9 @@ export declare abstract class SmartUnitBase<U extends string = string, D extends
28
29
  abstract getUnit(num: InputNumber, fractionDigits?: FractionDigits): FormattedValue<U>;
29
30
  protected _format(numStr: string, unit: U): string;
30
31
  abstract format(num: InputNumber, fractionDigits?: FractionDigits): string;
31
- /**
32
- * Gets the chain of units for the input number
33
- *
34
- * @param num - The input number to determine the chain for
35
- * @returns An array of FormattedValue objects representing the chain of units
36
- */
37
32
  abstract getChainUnit(num: InputNumber): FormattedValue<U>[];
38
33
  protected _formatChain(chain: FormattedValue<U>[], separator?: string): string;
39
- /**
40
- * Formats a number as a chain of units string
41
- * @param num - The input number to format
42
- * @returns The formatted chain string
43
- */
44
34
  formatChain(num: InputNumber, separator?: string): string;
45
- /**
46
- * Converts a value from the specified unit to the base unit
47
- *
48
- * @param num - The number to convert
49
- * @param unit - The original unit of the number
50
- * @returns The converted value in base unit
51
- */
52
35
  abstract toBase(num: InputNumber, unit: U): D extends true ? Decimal : number;
53
36
  /**
54
37
  * Splits a string into its numeric part and unit
@@ -59,19 +42,14 @@ export declare abstract class SmartUnitBase<U extends string = string, D extends
59
42
  */
60
43
  splitUnit(str: string): FormattedValue<U>;
61
44
  /**
62
- * Parses a string with unit into a base unit numeric value
45
+ * Splits a string into its numeric part and unit
63
46
  *
64
- * @param str - Input string containing a number and unit
65
- * @returns The value converted to base unit
47
+ * @param str - Input string containing a number followed by a unit
48
+ * @returns An object containing the numeric value, unit, and Decimal instance
49
+ * @throws An error if no predefined unit is matched
66
50
  */
51
+ splitChainUnit(str: string, separator?: string): FormattedValue<U>[];
67
52
  abstract parse(str: string): D extends true ? Decimal : number;
68
- /**
69
- * Converts a value from the original unit to the optimal unit with optional decimal precision
70
- *
71
- * @param num - The number to convert
72
- * @param unit - The original unit
73
- * @param fractionDigits - Optional decimal places for formatting output
74
- * @returns The converted number as a formatted string
75
- */
53
+ abstract parseChain(str: string): D extends true ? Decimal : number;
76
54
  abstract fromUnitFormat(num: InputNumber, unit: U, fractionDigits?: FractionDigits): string;
77
55
  }
@@ -63,6 +63,14 @@ export declare class SmartUnitPrecision<U extends string = string> extends Smart
63
63
  * @throws An error if no predefined unit is matched
64
64
  */
65
65
  splitUnit(str: string): FormattedValuePrecision<U>;
66
+ /**
67
+ * Splits a string into its numeric part and unit
68
+ *
69
+ * @param str - Input string containing a number followed by a unit
70
+ * @returns An object containing the numeric value, unit, and Decimal instance
71
+ * @throws An error if no predefined unit is matched
72
+ */
73
+ splitChainUnit(str: string, separator?: string): FormattedValuePrecision<U>[];
66
74
  /**
67
75
  * Parses a string with unit into a base unit numeric value
68
76
  *
@@ -70,6 +78,13 @@ export declare class SmartUnitPrecision<U extends string = string> extends Smart
70
78
  * @returns The value converted to base unit
71
79
  */
72
80
  parse(str: string): Decimal;
81
+ /**
82
+ * Parses a string with chain units into a base unit numeric value
83
+ *
84
+ * @param str - Input string containing a number and unit
85
+ * @returns The value converted to base unit
86
+ */
87
+ parseChain(str: string): Decimal;
73
88
  /**
74
89
  * Converts a value from the original unit to the optimal unit with optional decimal precision
75
90
  *
@@ -107,11 +107,6 @@ class SmartUnitBase {
107
107
  return chain.map(({ numStr, unit }) => this._format(numStr, unit)).join(separator);
108
108
  }
109
109
  // 将数字格式化为链式单位字符串
110
- /**
111
- * Formats a number as a chain of units string
112
- * @param num - The input number to format
113
- * @returns The formatted chain string
114
- */
115
110
  formatChain(num, separator = this.separator) {
116
111
  const chain = this.getChainUnit(num);
117
112
  return this._formatChain(chain, separator);
@@ -127,8 +122,9 @@ class SmartUnitBase {
127
122
  splitUnit(str) {
128
123
  const sortedUnits = this.sortedUnitNames;
129
124
  for (const unit of sortedUnits) {
130
- if (str.endsWith(unit)) {
131
- const numStr = str.slice(0, -unit.length);
125
+ const matchUnit = this.convert ? this.convert(unit) : unit;
126
+ if (str.endsWith(matchUnit)) {
127
+ const numStr = str.slice(0, -matchUnit.length);
132
128
  const num = +numStr;
133
129
  if (Number.isNaN(num))
134
130
  throw new Error(`Invalid number: "${numStr}".`);
@@ -141,6 +137,42 @@ class SmartUnitBase {
141
137
  }
142
138
  throw new Error(`Undefined unit: "${str}".`);
143
139
  }
140
+ // 从给定链式单位字符串中分离出数字部分和单位
141
+ /**
142
+ * Splits a string into its numeric part and unit
143
+ *
144
+ * @param str - Input string containing a number followed by a unit
145
+ * @returns An object containing the numeric value, unit, and Decimal instance
146
+ * @throws An error if no predefined unit is matched
147
+ */
148
+ splitChainUnit(str, separator = this.separator) {
149
+ const results = [];
150
+ const sortedUnits = this.sortedUnitNames;
151
+ const strLen = str.length;
152
+ let start = 0;
153
+ for (let i = 0; i < strLen; i++) {
154
+ for (const unit of sortedUnits) {
155
+ const matchUnit = this.convert ? this.convert(unit) : unit;
156
+ if (str.slice(i, i + matchUnit.length) === matchUnit) {
157
+ const numStr = str.slice(start, i);
158
+ const num = +numStr;
159
+ if (Number.isNaN(num))
160
+ throw new Error(`Invalid number: "${numStr}".`);
161
+ results.push({
162
+ num,
163
+ unit,
164
+ numStr,
165
+ });
166
+ i += matchUnit.length + separator.length;
167
+ start = i;
168
+ break;
169
+ }
170
+ }
171
+ }
172
+ if (results.length > 0)
173
+ return results;
174
+ throw new Error(`Undefined unit: "${str}".`);
175
+ }
144
176
  }
145
177
 
146
178
  const ERROR_NAN_INPUT = 'Accepting NaN as an argument may be unintentional and could lead to invalid results.';
@@ -105,11 +105,6 @@ class SmartUnitBase {
105
105
  return chain.map(({ numStr, unit }) => this._format(numStr, unit)).join(separator);
106
106
  }
107
107
  // 将数字格式化为链式单位字符串
108
- /**
109
- * Formats a number as a chain of units string
110
- * @param num - The input number to format
111
- * @returns The formatted chain string
112
- */
113
108
  formatChain(num, separator = this.separator) {
114
109
  const chain = this.getChainUnit(num);
115
110
  return this._formatChain(chain, separator);
@@ -125,8 +120,9 @@ class SmartUnitBase {
125
120
  splitUnit(str) {
126
121
  const sortedUnits = this.sortedUnitNames;
127
122
  for (const unit of sortedUnits) {
128
- if (str.endsWith(unit)) {
129
- const numStr = str.slice(0, -unit.length);
123
+ const matchUnit = this.convert ? this.convert(unit) : unit;
124
+ if (str.endsWith(matchUnit)) {
125
+ const numStr = str.slice(0, -matchUnit.length);
130
126
  const num = +numStr;
131
127
  if (Number.isNaN(num))
132
128
  throw new Error(`Invalid number: "${numStr}".`);
@@ -139,6 +135,42 @@ class SmartUnitBase {
139
135
  }
140
136
  throw new Error(`Undefined unit: "${str}".`);
141
137
  }
138
+ // 从给定链式单位字符串中分离出数字部分和单位
139
+ /**
140
+ * Splits a string into its numeric part and unit
141
+ *
142
+ * @param str - Input string containing a number followed by a unit
143
+ * @returns An object containing the numeric value, unit, and Decimal instance
144
+ * @throws An error if no predefined unit is matched
145
+ */
146
+ splitChainUnit(str, separator = this.separator) {
147
+ const results = [];
148
+ const sortedUnits = this.sortedUnitNames;
149
+ const strLen = str.length;
150
+ let start = 0;
151
+ for (let i = 0; i < strLen; i++) {
152
+ for (const unit of sortedUnits) {
153
+ const matchUnit = this.convert ? this.convert(unit) : unit;
154
+ if (str.slice(i, i + matchUnit.length) === matchUnit) {
155
+ const numStr = str.slice(start, i);
156
+ const num = +numStr;
157
+ if (Number.isNaN(num))
158
+ throw new Error(`Invalid number: "${numStr}".`);
159
+ results.push({
160
+ num,
161
+ unit,
162
+ numStr,
163
+ });
164
+ i += matchUnit.length + separator.length;
165
+ start = i;
166
+ break;
167
+ }
168
+ }
169
+ }
170
+ if (results.length > 0)
171
+ return results;
172
+ throw new Error(`Undefined unit: "${str}".`);
173
+ }
142
174
  }
143
175
 
144
176
  const ERROR_NAN_INPUT = 'Accepting NaN as an argument may be unintentional and could lead to invalid results.';
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "smart-unit",
3
- "version": "2.0.0",
3
+ "type": "module",
4
+ "version": "2.1.0",
4
5
  "author": "flycran",
5
6
  "scripts": {
6
7
  "build": "rollup -c",
7
8
  "test": "vitest run",
8
9
  "test:watch": "vitest",
10
+ "test:performance": "node scripts/src/performance.ts",
9
11
  "format": "biome format --fix .",
10
12
  "docs:dev": "vitepress dev docs",
11
13
  "docs:build": "vitepress build docs",
File without changes
File without changes
File without changes