intl-formatter 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.
@@ -0,0 +1,58 @@
1
+ const toMockDateStr = (v, fallback = "—") => {
2
+ if (v == null)
3
+ return fallback;
4
+ try {
5
+ if (typeof v === "boolean" || Array.isArray(v))
6
+ return fallback;
7
+ const isUnixTimestamp = typeof v === "string" && /^-?\d+$/.test(v) && v.length >= 12;
8
+ const parsedValue = isUnixTimestamp ? Number(v) : v;
9
+ const d = new Date(parsedValue);
10
+ return Number.isNaN(d.getTime()) ? fallback : d.toISOString();
11
+ }
12
+ catch {
13
+ return fallback;
14
+ }
15
+ };
16
+ export const mockFormatter = Object.freeze({
17
+ number: (v, options) => {
18
+ if (v == null || v === "" || typeof v === "boolean" || Array.isArray(v)) {
19
+ return options?.fallback ?? "—";
20
+ }
21
+ return String(v);
22
+ },
23
+ currency: (v, options) => {
24
+ if (v == null || v === "" || typeof v === "boolean" || Array.isArray(v)) {
25
+ return options?.fallback ?? "—";
26
+ }
27
+ return `$${v}`;
28
+ },
29
+ percentage: (v, options) => {
30
+ if (v == null || v === "" || typeof v === "boolean" || Array.isArray(v)) {
31
+ return options?.fallback ?? "—";
32
+ }
33
+ return `${v}%`;
34
+ },
35
+ duration: (v, options) => {
36
+ if (v == null || v === "" || typeof v === "boolean" || Array.isArray(v)) {
37
+ return options?.fallback ?? "—";
38
+ }
39
+ return `${v}s`;
40
+ },
41
+ date: (v, options) => {
42
+ const s = toMockDateStr(v, options?.fallback ?? "—");
43
+ return s === (options?.fallback ?? "—") ? s : (s.split("T")[0] ?? "—");
44
+ },
45
+ dateTime: (v, options) => {
46
+ return toMockDateStr(v, options?.fallback ?? "—");
47
+ },
48
+ relativeTime: (v, optionsOrNow) => {
49
+ let callFallback = "—";
50
+ if (optionsOrNow && typeof optionsOrNow === "object") {
51
+ callFallback = optionsOrNow.fallback ?? "—";
52
+ }
53
+ if (v == null || v === "") {
54
+ return callFallback;
55
+ }
56
+ return "just now";
57
+ },
58
+ });
package/package.json ADDED
@@ -0,0 +1,115 @@
1
+ {
2
+ "name": "intl-formatter",
3
+ "version": "1.0.0",
4
+ "description": "Isomorphic universal Intl formatter for numbers, currency, dates, relative time, percentage and duration. Zero hydration mismatches. Works identically on server and client — Node.js, React, Next.js App Router, RSC, Express, Bun, Deno, and edge runtimes. Built-in LRU caching, zero dependencies, under 3KB gzipped, TypeScript first.",
5
+ "author": "gauravgorade",
6
+ "license": "MIT",
7
+ "engines": {
8
+ "node": ">=18"
9
+ },
10
+ "keywords": [
11
+ "intl",
12
+ "intl-formatter",
13
+ "formatter",
14
+ "format",
15
+ "formatting",
16
+ "isomorphic",
17
+ "universal",
18
+ "number",
19
+ "number-format",
20
+ "number-formatter",
21
+ "currency",
22
+ "currency-format",
23
+ "currency-formatter",
24
+ "date",
25
+ "date-format",
26
+ "date-formatter",
27
+ "relative-time",
28
+ "relative-time-format",
29
+ "percentage",
30
+ "percentage-format",
31
+ "duration",
32
+ "duration-format",
33
+ "locale",
34
+ "locale-format",
35
+ "i18n",
36
+ "l10n",
37
+ "internationalization",
38
+ "localization",
39
+ "Intl.NumberFormat",
40
+ "Intl.DateTimeFormat",
41
+ "Intl.RelativeTimeFormat",
42
+ "Intl.PluralRules",
43
+ "intl-api",
44
+ "react",
45
+ "react-formatter",
46
+ "react-intl",
47
+ "react-i18n",
48
+ "react-format",
49
+ "nextjs",
50
+ "next-formatter",
51
+ "next-intl",
52
+ "next-i18n",
53
+ "app-router",
54
+ "server-components",
55
+ "rsc",
56
+ "server-side-rendering",
57
+ "ssr",
58
+ "nodejs",
59
+ "node-formatter",
60
+ "express",
61
+ "edge",
62
+ "edge-runtime",
63
+ "bun",
64
+ "deno",
65
+ "typescript",
66
+ "javascript",
67
+ "format-number",
68
+ "format-currency",
69
+ "format-date",
70
+ "format-duration",
71
+ "format-percentage"
72
+ ],
73
+ "repository": {
74
+ "type": "git",
75
+ "url": "https://github.com/gauravgorade/intl-formatter.git"
76
+ },
77
+ "bugs": {
78
+ "url": "https://github.com/gauravgorade/intl-formatter/issues"
79
+ },
80
+ "homepage": "https://gauravgorade.github.io/intl-formatter/",
81
+ "sideEffects": false,
82
+ "type": "module",
83
+ "main": "./dist/cjs/index.js",
84
+ "module": "./dist/esm/index.js",
85
+ "exports": {
86
+ ".": {
87
+ "types": "./dist/esm/index.d.ts",
88
+ "import": "./dist/esm/index.js",
89
+ "require": "./dist/cjs/index.js"
90
+ },
91
+ "./testing": {
92
+ "types": "./dist/esm/testing.d.ts",
93
+ "import": "./dist/esm/testing.js",
94
+ "require": "./dist/cjs/testing.js"
95
+ }
96
+ },
97
+ "files": [
98
+ "dist",
99
+ "README.md",
100
+ "LICENSE"
101
+ ],
102
+ "scripts": {
103
+ "prebuild": "node -e \"const fs = require('fs'); if (fs.existsSync('dist')) fs.rmSync('dist', { recursive: true, force: true })\"",
104
+ "build": "tsc && tsc -p tsconfig.cjs.json && node -e \"const fs = require('fs'); fs.writeFileSync('dist/cjs/package.json', JSON.stringify({ type: 'commonjs' }))\"",
105
+ "dev": "tsc --watch",
106
+ "test": "vitest run",
107
+ "test:watch": "vitest",
108
+ "prepublishOnly": "npm run test && npm run build"
109
+ },
110
+ "dependencies": {},
111
+ "devDependencies": {
112
+ "typescript": "^5.0.0",
113
+ "vitest": "^3.0.0"
114
+ }
115
+ }