@pisell/utils 3.0.6 → 3.0.7

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 (200) hide show
  1. package/es/arrayUtils.d.ts +23 -26
  2. package/es/arrayUtils.js +157 -97
  3. package/es/common/index.d.ts +5 -0
  4. package/es/common/index.js +5 -0
  5. package/es/common/stateHelper.d.ts +2 -6
  6. package/es/common/stateHelper.js +24 -22
  7. package/es/common/types.d.ts +10 -13
  8. package/es/common/types.js +1 -0
  9. package/es/componentStorage.d.ts +25 -27
  10. package/es/componentStorage.js +130 -94
  11. package/es/constants/index.js +9 -7
  12. package/es/date.js +53 -36
  13. package/es/document.js +67 -43
  14. package/es/escPosPrinter/EscPosPrinter.js +490 -353
  15. package/es/escPosPrinter/index.d.ts +9 -9
  16. package/es/escPosPrinter/index.js +52 -37
  17. package/es/firebase/config/index.js +54 -0
  18. package/es/firebase/index.js +232 -0
  19. package/es/format.d.ts +15 -12
  20. package/es/format.js +156 -89
  21. package/es/image.js +60 -48
  22. package/es/index.d.ts +24 -39
  23. package/es/index.js +35 -40
  24. package/es/jsBridge/index.d.ts +4 -8
  25. package/es/jsBridge/index.js +55 -56
  26. package/es/jsBridge/regDeviceApi.js +126 -87
  27. package/es/jsBridge/types.d.ts +115 -95
  28. package/es/jsBridge/types.js +1 -0
  29. package/es/locales.d.ts +8 -11
  30. package/es/locales.js +96 -77
  31. package/es/log.d.ts +6 -11
  32. package/es/log.js +59 -37
  33. package/es/miniRedux.d.ts +13 -20
  34. package/es/miniRedux.js +76 -42
  35. package/es/number-input/hooks/index.d.ts +4 -0
  36. package/es/number-input/hooks/index.js +5 -0
  37. package/es/number-input/hooks/useNumberInputState.d.ts +2 -6
  38. package/es/number-input/hooks/useNumberInputState.js +128 -71
  39. package/es/number-input/index.d.ts +14 -0
  40. package/es/number-input/index.js +21 -0
  41. package/es/number-input/types.d.ts +88 -88
  42. package/es/number-input/types.js +1 -0
  43. package/es/number-input/utils/formatter.d.ts +10 -13
  44. package/es/number-input/utils/formatter.js +77 -51
  45. package/es/number-input/utils/index.d.ts +7 -0
  46. package/es/number-input/utils/index.js +8 -0
  47. package/es/number-input/utils/validation.d.ts +2 -7
  48. package/es/number-input/utils/validation.js +66 -51
  49. package/es/otherUtils.d.ts +9 -12
  50. package/es/otherUtils.js +129 -71
  51. package/es/platform.d.ts +9 -12
  52. package/es/platform.js +85 -61
  53. package/es/select/hooks/index.d.ts +5 -0
  54. package/es/select/hooks/index.js +6 -0
  55. package/es/select/hooks/useDebouncedSearch.d.ts +1 -4
  56. package/es/select/hooks/useDebouncedSearch.js +53 -39
  57. package/es/select/hooks/useSelectState.d.ts +7 -7
  58. package/es/select/hooks/useSelectState.js +113 -56
  59. package/es/select/index.d.ts +8 -7
  60. package/es/select/index.js +13 -0
  61. package/es/select/types.d.ts +211 -211
  62. package/es/select/types.js +1 -0
  63. package/es/select/utils/filterOptions.d.ts +6 -7
  64. package/es/select/utils/filterOptions.js +31 -26
  65. package/es/select/utils/helpers.d.ts +9 -10
  66. package/es/select/utils/helpers.js +52 -42
  67. package/es/select/utils/index.d.ts +7 -4
  68. package/es/select/utils/index.js +8 -0
  69. package/es/select/utils/sortOptions.d.ts +5 -6
  70. package/es/select/utils/sortOptions.js +31 -19
  71. package/es/select/utils/tagAggregation.d.ts +6 -6
  72. package/es/select/utils/tagAggregation.js +47 -34
  73. package/es/text-input/hooks/index.d.ts +4 -0
  74. package/es/text-input/hooks/index.js +5 -0
  75. package/es/text-input/hooks/useTextInputState.d.ts +2 -6
  76. package/es/text-input/hooks/useTextInputState.js +126 -69
  77. package/es/text-input/index.d.ts +14 -0
  78. package/es/text-input/index.js +20 -0
  79. package/es/text-input/types.d.ts +48 -48
  80. package/es/text-input/types.js +1 -0
  81. package/es/text-input/utils/index.d.ts +5 -0
  82. package/es/text-input/utils/index.js +7 -0
  83. package/es/text-input/utils/validation.d.ts +2 -7
  84. package/es/text-input/utils/validation.js +70 -50
  85. package/es/typeUtils.js +84 -61
  86. package/es/walletValidity.d.ts +42 -44
  87. package/es/walletValidity.js +557 -396
  88. package/es/weighing.d.ts +45 -48
  89. package/es/weighing.js +106 -83
  90. package/lib/arrayUtils.d.ts +23 -26
  91. package/lib/arrayUtils.js +111 -101
  92. package/lib/common/index.d.ts +5 -0
  93. package/lib/common/index.js +29 -0
  94. package/lib/common/stateHelper.d.ts +2 -6
  95. package/lib/common/stateHelper.js +32 -21
  96. package/lib/common/types.d.ts +10 -13
  97. package/lib/common/types.js +17 -0
  98. package/lib/componentStorage.d.ts +25 -27
  99. package/lib/componentStorage.js +126 -96
  100. package/lib/constants/index.js +36 -7
  101. package/lib/date.js +65 -43
  102. package/lib/document.js +62 -46
  103. package/lib/escPosPrinter/EscPosPrinter.js +403 -327
  104. package/lib/escPosPrinter/index.d.ts +9 -9
  105. package/lib/escPosPrinter/index.js +84 -38
  106. package/lib/firebase/config/index.js +79 -0
  107. package/lib/firebase/index.js +180 -0
  108. package/lib/format.d.ts +15 -12
  109. package/lib/format.js +133 -91
  110. package/lib/image.js +74 -48
  111. package/lib/index.d.ts +24 -39
  112. package/lib/index.js +80 -137
  113. package/lib/jsBridge/index.d.ts +4 -8
  114. package/lib/jsBridge/index.js +76 -57
  115. package/lib/jsBridge/regDeviceApi.js +133 -86
  116. package/lib/jsBridge/types.d.ts +115 -95
  117. package/lib/jsBridge/types.js +17 -0
  118. package/lib/locales.d.ts +8 -11
  119. package/lib/locales.js +90 -79
  120. package/lib/log.d.ts +6 -11
  121. package/lib/log.js +61 -37
  122. package/lib/miniRedux.d.ts +13 -20
  123. package/lib/miniRedux.js +89 -43
  124. package/lib/number-input/hooks/index.d.ts +4 -0
  125. package/lib/number-input/hooks/index.js +29 -0
  126. package/lib/number-input/hooks/useNumberInputState.d.ts +2 -6
  127. package/lib/number-input/hooks/useNumberInputState.js +81 -71
  128. package/lib/number-input/index.d.ts +14 -0
  129. package/lib/number-input/index.js +41 -0
  130. package/lib/number-input/types.d.ts +88 -88
  131. package/lib/number-input/types.js +17 -0
  132. package/lib/number-input/utils/formatter.d.ts +10 -13
  133. package/lib/number-input/utils/formatter.js +49 -52
  134. package/lib/number-input/utils/index.d.ts +7 -0
  135. package/lib/number-input/utils/index.js +39 -0
  136. package/lib/number-input/utils/validation.d.ts +2 -7
  137. package/lib/number-input/utils/validation.js +52 -50
  138. package/lib/otherUtils.d.ts +9 -12
  139. package/lib/otherUtils.js +98 -86
  140. package/lib/platform.d.ts +9 -12
  141. package/lib/platform.js +123 -68
  142. package/lib/select/hooks/index.d.ts +5 -0
  143. package/lib/select/hooks/index.js +34 -0
  144. package/lib/select/hooks/useDebouncedSearch.d.ts +1 -4
  145. package/lib/select/hooks/useDebouncedSearch.js +41 -40
  146. package/lib/select/hooks/useSelectState.d.ts +7 -7
  147. package/lib/select/hooks/useSelectState.js +93 -58
  148. package/lib/select/index.d.ts +8 -7
  149. package/lib/select/index.js +53 -0
  150. package/lib/select/types.d.ts +211 -211
  151. package/lib/select/types.js +17 -0
  152. package/lib/select/utils/filterOptions.d.ts +6 -7
  153. package/lib/select/utils/filterOptions.js +37 -26
  154. package/lib/select/utils/helpers.d.ts +9 -10
  155. package/lib/select/utils/helpers.js +41 -45
  156. package/lib/select/utils/index.d.ts +7 -4
  157. package/lib/select/utils/index.js +48 -0
  158. package/lib/select/utils/sortOptions.d.ts +5 -6
  159. package/lib/select/utils/sortOptions.js +39 -18
  160. package/lib/select/utils/tagAggregation.d.ts +6 -6
  161. package/lib/select/utils/tagAggregation.js +41 -33
  162. package/lib/text-input/hooks/index.d.ts +4 -0
  163. package/lib/text-input/hooks/index.js +29 -0
  164. package/lib/text-input/hooks/useTextInputState.d.ts +2 -6
  165. package/lib/text-input/hooks/useTextInputState.js +79 -69
  166. package/lib/text-input/index.d.ts +14 -0
  167. package/lib/text-input/index.js +35 -0
  168. package/lib/text-input/types.d.ts +48 -48
  169. package/lib/text-input/types.js +17 -0
  170. package/lib/text-input/utils/index.d.ts +5 -0
  171. package/lib/text-input/utils/index.js +32 -0
  172. package/lib/text-input/utils/validation.d.ts +2 -7
  173. package/lib/text-input/utils/validation.js +58 -49
  174. package/lib/typeUtils.js +73 -68
  175. package/lib/walletValidity.d.ts +42 -44
  176. package/lib/walletValidity.js +393 -410
  177. package/lib/weighing.d.ts +45 -48
  178. package/lib/weighing.js +149 -104
  179. package/package.json +1 -1
  180. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js +0 -27
  181. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.js +0 -12
  182. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js +0 -25
  183. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPrimitive.js +0 -14
  184. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPropertyKey.js +0 -9
  185. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/typeof.js +0 -11
  186. package/es/date.d.ts +0 -31
  187. package/es/document.d.ts +0 -16
  188. package/es/image.d.ts +0 -14
  189. package/es/typeUtils.d.ts +0 -45
  190. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js +0 -27
  191. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.js +0 -12
  192. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js +0 -25
  193. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPrimitive.js +0 -14
  194. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPropertyKey.js +0 -9
  195. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/typeof.js +0 -16
  196. package/lib/_virtual/_rolldown/runtime.js +0 -23
  197. package/lib/date.d.ts +0 -31
  198. package/lib/document.d.ts +0 -16
  199. package/lib/image.d.ts +0 -14
  200. package/lib/typeUtils.d.ts +0 -45
