ansuko 1.3.7 → 1.3.8

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.ja.md CHANGED
@@ -229,7 +229,7 @@ extended.toPointGeoJson([139.7, 35.6])
229
229
 
230
230
  ### 型変換と検証
231
231
 
232
- - **`toNumber`** - カンマ・全角対応の数値パース、無効時は `null`
232
+ - **`toNumber`** - カンマ・全角対応の数値パース、`toFixed` で丸め可、無効時は `null`
233
233
  - **`toBool`** - スマートな真偽値変換("yes"/"no"/"true"/"false"/数値)、未検出時の動作を設定可能
234
234
  - **`boolIf`** - フォールバック付き安全な真偽値変換
235
235
  - **`isValidStr`** - 非空文字列検証
package/README.md CHANGED
@@ -107,7 +107,7 @@ const value = _.equalsOr(a, b, defaultValue) // null == undefined
107
107
 
108
108
  ### Type Conversion & Validation
109
109
 
110
- - **`toNumber`** - Parse numbers with comma/full-width support, returns `null` for invalid
110
+ - **`toNumber`** - Parse numbers with comma/full-width support, optional `toFixed` rounding, returns `null` for invalid
111
111
  - **`toBool`** - Smart boolean conversion ("yes"/"no"/"true"/"false"/numbers) with configurable undetected handling
112
112
  - **`boolIf`** - Safe boolean conversion with fallback
113
113
  - **`isValidStr`** - Non-empty string validation
package/README.zh.md CHANGED
@@ -104,7 +104,7 @@ const value = _.equalsOr(a, b, defaultValue) // null == undefined
104
104
 
105
105
  ### 类型转换与验证
106
106
 
107
- - **`toNumber`** - 解析包含逗号、全角字符的数字,无效时返回null
107
+ - **`toNumber`** - 解析包含逗号、全角字符的数字,可选 `toFixed` 四舍五入,无效时返回null
108
108
  - **`toBool`** - 智能布尔值转换(支持 "yes"/"no"/"true"/"false"/数字),可配置未检测到时的行为检测处理
109
109
  - **`boolIf`** - 带有回退机制的安全布尔值转换
110
110
  - **`isValidStr`** - 非空字符串验证
package/dist/index.d.ts CHANGED
@@ -45,14 +45,17 @@ declare const hasOr: <T, E>(value: MaybeFunction<MaybePromise<T | null | undefin
45
45
  declare const isEmpty: (value: unknown) => boolean;
46
46
  /**
47
47
  * Converts a value to number (full-width and comma aware). Returns null when invalid.
48
+ * Optionally rounds using Number.toFixed when `toFixed` is provided.
48
49
  * @param value - Value to convert
50
+ * @param toFixed - Fraction digits to round to (uses Number.toFixed)
49
51
  * @returns number or null
50
52
  * @example toNumber('1,234.5') // 1234.5
53
+ * @example toNumber('1,234.56', 1) // 1234.6
51
54
  * @example toNumber('123') // 123
52
55
  * @example toNumber('abc') // null
53
56
  * @category Core Functions
54
57
  */
55
- declare const toNumber: (value: unknown) => number | null;
58
+ declare const toNumber: (value: unknown, toFixed?: number) => number | null;
56
59
  /**
57
60
  * Converts various inputs to boolean. Numbers: 0 -> false, non-zero -> true.
58
61
  * Strings: 'true'|'t'|'y'|'yes'|'ok' -> true; 'false'|'f'|'n'|'no'|'ng' -> false.
package/dist/index.js CHANGED
@@ -133,14 +133,17 @@ const isEmpty = (value) => {
133
133
  };
134
134
  /**
135
135
  * Converts a value to number (full-width and comma aware). Returns null when invalid.
136
+ * Optionally rounds using Number.toFixed when `toFixed` is provided.
136
137
  * @param value - Value to convert
138
+ * @param toFixed - Fraction digits to round to (uses Number.toFixed)
137
139
  * @returns number or null
138
140
  * @example toNumber('1,234.5') // 1234.5
141
+ * @example toNumber('1,234.56', 1) // 1234.6
139
142
  * @example toNumber('123') // 123
140
143
  * @example toNumber('abc') // null
141
144
  * @category Core Functions
142
145
  */
143
- const toNumber = (value) => {
146
+ const toNumber = (value, toFixed) => {
144
147
  if (_.isNil(value)) {
145
148
  return null;
146
149
  }
@@ -157,7 +160,13 @@ const toNumber = (value) => {
157
160
  else {
158
161
  v = _.toNumber(v);
159
162
  }
160
- return _.isNaN(v) ? null : v;
163
+ if (_.isNaN(v)) {
164
+ return null;
165
+ }
166
+ if (toFixed !== undefined) {
167
+ return parseFloat(v.toFixed(toFixed));
168
+ }
169
+ return v;
161
170
  };
162
171
  /**
163
172
  * Converts various inputs to boolean. Numbers: 0 -> false, non-zero -> true.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ansuko",
3
- "version": "1.3.7",
3
+ "version": "1.3.8",
4
4
  "description": "A modern JavaScript/TypeScript utility library that extends lodash with practical, intuitive behaviors. Fixes lodash quirks, adds Promise support, Japanese text processing, and GeoJSON utilities.",
5
5
  "keywords": [
6
6
  "lodash",