@glister/currency-utils 1.0.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 ADDED
@@ -0,0 +1,152 @@
1
+
2
+ # @glister/currency-utils
3
+
4
+ Lightweight, zero-dependency currency and number formatting utilities built on
5
+ the native `Intl` API.
6
+
7
+ - Auto locale detection
8
+ - Configurable default currency per project
9
+ - Tree-shakable (ESM)
10
+ - TypeScript-first
11
+ - Works in browser, Node.js, and Next.js (SSR-safe)
12
+
13
+ ---
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @glister/currency-utils
19
+ # or
20
+ pnpm add @glister/currency-utils
21
+ # or
22
+ yarn add @glister/currency-utils
23
+ ````
24
+
25
+ ---
26
+
27
+ ## Quick Start
28
+
29
+ ```ts
30
+ import {
31
+ formatCurrency,
32
+ formatCompact,
33
+ formatCompactCurrency,
34
+ formatNumber,
35
+ formatPercent,
36
+ } from '@glister/currency-utils';
37
+
38
+ formatCurrency(1500); // $1,500.00
39
+ formatCompact(1500000); // 1.5M
40
+ formatCompactCurrency(1500); // $1.5K
41
+ formatNumber(1234567); // 1,234,567
42
+ formatPercent(0.075); // 7.5%
43
+ ```
44
+
45
+ ---
46
+
47
+ ## Project-level Configuration
48
+
49
+ You can configure the default currency and locale **once per project**.
50
+
51
+ ```ts
52
+ import { setCurrencyConfig } from '@glister/currency-utils';
53
+
54
+ setCurrencyConfig({
55
+ currency: 'NGN',
56
+ locale: 'en-NG',
57
+ });
58
+ ```
59
+
60
+ After this, **all formatters automatically respect the config**.
61
+
62
+ ```ts
63
+ formatCurrency(1000); // ₦1,000.00
64
+ ```
65
+
66
+ ---
67
+
68
+ ## Auto Locale Detection
69
+
70
+ If no locale is provided, the library automatically detects the user’s locale
71
+ using the `Intl` API.
72
+
73
+ * Browser → user locale
74
+ * Node.js / SSR → safe fallback
75
+
76
+ ---
77
+
78
+ ## API Reference
79
+
80
+ ### `formatCurrency(amount)`
81
+
82
+ Formats a number as currency.
83
+
84
+ ```ts
85
+ formatCurrency(1200);
86
+ // $1,200.00
87
+ ```
88
+
89
+ ---
90
+
91
+ ### `formatCompactCurrency(amount)`
92
+
93
+ Formats a currency value using compact notation.
94
+
95
+ ```ts
96
+ formatCompactCurrency(1200);
97
+ // $1.2K
98
+ ```
99
+
100
+ ---
101
+
102
+ ### `formatCompact(amount)`
103
+
104
+ Formats a number using compact notation.
105
+
106
+ ```ts
107
+ formatCompact(1500000);
108
+ // 1.5M
109
+ ```
110
+
111
+ ---
112
+
113
+ ### `formatNumber(amount)`
114
+
115
+ Formats a number with locale separators.
116
+
117
+ ```ts
118
+ formatNumber(1000000);
119
+ // 1,000,000
120
+ ```
121
+
122
+ ---
123
+
124
+ ### `formatPercent(amount, fractionDigits?)`
125
+
126
+ Formats a number as a percentage.
127
+
128
+ ```ts
129
+ formatPercent(0.25);
130
+ // 25%
131
+ ```
132
+
133
+ ---
134
+
135
+ ## TypeScript Support
136
+
137
+ This package is written in TypeScript and ships with full type definitions.
138
+
139
+ ---
140
+
141
+ ## Zero Dependencies
142
+
143
+ This library has **no external dependencies** and relies entirely on the
144
+ native `Intl` API.
145
+
146
+ ---
147
+
148
+ ## License
149
+
150
+ MIT
151
+
152
+ ````
@@ -0,0 +1,6 @@
1
+ export type CurrencyConfig = {
2
+ locale?: string;
3
+ currency?: string;
4
+ };
5
+ export declare const setCurrencyConfig: (overrides: CurrencyConfig) => void;
6
+ export declare const getCurrencyConfig: () => Required<CurrencyConfig>;
package/dist/config.js ADDED
@@ -0,0 +1,19 @@
1
+ let config = {
2
+ locale: 'en-US',
3
+ currency: 'USD',
4
+ };
5
+ export const setCurrencyConfig = (overrides) => {
6
+ config = { ...config, ...overrides };
7
+ };
8
+ export const getCurrencyConfig = () => {
9
+ if (config.locale) {
10
+ return config;
11
+ }
12
+ const locale = typeof Intl !== 'undefined'
13
+ ? Intl.DateTimeFormat().resolvedOptions().locale
14
+ : 'en-US';
15
+ return {
16
+ locale,
17
+ currency: config.currency,
18
+ };
19
+ };
@@ -0,0 +1,6 @@
1
+ import { Amount } from './utils';
2
+ export declare const formatCurrency: (amount: Amount) => string;
3
+ export declare const formatCompactCurrency: (amount: Amount) => string;
4
+ export declare const formatCompact: (amount: Amount) => string;
5
+ export declare const formatNumber: (amount: Amount) => string;
6
+ export declare const formatPercent: (amount: Amount, fractionDigits?: number) => string;
package/dist/format.js ADDED
@@ -0,0 +1,53 @@
1
+ import { toNumber } from './utils';
2
+ import { getCurrencyConfig } from './config';
3
+ export const formatCurrency = (amount) => {
4
+ const value = toNumber(amount);
5
+ const { locale, currency } = getCurrencyConfig();
6
+ if (value === null)
7
+ return `${currency} 0.00`;
8
+ return value.toLocaleString(locale, {
9
+ style: 'currency',
10
+ currency,
11
+ currencyDisplay: 'narrowSymbol',
12
+ });
13
+ };
14
+ export const formatCompactCurrency = (amount) => {
15
+ const value = toNumber(amount);
16
+ const { locale, currency } = getCurrencyConfig();
17
+ if (value === null)
18
+ return `${currency} 0`;
19
+ return value.toLocaleString(locale, {
20
+ style: 'currency',
21
+ currency,
22
+ currencyDisplay: 'narrowSymbol',
23
+ notation: 'compact',
24
+ maximumFractionDigits: 2,
25
+ });
26
+ };
27
+ export const formatCompact = (amount) => {
28
+ const value = toNumber(amount);
29
+ const { locale } = getCurrencyConfig();
30
+ if (value === null)
31
+ return '0';
32
+ return value.toLocaleString(locale, {
33
+ notation: 'compact',
34
+ maximumFractionDigits: 2,
35
+ });
36
+ };
37
+ export const formatNumber = (amount) => {
38
+ const value = toNumber(amount);
39
+ const { locale } = getCurrencyConfig();
40
+ if (value === null)
41
+ return '0';
42
+ return value.toLocaleString(locale);
43
+ };
44
+ export const formatPercent = (amount, fractionDigits = 2) => {
45
+ const value = toNumber(amount);
46
+ const { locale } = getCurrencyConfig();
47
+ if (value === null)
48
+ return '0%';
49
+ return value.toLocaleString(locale, {
50
+ style: 'percent',
51
+ maximumFractionDigits: fractionDigits,
52
+ });
53
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,25 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { formatCurrency, formatCompact, formatCompactCurrency, formatNumber, formatPercent, } from './format';
3
+ import { setCurrencyConfig } from './config';
4
+ describe('currency utils', () => {
5
+ it('formats currency', () => {
6
+ setCurrencyConfig({ currency: 'USD', locale: 'en-US' });
7
+ expect(formatCurrency(1200)).toBe('$1,200.00');
8
+ });
9
+ it('formats compact numbers', () => {
10
+ expect(formatCompact(1200000)).toContain('1.2');
11
+ });
12
+ it('formats compact currency', () => {
13
+ expect(formatCompactCurrency(1200)).toContain('$');
14
+ });
15
+ it('formats plain numbers', () => {
16
+ expect(formatNumber(1000000)).toBe('1,000,000');
17
+ });
18
+ it('formats percent', () => {
19
+ expect(formatPercent(0.25)).toBe('25%');
20
+ });
21
+ it('respects project currency config', () => {
22
+ setCurrencyConfig({ currency: 'NGN', locale: 'en-NG' });
23
+ expect(formatCurrency(1000)).toContain('₦');
24
+ });
25
+ });
@@ -0,0 +1,2 @@
1
+ export * from './format';
2
+ export * from './config';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './format';
2
+ export * from './config';
@@ -0,0 +1,2 @@
1
+ export type Amount = number | string;
2
+ export declare const toNumber: (amount: Amount) => number | null;
package/dist/utils.js ADDED
@@ -0,0 +1,4 @@
1
+ export const toNumber = (amount) => {
2
+ const value = Number(amount);
3
+ return Number.isFinite(value) ? value : null;
4
+ };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@glister/currency-utils",
3
+ "version": "1.0.0",
4
+ "description": "Lightweight, tree-shakable currency and number formatting utilities",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "keywords": [
15
+ "currency",
16
+ "intl",
17
+ "format",
18
+ "money",
19
+ "typescript",
20
+ "tree-shaking"
21
+ ],
22
+ "license": "MIT",
23
+ "bugs": {
24
+ "url": "https://github.com/kherleefer/currency-utils/issues"
25
+ },
26
+ "homepage": "https://github.com/kherleefer/currency-utils#readme",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/kherleefer/currency-utils.git"
30
+ },
31
+ "devDependencies": {
32
+ "typescript": "^5.9.3",
33
+ "vitest": "^4.0.18"
34
+ },
35
+ "scripts": {
36
+ "build": "tsc",
37
+ "test": "vitest run"
38
+ }
39
+ }