@zag-js/i18n-utils 1.34.0 → 1.35.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/dist/cache.d.mts +4 -0
- package/dist/cache.d.ts +4 -0
- package/dist/cache.js +41 -0
- package/dist/cache.mjs +16 -0
- package/dist/collator.d.mts +3 -0
- package/dist/collator.d.ts +3 -0
- package/dist/collator.js +34 -0
- package/dist/collator.mjs +9 -0
- package/dist/filter.d.mts +11 -0
- package/dist/filter.d.ts +11 -0
- package/dist/filter.js +73 -0
- package/dist/filter.mjs +48 -0
- package/dist/format-bytes.d.mts +32 -0
- package/dist/format-bytes.d.ts +32 -0
- package/dist/format-bytes.js +54 -0
- package/dist/format-bytes.mjs +29 -0
- package/dist/format-date.d.mts +7 -0
- package/dist/format-date.d.ts +7 -0
- package/dist/format-date.js +262 -0
- package/dist/format-date.mjs +237 -0
- package/dist/format-list.d.mts +3 -0
- package/dist/format-list.d.ts +3 -0
- package/dist/format-list.js +35 -0
- package/dist/format-list.mjs +10 -0
- package/dist/format-number.d.mts +3 -0
- package/dist/format-number.d.ts +3 -0
- package/dist/format-number.js +35 -0
- package/dist/format-number.mjs +10 -0
- package/dist/format-relative-time.d.mts +3 -0
- package/dist/format-relative-time.d.ts +3 -0
- package/dist/format-relative-time.js +65 -0
- package/dist/format-relative-time.mjs +40 -0
- package/dist/format-time.d.mts +13 -0
- package/dist/format-time.d.ts +13 -0
- package/dist/format-time.js +77 -0
- package/dist/format-time.mjs +52 -0
- package/dist/index.d.mts +11 -90
- package/dist/index.d.ts +11 -90
- package/dist/index.js +53 -509
- package/dist/index.mjs +17 -497
- package/dist/is-rtl.d.mts +4 -0
- package/dist/is-rtl.d.ts +4 -0
- package/dist/is-rtl.js +77 -0
- package/dist/is-rtl.mjs +51 -0
- package/dist/locale.d.mts +13 -0
- package/dist/locale.d.ts +13 -0
- package/dist/locale.js +42 -0
- package/dist/locale.mjs +17 -0
- package/dist/track-locale.d.mts +10 -0
- package/dist/track-locale.d.ts +10 -0
- package/dist/track-locale.js +43 -0
- package/dist/track-locale.mjs +18 -0
- package/package.json +2 -2
package/dist/cache.d.mts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
type FormatOptions<T> = T extends Intl.NumberFormat ? Intl.NumberFormatOptions : T extends Intl.DateTimeFormat ? Intl.DateTimeFormatOptions : T extends Intl.RelativeTimeFormat ? Intl.RelativeTimeFormatOptions : T extends Intl.ListFormat ? Intl.ListFormatOptions : T extends Intl.PluralRules ? Intl.PluralRulesOptions : T extends Intl.Collator ? Intl.CollatorOptions : never;
|
|
2
|
+
declare function i18nCache<T extends abstract new (...args: any) => any>(Ins: T): (locale: string, options?: FormatOptions<InstanceType<T>>) => InstanceType<T>;
|
|
3
|
+
|
|
4
|
+
export { i18nCache };
|
package/dist/cache.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
type FormatOptions<T> = T extends Intl.NumberFormat ? Intl.NumberFormatOptions : T extends Intl.DateTimeFormat ? Intl.DateTimeFormatOptions : T extends Intl.RelativeTimeFormat ? Intl.RelativeTimeFormatOptions : T extends Intl.ListFormat ? Intl.ListFormatOptions : T extends Intl.PluralRules ? Intl.PluralRulesOptions : T extends Intl.Collator ? Intl.CollatorOptions : never;
|
|
2
|
+
declare function i18nCache<T extends abstract new (...args: any) => any>(Ins: T): (locale: string, options?: FormatOptions<InstanceType<T>>) => InstanceType<T>;
|
|
3
|
+
|
|
4
|
+
export { i18nCache };
|
package/dist/cache.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/cache.ts
|
|
21
|
+
var cache_exports = {};
|
|
22
|
+
__export(cache_exports, {
|
|
23
|
+
i18nCache: () => i18nCache
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(cache_exports);
|
|
26
|
+
function i18nCache(Ins) {
|
|
27
|
+
const formatterCache = /* @__PURE__ */ new Map();
|
|
28
|
+
return function create(locale, options) {
|
|
29
|
+
const cacheKey = locale + (options ? Object.entries(options).sort((a, b) => a[0] < b[0] ? -1 : 1).join() : "");
|
|
30
|
+
if (formatterCache.has(cacheKey)) {
|
|
31
|
+
return formatterCache.get(cacheKey);
|
|
32
|
+
}
|
|
33
|
+
let formatter = new Ins(locale, options);
|
|
34
|
+
formatterCache.set(cacheKey, formatter);
|
|
35
|
+
return formatter;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
39
|
+
0 && (module.exports = {
|
|
40
|
+
i18nCache
|
|
41
|
+
});
|
package/dist/cache.mjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/cache.ts
|
|
2
|
+
function i18nCache(Ins) {
|
|
3
|
+
const formatterCache = /* @__PURE__ */ new Map();
|
|
4
|
+
return function create(locale, options) {
|
|
5
|
+
const cacheKey = locale + (options ? Object.entries(options).sort((a, b) => a[0] < b[0] ? -1 : 1).join() : "");
|
|
6
|
+
if (formatterCache.has(cacheKey)) {
|
|
7
|
+
return formatterCache.get(cacheKey);
|
|
8
|
+
}
|
|
9
|
+
let formatter = new Ins(locale, options);
|
|
10
|
+
formatterCache.set(cacheKey, formatter);
|
|
11
|
+
return formatter;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
i18nCache
|
|
16
|
+
};
|
package/dist/collator.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/collator.ts
|
|
21
|
+
var collator_exports = {};
|
|
22
|
+
__export(collator_exports, {
|
|
23
|
+
createCollator: () => createCollator
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(collator_exports);
|
|
26
|
+
var import_cache = require("./cache.cjs");
|
|
27
|
+
var getCollator = (0, import_cache.i18nCache)(Intl.Collator);
|
|
28
|
+
function createCollator(locale = "en-US", options = {}) {
|
|
29
|
+
return getCollator(locale, options);
|
|
30
|
+
}
|
|
31
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
32
|
+
0 && (module.exports = {
|
|
33
|
+
createCollator
|
|
34
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
interface FilterReturn {
|
|
2
|
+
startsWith(string: string, substring: string): boolean;
|
|
3
|
+
endsWith(string: string, substring: string): boolean;
|
|
4
|
+
contains(string: string, substring: string): boolean;
|
|
5
|
+
}
|
|
6
|
+
interface FilterOptions extends Intl.CollatorOptions {
|
|
7
|
+
locale?: string | undefined;
|
|
8
|
+
}
|
|
9
|
+
declare function createFilter(options?: FilterOptions): FilterReturn;
|
|
10
|
+
|
|
11
|
+
export { type FilterOptions, type FilterReturn, createFilter };
|
package/dist/filter.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
interface FilterReturn {
|
|
2
|
+
startsWith(string: string, substring: string): boolean;
|
|
3
|
+
endsWith(string: string, substring: string): boolean;
|
|
4
|
+
contains(string: string, substring: string): boolean;
|
|
5
|
+
}
|
|
6
|
+
interface FilterOptions extends Intl.CollatorOptions {
|
|
7
|
+
locale?: string | undefined;
|
|
8
|
+
}
|
|
9
|
+
declare function createFilter(options?: FilterOptions): FilterReturn;
|
|
10
|
+
|
|
11
|
+
export { type FilterOptions, type FilterReturn, createFilter };
|
package/dist/filter.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/filter.ts
|
|
21
|
+
var filter_exports = {};
|
|
22
|
+
__export(filter_exports, {
|
|
23
|
+
createFilter: () => createFilter
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(filter_exports);
|
|
26
|
+
var import_cache = require("./cache.cjs");
|
|
27
|
+
var collatorCache = (0, import_cache.i18nCache)(Intl.Collator);
|
|
28
|
+
function createFilter(options) {
|
|
29
|
+
const { locale, ...rest } = options || {};
|
|
30
|
+
const collator = collatorCache(locale || "en-US", { usage: "search", ...rest });
|
|
31
|
+
function normalize(string) {
|
|
32
|
+
string = string.normalize("NFC");
|
|
33
|
+
if (collator.resolvedOptions().ignorePunctuation) {
|
|
34
|
+
string = string.replace(/\p{P}/gu, "");
|
|
35
|
+
}
|
|
36
|
+
return string;
|
|
37
|
+
}
|
|
38
|
+
function startsWith(string, substring) {
|
|
39
|
+
if (substring.length === 0) return true;
|
|
40
|
+
string = normalize(string);
|
|
41
|
+
substring = normalize(substring);
|
|
42
|
+
return collator.compare(string.slice(0, substring.length), substring) === 0;
|
|
43
|
+
}
|
|
44
|
+
function endsWith(string, substring) {
|
|
45
|
+
if (substring.length === 0) return true;
|
|
46
|
+
string = normalize(string);
|
|
47
|
+
substring = normalize(substring);
|
|
48
|
+
return collator.compare(string.slice(-substring.length), substring) === 0;
|
|
49
|
+
}
|
|
50
|
+
function contains(string, substring) {
|
|
51
|
+
if (substring.length === 0) return true;
|
|
52
|
+
string = normalize(string);
|
|
53
|
+
substring = normalize(substring);
|
|
54
|
+
let scan = 0;
|
|
55
|
+
let sliceLen = substring.length;
|
|
56
|
+
for (; scan + sliceLen <= string.length; scan++) {
|
|
57
|
+
let slice = string.slice(scan, scan + sliceLen);
|
|
58
|
+
if (collator.compare(substring, slice) === 0) {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
startsWith,
|
|
66
|
+
endsWith,
|
|
67
|
+
contains
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
71
|
+
0 && (module.exports = {
|
|
72
|
+
createFilter
|
|
73
|
+
});
|
package/dist/filter.mjs
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// src/filter.ts
|
|
2
|
+
import { i18nCache } from "./cache.mjs";
|
|
3
|
+
var collatorCache = i18nCache(Intl.Collator);
|
|
4
|
+
function createFilter(options) {
|
|
5
|
+
const { locale, ...rest } = options || {};
|
|
6
|
+
const collator = collatorCache(locale || "en-US", { usage: "search", ...rest });
|
|
7
|
+
function normalize(string) {
|
|
8
|
+
string = string.normalize("NFC");
|
|
9
|
+
if (collator.resolvedOptions().ignorePunctuation) {
|
|
10
|
+
string = string.replace(/\p{P}/gu, "");
|
|
11
|
+
}
|
|
12
|
+
return string;
|
|
13
|
+
}
|
|
14
|
+
function startsWith(string, substring) {
|
|
15
|
+
if (substring.length === 0) return true;
|
|
16
|
+
string = normalize(string);
|
|
17
|
+
substring = normalize(substring);
|
|
18
|
+
return collator.compare(string.slice(0, substring.length), substring) === 0;
|
|
19
|
+
}
|
|
20
|
+
function endsWith(string, substring) {
|
|
21
|
+
if (substring.length === 0) return true;
|
|
22
|
+
string = normalize(string);
|
|
23
|
+
substring = normalize(substring);
|
|
24
|
+
return collator.compare(string.slice(-substring.length), substring) === 0;
|
|
25
|
+
}
|
|
26
|
+
function contains(string, substring) {
|
|
27
|
+
if (substring.length === 0) return true;
|
|
28
|
+
string = normalize(string);
|
|
29
|
+
substring = normalize(substring);
|
|
30
|
+
let scan = 0;
|
|
31
|
+
let sliceLen = substring.length;
|
|
32
|
+
for (; scan + sliceLen <= string.length; scan++) {
|
|
33
|
+
let slice = string.slice(scan, scan + sliceLen);
|
|
34
|
+
if (collator.compare(substring, slice) === 0) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
startsWith,
|
|
42
|
+
endsWith,
|
|
43
|
+
contains
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export {
|
|
47
|
+
createFilter
|
|
48
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
interface FormatBytesOptions {
|
|
2
|
+
/**
|
|
3
|
+
* The number of significant digits to include in the formatted output.
|
|
4
|
+
* @default 3
|
|
5
|
+
*/
|
|
6
|
+
precision?: number | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* The unit system to use for calculations.
|
|
9
|
+
* - "binary": Uses 1024 as the base (e.g., 1 KiB = 1024 bytes)
|
|
10
|
+
* - "decimal": Uses 1000 as the base (e.g., 1 KB = 1000 bytes)
|
|
11
|
+
* @default "decimal"
|
|
12
|
+
*/
|
|
13
|
+
unitSystem?: "binary" | "decimal" | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* The type of unit to format the value as.
|
|
16
|
+
* - "bit": Format as bits (b)
|
|
17
|
+
* - "byte": Format as bytes (B)
|
|
18
|
+
* @default "byte"
|
|
19
|
+
*/
|
|
20
|
+
unit?: "bit" | "byte" | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* The display style for the unit.
|
|
23
|
+
* - "long": Full unit name (e.g., "kilobytes")
|
|
24
|
+
* - "short": Abbreviated unit (e.g., "KB")
|
|
25
|
+
* - "narrow": Compact unit (e.g., "K")
|
|
26
|
+
* @default "short"
|
|
27
|
+
*/
|
|
28
|
+
unitDisplay?: "long" | "short" | "narrow" | undefined;
|
|
29
|
+
}
|
|
30
|
+
declare const formatBytes: (bytes: number, locale?: string, options?: FormatBytesOptions) => string;
|
|
31
|
+
|
|
32
|
+
export { type FormatBytesOptions, formatBytes };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
interface FormatBytesOptions {
|
|
2
|
+
/**
|
|
3
|
+
* The number of significant digits to include in the formatted output.
|
|
4
|
+
* @default 3
|
|
5
|
+
*/
|
|
6
|
+
precision?: number | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* The unit system to use for calculations.
|
|
9
|
+
* - "binary": Uses 1024 as the base (e.g., 1 KiB = 1024 bytes)
|
|
10
|
+
* - "decimal": Uses 1000 as the base (e.g., 1 KB = 1000 bytes)
|
|
11
|
+
* @default "decimal"
|
|
12
|
+
*/
|
|
13
|
+
unitSystem?: "binary" | "decimal" | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* The type of unit to format the value as.
|
|
16
|
+
* - "bit": Format as bits (b)
|
|
17
|
+
* - "byte": Format as bytes (B)
|
|
18
|
+
* @default "byte"
|
|
19
|
+
*/
|
|
20
|
+
unit?: "bit" | "byte" | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* The display style for the unit.
|
|
23
|
+
* - "long": Full unit name (e.g., "kilobytes")
|
|
24
|
+
* - "short": Abbreviated unit (e.g., "KB")
|
|
25
|
+
* - "narrow": Compact unit (e.g., "K")
|
|
26
|
+
* @default "short"
|
|
27
|
+
*/
|
|
28
|
+
unitDisplay?: "long" | "short" | "narrow" | undefined;
|
|
29
|
+
}
|
|
30
|
+
declare const formatBytes: (bytes: number, locale?: string, options?: FormatBytesOptions) => string;
|
|
31
|
+
|
|
32
|
+
export { type FormatBytesOptions, formatBytes };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/format-bytes.ts
|
|
21
|
+
var format_bytes_exports = {};
|
|
22
|
+
__export(format_bytes_exports, {
|
|
23
|
+
formatBytes: () => formatBytes
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(format_bytes_exports);
|
|
26
|
+
var import_format_number = require("./format-number.cjs");
|
|
27
|
+
var bitPrefixes = ["", "kilo", "mega", "giga", "tera"];
|
|
28
|
+
var bytePrefixes = ["", "kilo", "mega", "giga", "tera", "peta"];
|
|
29
|
+
var formatBytes = (bytes, locale = "en-US", options = {}) => {
|
|
30
|
+
if (Number.isNaN(bytes)) return "";
|
|
31
|
+
if (bytes === 0) return "0 B";
|
|
32
|
+
const { unitSystem = "decimal", precision = 3, unit = "byte", unitDisplay = "short" } = options;
|
|
33
|
+
const factor = unitSystem === "binary" ? 1024 : 1e3;
|
|
34
|
+
const prefix = unit === "bit" ? bitPrefixes : bytePrefixes;
|
|
35
|
+
const isNegative = bytes < 0;
|
|
36
|
+
const absoluteBytes = Math.abs(bytes);
|
|
37
|
+
let value = absoluteBytes;
|
|
38
|
+
let index = 0;
|
|
39
|
+
while (value >= factor && index < prefix.length - 1) {
|
|
40
|
+
value /= factor;
|
|
41
|
+
index++;
|
|
42
|
+
}
|
|
43
|
+
const v = parseFloat(value.toPrecision(precision));
|
|
44
|
+
const finalValue = isNegative ? -v : v;
|
|
45
|
+
return (0, import_format_number.formatNumber)(finalValue, locale, {
|
|
46
|
+
style: "unit",
|
|
47
|
+
unit: prefix[index] + unit,
|
|
48
|
+
unitDisplay
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
52
|
+
0 && (module.exports = {
|
|
53
|
+
formatBytes
|
|
54
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// src/format-bytes.ts
|
|
2
|
+
import { formatNumber } from "./format-number.mjs";
|
|
3
|
+
var bitPrefixes = ["", "kilo", "mega", "giga", "tera"];
|
|
4
|
+
var bytePrefixes = ["", "kilo", "mega", "giga", "tera", "peta"];
|
|
5
|
+
var formatBytes = (bytes, locale = "en-US", options = {}) => {
|
|
6
|
+
if (Number.isNaN(bytes)) return "";
|
|
7
|
+
if (bytes === 0) return "0 B";
|
|
8
|
+
const { unitSystem = "decimal", precision = 3, unit = "byte", unitDisplay = "short" } = options;
|
|
9
|
+
const factor = unitSystem === "binary" ? 1024 : 1e3;
|
|
10
|
+
const prefix = unit === "bit" ? bitPrefixes : bytePrefixes;
|
|
11
|
+
const isNegative = bytes < 0;
|
|
12
|
+
const absoluteBytes = Math.abs(bytes);
|
|
13
|
+
let value = absoluteBytes;
|
|
14
|
+
let index = 0;
|
|
15
|
+
while (value >= factor && index < prefix.length - 1) {
|
|
16
|
+
value /= factor;
|
|
17
|
+
index++;
|
|
18
|
+
}
|
|
19
|
+
const v = parseFloat(value.toPrecision(precision));
|
|
20
|
+
const finalValue = isNegative ? -v : v;
|
|
21
|
+
return formatNumber(finalValue, locale, {
|
|
22
|
+
style: "unit",
|
|
23
|
+
unit: prefix[index] + unit,
|
|
24
|
+
unitDisplay
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
export {
|
|
28
|
+
formatBytes
|
|
29
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a date using the given format string as defined in:
|
|
3
|
+
* https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
|
|
4
|
+
*/
|
|
5
|
+
declare function formatDate(date: Date, format: string, locale: string, timeZone?: string): string;
|
|
6
|
+
|
|
7
|
+
export { formatDate };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a date using the given format string as defined in:
|
|
3
|
+
* https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
|
|
4
|
+
*/
|
|
5
|
+
declare function formatDate(date: Date, format: string, locale: string, timeZone?: string): string;
|
|
6
|
+
|
|
7
|
+
export { formatDate };
|