@ptx-showcase/utils 0.4.0-beta.1 → 0.5.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 +52 -1
- package/dist/build-query-params/index.cjs +1 -1
- package/dist/build-query-params/index.js +1 -1
- package/dist/chunks/currency-symbol-CBowgXv-.es +26 -0
- package/dist/chunks/currency-symbol-gb63Q_9Q.cjs +1 -0
- package/dist/cookies/index.cjs +1 -1
- package/dist/currency-symbol/constants/currency-symbol.constant.d.ts +6 -0
- package/dist/currency-symbol/index.cjs +1 -0
- package/dist/currency-symbol/index.d.ts +23 -0
- package/dist/currency-symbol/index.js +2 -0
- package/dist/download-file/index.cjs +1 -1
- package/dist/download-file/index.js +2 -1
- package/dist/handle-api-error/index.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -4
- package/dist/types/index.d.ts +7 -0
- package/package.json +11 -6
- /package/dist/chunks/{handle-api-error-CMd_LxPS.esm → handle-api-error-CMd_LxPS.es} +0 -0
package/README.md
CHANGED
|
@@ -126,6 +126,30 @@ await downloadBlob(blob, { hash: 12345 })
|
|
|
126
126
|
|
|
127
127
|
---
|
|
128
128
|
|
|
129
|
+
### getCurrencySymbol
|
|
130
|
+
|
|
131
|
+
Resolves the symbol for an ISO 4217 currency code via `Intl`. A handful of currencies (`AUD`, `CAD`, `NZD`, `CHF`, etc.) are overridden where the `Intl` narrow symbol would be ambiguous.
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
// from root
|
|
135
|
+
import { getCurrencySymbol } from '@ptx-showcase/utils'
|
|
136
|
+
|
|
137
|
+
// from subpath
|
|
138
|
+
import { getCurrencySymbol } from '@ptx-showcase/utils/currency-symbol'
|
|
139
|
+
|
|
140
|
+
getCurrencySymbol('EUR') // → '€'
|
|
141
|
+
getCurrencySymbol('USD') // → '$'
|
|
142
|
+
getCurrencySymbol('AUD') // → 'A$' (override)
|
|
143
|
+
getCurrencySymbol('CHF') // → '₣' (override)
|
|
144
|
+
getCurrencySymbol('ZZZ') // → 'ZZZ' (well-formed but unknown code)
|
|
145
|
+
getCurrencySymbol('EURO') // → 'N/A' (not a 3-letter code)
|
|
146
|
+
|
|
147
|
+
// custom locale
|
|
148
|
+
getCurrencySymbol('EUR', 'de-DE') // → '€'
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
129
153
|
### handleApiError
|
|
130
154
|
|
|
131
155
|
Parses backend error responses (including `ky`'s `HTTPError`) and reports human-readable messages through whatever notification system your project uses (`vue-sonner`, `react-hot-toast`, `console.error`, etc).
|
|
@@ -159,6 +183,31 @@ try {
|
|
|
159
183
|
|
|
160
184
|
---
|
|
161
185
|
|
|
186
|
+
### normalizeDecimalInput / normalizePhoneNumberInput
|
|
187
|
+
|
|
188
|
+
In-place `input`/`change` event handlers that sanitize a text input's value as the user types.
|
|
189
|
+
|
|
190
|
+
- `normalizeDecimalInput` — keeps only digits and a single dot, converts a comma to a dot, prefixes a leading dot with `0`, and truncates the decimal part to `decimals` digits (default `2`).
|
|
191
|
+
- `normalizePhoneNumberInput` — keeps only digits, truncates to `maxDigits` (default `15`, the E.164 limit), and prefixes the result with `+`.
|
|
192
|
+
|
|
193
|
+
```ts
|
|
194
|
+
// from root
|
|
195
|
+
import { normalizeDecimalInput, normalizePhoneNumberInput } from '@ptx-showcase/utils'
|
|
196
|
+
|
|
197
|
+
// from subpath
|
|
198
|
+
import { normalizeDecimalInput, normalizePhoneNumberInput } from '@ptx-showcase/utils/normalize-input'
|
|
199
|
+
|
|
200
|
+
// Vue
|
|
201
|
+
<input type="text" inputmode="decimal" @input="normalizeDecimalInput" />
|
|
202
|
+
<input type="text" inputmode="tel" @input="normalizePhoneNumberInput" />
|
|
203
|
+
|
|
204
|
+
// React (wrap to access the native event)
|
|
205
|
+
<input type="text" inputMode="decimal" onChange={(e) => normalizeDecimalInput(e.nativeEvent, 4)} />
|
|
206
|
+
<input type="text" inputMode="tel" onChange={(e) => normalizePhoneNumberInput(e.nativeEvent, 11)} />
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
162
211
|
## Available modules
|
|
163
212
|
|
|
164
213
|
| Subpath | Exports |
|
|
@@ -167,9 +216,11 @@ try {
|
|
|
167
216
|
| `@ptx-showcase/utils/build-query-params` | `buildQueryParams` |
|
|
168
217
|
| `@ptx-showcase/utils/build-sort-param` | `buildSortParam` |
|
|
169
218
|
| `@ptx-showcase/utils/cookies` | `setCookie`, `getCookie`, `deleteCookie`, `defaultCookieOptions` |
|
|
219
|
+
| `@ptx-showcase/utils/currency-symbol` | `getCurrencySymbol` |
|
|
170
220
|
| `@ptx-showcase/utils/download-file` | `downloadBlob` |
|
|
171
221
|
| `@ptx-showcase/utils/handle-api-error` | `handleApiError`, `initHandleApiError` |
|
|
172
|
-
| `@ptx-showcase/utils/
|
|
222
|
+
| `@ptx-showcase/utils/normalize-input` | `normalizeDecimalInput`, `normalizePhoneNumberInput` |
|
|
223
|
+
| `@ptx-showcase/utils/types` | `TableColumnConfig`, `PaginatedList` |
|
|
173
224
|
|
|
174
225
|
---
|
|
175
226
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=e=>{let t=new URLSearchParams;Object.entries(e).forEach(([e,n])=>{n
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=e=>{let t=new URLSearchParams;Object.entries(e).forEach(([e,n])=>{n!=null&&n!==``&&(Array.isArray(n)&&n.length===0||t.append(e,String(n)))});let n=t.toString();return n?`?${n}`:``};exports.buildQueryParams=e;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
var e = (e) => {
|
|
3
3
|
let t = new URLSearchParams();
|
|
4
4
|
Object.entries(e).forEach(([e, n]) => {
|
|
5
|
-
n
|
|
5
|
+
n != null && n !== "" && (Array.isArray(n) && n.length === 0 || t.append(e, String(n)));
|
|
6
6
|
});
|
|
7
7
|
let n = t.toString();
|
|
8
8
|
return n ? `?${n}` : "";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region src/currency-symbol/constants/currency-symbol.constant.ts
|
|
2
|
+
var e = {
|
|
3
|
+
CHF: "₣",
|
|
4
|
+
AUD: "A$",
|
|
5
|
+
CAD: "CA$",
|
|
6
|
+
NZD: "NZ$",
|
|
7
|
+
HKD: "HK$",
|
|
8
|
+
MXN: "MX$",
|
|
9
|
+
SGD: "S$"
|
|
10
|
+
}, t = (t, n = "en-US") => {
|
|
11
|
+
if (typeof t != "string" || t == null || t === "" || t === " " || t.length !== 3) return "N/A";
|
|
12
|
+
let r = e[t.toUpperCase()];
|
|
13
|
+
if (r !== void 0) return r;
|
|
14
|
+
try {
|
|
15
|
+
let e = new Intl.NumberFormat(n, {
|
|
16
|
+
style: "currency",
|
|
17
|
+
currency: t,
|
|
18
|
+
currencyDisplay: "narrowSymbol"
|
|
19
|
+
}).formatToParts(0).find((e) => e.type === "currency");
|
|
20
|
+
return e === void 0 ? t : e.value;
|
|
21
|
+
} catch {
|
|
22
|
+
return t;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
//#endregion
|
|
26
|
+
export { t };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e={CHF:`₣`,AUD:`A$`,CAD:`CA$`,NZD:`NZ$`,HKD:`HK$`,MXN:`MX$`,SGD:`S$`},t=(t,n=`en-US`)=>{if(typeof t!=`string`||t==null||t===``||t===` `||t.length!==3)return`N/A`;let r=e[t.toUpperCase()];if(r!==void 0)return r;try{let e=new Intl.NumberFormat(n,{style:`currency`,currency:t,currencyDisplay:`narrowSymbol`}).formatToParts(0).find(e=>e.type===`currency`);return e===void 0?t:e.value}catch{return t}};Object.defineProperty(exports,"t",{enumerable:!0,get:function(){return t}});
|
package/dist/cookies/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,s)=>(s=n==null?{}:e(i(n)),o(r||!n||!n.__esModule||!a.call(n,`default`)?t(s,`default`,{value:n,enumerable:!0}):s,n));let c=require("js-cookie");c=s(c,1);var l={expires:1/24,secure:!0,sameSite:`Strict`},u=(e,t,n)=>{c.default.set(e,t,{...l,...n})},d=e=>c.default.get(e),f=(e,t)=>{c.default.remove(e,t)};exports.defaultCookieOptions=l,exports.deleteCookie=f,exports.getCookie=d,exports.setCookie=u;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Explicit code → symbol overrides, checked before falling back to `Intl`.
|
|
3
|
+
* Use this for symbols `Intl` doesn't resolve the way you want (missing,
|
|
4
|
+
* wrong, or just not the convention your product uses).
|
|
5
|
+
*/
|
|
6
|
+
export declare const CURRENCY_SYMBOL_OVERRIDES: Record<string, string>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../chunks/currency-symbol-gb63Q_9Q.cjs");exports.getCurrencySymbol=e.t;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the symbol for a given ISO 4217 currency code.
|
|
3
|
+
*
|
|
4
|
+
* Codes listed in `CURRENCY_SYMBOL_OVERRIDES` are returned as-is — this
|
|
5
|
+
* covers currencies whose `Intl` narrow symbol is ambiguous (e.g. `AUD`,
|
|
6
|
+
* `CAD`, `NZD` all narrow to a plain `$`) or otherwise not what the product
|
|
7
|
+
* wants. Everything else is resolved via the runtime's `Intl` data, so all
|
|
8
|
+
* other currencies are supported without a hardcoded map.
|
|
9
|
+
*
|
|
10
|
+
* @param code - ISO 4217 currency code (e.g. `"EUR"`)
|
|
11
|
+
* @param locale - Locale used to resolve the symbol. Defaults to `"en-US"`.
|
|
12
|
+
* @returns The currency symbol; `code` itself if it's well-formed but not a
|
|
13
|
+
* recognized currency; `"N/A"` if `code` isn't a 3-letter string
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* getCurrencySymbol('EUR') // => '€'
|
|
18
|
+
* getCurrencySymbol('USD') // => '$'
|
|
19
|
+
* getCurrencySymbol('AUD') // => 'A$' (override)
|
|
20
|
+
* getCurrencySymbol('CHF') // => '₣' (override)
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare const getCurrencySymbol: (code: string, locale?: string) => string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("file-saver");var t=async e=>{let t=new TextEncoder().encode(String(e)),n=await crypto.subtle.digest(`SHA-1`,t);return Array.from(new Uint8Array(n)).map(e=>e.toString(16).padStart(2,`0`)).join(``)},n=async(n,r)=>{if(!(n instanceof Blob))throw Error(`downloadBlob: expected Blob, got ${typeof n}`);
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("file-saver");var t=async e=>{let t=new TextEncoder().encode(String(e)),n=await crypto.subtle.digest(`SHA-1`,t);return Array.from(new Uint8Array(n)).map(e=>e.toString(16).padStart(2,`0`)).join(``)},n=async(n,r)=>{if(!(n instanceof Blob))throw Error(`downloadBlob: expected Blob, got ${typeof n}`);let i=`filename`in r?r.filename:await t(r.hash);(0,e.saveAs)(n,i)};exports.downloadBlob=n;
|
|
@@ -5,7 +5,8 @@ var t = async (e) => {
|
|
|
5
5
|
return Array.from(new Uint8Array(n)).map((e) => e.toString(16).padStart(2, "0")).join("");
|
|
6
6
|
}, n = async (n, r) => {
|
|
7
7
|
if (!(n instanceof Blob)) throw Error(`downloadBlob: expected Blob, got ${typeof n}`);
|
|
8
|
-
|
|
8
|
+
let i = "filename" in r ? r.filename : await t(r.hash);
|
|
9
|
+
e(n, i);
|
|
9
10
|
};
|
|
10
11
|
//#endregion
|
|
11
12
|
export { n as downloadBlob };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as e, t } from "../chunks/handle-api-error-CMd_LxPS.
|
|
1
|
+
import { n as e, t } from "../chunks/handle-api-error-CMd_LxPS.es";
|
|
2
2
|
export { t as handleApiError, e as initHandleApiError };
|
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("./download-file/index.cjs"),
|
|
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;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from './clean-object';
|
|
|
3
3
|
export * from './cookies';
|
|
4
4
|
export type * from './types';
|
|
5
5
|
export * from './build-sort-param';
|
|
6
|
+
export * from './currency-symbol';
|
|
6
7
|
export * from './download-file';
|
|
7
8
|
export * from './handle-api-error';
|
|
8
9
|
export * from './normalize-input';
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,8 @@ import { buildQueryParams as e } from "./build-query-params/index.js";
|
|
|
2
2
|
import { cleanObject as t } from "./clean-object/index.js";
|
|
3
3
|
import { defaultCookieOptions as n, deleteCookie as r, getCookie as i, setCookie as a } from "./cookies/index.js";
|
|
4
4
|
import { buildSortParam as o } from "./build-sort-param/index.js";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
5
|
+
import { t as s } from "./chunks/currency-symbol-CBowgXv-.es";
|
|
6
|
+
import { downloadBlob as c } from "./download-file/index.js";
|
|
7
|
+
import { n as l, t as u } from "./chunks/handle-api-error-CMd_LxPS.es";
|
|
8
|
+
import { normalizeDecimalInput as d, normalizePhoneNumberInput as f } from "./normalize-input/index.js";
|
|
9
|
+
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 };
|
package/dist/types/index.d.ts
CHANGED
|
@@ -4,3 +4,10 @@ export interface TableColumnConfig<T, ExtraKeys extends string = never> {
|
|
|
4
4
|
key: keyof T | ExtraKeys;
|
|
5
5
|
sortable?: boolean;
|
|
6
6
|
}
|
|
7
|
+
export interface PaginatedList<T> {
|
|
8
|
+
items: T[];
|
|
9
|
+
page: number;
|
|
10
|
+
per_page: number;
|
|
11
|
+
total_items: number;
|
|
12
|
+
total_pages: number;
|
|
13
|
+
}
|
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.5.0",
|
|
6
6
|
"private": false,
|
|
7
7
|
"type": "module",
|
|
8
8
|
"license": "MIT",
|
|
@@ -51,6 +51,11 @@
|
|
|
51
51
|
"require": "./dist/clean-object/index.cjs",
|
|
52
52
|
"types": "./dist/clean-object/index.d.ts"
|
|
53
53
|
},
|
|
54
|
+
"./currency-symbol": {
|
|
55
|
+
"import": "./dist/currency-symbol/index.js",
|
|
56
|
+
"require": "./dist/currency-symbol/index.cjs",
|
|
57
|
+
"types": "./dist/currency-symbol/index.d.ts"
|
|
58
|
+
},
|
|
54
59
|
"./download-file": {
|
|
55
60
|
"import": "./dist/download-file/index.js",
|
|
56
61
|
"require": "./dist/download-file/index.cjs",
|
|
@@ -87,12 +92,12 @@
|
|
|
87
92
|
"access": "public"
|
|
88
93
|
},
|
|
89
94
|
"devDependencies": {
|
|
90
|
-
"@types/node": "^26.1.
|
|
91
|
-
"bumpp": "^
|
|
92
|
-
"oxfmt": "^0.
|
|
93
|
-
"oxlint": "^1.
|
|
95
|
+
"@types/node": "^26.1.2",
|
|
96
|
+
"bumpp": "^12.2.0",
|
|
97
|
+
"oxfmt": "^0.62.0",
|
|
98
|
+
"oxlint": "^1.77.0",
|
|
94
99
|
"typescript": "~6.0.3",
|
|
95
|
-
"vite": "^8.1
|
|
100
|
+
"vite": "^8.2.1",
|
|
96
101
|
"vite-plugin-dts": "^5.0.3",
|
|
97
102
|
"vitest": "^4.1.10"
|
|
98
103
|
},
|
|
File without changes
|