@nexim/localizer 1.0.0 → 1.1.1
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/CHANGELOG.md +12 -0
- package/dist/main.cjs +4 -8
- package/dist/main.cjs.map +2 -2
- package/dist/main.d.ts.map +1 -1
- package/dist/main.mjs +4 -8
- package/dist/main.mjs.map +2 -2
- package/package.json +6 -8
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [1.1.1](https://github.com/the-nexim/nanolib/compare/@nexim/localizer@1.1.0...@nexim/localizer@1.1.1) (2025-06-29)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* **localizer:** disable logger to prevent redundant log in console ([5cae168](https://github.com/the-nexim/nanolib/commit/5cae168e692220fd0de02da5f65bc9b87e6f8d14)) by @
|
|
11
|
+
|
|
12
|
+
## [1.1.0](https://github.com/the-nexim/nanolib/compare/@nexim/localizer@1.0.0...@nexim/localizer@1.1.0) (2025-04-27)
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
* **action-state:** new package ([8e8f0af](https://github.com/the-nexim/nanolib/commit/8e8f0af50e663daeb6022f666b6eebd01a88bb88)) by @
|
|
17
|
+
|
|
6
18
|
## 1.0.0 (2025-04-21)
|
|
7
19
|
|
|
8
20
|
### Features
|
package/dist/main.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/* @nexim/localizer v1.
|
|
1
|
+
/* @nexim/localizer v1.1.1 */
|
|
2
2
|
"use strict";
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -24,10 +24,10 @@ __export(main_exports, {
|
|
|
24
24
|
Localizer: () => Localizer
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(main_exports);
|
|
27
|
-
var import_unicode_digits = require("@alwatr/unicode-digits");
|
|
28
|
-
var import_package_tracer = require("@alwatr/package-tracer");
|
|
29
27
|
var import_logger = require("@alwatr/logger");
|
|
30
|
-
|
|
28
|
+
var import_package_tracer = require("@alwatr/package-tracer");
|
|
29
|
+
var import_unicode_digits = require("@alwatr/unicode-digits");
|
|
30
|
+
__dev_mode__: import_package_tracer.packageTracer.add("@nexim/localizer", "1.1.1");
|
|
31
31
|
var _Localizer = class _Localizer {
|
|
32
32
|
/**
|
|
33
33
|
* Creates a new instance of the Localizer class.
|
|
@@ -58,7 +58,6 @@ var _Localizer = class _Localizer {
|
|
|
58
58
|
* ```
|
|
59
59
|
*/
|
|
60
60
|
message(key) {
|
|
61
|
-
this.logger_.logMethodArgs?.("message", key);
|
|
62
61
|
if (!key) return "";
|
|
63
62
|
const msg = this.options__.resource?.[key];
|
|
64
63
|
if (msg === void 0) {
|
|
@@ -103,7 +102,6 @@ var _Localizer = class _Localizer {
|
|
|
103
102
|
* ```
|
|
104
103
|
*/
|
|
105
104
|
replaceNumber(str) {
|
|
106
|
-
this.logger_.logMethodArgs?.("replaceNumber", str);
|
|
107
105
|
return this.unicodeDigits_.translate(str);
|
|
108
106
|
}
|
|
109
107
|
/**
|
|
@@ -120,7 +118,6 @@ var _Localizer = class _Localizer {
|
|
|
120
118
|
* ```
|
|
121
119
|
*/
|
|
122
120
|
time(date, options) {
|
|
123
|
-
this.logger_.logMethodArgs?.("time", { date, options });
|
|
124
121
|
if (typeof date === "number") date = new Date(date);
|
|
125
122
|
return date.toLocaleDateString(this.options__.locale.code, options);
|
|
126
123
|
}
|
|
@@ -140,7 +137,6 @@ var _Localizer = class _Localizer {
|
|
|
140
137
|
* ```
|
|
141
138
|
*/
|
|
142
139
|
relativeTime(date, from = Date.now(), options = { numeric: "auto", style: "narrow" }) {
|
|
143
|
-
this.logger_.logMethodArgs?.("relativeTime", { date, from, options });
|
|
144
140
|
const rtf = new Intl.RelativeTimeFormat(this.options__.locale.code, options);
|
|
145
141
|
if (typeof date !== "number") date = date.getTime();
|
|
146
142
|
if (typeof from !== "number") from = from.getTime();
|
package/dist/main.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/main.ts"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,
|
|
4
|
+
"sourcesContent": ["import { createLogger } from '@alwatr/logger';\nimport { packageTracer } from '@alwatr/package-tracer';\nimport { UnicodeDigits, type UnicodeLangKeys } from '@alwatr/unicode-digits';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\n/**\n * Represents a locale code in the format \"language-COUNTRY\".\n * The language part must be in lowercase, and the country part must be in uppercase.\n *\n * @example \"en-US\"\n * @example \"fr-FR\"\n * @example \"de-DE\"\n */\nexport type LocaleCode = `${Lowercase<string>}-${Uppercase<string>}`;\n\n/**\n * Represents a locale configuration with language code and language identifier.\n */\nexport interface Locale {\n /**\n * fa-IR, en-US, ...\n */\n code: LocaleCode;\n\n /**\n * The ISO 639-1 language code (e.g., 'fa', 'en')\n */\n language: UnicodeLangKeys;\n}\n\n/**\n * Provides localization and internationalization functionality.\n *\n * @remarks\n * This class handles number formatting, text translation, date formatting,\n * and relative time calculations based on the specified locale.\n */\nexport class Localizer {\n /**\n * Number formatter instance for formatting numbers according to the locale.\n *\n * @internal\n */\n protected numberFormatter_;\n\n /**\n * UnicodeDigits instance for converting numbers to locale-specific digits.\n *\n * @internal\n */\n protected unicodeDigits_;\n\n protected logger_ = createLogger(__package_name__);\n\n /**\n * Time units used for relative time calculations.\n * @internal\n */\n static timeUnits_ = [\n { label: 'year', seconds: 31536000 },\n { label: 'month', seconds: 2592000 },\n { label: 'week', seconds: 604800 },\n { label: 'day', seconds: 86400 },\n { label: 'hour', seconds: 3600 },\n { label: 'minute', seconds: 60 },\n { label: 'second', seconds: 1 },\n ] as const;\n\n /**\n * Creates a new instance of the Localizer class.\n *\n * @param options__ - Configuration options for localization\n */\n constructor(private options__: { locale: Locale; resource: DictionaryReq | null }) {\n this.numberFormatter_ = new Intl.NumberFormat(this.options__.locale.code);\n this.unicodeDigits_ = new UnicodeDigits(this.options__.locale.language);\n }\n\n /**\n * Retrieves a localized message by key from the resource dictionary.\n *\n * @param key - The key to lookup in the resource dictionary\n * @returns The localized message string\n *\n * @remarks\n * - Returns \"\\{key\\}\" if the key is not found in the dictionary\n * - Returns an empty string if the key is null or undefined\n *\n * @example\n * ```typescript\n * const localizer = new Localizer({...});\n * console.log(localizer.message('hello_world')); // \"Hello world!\"\n * console.log(localizer.message('missing_key')); // \"{missing_key}\"\n * ```\n */\n message(key: keyof NonNullable<typeof this.options__.resource>): string {\n // this.logger_.logMethodArgs?.('message', key);\n if (!key) return '';\n\n const msg = this.options__.resource?.[key];\n if (msg === undefined) {\n this.logger_.accident('message', 'l10n_key_not_found', 'Key not defined in the localization resource', {\n key,\n locale: this.options__.locale,\n });\n return `{${key}}`;\n }\n // else\n return msg;\n }\n\n /**\n * Formats a number according to the current locale settings.\n *\n * @param number - The number to format\n * @param decimal - Number of decimal places (default: 2)\n * @returns Formatted number string using locale-specific formatting\n *\n * @example\n * ```typescript\n * const localizer = new Localizer({...});\n * console.log(localizer.number(1234.567)); // \"1,234.57\"\n * console.log(localizer.number(1234.567, 1)); // \"1,234.6\"\n * ```\n */\n number(number?: number | null, decimal = 2): string {\n if (number == null) return '';\n decimal = Math.pow(10, decimal);\n number = Math.round(number * decimal) / decimal;\n return this.numberFormatter_.format(number);\n }\n\n /**\n * Replaces all numeric digits in a string with their locale-specific Unicode equivalents.\n *\n * @param str - The input string containing numbers to replace\n * @returns String with numbers converted to locale-specific digits\n *\n * @example\n * ```typescript\n * const localizer = new Localizer({locale: {code: 'fa-IR', language: 'fa'}, ...});\n * console.log(localizer.replaceNumber('123')); // '۱۲۳'\n * ```\n */\n replaceNumber(str: string): string {\n // this.logger_.logMethodArgs?.('replaceNumber', str);\n return this.unicodeDigits_.translate(str);\n }\n\n /**\n * Formats a date according to the current locale settings.\n *\n * @param date - Date to format (as Date object or timestamp)\n * @param options - Intl.DateTimeFormatOptions for customizing the output\n * @returns Localized date string\n *\n * @example\n * ```typescript\n * const localizer = new Localizer({...});\n * console.log(localizer.time(new Date())); // \"4/21/2025\"\n * ```\n */\n time(date: number | Date, options?: Intl.DateTimeFormatOptions): string {\n // this.logger_.logMethodArgs?.('time', { date, options });\n if (typeof date === 'number') date = new Date(date);\n return date.toLocaleDateString(this.options__.locale.code, options);\n }\n\n /**\n * Creates a relative time string comparing two dates.\n *\n * @param date - The date to compare (as Date object or timestamp)\n * @param from - Reference date for comparison (defaults to current time)\n * @param options - Formatting options for relative time\n * @returns Localized relative time string\n *\n * @example\n * ```typescript\n * const localizer = new Localizer({...});\n * const date = new Date(Date.now() - 3600000); // 1 hour ago\n * console.log(localizer.relativeTime(date)); // \"1 hour ago\"\n * ```\n */\n relativeTime(\n date: number | Date,\n from: number | Date = Date.now(),\n options: Intl.RelativeTimeFormatOptions = { numeric: 'auto', style: 'narrow' },\n ): string {\n // this.logger_.logMethodArgs?.('relativeTime', { date, from, options });\n\n const rtf = new Intl.RelativeTimeFormat(this.options__.locale.code, options);\n\n if (typeof date !== 'number') date = date.getTime();\n if (typeof from !== 'number') from = from.getTime();\n const diffSec = (from - date) / 1000;\n\n // Find the appropriate unit for the time difference\n for (const unit of Localizer.timeUnits_) {\n const interval = Math.floor(Math.abs(diffSec / unit.seconds));\n if (interval >= 1) {\n return rtf.format(diffSec > 0 ? interval : -interval, unit.label);\n }\n }\n\n return rtf.format(0, 'second');\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA6B;AAC7B,4BAA8B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d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,KAAK,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAI7E;;;;;;;GAOG;AACH,MAAM,MAAM,UAAU,GAAG,GAAG,SAAS,CAAC,MAAM,CAAC,IAAI,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC;AAErE;;GAEG;AACH,MAAM,WAAW,MAAM;IACrB;;OAEG;IACH,IAAI,EAAE,UAAU,CAAC;IAEjB;;OAEG;IACH,QAAQ,EAAE,eAAe,CAAC;CAC3B;AAED;;;;;;GAMG;AACH,qBAAa,SAAS;IAoCR,OAAO,CAAC,SAAS;IAnC7B;;;;OAIG;IACH,SAAS,CAAC,gBAAgB,oBAAC;IAE3B;;;;OAIG;IACH,SAAS,CAAC,cAAc,gBAAC;IAEzB,SAAS,CAAC,OAAO,wCAAkC;IAEnD;;;OAGG;IACH,MAAM,CAAC,UAAU;;;;;;;;;;;;;;;;;;;;;OAQN;IAEX;;;;OAIG;gBACiB,SAAS,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,aAAa,GAAG,IAAI,CAAA;KAAE;IAKjF;;;;;;;;;;;;;;;;OAgBG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,WAAW,CAAC,OAAO,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,MAAM;IAgBvE;;;;;;;;;;;;;OAaG;IACH,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,EAAE,OAAO,SAAI,GAAG,MAAM;IAOnD;;;;;;;;;;;OAWG;IACH,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAKlC;;;;;;;;;;;;OAYG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,qBAAqB,GAAG,MAAM;IAMvE;;;;;;;;;;;;;;OAcG;IACH,YAAY,CACV,IAAI,EAAE,MAAM,GAAG,IAAI,EACnB,IAAI,GAAE,MAAM,GAAG,IAAiB,EAChC,OAAO,GAAE,IAAI,CAAC,yBAAgE,GAC7E,MAAM;CAmBV"}
|
package/dist/main.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
/* @nexim/localizer v1.
|
|
1
|
+
/* @nexim/localizer v1.1.1 */
|
|
2
2
|
|
|
3
3
|
// src/main.ts
|
|
4
|
-
import { UnicodeDigits } from "@alwatr/unicode-digits";
|
|
5
|
-
import { packageTracer } from "@alwatr/package-tracer";
|
|
6
4
|
import { createLogger } from "@alwatr/logger";
|
|
7
|
-
|
|
5
|
+
import { packageTracer } from "@alwatr/package-tracer";
|
|
6
|
+
import { UnicodeDigits } from "@alwatr/unicode-digits";
|
|
7
|
+
__dev_mode__: packageTracer.add("@nexim/localizer", "1.1.1");
|
|
8
8
|
var _Localizer = class _Localizer {
|
|
9
9
|
/**
|
|
10
10
|
* Creates a new instance of the Localizer class.
|
|
@@ -35,7 +35,6 @@ var _Localizer = class _Localizer {
|
|
|
35
35
|
* ```
|
|
36
36
|
*/
|
|
37
37
|
message(key) {
|
|
38
|
-
this.logger_.logMethodArgs?.("message", key);
|
|
39
38
|
if (!key) return "";
|
|
40
39
|
const msg = this.options__.resource?.[key];
|
|
41
40
|
if (msg === void 0) {
|
|
@@ -80,7 +79,6 @@ var _Localizer = class _Localizer {
|
|
|
80
79
|
* ```
|
|
81
80
|
*/
|
|
82
81
|
replaceNumber(str) {
|
|
83
|
-
this.logger_.logMethodArgs?.("replaceNumber", str);
|
|
84
82
|
return this.unicodeDigits_.translate(str);
|
|
85
83
|
}
|
|
86
84
|
/**
|
|
@@ -97,7 +95,6 @@ var _Localizer = class _Localizer {
|
|
|
97
95
|
* ```
|
|
98
96
|
*/
|
|
99
97
|
time(date, options) {
|
|
100
|
-
this.logger_.logMethodArgs?.("time", { date, options });
|
|
101
98
|
if (typeof date === "number") date = new Date(date);
|
|
102
99
|
return date.toLocaleDateString(this.options__.locale.code, options);
|
|
103
100
|
}
|
|
@@ -117,7 +114,6 @@ var _Localizer = class _Localizer {
|
|
|
117
114
|
* ```
|
|
118
115
|
*/
|
|
119
116
|
relativeTime(date, from = Date.now(), options = { numeric: "auto", style: "narrow" }) {
|
|
120
|
-
this.logger_.logMethodArgs?.("relativeTime", { date, from, options });
|
|
121
117
|
const rtf = new Intl.RelativeTimeFormat(this.options__.locale.code, options);
|
|
122
118
|
if (typeof date !== "number") date = date.getTime();
|
|
123
119
|
if (typeof from !== "number") from = from.getTime();
|
package/dist/main.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/main.ts"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": ";;;AAAA,SAAS,
|
|
4
|
+
"sourcesContent": ["import { createLogger } from '@alwatr/logger';\nimport { packageTracer } from '@alwatr/package-tracer';\nimport { UnicodeDigits, type UnicodeLangKeys } from '@alwatr/unicode-digits';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\n/**\n * Represents a locale code in the format \"language-COUNTRY\".\n * The language part must be in lowercase, and the country part must be in uppercase.\n *\n * @example \"en-US\"\n * @example \"fr-FR\"\n * @example \"de-DE\"\n */\nexport type LocaleCode = `${Lowercase<string>}-${Uppercase<string>}`;\n\n/**\n * Represents a locale configuration with language code and language identifier.\n */\nexport interface Locale {\n /**\n * fa-IR, en-US, ...\n */\n code: LocaleCode;\n\n /**\n * The ISO 639-1 language code (e.g., 'fa', 'en')\n */\n language: UnicodeLangKeys;\n}\n\n/**\n * Provides localization and internationalization functionality.\n *\n * @remarks\n * This class handles number formatting, text translation, date formatting,\n * and relative time calculations based on the specified locale.\n */\nexport class Localizer {\n /**\n * Number formatter instance for formatting numbers according to the locale.\n *\n * @internal\n */\n protected numberFormatter_;\n\n /**\n * UnicodeDigits instance for converting numbers to locale-specific digits.\n *\n * @internal\n */\n protected unicodeDigits_;\n\n protected logger_ = createLogger(__package_name__);\n\n /**\n * Time units used for relative time calculations.\n * @internal\n */\n static timeUnits_ = [\n { label: 'year', seconds: 31536000 },\n { label: 'month', seconds: 2592000 },\n { label: 'week', seconds: 604800 },\n { label: 'day', seconds: 86400 },\n { label: 'hour', seconds: 3600 },\n { label: 'minute', seconds: 60 },\n { label: 'second', seconds: 1 },\n ] as const;\n\n /**\n * Creates a new instance of the Localizer class.\n *\n * @param options__ - Configuration options for localization\n */\n constructor(private options__: { locale: Locale; resource: DictionaryReq | null }) {\n this.numberFormatter_ = new Intl.NumberFormat(this.options__.locale.code);\n this.unicodeDigits_ = new UnicodeDigits(this.options__.locale.language);\n }\n\n /**\n * Retrieves a localized message by key from the resource dictionary.\n *\n * @param key - The key to lookup in the resource dictionary\n * @returns The localized message string\n *\n * @remarks\n * - Returns \"\\{key\\}\" if the key is not found in the dictionary\n * - Returns an empty string if the key is null or undefined\n *\n * @example\n * ```typescript\n * const localizer = new Localizer({...});\n * console.log(localizer.message('hello_world')); // \"Hello world!\"\n * console.log(localizer.message('missing_key')); // \"{missing_key}\"\n * ```\n */\n message(key: keyof NonNullable<typeof this.options__.resource>): string {\n // this.logger_.logMethodArgs?.('message', key);\n if (!key) return '';\n\n const msg = this.options__.resource?.[key];\n if (msg === undefined) {\n this.logger_.accident('message', 'l10n_key_not_found', 'Key not defined in the localization resource', {\n key,\n locale: this.options__.locale,\n });\n return `{${key}}`;\n }\n // else\n return msg;\n }\n\n /**\n * Formats a number according to the current locale settings.\n *\n * @param number - The number to format\n * @param decimal - Number of decimal places (default: 2)\n * @returns Formatted number string using locale-specific formatting\n *\n * @example\n * ```typescript\n * const localizer = new Localizer({...});\n * console.log(localizer.number(1234.567)); // \"1,234.57\"\n * console.log(localizer.number(1234.567, 1)); // \"1,234.6\"\n * ```\n */\n number(number?: number | null, decimal = 2): string {\n if (number == null) return '';\n decimal = Math.pow(10, decimal);\n number = Math.round(number * decimal) / decimal;\n return this.numberFormatter_.format(number);\n }\n\n /**\n * Replaces all numeric digits in a string with their locale-specific Unicode equivalents.\n *\n * @param str - The input string containing numbers to replace\n * @returns String with numbers converted to locale-specific digits\n *\n * @example\n * ```typescript\n * const localizer = new Localizer({locale: {code: 'fa-IR', language: 'fa'}, ...});\n * console.log(localizer.replaceNumber('123')); // '۱۲۳'\n * ```\n */\n replaceNumber(str: string): string {\n // this.logger_.logMethodArgs?.('replaceNumber', str);\n return this.unicodeDigits_.translate(str);\n }\n\n /**\n * Formats a date according to the current locale settings.\n *\n * @param date - Date to format (as Date object or timestamp)\n * @param options - Intl.DateTimeFormatOptions for customizing the output\n * @returns Localized date string\n *\n * @example\n * ```typescript\n * const localizer = new Localizer({...});\n * console.log(localizer.time(new Date())); // \"4/21/2025\"\n * ```\n */\n time(date: number | Date, options?: Intl.DateTimeFormatOptions): string {\n // this.logger_.logMethodArgs?.('time', { date, options });\n if (typeof date === 'number') date = new Date(date);\n return date.toLocaleDateString(this.options__.locale.code, options);\n }\n\n /**\n * Creates a relative time string comparing two dates.\n *\n * @param date - The date to compare (as Date object or timestamp)\n * @param from - Reference date for comparison (defaults to current time)\n * @param options - Formatting options for relative time\n * @returns Localized relative time string\n *\n * @example\n * ```typescript\n * const localizer = new Localizer({...});\n * const date = new Date(Date.now() - 3600000); // 1 hour ago\n * console.log(localizer.relativeTime(date)); // \"1 hour ago\"\n * ```\n */\n relativeTime(\n date: number | Date,\n from: number | Date = Date.now(),\n options: Intl.RelativeTimeFormatOptions = { numeric: 'auto', style: 'narrow' },\n ): string {\n // this.logger_.logMethodArgs?.('relativeTime', { date, from, options });\n\n const rtf = new Intl.RelativeTimeFormat(this.options__.locale.code, options);\n\n if (typeof date !== 'number') date = date.getTime();\n if (typeof from !== 'number') from = from.getTime();\n const diffSec = (from - date) / 1000;\n\n // Find the appropriate unit for the time difference\n for (const unit of Localizer.timeUnits_) {\n const interval = Math.floor(Math.abs(diffSec / unit.seconds));\n if (interval >= 1) {\n return rtf.format(diffSec > 0 ? interval : -interval, unit.label);\n }\n }\n\n return rtf.format(0, 'second');\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;AAAA,SAAS,oBAAoB;AAC7B,SAAS,qBAAqB;AAC9B,SAAS,qBAA2C;AAEpD,aAAc,eAAc,IAAI,oBAAkB,OAAmB;AAkC9D,IAAM,aAAN,MAAM,WAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoCrB,YAAoB,WAA+D;AAA/D;AArBpB,SAAU,UAAU,aAAa,kBAAgB;AAsB/C,SAAK,mBAAmB,IAAI,KAAK,aAAa,KAAK,UAAU,OAAO,IAAI;AACxE,SAAK,iBAAiB,IAAI,cAAc,KAAK,UAAU,OAAO,QAAQ;AAAA,EACxE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmBA,QAAQ,KAAgE;AAEtE,QAAI,CAAC,IAAK,QAAO;AAEjB,UAAM,MAAM,KAAK,UAAU,WAAW,GAAG;AACzC,QAAI,QAAQ,QAAW;AACrB,WAAK,QAAQ,SAAS,WAAW,sBAAsB,gDAAgD;AAAA,QACrG;AAAA,QACA,QAAQ,KAAK,UAAU;AAAA,MACzB,CAAC;AACD,aAAO,IAAI,GAAG;AAAA,IAChB;AAEA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgBA,OAAO,QAAwB,UAAU,GAAW;AAClD,QAAI,UAAU,KAAM,QAAO;AAC3B,cAAU,KAAK,IAAI,IAAI,OAAO;AAC9B,aAAS,KAAK,MAAM,SAAS,OAAO,IAAI;AACxC,WA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O,QAAQ,SAAS,OAAO;AAAA,EACjC,EAAE,OAAO,OAAO,SAAS,MAAM;AAAA,EAC/B,EAAE,OAAO,QAAQ,SAAS,KAAK;AAAA,EAC/B,EAAE,OAAO,UAAU,SAAS,GAAG;AAAA,EAC/B,EAAE,OAAO,UAAU,SAAS,EAAE;AAChC;AA7BK,IAAM,YAAN;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexim/localizer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Lightweight i18n utilities to handle translations, number formatting, date/time localization using native browser APIs.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"localization",
|
|
@@ -24,9 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"license": "AGPL-3.0-only",
|
|
26
26
|
"author": "S. Amir Mohammad Najafi <njfamirm@gmail.com> (https://www.njfamirm.ir)",
|
|
27
|
-
"contributors": [
|
|
28
|
-
"Arash Ghardashpoor <arash.qardashpoor@gmail.com> (https://www.agpagp.ir)"
|
|
29
|
-
],
|
|
27
|
+
"contributors": [],
|
|
30
28
|
"type": "module",
|
|
31
29
|
"exports": {
|
|
32
30
|
".": {
|
|
@@ -58,9 +56,9 @@
|
|
|
58
56
|
"@alwatr/nano-build": "^5.5.3",
|
|
59
57
|
"@alwatr/type-helper": "^5.4.1",
|
|
60
58
|
"@nexim/typescript-config": "^2.0.1",
|
|
61
|
-
"ava": "^6.
|
|
62
|
-
"typedoc": "^0.28.
|
|
63
|
-
"typedoc-plugin-markdown": "^4.6.
|
|
59
|
+
"ava": "^6.4.0",
|
|
60
|
+
"typedoc": "^0.28.5",
|
|
61
|
+
"typedoc-plugin-markdown": "^4.6.4",
|
|
64
62
|
"typedoc-plugin-no-inherit": "^1.6.1",
|
|
65
63
|
"typescript": "^5.8.3",
|
|
66
64
|
"wireit": "^0.14.12"
|
|
@@ -115,5 +113,5 @@
|
|
|
115
113
|
"command": "typedoc"
|
|
116
114
|
}
|
|
117
115
|
},
|
|
118
|
-
"gitHead": "
|
|
116
|
+
"gitHead": "5ef8529640812bdac227ccd0d1abda7789d4d732"
|
|
119
117
|
}
|