package/es/weighing.d.ts CHANGED
@@ -1,65 +1,62 @@
1
- //#region src/weighing.d.ts
2
1
  /** 商品配置与 NativeScale 使用的重量单位。 */
3
- type WeightUnitName = 'KILOGRAMS' | 'GRAMS' | 'POUNDS' | 'OUNCES';
2
+ export declare type WeightUnitName = 'KILOGRAMS' | 'GRAMS' | 'POUNDS' | 'OUNCES';
4
3
  /** 订单快照与 UI 使用的标准单位符号。 */
5
- type WeightUnitSymbol = 'kg' | 'g' | 'lb' | 'oz';
6
- type WeightUnit = WeightUnitName | WeightUnitSymbol;
4
+ export declare type WeightUnitSymbol = 'kg' | 'g' | 'lb' | 'oz';
5
+ export declare type WeightUnit = WeightUnitName | WeightUnitSymbol;
7
6
  /**
8
7
  * Pisell 的标准重量单位。目录单价、缺省秤单位和缺省展示均以它为准。
9
8
  * 后续标准单位变化时,应优先只修改此处。
10
9
  */
11
- declare const STANDARD_WEIGHT_UNIT: WeightUnitName;
12
- declare const WEIGHT_PRECISION = 3;
13
- declare const UNIT_PRICE_PRECISION = 2;
14
- declare const MAX_RETAIL_SCALE_WEIGHT_KG = 1000;
15
- declare const WEIGHT_UNIT_SYMBOLS: Record<WeightUnitName, WeightUnitSymbol>;
16
- declare const WEIGHT_UNIT_NAMES: Record<WeightUnitSymbol, WeightUnitName>;
10
+ export declare const STANDARD_WEIGHT_UNIT: WeightUnitName;
11
+ export declare const WEIGHT_PRECISION = 3;
12
+ export declare const UNIT_PRICE_PRECISION = 2;
13
+ export declare const MAX_RETAIL_SCALE_WEIGHT_KG = 1000;
14
+ export declare const WEIGHT_UNIT_SYMBOLS: Record<WeightUnitName, WeightUnitSymbol>;
15
+ export declare const WEIGHT_UNIT_NAMES: Record<WeightUnitSymbol, WeightUnitName>;
17
16
  /** 单位对应的克数,作为不依赖 NativeScale 的目录价格换算基准。 */
