@ptx-showcase/utils 0.5.0 → 0.6.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/README.md +27 -0
- package/dist/chunks/truncate-decimals-BFMoEzul.es +7 -0
- package/dist/chunks/truncate-decimals-CArL-IeO.cjs +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/truncate-decimals/constants/truncate-decimals.constant.d.ts +1 -0
- package/dist/truncate-decimals/index.cjs +1 -0
- package/dist/truncate-decimals/index.d.ts +32 -0
- package/dist/truncate-decimals/index.js +2 -0
- package/package.json +13 -8
package/README.md
CHANGED
|
@@ -208,6 +208,32 @@ import { normalizeDecimalInput, normalizePhoneNumberInput } from '@ptx-showcase/
|
|
|
208
208
|
|
|
209
209
|
---
|
|
210
210
|
|
|
211
|
+
### truncateDecimals
|
|
212
|
+
|
|
213
|
+
Formats a number (or numeric string) to a fixed number of decimal places by cutting the extra digits off, **without rounding**. Accepts a string too, so values more precise than a JS `number` can hold (e.g. amounts from an API) can be truncated without a lossy float conversion first. Returns `'N/A'` for anything that isn't a finite number or a well-formed numeric string.
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
// from root
|
|
217
|
+
import { truncateDecimals } from '@ptx-showcase/utils'
|
|
218
|
+
|
|
219
|
+
// from subpath
|
|
220
|
+
import { truncateDecimals } from '@ptx-showcase/utils/truncate-decimals'
|
|
221
|
+
|
|
222
|
+
truncateDecimals(4.21312312) // → '4.21'
|
|
223
|
+
truncateDecimals(4.995) // → '4.99' (not rounded to 5.00)
|
|
224
|
+
truncateDecimals(4.2) // → '4.2' (fewer decimals than requested — left untouched)
|
|
225
|
+
truncateDecimals(4.219, 0) // → '4'
|
|
226
|
+
|
|
227
|
+
// numeric string input — no float precision loss
|
|
228
|
+
truncateDecimals('4.219999999999999999999999', 2) // → '4.21'
|
|
229
|
+
|
|
230
|
+
// invalid input
|
|
231
|
+
truncateDecimals('not a number') // → 'N/A'
|
|
232
|
+
truncateDecimals(undefined) // → 'N/A'
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
---
|
|
236
|
+
|
|
211
237
|
## Available modules
|
|
212
238
|
|
|
213
239
|
| Subpath | Exports |
|
|
@@ -220,6 +246,7 @@ import { normalizeDecimalInput, normalizePhoneNumberInput } from '@ptx-showcase/
|
|
|
220
246
|
| `@ptx-showcase/utils/download-file` | `downloadBlob` |
|
|
221
247
|
| `@ptx-showcase/utils/handle-api-error` | `handleApiError`, `initHandleApiError` |
|
|
222
248
|
| `@ptx-showcase/utils/normalize-input` | `normalizeDecimalInput`, `normalizePhoneNumberInput` |
|
|
249
|
+
| `@ptx-showcase/utils/truncate-decimals` | `truncateDecimals` |
|
|
223
250
|
| `@ptx-showcase/utils/types` | `TableColumnConfig`, `PaginatedList` |
|
|
224
251
|
|
|
225
252
|
---
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region src/truncate-decimals/constants/truncate-decimals.constant.ts
|
|
2
|
+
var e = /^-?\d+(\.\d+)?$/, t = (t, r = 2) => typeof t == "number" ? Number.isFinite(t) ? n(String(t), r) : "N/A" : typeof t == "string" && e.test(t.trim()) ? n(t.trim(), r) : "N/A", n = (e, t) => {
|
|
3
|
+
let [n, r] = e.split(".");
|
|
4
|
+
return r === void 0 ? e : t > 0 ? `${n}.${r.slice(0, t)}` : n;
|
|
5
|
+
};
|
|
6
|
+
//#endregion
|
|
7
|
+
export { t };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e=/^-?\d+(\.\d+)?$/,t=(t,r=2)=>typeof t==`number`?Number.isFinite(t)?n(String(t),r):`N/A`:typeof t==`string`&&e.test(t.trim())?n(t.trim(),r):`N/A`,n=(e,t)=>{let[n,r]=e.split(`.`);return r===void 0?e:t>0?`${n}.${r.slice(0,t)}`:n};Object.defineProperty(exports,"t",{enumerable:!0,get:function(){return t}});
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./build-query-params/index.cjs"),t=require("./clean-object/index.cjs"),n=require("./cookies/index.cjs"),r=require("./build-sort-param/index.cjs"),i=require("./chunks/currency-symbol-gb63Q_9Q.cjs"),a=require("./download-file/index.cjs"),o=require("./chunks/handle-api-error-D714W9rp.cjs"),s=require("./normalize-input/index.cjs");exports.buildQueryParams=e.buildQueryParams,exports.buildSortParam=r.buildSortParam,exports.cleanObject=t.cleanObject,exports.defaultCookieOptions=n.defaultCookieOptions,exports.deleteCookie=n.deleteCookie,exports.downloadBlob=a.downloadBlob,exports.getCookie=n.getCookie,exports.getCurrencySymbol=i.t,exports.handleApiError=o.t,exports.initHandleApiError=o.n,exports.normalizeDecimalInput=s.normalizeDecimalInput,exports.normalizePhoneNumberInput=s.normalizePhoneNumberInput,exports.setCookie=n.setCookie;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./build-query-params/index.cjs"),t=require("./clean-object/index.cjs"),n=require("./cookies/index.cjs"),r=require("./build-sort-param/index.cjs"),i=require("./chunks/currency-symbol-gb63Q_9Q.cjs"),a=require("./download-file/index.cjs"),o=require("./chunks/handle-api-error-D714W9rp.cjs"),s=require("./normalize-input/index.cjs"),c=require("./chunks/truncate-decimals-CArL-IeO.cjs");exports.buildQueryParams=e.buildQueryParams,exports.buildSortParam=r.buildSortParam,exports.cleanObject=t.cleanObject,exports.defaultCookieOptions=n.defaultCookieOptions,exports.deleteCookie=n.deleteCookie,exports.downloadBlob=a.downloadBlob,exports.getCookie=n.getCookie,exports.getCurrencySymbol=i.t,exports.handleApiError=o.t,exports.initHandleApiError=o.n,exports.normalizeDecimalInput=s.normalizeDecimalInput,exports.normalizePhoneNumberInput=s.normalizePhoneNumberInput,exports.setCookie=n.setCookie,exports.truncateDecimals=c.t;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -6,4 +6,5 @@ import { t as s } from "./chunks/currency-symbol-CBowgXv-.es";
|
|
|
6
6
|
import { downloadBlob as c } from "./download-file/index.js";
|
|
7
7
|
import { n as l, t as u } from "./chunks/handle-api-error-CMd_LxPS.es";
|
|
8
8
|
import { normalizeDecimalInput as d, normalizePhoneNumberInput as f } from "./normalize-input/index.js";
|
|
9
|
-
|
|
9
|
+
import { t as p } from "./chunks/truncate-decimals-BFMoEzul.es";
|
|
10
|
+
export { e as buildQueryParams, o as buildSortParam, t as cleanObject, n as defaultCookieOptions, r as deleteCookie, c as downloadBlob, i as getCookie, s as getCurrencySymbol, u as handleApiError, l as initHandleApiError, d as normalizeDecimalInput, f as normalizePhoneNumberInput, a as setCookie, p as truncateDecimals };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const NUMERIC_STRING_PATTERN: RegExp;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../chunks/truncate-decimals-CArL-IeO.cjs");exports.truncateDecimals=e.t;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a number (or numeric string) to a fixed number of decimal places
|
|
3
|
+
* by cutting the extra digits off, without rounding.
|
|
4
|
+
*
|
|
5
|
+
* Unlike `toFixed`, `4.21312312` truncated to 2 decimals stays `4.21`
|
|
6
|
+
* instead of being rounded to `4.21`/`4.22` depending on the digits that follow.
|
|
7
|
+
*
|
|
8
|
+
* Accepts a string as well as a number so that values with more precision
|
|
9
|
+
* than a JS `number` can hold safely (e.g. amounts coming from an API as
|
|
10
|
+
* strings) can be truncated without going through a lossy float conversion first.
|
|
11
|
+
*
|
|
12
|
+
* Numbers large/small enough to be stringified in exponential notation
|
|
13
|
+
* (e.g. `1e+21`, `1e-7`) are returned as-is, since there's no decimal part to cut.
|
|
14
|
+
*
|
|
15
|
+
* @param value - The number (or numeric string) to truncate
|
|
16
|
+
* @param decimals - Number of digits to keep after the decimal point.
|
|
17
|
+
* Pass `0` to drop the decimal part entirely. Defaults to `2`.
|
|
18
|
+
* @returns The truncated value as a string; `'N/A'` if `value` isn't a finite
|
|
19
|
+
* number or a well-formed numeric string (e.g. `undefined`, `null`, `NaN`,
|
|
20
|
+
* `{}`, `[]`, `'not a number'`)
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* truncateDecimals(4.21312312) // => '4.21'
|
|
25
|
+
* truncateDecimals(4.2) // => '4.2' (fewer decimals than requested — left untouched)
|
|
26
|
+
* truncateDecimals(4.219, 0) // => '4'
|
|
27
|
+
* truncateDecimals('4.219999999999999999999999', 2) // => '4.21' (string input, no float precision loss)
|
|
28
|
+
* truncateDecimals('not a number') // => 'N/A'
|
|
29
|
+
* truncateDecimals(undefined as unknown as number) // => 'N/A'
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare const truncateDecimals: (value: number | string, decimals?: number) => string;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@ptx-showcase/utils",
|
|
3
3
|
"author": "ptx-showcase",
|
|
4
4
|
"description": "Shared utilities for PTX Showcase projects",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.6.0",
|
|
6
6
|
"private": false,
|
|
7
7
|
"type": "module",
|
|
8
8
|
"license": "MIT",
|
|
@@ -71,6 +71,11 @@
|
|
|
71
71
|
"require": "./dist/normalize-input/index.cjs",
|
|
72
72
|
"types": "./dist/normalize-input/index.d.ts"
|
|
73
73
|
},
|
|
74
|
+
"./truncate-decimals": {
|
|
75
|
+
"import": "./dist/truncate-decimals/index.js",
|
|
76
|
+
"require": "./dist/truncate-decimals/index.cjs",
|
|
77
|
+
"types": "./dist/truncate-decimals/index.d.ts"
|
|
78
|
+
},
|
|
74
79
|
"./types": {
|
|
75
80
|
"import": "./dist/types/index.js",
|
|
76
81
|
"require": "./dist/types/index.cjs",
|
|
@@ -92,20 +97,20 @@
|
|
|
92
97
|
"access": "public"
|
|
93
98
|
},
|
|
94
99
|
"devDependencies": {
|
|
95
|
-
"@types/node": "^26.
|
|
96
|
-
"bumpp": "^12.2.
|
|
97
|
-
"oxfmt": "^0.
|
|
98
|
-
"oxlint": "^1.
|
|
100
|
+
"@types/node": "^26.4.0",
|
|
101
|
+
"bumpp": "^12.2.2",
|
|
102
|
+
"oxfmt": "^0.65.0",
|
|
103
|
+
"oxlint": "^1.80.0",
|
|
99
104
|
"typescript": "~6.0.3",
|
|
100
|
-
"vite": "^8.2.
|
|
105
|
+
"vite": "^8.2.2",
|
|
101
106
|
"vite-plugin-dts": "^5.0.3",
|
|
102
|
-
"vitest": "^4.1.
|
|
107
|
+
"vitest": "^4.1.11"
|
|
103
108
|
},
|
|
104
109
|
"dependencies": {
|
|
105
110
|
"@types/file-saver": "^2.0.7",
|
|
106
111
|
"@types/js-cookie": "^3.0.6",
|
|
107
112
|
"file-saver": "^2.0.5",
|
|
108
113
|
"js-cookie": "^3.0.8",
|
|
109
|
-
"ky": "^2.0
|
|
114
|
+
"ky": "^2.1.0"
|
|
110
115
|
}
|
|
111
116
|
}
|