@liam-public/text 0.1.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 +3 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/package.json +36 -0
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
type LocalizedText = Readonly<Record<string, string>>;
|
|
2
|
+
declare function isLocalizedText(value: unknown, requiredLanguages: readonly string[]): value is LocalizedText;
|
|
3
|
+
declare function selectText(text: LocalizedText | string | undefined, language: string, fallbackLanguages?: readonly string[]): string;
|
|
4
|
+
declare function formatCurrency(amount: number, currency?: string, locale?: string): string;
|
|
5
|
+
declare function formatNumber(value: number, locale?: string): string;
|
|
6
|
+
declare function formatPercentage(value: number, decimals?: number, locale?: string): string;
|
|
7
|
+
declare function formatDate(date: Date | string, locale?: string, options?: Intl.DateTimeFormatOptions): string;
|
|
8
|
+
declare function formatFileSize(bytes: number): string;
|
|
9
|
+
|
|
10
|
+
export { type LocalizedText, formatCurrency, formatDate, formatFileSize, formatNumber, formatPercentage, isLocalizedText, selectText };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function isLocalizedText(value, requiredLanguages) {
|
|
3
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
4
|
+
const record = value;
|
|
5
|
+
return requiredLanguages.every((language) => typeof record[language] === "string");
|
|
6
|
+
}
|
|
7
|
+
function selectText(text, language, fallbackLanguages = []) {
|
|
8
|
+
if (!text) return "";
|
|
9
|
+
if (typeof text === "string") return text;
|
|
10
|
+
return text[language] || fallbackLanguages.map((fallback) => text[fallback]).find(Boolean) || "";
|
|
11
|
+
}
|
|
12
|
+
function formatCurrency(amount, currency = "USD", locale = "en-US") {
|
|
13
|
+
return new Intl.NumberFormat(locale, { style: "currency", currency }).format(amount);
|
|
14
|
+
}
|
|
15
|
+
function formatNumber(value, locale = "en-US") {
|
|
16
|
+
return new Intl.NumberFormat(locale).format(value);
|
|
17
|
+
}
|
|
18
|
+
function formatPercentage(value, decimals = 2, locale = "en-US") {
|
|
19
|
+
return new Intl.NumberFormat(locale, {
|
|
20
|
+
style: "percent",
|
|
21
|
+
minimumFractionDigits: decimals,
|
|
22
|
+
maximumFractionDigits: decimals
|
|
23
|
+
}).format(value / 100);
|
|
24
|
+
}
|
|
25
|
+
function formatDate(date, locale = "en-US", options = { year: "numeric", month: "short", day: "numeric" }) {
|
|
26
|
+
return new Intl.DateTimeFormat(locale, options).format(typeof date === "string" ? new Date(date) : date);
|
|
27
|
+
}
|
|
28
|
+
function formatFileSize(bytes) {
|
|
29
|
+
if (bytes === 0) return "0 Bytes";
|
|
30
|
+
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
|
|
31
|
+
const index = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), sizes.length - 1);
|
|
32
|
+
return `${(bytes / 1024 ** index).toFixed(2)} ${sizes[index]}`;
|
|
33
|
+
}
|
|
34
|
+
export {
|
|
35
|
+
formatCurrency,
|
|
36
|
+
formatDate,
|
|
37
|
+
formatFileSize,
|
|
38
|
+
formatNumber,
|
|
39
|
+
formatPercentage,
|
|
40
|
+
isLocalizedText,
|
|
41
|
+
selectText
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type LocalizedText = Readonly<Record<string, string>>\n\nexport function isLocalizedText(value: unknown, requiredLanguages: readonly string[]): value is LocalizedText {\n if (!value || typeof value !== 'object' || Array.isArray(value)) return false\n const record = value as Record<string, unknown>\n return requiredLanguages.every((language) => typeof record[language] === 'string')\n}\n\nexport function selectText(\n text: LocalizedText | string | undefined,\n language: string,\n fallbackLanguages: readonly string[] = [],\n): string {\n if (!text) return ''\n if (typeof text === 'string') return text\n return text[language] || fallbackLanguages.map((fallback) => text[fallback]).find(Boolean) || ''\n}\n\nexport function formatCurrency(amount: number, currency = 'USD', locale = 'en-US'): string {\n return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(amount)\n}\n\nexport function formatNumber(value: number, locale = 'en-US'): string {\n return new Intl.NumberFormat(locale).format(value)\n}\n\nexport function formatPercentage(value: number, decimals = 2, locale = 'en-US'): string {\n return new Intl.NumberFormat(locale, {\n style: 'percent',\n minimumFractionDigits: decimals,\n maximumFractionDigits: decimals,\n }).format(value / 100)\n}\n\nexport function formatDate(\n date: Date | string,\n locale = 'en-US',\n options: Intl.DateTimeFormatOptions = { year: 'numeric', month: 'short', day: 'numeric' },\n): string {\n return new Intl.DateTimeFormat(locale, options).format(typeof date === 'string' ? new Date(date) : date)\n}\n\nexport function formatFileSize(bytes: number): string {\n if (bytes === 0) return '0 Bytes'\n const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']\n const index = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), sizes.length - 1)\n return `${(bytes / 1024 ** index).toFixed(2)} ${sizes[index]}`\n}\n"],"mappings":";AAEO,SAAS,gBAAgB,OAAgB,mBAA8D;AAC5G,MAAI,CAAC,SAAS,OAAO,UAAU,YAAY,MAAM,QAAQ,KAAK,EAAG,QAAO;AACxE,QAAM,SAAS;AACf,SAAO,kBAAkB,MAAM,CAAC,aAAa,OAAO,OAAO,QAAQ,MAAM,QAAQ;AACnF;AAEO,SAAS,WACd,MACA,UACA,oBAAuC,CAAC,GAChC;AACR,MAAI,CAAC,KAAM,QAAO;AAClB,MAAI,OAAO,SAAS,SAAU,QAAO;AACrC,SAAO,KAAK,QAAQ,KAAK,kBAAkB,IAAI,CAAC,aAAa,KAAK,QAAQ,CAAC,EAAE,KAAK,OAAO,KAAK;AAChG;AAEO,SAAS,eAAe,QAAgB,WAAW,OAAO,SAAS,SAAiB;AACzF,SAAO,IAAI,KAAK,aAAa,QAAQ,EAAE,OAAO,YAAY,SAAS,CAAC,EAAE,OAAO,MAAM;AACrF;AAEO,SAAS,aAAa,OAAe,SAAS,SAAiB;AACpE,SAAO,IAAI,KAAK,aAAa,MAAM,EAAE,OAAO,KAAK;AACnD;AAEO,SAAS,iBAAiB,OAAe,WAAW,GAAG,SAAS,SAAiB;AACtF,SAAO,IAAI,KAAK,aAAa,QAAQ;AAAA,IACnC,OAAO;AAAA,IACP,uBAAuB;AAAA,IACvB,uBAAuB;AAAA,EACzB,CAAC,EAAE,OAAO,QAAQ,GAAG;AACvB;AAEO,SAAS,WACd,MACA,SAAS,SACT,UAAsC,EAAE,MAAM,WAAW,OAAO,SAAS,KAAK,UAAU,GAChF;AACR,SAAO,IAAI,KAAK,eAAe,QAAQ,OAAO,EAAE,OAAO,OAAO,SAAS,WAAW,IAAI,KAAK,IAAI,IAAI,IAAI;AACzG;AAEO,SAAS,eAAe,OAAuB;AACpD,MAAI,UAAU,EAAG,QAAO;AACxB,QAAM,QAAQ,CAAC,SAAS,MAAM,MAAM,MAAM,IAAI;AAC9C,QAAM,QAAQ,KAAK,IAAI,KAAK,MAAM,KAAK,IAAI,KAAK,IAAI,KAAK,IAAI,IAAI,CAAC,GAAG,MAAM,SAAS,CAAC;AACrF,SAAO,IAAI,QAAQ,QAAQ,OAAO,QAAQ,CAAC,CAAC,IAAI,MAAM,KAAK,CAAC;AAC9D;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@liam-public/text",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Universal text guards and locale-aware formatters.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"liamCompatibility": {
|
|
7
|
+
"runtime": [
|
|
8
|
+
"universal"
|
|
9
|
+
],
|
|
10
|
+
"framework": [
|
|
11
|
+
"agnostic"
|
|
12
|
+
]
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public",
|
|
28
|
+
"registry": "https://registry.npmjs.org/"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsup src/index.ts --format esm --dts --sourcemap",
|
|
32
|
+
"clean": "rm -rf dist",
|
|
33
|
+
"test": "vitest run",
|
|
34
|
+
"typecheck": "tsc --noEmit"
|
|
35
|
+
}
|
|
36
|
+
}
|