18
- declare const WEIGHT_UNIT_GRAMS: Record<WeightUnit, number>;
19
- declare const STANDARD_WEIGHT_UNIT_SYMBOL: WeightUnitSymbol;
20
- declare const normalizeWeightUnit: (unit?: string | null, fallback?: WeightUnitName) => WeightUnitName;
21
- declare const getWeightUnitSymbol: (unit?: string | null) => string;
22
- declare const getWeightUnitGrams: (unit?: string | null) => number | undefined;
17
+ export declare const WEIGHT_UNIT_GRAMS: Record<WeightUnit, number>;
18
+ export declare const STANDARD_WEIGHT_UNIT_SYMBOL: WeightUnitSymbol;
19
+ export declare const normalizeWeightUnit: (unit?: string | null, fallback?: WeightUnitName) => WeightUnitName;
20
+ export declare const getWeightUnitSymbol: (unit?: string | null) => string;
21
+ export declare const getWeightUnitGrams: (unit?: string | null) => number | undefined;
23
22
  /**
24
23
  * 将“每 sourceUnitValue 个 sourceUnit 的价格”换算到每 1 个目标单位的倍率。
25
24
  * 这里只处理目录单价的单位关系;实际秤读数计价仍应使用 NativeScale。
26
25
  */
27
- declare const getWeightUnitPriceConversionFactor: (params: {
28
- sourceUnit?: string | null;
29
- sourceUnitValue?: string | number | null;
30
- targetUnit?: string | null;
26
+ export declare const getWeightUnitPriceConversionFactor: (params: {
27
+ sourceUnit?: string | null;
28
+ sourceUnitValue?: string | number | null;
29
+ targetUnit?: string | null;
31
30
  }) => number | undefined;
32
- declare const isSupportedWeightUnit: (unit?: unknown) => unit is WeightUnit;
33
- declare const formatWeightValue: (value: unknown, precision?: number) => string;
34
- declare const formatUnitPriceValue: (value: unknown, precision?: number) => string;
35
- type ScaleReadingStatus = 'stable' | 'unstable' | 'overload' | 'negative' | string | null;
36
- declare const INVALID_SCALE_READING_STATUSES: readonly ["overload", "negative"];
37
- declare const isInvalidScaleReadingStatus: (status?: ScaleReadingStatus) => boolean;
38
- declare const getUsableScaleWeight: (status: ScaleReadingStatus | undefined, weight: number) => number;
39
- interface WeighingSnapshot {
40
- open_sold_weight?: number | string | boolean;
41
- mode?: 'auto' | 'manual' | string;
42
- weight_source?: 'auto' | 'manual' | string;
43
- weight?: string | number;
44
- tare?: {
45
- id?: string;
46
- label?: string;
47
- value?: string | number;
31
+ export declare const isSupportedWeightUnit: (unit?: unknown) => unit is WeightUnit;
32
+ export declare const formatWeightValue: (value: unknown, precision?: number) => string;
33
+ export declare const formatUnitPriceValue: (value: unknown, precision?: number) => string;
34
+ export declare type ScaleReadingStatus = 'stable' | 'unstable' | 'overload' | 'negative' | string | null;
35
+ export declare const INVALID_SCALE_READING_STATUSES: readonly ["overload", "negative"];
36
+ export declare const isInvalidScaleReadingStatus: (status?: ScaleReadingStatus) => boolean;
37
+ export declare const getUsableScaleWeight: (status: ScaleReadingStatus | undefined, weight: number) => number;
38
+ export interface WeighingSnapshot {
39
+ open_sold_weight?: number | string | boolean;
40
+ mode?: 'auto' | 'manual' | string;
41
+ weight_source?: 'auto' | 'manual' | string;
42
+ weight?: string | number;
43
+ tare?: {
44
+ id?: string;
45
+ label?: string;
46
+ value?: string | number;
47
+ unit?: WeightUnit | string;
48
+ };
49
+ tare_name?: string;
50
+ tare_weight?: string | number;
51
+ net_weight?: string | number;
52
+ unit_price?: string | number;
48
53
  unit?: WeightUnit | string;
49
- };
50
- tare_name?: string;
51
- tare_weight?: string | number;
52
- net_weight?: string | number;
53
- unit_price?: string | number;
54
- unit?: WeightUnit | string;
55
- unit_weight?: string | number;
56
- unit_data_snapshot?: unknown;
57
- scalePriceProduct?: unknown;
58
- total_amount?: string | number;
54
+ unit_weight?: string | number;
55
+ unit_data_snapshot?: unknown;
56
+ scalePriceProduct?: unknown;
57
+ total_amount?: string | number;
59
58
  }
60
59
  /**
61
60
  * 解析订单行称重快照:product_sku 是权威源,_extend.weighing 仅用于旧流程兼容。
62
61
  */
63
- declare const resolveWeighingSnapshot: (dataSource?: Record<string, any> | null) => WeighingSnapshot | undefined;
64
- //#endregion
65
- export { INVALID_SCALE_READING_STATUSES, MAX_RETAIL_SCALE_WEIGHT_KG, STANDARD_WEIGHT_UNIT, STANDARD_WEIGHT_UNIT_SYMBOL, ScaleReadingStatus, UNIT_PRICE_PRECISION, WEIGHT_PRECISION, WEIGHT_UNIT_GRAMS, WEIGHT_UNIT_NAMES, WEIGHT_UNIT_SYMBOLS, WeighingSnapshot, WeightUnit, WeightUnitName, WeightUnitSymbol, formatUnitPriceValue, formatWeightValue, getUsableScaleWeight, getWeightUnitGrams, getWeightUnitPriceConversionFactor, getWeightUnitSymbol, isInvalidScaleReadingStatus, isSupportedWeightUnit, normalizeWeightUnit, resolveWeighingSnapshot };
62
+ export declare const resolveWeighingSnapshot: (dataSource?: Record<string, any> | null) => WeighingSnapshot | undefined;
package/es/weighing.js CHANGED
@@ -1,96 +1,119 @@
1
- //#region src/weighing.ts
1
+ /** 商品配置与 NativeScale 使用的重量单位。 */
2
+
3
+ /** 订单快照与 UI 使用的标准单位符号。 */
4
+
2
5
  /**
3
- * Pisell 的标准重量单位。目录单价、缺省秤单位和缺省展示均以它为准。
4
- * 后续标准单位变化时,应优先只修改此处。
5
- */
6
- const STANDARD_WEIGHT_UNIT = "KILOGRAMS";
7
- const WEIGHT_PRECISION = 3;
8
- const UNIT_PRICE_PRECISION = 2;
9
- const MAX_RETAIL_SCALE_WEIGHT_KG = 1e3;
10
- const WEIGHT_UNIT_SYMBOLS = {
11
- KILOGRAMS: "kg",
12
- GRAMS: "g",
13
- POUNDS: "lb",
14
- OUNCES: "oz"
6
+ * Pisell 的标准重量单位。目录单价、缺省秤单位和缺省展示均以它为准。
7
+ * 后续标准单位变化时,应优先只修改此处。
8
+ */
9
+ export var STANDARD_WEIGHT_UNIT = 'KILOGRAMS';
10
+ export var WEIGHT_PRECISION = 3;
11
+ export var UNIT_PRICE_PRECISION = 2;
12
+ export var MAX_RETAIL_SCALE_WEIGHT_KG = 1000;
13
+ export var WEIGHT_UNIT_SYMBOLS = {
14
+ KILOGRAMS: 'kg',
15
+ GRAMS: 'g',
16
+ POUNDS: 'lb',
17
+ OUNCES: 'oz'
15
18
  };
16
- const WEIGHT_UNIT_NAMES = {
17
- kg: "KILOGRAMS",
18
- g: "GRAMS",
19
- lb: "POUNDS",
20
- oz: "OUNCES"
19
+ export var WEIGHT_UNIT_NAMES = {
20
+ kg: 'KILOGRAMS',
21
+ g: 'GRAMS',
22
+ lb: 'POUNDS',
23
+ oz: 'OUNCES'
21
24
  };
25
+
22
26
  /** 单位对应的克数,作为不依赖 NativeScale 的目录价格换算基准。 */
23
- const WEIGHT_UNIT_GRAMS = {
24
- KILOGRAMS: 1e3,
25
- GRAMS: 1,
26
- POUNDS: 453.59237,
27
- OUNCES: 28.349523125,
28
- kg: 1e3,
29
- g: 1,
30
- lb: 453.59237,
31
- oz: 28.349523125
27
+ export var WEIGHT_UNIT_GRAMS = {
28
+ KILOGRAMS: 1000,
29
+ GRAMS: 1,
30
+ POUNDS: 453.59237,
31
+ OUNCES: 28.349523125,
32
+ kg: 1000,
33
+ g: 1,
34
+ lb: 453.59237,
35
+ oz: 28.349523125
32
36
  };
33
- const STANDARD_WEIGHT_UNIT_SYMBOL = WEIGHT_UNIT_SYMBOLS[STANDARD_WEIGHT_UNIT];
34
- const normalizeWeightUnit = (unit, fallback = STANDARD_WEIGHT_UNIT) => {
35
- if (unit && unit in WEIGHT_UNIT_SYMBOLS) return unit;
36
- if (unit && unit in WEIGHT_UNIT_NAMES) return WEIGHT_UNIT_NAMES[unit];
37
- return fallback;
37
+ export var STANDARD_WEIGHT_UNIT_SYMBOL = WEIGHT_UNIT_SYMBOLS[STANDARD_WEIGHT_UNIT];
38
+ export var normalizeWeightUnit = function normalizeWeightUnit(unit) {
39
+ var fallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : STANDARD_WEIGHT_UNIT;
40
+ if (unit && unit in WEIGHT_UNIT_SYMBOLS) {
41
+ return unit;
42
+ }
43
+ if (unit && unit in WEIGHT_UNIT_NAMES) {
44
+ return WEIGHT_UNIT_NAMES[unit];
45
+ }
46
+ return fallback;
38
47
  };
39
- const getWeightUnitSymbol = (unit) => {
40
- if (unit && unit in WEIGHT_UNIT_SYMBOLS) return WEIGHT_UNIT_SYMBOLS[unit];
41
- if (unit && unit in WEIGHT_UNIT_NAMES) return unit;
42
- return unit || STANDARD_WEIGHT_UNIT_SYMBOL;
48
+ export var getWeightUnitSymbol = function getWeightUnitSymbol(unit) {
49
+ if (unit && unit in WEIGHT_UNIT_SYMBOLS) {
50
+ return WEIGHT_UNIT_SYMBOLS[unit];
51
+ }
52
+ if (unit && unit in WEIGHT_UNIT_NAMES) {
53
+ return unit;
54
+ }
55
+ return unit || STANDARD_WEIGHT_UNIT_SYMBOL;
43
56
  };
44
- const getWeightUnitGrams = (unit) => unit ? WEIGHT_UNIT_GRAMS[unit] : WEIGHT_UNIT_GRAMS[STANDARD_WEIGHT_UNIT];
57
+ export var getWeightUnitGrams = function getWeightUnitGrams(unit) {
58
+ return unit ? WEIGHT_UNIT_GRAMS[unit] : WEIGHT_UNIT_GRAMS[STANDARD_WEIGHT_UNIT];
59
+ };
60
+
45
61
  /**
46
- * 将“每 sourceUnitValue 个 sourceUnit 的价格”换算到每 1 个目标单位的倍率。
47
- * 这里只处理目录单价的单位关系;实际秤读数计价仍应使用 NativeScale。
48
- */
49
- const getWeightUnitPriceConversionFactor = (params) => {
50
- var _params$sourceUnitVal;
51
- const sourceGrams = getWeightUnitGrams(params.sourceUnit || "KILOGRAMS");
52
- const targetGrams = getWeightUnitGrams(params.targetUnit || "KILOGRAMS");
53
- const sourceUnitValue = Number((_params$sourceUnitVal = params.sourceUnitValue) !== null && _params$sourceUnitVal !== void 0 ? _params$sourceUnitVal : 1);
54
- if (!sourceGrams || !targetGrams || !Number.isFinite(sourceUnitValue) || sourceUnitValue <= 0) return;
55
- return targetGrams / (sourceGrams * sourceUnitValue);
62
+ * 将“每 sourceUnitValue 个 sourceUnit 的价格”换算到每 1 个目标单位的倍率。
63
+ * 这里只处理目录单价的单位关系;实际秤读数计价仍应使用 NativeScale。
64
+ */
65
+ export var getWeightUnitPriceConversionFactor = function getWeightUnitPriceConversionFactor(params) {
66
+ var _params$sourceUnitVal;
67
+ var sourceGrams = getWeightUnitGrams(params.sourceUnit || STANDARD_WEIGHT_UNIT);
68
+ var targetGrams = getWeightUnitGrams(params.targetUnit || STANDARD_WEIGHT_UNIT);
69
+ var sourceUnitValue = Number((_params$sourceUnitVal = params.sourceUnitValue) !== null && _params$sourceUnitVal !== void 0 ? _params$sourceUnitVal : 1);
70
+ if (!sourceGrams || !targetGrams || !Number.isFinite(sourceUnitValue) || sourceUnitValue <= 0) {
71
+ return undefined;
72
+ }
73
+ return targetGrams / (sourceGrams * sourceUnitValue);
56
74
  };
57
- const isSupportedWeightUnit = (unit) => typeof unit === "string" && unit in WEIGHT_UNIT_GRAMS;
58
- const formatWeightValue = (value, precision = 3) => {
59
- const parsedValue = Number(value);
60
- return (Number.isFinite(parsedValue) ? parsedValue : 0).toFixed(precision);
75
+ export var isSupportedWeightUnit = function isSupportedWeightUnit(unit) {
76
+ return typeof unit === 'string' && unit in WEIGHT_UNIT_GRAMS;
61
77
  };
62
- const formatUnitPriceValue = (value, precision = 2) => {
63
- const parsedValue = Number(value);
64
- return (Number.isFinite(parsedValue) ? parsedValue : 0).toFixed(precision);
78
+ export var formatWeightValue = function formatWeightValue(value) {
79
+ var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : WEIGHT_PRECISION;
80
+ var parsedValue = Number(value);
81
+ return (Number.isFinite(parsedValue) ? parsedValue : 0).toFixed(precision);
65
82
  };
66
- const INVALID_SCALE_READING_STATUSES = ["overload", "negative"];
67
- const isInvalidScaleReadingStatus = (status) => INVALID_SCALE_READING_STATUSES.includes(status);
68
- const getUsableScaleWeight = (status, weight) => isInvalidScaleReadingStatus(status) ? 0 : weight;
69
- const hasDisplayWeight = (value) => Boolean(value && (Number(value.weight) > 0 || Number(value.net_weight) > 0));
70
- const resolveProductSkuSnapshot = (value) => {
71
- const snapshot = value;
72
- return Number(snapshot === null || snapshot === void 0 ? void 0 : snapshot.open_sold_weight) === 1 && hasDisplayWeight(snapshot) ? snapshot : void 0;
83
+ export var formatUnitPriceValue = function formatUnitPriceValue(value) {
84
+ var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : UNIT_PRICE_PRECISION;
85
+ var parsedValue = Number(value);
86
+ return (Number.isFinite(parsedValue) ? parsedValue : 0).toFixed(precision);
73
87
  };
74
- /**
75
- * 解析订单行称重快照:product_sku 是权威源,_extend.weighing 仅用于旧流程兼容。
76
- */
77
- const resolveWeighingSnapshot = (dataSource) => {
78
- var _dataSource$_origin, _dataSource$metadata, _dataSource$_origin2, _dataSource$_extend, _dataSource$metadata2;
79
- if (!dataSource) return void 0;
80
- const productSkuCandidates = [
81
- dataSource.product_sku,
82
- (_dataSource$_origin = dataSource._origin) === null || _dataSource$_origin === void 0 ? void 0 : _dataSource$_origin.product_sku,
83
- (_dataSource$metadata = dataSource.metadata) === null || _dataSource$metadata === void 0 || (_dataSource$metadata = _dataSource$metadata.origin) === null || _dataSource$metadata === void 0 ? void 0 : _dataSource$metadata.product_sku
84
- ];
85
- for (const candidate of productSkuCandidates) {
86
- const snapshot = resolveProductSkuSnapshot(candidate);
87
- if (snapshot) return snapshot;
88
- }
89
- return [
90
- (_dataSource$_origin2 = dataSource._origin) === null || _dataSource$_origin2 === void 0 || (_dataSource$_origin2 = _dataSource$_origin2._extend) === null || _dataSource$_origin2 === void 0 ? void 0 : _dataSource$_origin2.weighing,
91
- (_dataSource$_extend = dataSource._extend) === null || _dataSource$_extend === void 0 ? void 0 : _dataSource$_extend.weighing,
92
- (_dataSource$metadata2 = dataSource.metadata) === null || _dataSource$metadata2 === void 0 || (_dataSource$metadata2 = _dataSource$metadata2.origin) === null || _dataSource$metadata2 === void 0 || (_dataSource$metadata2 = _dataSource$metadata2._extend) === null || _dataSource$metadata2 === void 0 ? void 0 : _dataSource$metadata2.weighing
93
- ].find((candidate) => hasDisplayWeight(candidate));
88
+ export var INVALID_SCALE_READING_STATUSES = ['overload', 'negative'];
89
+ export var isInvalidScaleReadingStatus = function isInvalidScaleReadingStatus(status) {
90
+ return INVALID_SCALE_READING_STATUSES.includes(status);
91
+ };
92
+ export var getUsableScaleWeight = function getUsableScaleWeight(status, weight) {
93
+ return isInvalidScaleReadingStatus(status) ? 0 : weight;
94
94
  };
95
- //#endregion
96
- export { INVALID_SCALE_READING_STATUSES, MAX_RETAIL_SCALE_WEIGHT_KG, STANDARD_WEIGHT_UNIT, STANDARD_WEIGHT_UNIT_SYMBOL, UNIT_PRICE_PRECISION, WEIGHT_PRECISION, WEIGHT_UNIT_GRAMS, WEIGHT_UNIT_NAMES, WEIGHT_UNIT_SYMBOLS, formatUnitPriceValue, formatWeightValue, getUsableScaleWeight, getWeightUnitGrams, getWeightUnitPriceConversionFactor, getWeightUnitSymbol, isInvalidScaleReadingStatus, isSupportedWeightUnit, normalizeWeightUnit, resolveWeighingSnapshot };
95
+ var hasDisplayWeight = function hasDisplayWeight(value) {
96
+ return Boolean(value && (Number(value.weight) > 0 || Number(value.net_weight) > 0));
97
+ };
98
+ var resolveProductSkuSnapshot = function resolveProductSkuSnapshot(value) {
99
+ var snapshot = value;
100
+ return Number(snapshot === null || snapshot === void 0 ? void 0 : snapshot.open_sold_weight) === 1 && hasDisplayWeight(snapshot) ? snapshot : undefined;
101
+ };
102
+
103
+ /**
104
+ * 解析订单行称重快照:product_sku 是权威源,_extend.weighing 仅用于旧流程兼容。
105
+ */
106
+ export var resolveWeighingSnapshot = function resolveWeighingSnapshot(dataSource) {
107
+ var _dataSource$_origin, _dataSource$metadata, _dataSource$_origin2, _dataSource$_extend, _dataSource$metadata2;
108
+ if (!dataSource) return undefined;
109
+ var productSkuCandidates = [dataSource.product_sku, (_dataSource$_origin = dataSource._origin) === null || _dataSource$_origin === void 0 ? void 0 : _dataSource$_origin.product_sku, (_dataSource$metadata = dataSource.metadata) === null || _dataSource$metadata === void 0 || (_dataSource$metadata = _dataSource$metadata.origin) === null || _dataSource$metadata === void 0 ? void 0 : _dataSource$metadata.product_sku];
110
+ for (var _i = 0, _productSkuCandidates = productSkuCandidates; _i < _productSkuCandidates.length; _i++) {
111
+ var candidate = _productSkuCandidates[_i];
112
+ var snapshot = resolveProductSkuSnapshot(candidate);
113
+ if (snapshot) return snapshot;
114
+ }
115
+ var legacyCandidates = [(_dataSource$_origin2 = dataSource._origin) === null || _dataSource$_origin2 === void 0 || (_dataSource$_origin2 = _dataSource$_origin2._extend) === null || _dataSource$_origin2 === void 0 ? void 0 : _dataSource$_origin2.weighing, (_dataSource$_extend = dataSource._extend) === null || _dataSource$_extend === void 0 ? void 0 : _dataSource$_extend.weighing, (_dataSource$metadata2 = dataSource.metadata) === null || _dataSource$metadata2 === void 0 || (_dataSource$metadata2 = _dataSource$metadata2.origin) === null || _dataSource$metadata2 === void 0 || (_dataSource$metadata2 = _dataSource$metadata2._extend) === null || _dataSource$metadata2 === void 0 ? void 0 : _dataSource$metadata2.weighing];
116
+ return legacyCandidates.find(function (candidate) {
117
+ return hasDisplayWeight(candidate);
118
+ });
119
+ };
@@ -1,10 +1,9 @@
1
- //#region src/arrayUtils.d.ts
2
- declare const uniqueByKey: <T>(arr: T[], key: keyof T) => T[];
1
+ export declare const uniqueByKey: <T>(arr: T[], key: keyof T) => T[];
3
2
  /**
4
3
  * 分组键的基础类型
5
4
  * @description 分组条件计算后得到的标量值
6
5
  */
7
- type GroupKey = string | number | boolean;
6
+ export declare type GroupKey = string | number | boolean;
8
7
  /**
9
8
  * 分组规则
10
9
  * @template T - 数据项类型
@@ -13,34 +12,34 @@ type GroupKey = string | number | boolean;
13
12
  * - `string[]`:多字段组合,按顺序拼接为组合键
14
13
  * - `(item, index) => GroupKey | GroupKey[]`:完全自定义分组键
15
14
  */
16
- type GroupBySpec<T> = string | string[] | ((item: T, index: number) => GroupKey | GroupKey[]);
15
+ export declare type GroupBySpec<T> = string | string[] | ((item: T, index: number) => GroupKey | GroupKey[]);
17
16
  /**
18
17
  * 单个分组的结果模型
19
18
  * @template T - 数据项类型
20
19
  */
21
- interface GroupResult<T> {
22
- /** 分组唯一标识(默认由各分组键以 `|` 拼接) */
23
- groupId: string;
24
- /** 分组键值数组,与 `GroupBySpec` 顺序对应 */
25
- keys: GroupKey[];
26
- /** 字段名 -> 键值映射,便于头部直接按字段名取值(自定义函数分组时为空对象) */
27
- keyMap: Record<string, GroupKey>;
28
- /** 该分组内的原始数据项(保持原数组中的相对顺序) */
29
- items: T[];
30
- /** 分组的最终顺序下标 */
31
- index: number;
20
+ export interface GroupResult<T> {
21
+ /** 分组唯一标识(默认由各分组键以 `|` 拼接) */
22
+ groupId: string;
23
+ /** 分组键值数组,与 `GroupBySpec` 顺序对应 */
24
+ keys: GroupKey[];
25
+ /** 字段名 -> 键值映射,便于头部直接按字段名取值(自定义函数分组时为空对象) */
26
+ keyMap: Record<string, GroupKey>;
27
+ /** 该分组内的原始数据项(保持原数组中的相对顺序) */
28
+ items: T[];
29
+ /** 分组的最终顺序下标 */
30
+ index: number;
32
31
  }
33
32
  /**
34
33
  * 分组可选项
35
34
  * @template T - 数据项类型
36
35
  */
37
- interface GroupByOptions<T> {
38
- /** 自定义分组 id 生成(默认 `keys.join('|')`) */
39
- getGroupId?: (keys: GroupKey[]) => string;
40
- /** 分组之间的排序;不传则保持各分组首次出现的顺序 */
41
- sortGroups?: (a: GroupResult<T>, b: GroupResult<T>) => number;
42
- /** 分组内数据项的排序;不传则保持原始相对顺序 */
43
- sortItems?: (a: T, b: T) => number;
36
+ export interface GroupByOptions<T> {
37
+ /** 自定义分组 id 生成(默认 `keys.join('|')`) */
38
+ getGroupId?: (keys: GroupKey[]) => string;
39
+ /** 分组之间的排序;不传则保持各分组首次出现的顺序 */
40
+ sortGroups?: (a: GroupResult<T>, b: GroupResult<T>) => number;
41
+ /** 分组内数据项的排序;不传则保持原始相对顺序 */
42
+ sortItems?: (a: T, b: T) => number;
44
43
  }
45
44
  /**
46
45
  * 按点路径安全取值
@@ -51,7 +50,7 @@ interface GroupByOptions<T> {
51
50
  * getByPath({ a: { b: 1 } }, 'a.b') // 1
52
51
  * getByPath({ id: 9 }, 'id') // 9
53
52
  */
54
- declare const getByPath: (obj: unknown, path: string) => unknown;
53
+ export declare const getByPath: (obj: unknown, path: string) => unknown;
55
54
  /**
56
55
  * 将数组按指定条件分组
57
56
  * @template T - 数据项类型
@@ -71,6 +70,4 @@ declare const getByPath: (obj: unknown, path: string) => unknown;
71
70
  * // 自定义键
72
71
  * groupBy(list, (item) => `${item.productId}_${item.time}`);
73
72
  */
74
- declare const groupBy: <T>(dataSource: T[], spec: GroupBySpec<T>, options?: GroupByOptions<T>) => GroupResult<T>[];
75
- //#endregion
76
- export { GroupByOptions, GroupBySpec, GroupKey, GroupResult, getByPath, groupBy, uniqueByKey };
73
+ export declare const groupBy: <T>(dataSource: T[], spec: GroupBySpec<T>, options?: GroupByOptions<T>) => GroupResult<T>[];
package/lib/arrayUtils.js CHANGED
@@ -1,105 +1,115 @@
1
- //#region src/arrayUtils.ts
2
- const uniqueByKey = (arr, key) => {
3
- const seen = /* @__PURE__ */ new Set();
4
- const result = [];
5
- for (const item of arr) {
6
- const keyValue = item[key];
7
- if (!seen.has(keyValue)) {
8
- seen.add(keyValue);
9
- result.push(item);
10
- }
11
- }
12
- return result;
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 });
13
8
  };
14
- /**
15
- * 按点路径安全取值
16
- * @param obj - 取值对象
17
- * @param path - 字段路径,支持 `'a.b.c'`
18
- * @returns 路径对应的值,路径不存在时返回 undefined
19
- * @example
20
- * getByPath({ a: { b: 1 } }, 'a.b') // 1
21
- * getByPath({ id: 9 }, 'id') // 9
22
- */
23
- const getByPath = (obj, path) => {
24
- if (obj === null || obj === void 0) return;
25
- if (path.indexOf(".") === -1) return obj[path];
26
- return path.split(".").reduce((acc, key) => {
27
- if (acc === null || acc === void 0) return;
28
- return acc[key];
29
- }, obj);
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;
30
16
  };
31
- /**
32
- * 将任意值规范化为可用于分组的标量键
33
- * @description null/undefined 统一归一为空字符串,对象/数组等转为字符串,避免分组键不稳定
34
- */
35
- const normalizeGroupKey = (value) => {
36
- if (value === null || value === void 0) return "";
37
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return value;
38
- return String(value);
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/arrayUtils.ts
20
+ var arrayUtils_exports = {};
21
+ __export(arrayUtils_exports, {
22
+ getByPath: () => getByPath,
23
+ groupBy: () => groupBy,
24
+ uniqueByKey: () => uniqueByKey
25
+ });
26
+ module.exports = __toCommonJS(arrayUtils_exports);
27
+ var uniqueByKey = (arr, key) => {
28
+ const seen = /* @__PURE__ */ new Set();
29
+ const result = [];
30
+ for (const item of arr) {
31
+ const keyValue = item[key];
32
+ if (!seen.has(keyValue)) {
33
+ seen.add(keyValue);
34
+ result.push(item);
35
+ }
36
+ }
37
+ return result;
39
38
  };
40
- /**
41
- * 将数组按指定条件分组
42
- * @template T - 数据项类型
43
- * @param dataSource - 待分组的数据数组
44
- * @param spec - 分组规则,详见 {@link GroupBySpec}
45
- * @param options - 可选项(自定义 groupId、分组/组内排序)
46
- * @returns 分组结果数组,默认保持各分组首次出现的顺序、组内保持原始相对顺序
47
- * @description
48
- * 该函数为纯函数,不修改入参(排序时对内部拷贝操作)。相同分组键的数据会被归入同一分组。
49
- * @example
50
- * // 单字段
51
- * groupBy(list, 'productId');
52
- * @example
53
- * // 商品 + 时间组合
54
- * groupBy(list, ['productId', 'time']);
55
- * @example
56
- * // 自定义键
57
- * groupBy(list, (item) => `${item.productId}_${item.time}`);
58
- */
59
- const groupBy = (dataSource, spec, options = {}) => {
60
- const { getGroupId, sortGroups, sortItems } = options;
61
- const fieldNames = Array.isArray(spec) ? spec : typeof spec === "string" ? [spec] : [];
62
- const groupMap = /* @__PURE__ */ new Map();
63
- const order = [];
64
- (dataSource || []).forEach((item, index) => {
65
- let keys;
66
- if (typeof spec === "function") {
67
- const result = spec(item, index);
68
- keys = (Array.isArray(result) ? result : [result]).map(normalizeGroupKey);
69
- } else if (Array.isArray(spec)) keys = spec.map((field) => normalizeGroupKey(getByPath(item, field)));
70
- else keys = [normalizeGroupKey(getByPath(item, spec))];
71
- const groupId = getGroupId ? getGroupId(keys) : keys.join("|");
72
- let group = groupMap.get(groupId);
73
- if (!group) {
74
- const keyMap = {};
75
- fieldNames.forEach((field, i) => {
76
- keyMap[field] = keys[i];
77
- });
78
- group = {
79
- groupId,
80
- keys,
81
- keyMap,
82
- items: [],
83
- index: order.length
84
- };
85
- groupMap.set(groupId, group);
86
- order.push(groupId);
87
- }
88
- group.items.push(item);
89
- });
90
- let groups = order.map((id) => groupMap.get(id));
91
- if (sortItems) groups.forEach((group) => {
92
- group.items.sort(sortItems);
93
- });
94
- if (sortGroups) {
95
- groups = groups.slice().sort(sortGroups);
96
- groups.forEach((group, i) => {
97
- group.index = i;
98
- });
99
- }
100
- return groups;
39
+ var getByPath = (obj, path) => {
40
+ if (obj === null || obj === void 0) {
41
+ return void 0;
42
+ }
43
+ if (path.indexOf(".") === -1) {
44
+ return obj[path];
45
+ }
46
+ return path.split(".").reduce((acc, key) => {
47
+ if (acc === null || acc === void 0) {
48
+ return void 0;
49
+ }
50
+ return acc[key];
51
+ }, obj);
101
52
  };
102
- //#endregion
103
- exports.getByPath = getByPath;
104
- exports.groupBy = groupBy;
105
- exports.uniqueByKey = uniqueByKey;
53
+ var normalizeGroupKey = (value) => {
54
+ if (value === null || value === void 0) {
55
+ return "";
56
+ }
57
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
58
+ return value;
59
+ }
60
+ return String(value);
61
+ };
62
+ var groupBy = (dataSource, spec, options = {}) => {
63
+ const { getGroupId, sortGroups, sortItems } = options;
64
+ const fieldNames = Array.isArray(spec) ? spec : typeof spec === "string" ? [spec] : [];
65
+ const groupMap = /* @__PURE__ */ new Map();
66
+ const order = [];
67
+ (dataSource || []).forEach((item, index) => {
68
+ let keys;
69
+ if (typeof spec === "function") {
70
+ const result = spec(item, index);
71
+ keys = (Array.isArray(result) ? result : [result]).map(normalizeGroupKey);
72
+ } else if (Array.isArray(spec)) {
73
+ keys = spec.map((field) => normalizeGroupKey(getByPath(item, field)));
74
+ } else {
75
+ keys = [normalizeGroupKey(getByPath(item, spec))];
76
+ }
77
+ const groupId = getGroupId ? getGroupId(keys) : keys.join("|");
78
+ let group = groupMap.get(groupId);
79
+ if (!group) {
80
+ const keyMap = {};
81
+ fieldNames.forEach((field, i) => {
82
+ keyMap[field] = keys[i];
83
+ });
84
+ group = {
85
+ groupId,
86
+ keys,
87
+ keyMap,
88
+ items: [],
89
+ index: order.length
90
+ };
91
+ groupMap.set(groupId, group);
92
+ order.push(groupId);
93
+ }
94
+ group.items.push(item);
95
+ });
96
+ let groups = order.map((id) => groupMap.get(id));
97
+ if (sortItems) {
98
+ groups.forEach((group) => {
99
+ group.items.sort(sortItems);
100
+ });
101
+ }
102
+ if (sortGroups) {
103
+ groups = groups.slice().sort(sortGroups);
104
+ groups.forEach((group, i) => {
105
+ group.index = i;
106
+ });
107
+ }
108
+ return groups;
109
+ };
110
+ // Annotate the CommonJS export names for ESM import in node:
111
+ 0 && (module.exports = {
112
+ getByPath,
113
+ groupBy,
114
+ uniqueByKey
115
+ });
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 公共工具和类型导出
3
+ */
4
+ export type { DisplayState, ValidationResult } from './types';
5
+ export { getDisplayState } from './stateHelper';