@herowcode/utils 1.1.10 → 1.1.11

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.
@@ -1,3 +1,20 @@
1
1
  import dayJs from "dayjs";
2
+ import "dayjs/locale/en";
3
+ import "dayjs/locale/es";
4
+ import "dayjs/locale/fr";
5
+ import "dayjs/locale/pt-br";
6
+ export interface IDayjsIntlConfig {
7
+ locale?: Intl.LocalesArgument;
8
+ options?: Intl.DateTimeFormatOptions;
9
+ }
10
+ export interface IDayjsIntlResolvedConfig {
11
+ locale: Intl.LocalesArgument;
12
+ options: Intl.DateTimeFormatOptions;
13
+ }
14
+ export declare function getDayjsGlobalIntl(): IDayjsIntlResolvedConfig;
15
+ export declare function setDayjsGlobalIntl(config: IDayjsIntlConfig): IDayjsIntlResolvedConfig;
16
+ export declare function resetDayjsGlobalIntl(): IDayjsIntlResolvedConfig;
17
+ export declare function withDayjsGlobalIntl<T>(config: IDayjsIntlConfig, callback: () => T): T;
18
+ export declare function resolveDayjsIntlConfig(config?: IDayjsIntlConfig): IDayjsIntlResolvedConfig;
2
19
  export declare const dayjs: typeof dayJs;
3
20
  //# sourceMappingURL=dayjs.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"dayjs.d.ts","sourceRoot":"","sources":["../../../src/date/dayjs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAezB,eAAO,MAAM,KAAK,cAAQ,CAAA"}
1
+ {"version":3,"file":"dayjs.d.ts","sourceRoot":"","sources":["../../../src/date/dayjs.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAA;AAOzB,OAAO,iBAAiB,CAAA;AACxB,OAAO,iBAAiB,CAAA;AACxB,OAAO,iBAAiB,CAAA;AACxB,OAAO,oBAAoB,CAAA;AAI3B,MAAM,WAAW,gBAAgB;IAC/B,MAAM,CAAC,EAAE,IAAI,CAAC,eAAe,CAAA;IAC7B,OAAO,CAAC,EAAE,IAAI,CAAC,qBAAqB,CAAA;CACrC;AAED,MAAM,WAAW,wBAAwB;IACvC,MAAM,EAAE,IAAI,CAAC,eAAe,CAAA;IAC5B,OAAO,EAAE,IAAI,CAAC,qBAAqB,CAAA;CACpC;AAuFD,wBAAgB,kBAAkB,IAAI,wBAAwB,CAK7D;AAED,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,gBAAgB,GACvB,wBAAwB,CAG1B;AAED,wBAAgB,oBAAoB,IAAI,wBAAwB,CAM/D;AAED,wBAAgB,mBAAmB,CAAC,CAAC,EACnC,MAAM,EAAE,gBAAgB,EACxB,QAAQ,EAAE,MAAM,CAAC,GAChB,CAAC,CAYH;AAED,wBAAgB,sBAAsB,CACpC,MAAM,CAAC,EAAE,gBAAgB,GACxB,wBAAwB,CAE1B;AAUD,eAAO,MAAM,KAAK,cAAQ,CAAA"}
@@ -4,18 +4,121 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.dayjs = void 0;
7
+ exports.getDayjsGlobalIntl = getDayjsGlobalIntl;
8
+ exports.setDayjsGlobalIntl = setDayjsGlobalIntl;
9
+ exports.resetDayjsGlobalIntl = resetDayjsGlobalIntl;
10
+ exports.withDayjsGlobalIntl = withDayjsGlobalIntl;
11
+ exports.resolveDayjsIntlConfig = resolveDayjsIntlConfig;
7
12
  const dayjs_1 = __importDefault(require("dayjs"));
8
- const advancedFormat_js_1 = __importDefault(require("dayjs/plugin/advancedFormat.js"));
9
- const customParseFormat_js_1 = __importDefault(require("dayjs/plugin/customParseFormat.js"));
10
- const localizedFormat_js_1 = __importDefault(require("dayjs/plugin/localizedFormat.js"));
11
- const relativeTime_js_1 = __importDefault(require("dayjs/plugin/relativeTime.js"));
12
- const timezone_js_1 = __importDefault(require("dayjs/plugin/timezone.js"));
13
- const utc_js_1 = __importDefault(require("dayjs/plugin/utc.js"));
14
- dayjs_1.default.extend(utc_js_1.default);
15
- dayjs_1.default.extend(customParseFormat_js_1.default);
16
- dayjs_1.default.extend(advancedFormat_js_1.default);
17
- dayjs_1.default.extend(timezone_js_1.default);
18
- dayjs_1.default.extend(localizedFormat_js_1.default);
19
- dayjs_1.default.extend(relativeTime_js_1.default);
13
+ const advancedFormat_1 = __importDefault(require("dayjs/plugin/advancedFormat"));
14
+ const customParseFormat_1 = __importDefault(require("dayjs/plugin/customParseFormat"));
15
+ const localizedFormat_1 = __importDefault(require("dayjs/plugin/localizedFormat"));
16
+ const relativeTime_1 = __importDefault(require("dayjs/plugin/relativeTime"));
17
+ const timezone_1 = __importDefault(require("dayjs/plugin/timezone"));
18
+ const utc_1 = __importDefault(require("dayjs/plugin/utc"));
19
+ require("dayjs/locale/en");
20
+ require("dayjs/locale/es");
21
+ require("dayjs/locale/fr");
22
+ require("dayjs/locale/pt-br");
23
+ const DEFAULT_INTL_CONFIG = {
24
+ locale: "pt-BR",
25
+ options: {
26
+ month: "long",
27
+ day: "numeric",
28
+ year: "numeric",
29
+ },
30
+ };
31
+ let globalIntlConfig = {
32
+ locale: DEFAULT_INTL_CONFIG.locale,
33
+ options: { ...DEFAULT_INTL_CONFIG.options },
34
+ };
35
+ function mergeIntlConfigs(base, override) {
36
+ var _a;
37
+ if (!override) {
38
+ return {
39
+ locale: base.locale,
40
+ options: { ...base.options },
41
+ };
42
+ }
43
+ const mergedLocale = (_a = override.locale) !== null && _a !== void 0 ? _a : base.locale;
44
+ const mergedOptions = { ...base.options };
45
+ if (override.options) {
46
+ for (const key of Object.keys(override.options)) {
47
+ const value = override.options[key];
48
+ if (value === undefined || value === null) {
49
+ delete mergedOptions[key];
50
+ }
51
+ else {
52
+ ;
53
+ mergedOptions[key] = value;
54
+ }
55
+ }
56
+ }
57
+ return {
58
+ locale: mergedLocale,
59
+ options: mergedOptions,
60
+ };
61
+ }
62
+ const intlPlugin = (pluginConfig, DayjsClass) => {
63
+ if (pluginConfig) {
64
+ globalIntlConfig = mergeIntlConfigs(globalIntlConfig, pluginConfig);
65
+ }
66
+ const formatIntl = function formatIntl(config) {
67
+ const resolved = mergeIntlConfigs(globalIntlConfig, config);
68
+ return new Intl.DateTimeFormat(resolved.locale, resolved.options).format(this.toDate());
69
+ };
70
+ const toIntlFormatter = function toIntlFormatter(config) {
71
+ const resolved = mergeIntlConfigs(globalIntlConfig, config);
72
+ return new Intl.DateTimeFormat(resolved.locale, resolved.options);
73
+ };
74
+ Object.defineProperty(DayjsClass.prototype, "formatIntl", {
75
+ value: formatIntl,
76
+ configurable: true,
77
+ });
78
+ Object.defineProperty(DayjsClass.prototype, "toIntlFormatter", {
79
+ value: toIntlFormatter,
80
+ configurable: true,
81
+ });
82
+ };
83
+ function getDayjsGlobalIntl() {
84
+ return {
85
+ locale: globalIntlConfig.locale,
86
+ options: { ...globalIntlConfig.options },
87
+ };
88
+ }
89
+ function setDayjsGlobalIntl(config) {
90
+ globalIntlConfig = mergeIntlConfigs(globalIntlConfig, config);
91
+ return getDayjsGlobalIntl();
92
+ }
93
+ function resetDayjsGlobalIntl() {
94
+ globalIntlConfig = {
95
+ locale: DEFAULT_INTL_CONFIG.locale,
96
+ options: { ...DEFAULT_INTL_CONFIG.options },
97
+ };
98
+ return getDayjsGlobalIntl();
99
+ }
100
+ function withDayjsGlobalIntl(config, callback) {
101
+ const previous = getDayjsGlobalIntl();
102
+ globalIntlConfig = mergeIntlConfigs(globalIntlConfig, config);
103
+ try {
104
+ return callback();
105
+ }
106
+ finally {
107
+ globalIntlConfig = {
108
+ locale: previous.locale,
109
+ options: { ...previous.options },
110
+ };
111
+ }
112
+ }
113
+ function resolveDayjsIntlConfig(config) {
114
+ return mergeIntlConfigs(globalIntlConfig, config);
115
+ }
116
+ dayjs_1.default.extend(utc_1.default);
117
+ dayjs_1.default.extend(customParseFormat_1.default);
118
+ dayjs_1.default.extend(advancedFormat_1.default);
119
+ dayjs_1.default.extend(timezone_1.default);
120
+ dayjs_1.default.extend(localizedFormat_1.default);
121
+ dayjs_1.default.extend(relativeTime_1.default);
122
+ dayjs_1.default.extend(intlPlugin);
20
123
  exports.dayjs = dayjs_1.default;
21
124
  //# sourceMappingURL=dayjs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"dayjs.js","sourceRoot":"","sources":["../../../src/date/dayjs.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAyB;AACzB,uFAA2D;AAC3D,6FAAiE;AACjE,yFAA6D;AAC7D,mFAAuD;AACvD,2EAA+C;AAC/C,iEAAqC;AAErC,eAAK,CAAC,MAAM,CAAC,gBAAG,CAAC,CAAA;AACjB,eAAK,CAAC,MAAM,CAAC,8BAAiB,CAAC,CAAA;AAC/B,eAAK,CAAC,MAAM,CAAC,2BAAc,CAAC,CAAA;AAC5B,eAAK,CAAC,MAAM,CAAC,qBAAQ,CAAC,CAAA;AACtB,eAAK,CAAC,MAAM,CAAC,4BAAe,CAAC,CAAA;AAC7B,eAAK,CAAC,MAAM,CAAC,yBAAY,CAAC,CAAA;AAEb,QAAA,KAAK,GAAG,eAAK,CAAA"}
1
+ {"version":3,"file":"dayjs.js","sourceRoot":"","sources":["../../../src/date/dayjs.ts"],"names":[],"mappings":";;;;;;AA8GA,gDAKC;AAED,gDAKC;AAED,oDAMC;AAED,kDAeC;AAED,wDAIC;AAxJD,kDAAyB;AACzB,iFAAwD;AACxD,uFAA8D;AAC9D,mFAA0D;AAC1D,6EAAoD;AACpD,qEAA4C;AAC5C,2DAAkC;AAClC,2BAAwB;AACxB,2BAAwB;AACxB,2BAAwB;AACxB,8BAA2B;AAc3B,MAAM,mBAAmB,GAA6B;IACpD,MAAM,EAAE,OAAO;IACf,OAAO,EAAE;QACP,KAAK,EAAE,MAAM;QACb,GAAG,EAAE,SAAS;QACd,IAAI,EAAE,SAAS;KAChB;CACF,CAAA;AAED,IAAI,gBAAgB,GAA6B;IAC/C,MAAM,EAAE,mBAAmB,CAAC,MAAM;IAClC,OAAO,EAAE,EAAE,GAAG,mBAAmB,CAAC,OAAO,EAAE;CAC5C,C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AAG,gBAAgB,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAA;IAE7D,IAAI,CAAC;QACH,OAAO,QAAQ,EAAE,CAAA;IACnB,CAAC;YAAS,CAAC;QACT,gBAAgB,GAAG;YACjB,MAAM,EAAE,QAAQ,CAAC,MAAM;YACvB,OAAO,EAAE,EAAE,GAAG,QAAQ,CAAC,OAAO,EAAE;SACjC,CAAA;IACH,CAAC;AACH,CAAC;AAED,SAAgB,sBAAsB,CACpC,MAAyB;IAEzB,OAAO,gBAAgB,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAA;AACnD,CAAC;AAED,eAAK,CAAC,MAAM,CAAC,aAAG,CAAC,CAAA;AACjB,eAAK,CAAC,MAAM,CAAC,2BAAiB,CAAC,CAAA;AAC/B,eAAK,CAAC,MAAM,CAAC,wBAAc,CAAC,CAAA;AAC5B,eAAK,CAAC,MAAM,CAAC,kBAAQ,CAAC,CAAA;AACtB,eAAK,CAAC,MAAM,CAAC,yBAAe,CAAC,CAAA;AAC7B,eAAK,CAAC,MAAM,CAAC,sBAAY,CAAC,CAAA;AAC1B,eAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAA;AAEX,QAAA,KAAK,GAAG,eAAK,CAAA"}
@@ -1,2 +1,23 @@
1
- export declare function formatDate(date: Date | string | number, locale?: Intl.LocalesArgument, opts?: Intl.DateTimeFormatOptions): string;
1
+ import type { IDayjsIntlConfig } from "./dayjs";
2
+ export interface IFormatDateConfig {
3
+ /** Optional locale override applied to the underlying dayjs instance */
4
+ locale?: string;
5
+ /**
6
+ * When provided, format using dayjs tokens (e.g. "DD/MM/YYYY").
7
+ * Unless `useIntl` is true, this takes precedence over Intl formatting.
8
+ */
9
+ format?: string;
10
+ /**
11
+ * Intl configuration used when formatting through `Intl.DateTimeFormat`.
12
+ * Values are merged on top of the global Intl configuration.
13
+ */
14
+ intl?: IDayjsIntlConfig;
15
+ /**
16
+ * Forces the use of Intl formatting even if a format string is supplied.
17
+ */
18
+ useIntl?: boolean;
19
+ }
20
+ type TLocaleOrConfig = Intl.LocalesArgument | IFormatDateConfig;
21
+ export declare function formatDate(date: Date | string | number, localeOrConfig?: TLocaleOrConfig, opts?: Intl.DateTimeFormatOptions): string;
22
+ export {};
2
23
  //# sourceMappingURL=format-date.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"format-date.d.ts","sourceRoot":"","sources":["../../../src/date/format-date.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CACxB,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC5B,MAAM,GAAE,IAAI,CAAC,eAAyB,EACtC,IAAI,GAAE,IAAI,CAAC,qBAA0B,UAQtC"}
1
+ {"version":3,"file":"format-date.d.ts","sourceRoot":"","sources":["../../../src/date/format-date.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAG/C,MAAM,WAAW,iBAAiB;IAChC,wEAAwE;IACxE,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;OAGG;IACH,IAAI,CAAC,EAAE,gBAAgB,CAAA;IACvB;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,KAAK,eAAe,GAAG,IAAI,CAAC,eAAe,GAAG,iBAAiB,CAAA;AAqC/D,wBAAgB,UAAU,CACxB,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC5B,cAAc,GAAE,eAA6C,EAC7D,IAAI,CAAC,EAAE,IAAI,CAAC,qBAAqB,GAChC,MAAM,CAkDR"}
@@ -1,13 +1,71 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.formatDate = formatDate;
4
- function formatDate(date, locale = "pt-BR", opts = {}) {
5
- var _a, _b, _c;
6
- return new Intl.DateTimeFormat(locale, {
7
- month: (_a = opts.month) !== null && _a !== void 0 ? _a : "long",
8
- day: (_b = opts.day) !== null && _b !== void 0 ? _b : "numeric",
9
- year: (_c = opts.year) !== null && _c !== void 0 ? _c : "numeric",
10
- ...opts,
11
- }).format(new Date(date));
4
+ const dayjs_1 = require("./dayjs");
5
+ function isFormatDateConfig(value) {
6
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
7
+ }
8
+ function isValidLocaleTag(value) {
9
+ if (typeof Intl !== "undefined" &&
10
+ typeof Intl.getCanonicalLocales === "function") {
11
+ try {
12
+ Intl.getCanonicalLocales(value);
13
+ return true;
14
+ }
15
+ catch (_a) {
16
+ return false;
17
+ }
18
+ }
19
+ return /^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8})*$/.test(value);
20
+ }
21
+ function toDayjsLocale(locale) {
22
+ if (!locale)
23
+ return undefined;
24
+ const first = Array.isArray(locale)
25
+ ? locale[0]
26
+ : locale;
27
+ return typeof first === "string" ? first : String(first);
28
+ }
29
+ function formatDate(date, localeOrConfig = (0, dayjs_1.getDayjsGlobalIntl)().locale, opts) {
30
+ var _a;
31
+ const instance = (0, dayjs_1.dayjs)(date);
32
+ if (!instance.isValid()) {
33
+ throw new RangeError("Invalid time value");
34
+ }
35
+ if (isFormatDateConfig(localeOrConfig)) {
36
+ const { format, locale, intl, useIntl } = localeOrConfig;
37
+ const baseLocale = locale !== null && locale !== void 0 ? locale : toDayjsLocale(intl === null || intl === void 0 ? void 0 : intl.locale);
38
+ const workingInstance = baseLocale ? instance.locale(baseLocale) : instance;
39
+ if (format && !useIntl) {
40
+ return workingInstance.format(format);
41
+ }
42
+ const intlConfig = intl
43
+ ? {
44
+ locale: (_a = intl.locale) !== null && _a !== void 0 ? _a : baseLocale,
45
+ options: intl.options,
46
+ }
47
+ : baseLocale
48
+ ? {
49
+ locale: baseLocale,
50
+ }
51
+ : undefined;
52
+ return workingInstance.formatIntl(intlConfig);
53
+ }
54
+ if (typeof localeOrConfig === "string" && opts === undefined) {
55
+ if (!isValidLocaleTag(localeOrConfig)) {
56
+ return instance.format(localeOrConfig);
57
+ }
58
+ }
59
+ const localeForDayjs = toDayjsLocale(localeOrConfig);
60
+ const workingInstance = localeForDayjs
61
+ ? instance.locale(localeForDayjs)
62
+ : instance;
63
+ const intlConfig = localeOrConfig !== undefined || opts !== undefined
64
+ ? {
65
+ locale: localeOrConfig,
66
+ options: opts,
67
+ }
68
+ : undefined;
69
+ return workingInstance.formatIntl(intlConfig);
12
70
  }
13
71
  //# sourceMappingURL=format-date.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"format-date.js","sourceRoot":"","sources":["../../../src/date/format-date.ts"],"names":[],"mappings":";;AAAA,gCAWC;AAXD,SAAgB,UAAU,CACxB,IAA4B,EAC5B,SAA+B,OAAO,EACtC,OAAmC,EAAE;;IAErC,OAAO,IAAI,IAAI,CAAC,cAAc,CAAC,MAAM,EAAE;QACrC,KAAK,EAAE,MAAA,IAAI,CAAC,KAAK,mCAAI,MAAM;QAC3B,GAAG,EAAE,MAAA,IAAI,CAAC,GAAG,mCAAI,SAAS;QAC1B,IAAI,EAAE,MAAA,IAAI,CAAC,IAAI,mCAAI,SAAS;QAC5B,GAAG,IAAI;KACR,CAAC,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;AAC3B,CAAC"}
1
+ {"version":3,"file":"format-date.js","sourceRoot":"","sources":["../../../src/date/format-date.ts"],"names":[],"mappings":";;AA2DA,gCAsDC;AAhHD,mCAAmD;AAuBnD,SAAS,kBAAkB,CACzB,KAAsB;IAEtB,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;AAC7E,CAAC;AAED,SAAS,gBAAgB,CAAC,KAAa;IACrC,IACE,OAAO,IAAI,KAAK,WAAW;QAC3B,OAAO,IAAI,CAAC,mBAAmB,KAAK,UAAU,EAC9C,CAAC;QACD,IAAI,CAAC;YACH,IAAI,CAAC,mBAAmB,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C;YACvB,OAAO,eAAe,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;QACvC,CAAC;QAED,MAAM,UAAU,GAAiC,IAAI;YACnD,CAAC,CAAC;gBACE,MAAM,EAAE,MAAA,IAAI,CAAC,MAAM,mCAAI,UAAU;gBACjC,OAAO,EAAE,IAAI,CAAC,OAAO;aACtB;YACH,CAAC,CAAC,UAAU;gBACV,CAAC,CAAC;oBACE,MAAM,EAAE,UAAU;iBACnB;gBACH,CAAC,CAAC,SAAS,CAAA;QAEf,OAAO,eAAe,CAAC,UAAU,CAAC,UAAU,CAAC,CAAA;IAC/C,CAAC;IAED,IAAI,OAAO,cAAc,KAAK,QAAQ,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QAC7D,IAAI,CAAC,gBAAgB,CAAC,cAAc,CAAC,EAAE,CAAC;YACtC,OAAO,QAAQ,CAAC,MAAM,CAAC,cAAc,CAAC,CAAA;QACxC,CAAC;IACH,CAAC;IAED,MAAM,cAAc,GAAG,aAAa,CAAC,cAAc,CAAC,CAAA;IACpD,MAAM,eAAe,GAAG,cAAc;QACpC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,cAAc,CAAC;QACjC,CAAC,CAAC,QAAQ,CAAA;IAEZ,MAAM,UAAU,GACd,cAAc,KAAK,SAAS,IAAI,IAAI,KAAK,SAAS;QAChD,CAAC,CAAC;YACE,MAAM,EAAE,cAAc;YACtB,OAAO,EAAE,IAAI;SACd;QACH,CAAC,CAAC,SAAS,CAAA;IAEf,OAAO,eAAe,CAAC,UAAU,CAAC,UAAU,CAAC,CAAA;AAC/C,CAAC"}
@@ -1,3 +1,20 @@
1
1
  import dayJs from "dayjs";
2
+ import "dayjs/locale/en";
3
+ import "dayjs/locale/es";
4
+ import "dayjs/locale/fr";
5
+ import "dayjs/locale/pt-br";
6
+ export interface IDayjsIntlConfig {
7
+ locale?: Intl.LocalesArgument;
8
+ options?: Intl.DateTimeFormatOptions;
9
+ }
10
+ export interface IDayjsIntlResolvedConfig {
11
+ locale: Intl.LocalesArgument;
12
+ options: Intl.DateTimeFormatOptions;
13
+ }
14
+ export declare function getDayjsGlobalIntl(): IDayjsIntlResolvedConfig;
15
+ export declare function setDayjsGlobalIntl(config: IDayjsIntlConfig): IDayjsIntlResolvedConfig;
16
+ export declare function resetDayjsGlobalIntl(): IDayjsIntlResolvedConfig;
17
+ export declare function withDayjsGlobalIntl<T>(config: IDayjsIntlConfig, callback: () => T): T;
18
+ export declare function resolveDayjsIntlConfig(config?: IDayjsIntlConfig): IDayjsIntlResolvedConfig;
2
19
  export declare const dayjs: typeof dayJs;
3
20
  //# sourceMappingURL=dayjs.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"dayjs.d.ts","sourceRoot":"","sources":["../../src/date/dayjs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAezB,eAAO,MAAM,KAAK,cAAQ,CAAA"}
1
+ {"version":3,"file":"dayjs.d.ts","sourceRoot":"","sources":["../../src/date/dayjs.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAA;AAOzB,OAAO,iBAAiB,CAAA;AACxB,OAAO,iBAAiB,CAAA;AACxB,OAAO,iBAAiB,CAAA;AACxB,OAAO,oBAAoB,CAAA;AAI3B,MAAM,WAAW,gBAAgB;IAC/B,MAAM,CAAC,EAAE,IAAI,CAAC,eAAe,CAAA;IAC7B,OAAO,CAAC,EAAE,IAAI,CAAC,qBAAqB,CAAA;CACrC;AAED,MAAM,WAAW,wBAAwB;IACvC,MAAM,EAAE,IAAI,CAAC,eAAe,CAAA;IAC5B,OAAO,EAAE,IAAI,CAAC,qBAAqB,CAAA;CACpC;AAuFD,wBAAgB,kBAAkB,IAAI,wBAAwB,CAK7D;AAED,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,gBAAgB,GACvB,wBAAwB,CAG1B;AAED,wBAAgB,oBAAoB,IAAI,wBAAwB,CAM/D;AAED,wBAAgB,mBAAmB,CAAC,CAAC,EACnC,MAAM,EAAE,gBAAgB,EACxB,QAAQ,EAAE,MAAM,CAAC,GAChB,CAAC,CAYH;AAED,wBAAgB,sBAAsB,CACpC,MAAM,CAAC,EAAE,gBAAgB,GACxB,wBAAwB,CAE1B;AAUD,eAAO,MAAM,KAAK,cAAQ,CAAA"}
@@ -1,2 +1,23 @@
1
- export declare function formatDate(date: Date | string | number, locale?: Intl.LocalesArgument, opts?: Intl.DateTimeFormatOptions): string;
1
+ import type { IDayjsIntlConfig } from "./dayjs";
2
+ export interface IFormatDateConfig {
3
+ /** Optional locale override applied to the underlying dayjs instance */
4
+ locale?: string;
5
+ /**
6
+ * When provided, format using dayjs tokens (e.g. "DD/MM/YYYY").
7
+ * Unless `useIntl` is true, this takes precedence over Intl formatting.
8
+ */
9
+ format?: string;
10
+ /**
11
+ * Intl configuration used when formatting through `Intl.DateTimeFormat`.
12
+ * Values are merged on top of the global Intl configuration.
13
+ */
14
+ intl?: IDayjsIntlConfig;
15
+ /**
16
+ * Forces the use of Intl formatting even if a format string is supplied.
17
+ */
18
+ useIntl?: boolean;
19
+ }
20
+ type TLocaleOrConfig = Intl.LocalesArgument | IFormatDateConfig;
21
+ export declare function formatDate(date: Date | string | number, localeOrConfig?: TLocaleOrConfig, opts?: Intl.DateTimeFormatOptions): string;
22
+ export {};
2
23
  //# sourceMappingURL=format-date.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"format-date.d.ts","sourceRoot":"","sources":["../../src/date/format-date.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CACxB,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC5B,MAAM,GAAE,IAAI,CAAC,eAAyB,EACtC,IAAI,GAAE,IAAI,CAAC,qBAA0B,UAQtC"}
1
+ {"version":3,"file":"format-date.d.ts","sourceRoot":"","sources":["../../src/date/format-date.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAG/C,MAAM,WAAW,iBAAiB;IAChC,wEAAwE;IACxE,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;OAGG;IACH,IAAI,CAAC,EAAE,gBAAgB,CAAA;IACvB;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,KAAK,eAAe,GAAG,IAAI,CAAC,eAAe,GAAG,iBAAiB,CAAA;AAqC/D,wBAAgB,UAAU,CACxB,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC5B,cAAc,GAAE,eAA6C,EAC7D,IAAI,CAAC,EAAE,IAAI,CAAC,qBAAqB,GAChC,MAAM,CAkDR"}
@@ -1,14 +1,112 @@
1
1
  import dayJs from "dayjs";
2
- import advancedFormat from "dayjs/plugin/advancedFormat.js";
3
- import customParseFormat from "dayjs/plugin/customParseFormat.js";
4
- import localizedFormat from "dayjs/plugin/localizedFormat.js";
5
- import relativeTime from "dayjs/plugin/relativeTime.js";
6
- import timezone from "dayjs/plugin/timezone.js";
7
- import utc from "dayjs/plugin/utc.js";
2
+ import advancedFormat from "dayjs/plugin/advancedFormat";
3
+ import customParseFormat from "dayjs/plugin/customParseFormat";
4
+ import localizedFormat from "dayjs/plugin/localizedFormat";
5
+ import relativeTime from "dayjs/plugin/relativeTime";
6
+ import timezone from "dayjs/plugin/timezone";
7
+ import utc from "dayjs/plugin/utc";
8
+ import "dayjs/locale/en";
9
+ import "dayjs/locale/es";
10
+ import "dayjs/locale/fr";
11
+ import "dayjs/locale/pt-br";
12
+ const DEFAULT_INTL_CONFIG = {
13
+ locale: "pt-BR",
14
+ options: {
15
+ month: "long",
16
+ day: "numeric",
17
+ year: "numeric",
18
+ },
19
+ };
20
+ let globalIntlConfig = {
21
+ locale: DEFAULT_INTL_CONFIG.locale,
22
+ options: { ...DEFAULT_INTL_CONFIG.options },
23
+ };
24
+ function mergeIntlConfigs(base, override) {
25
+ var _a;
26
+ if (!override) {
27
+ return {
28
+ locale: base.locale,
29
+ options: { ...base.options },
30
+ };
31
+ }
32
+ const mergedLocale = (_a = override.locale) !== null && _a !== void 0 ? _a : base.locale;
33
+ const mergedOptions = { ...base.options };
34
+ if (override.options) {
35
+ for (const key of Object.keys(override.options)) {
36
+ const value = override.options[key];
37
+ if (value === undefined || value === null) {
38
+ delete mergedOptions[key];
39
+ }
40
+ else {
41
+ ;
42
+ mergedOptions[key] = value;
43
+ }
44
+ }
45
+ }
46
+ return {
47
+ locale: mergedLocale,
48
+ options: mergedOptions,
49
+ };
50
+ }
51
+ const intlPlugin = (pluginConfig, DayjsClass) => {
52
+ if (pluginConfig) {
53
+ globalIntlConfig = mergeIntlConfigs(globalIntlConfig, pluginConfig);
54
+ }
55
+ const formatIntl = function formatIntl(config) {
56
+ const resolved = mergeIntlConfigs(globalIntlConfig, config);
57
+ return new Intl.DateTimeFormat(resolved.locale, resolved.options).format(this.toDate());
58
+ };
59
+ const toIntlFormatter = function toIntlFormatter(config) {
60
+ const resolved = mergeIntlConfigs(globalIntlConfig, config);
61
+ return new Intl.DateTimeFormat(resolved.locale, resolved.options);
62
+ };
63
+ Object.defineProperty(DayjsClass.prototype, "formatIntl", {
64
+ value: formatIntl,
65
+ configurable: true,
66
+ });
67
+ Object.defineProperty(DayjsClass.prototype, "toIntlFormatter", {
68
+ value: toIntlFormatter,
69
+ configurable: true,
70
+ });
71
+ };
72
+ export function getDayjsGlobalIntl() {
73
+ return {
74
+ locale: globalIntlConfig.locale,
75
+ options: { ...globalIntlConfig.options },
76
+ };
77
+ }
78
+ export function setDayjsGlobalIntl(config) {
79
+ globalIntlConfig = mergeIntlConfigs(globalIntlConfig, config);
80
+ return getDayjsGlobalIntl();
81
+ }
82
+ export function resetDayjsGlobalIntl() {
83
+ globalIntlConfig = {
84
+ locale: DEFAULT_INTL_CONFIG.locale,
85
+ options: { ...DEFAULT_INTL_CONFIG.options },
86
+ };
87
+ return getDayjsGlobalIntl();
88
+ }
89
+ export function withDayjsGlobalIntl(config, callback) {
90
+ const previous = getDayjsGlobalIntl();
91
+ globalIntlConfig = mergeIntlConfigs(globalIntlConfig, config);
92
+ try {
93
+ return callback();
94
+ }
95
+ finally {
96
+ globalIntlConfig = {
97
+ locale: previous.locale,
98
+ options: { ...previous.options },
99
+ };
100
+ }
101
+ }
102
+ export function resolveDayjsIntlConfig(config) {
103
+ return mergeIntlConfigs(globalIntlConfig, config);
104
+ }
8
105
  dayJs.extend(utc);
9
106
  dayJs.extend(customParseFormat);
10
107
  dayJs.extend(advancedFormat);
11
108
  dayJs.extend(timezone);
12
109
  dayJs.extend(localizedFormat);
13
110
  dayJs.extend(relativeTime);
111
+ dayJs.extend(intlPlugin);
14
112
  export const dayjs = dayJs;
@@ -1,9 +1,67 @@
1
- export function formatDate(date, locale = "pt-BR", opts = {}) {
2
- var _a, _b, _c;
3
- return new Intl.DateTimeFormat(locale, {
4
- month: (_a = opts.month) !== null && _a !== void 0 ? _a : "long",
5
- day: (_b = opts.day) !== null && _b !== void 0 ? _b : "numeric",
6
- year: (_c = opts.year) !== null && _c !== void 0 ? _c : "numeric",
7
- ...opts,
8
- }).format(new Date(date));
1
+ import { dayjs, getDayjsGlobalIntl } from "./dayjs.js";
2
+ function isFormatDateConfig(value) {
3
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
4
+ }
5
+ function isValidLocaleTag(value) {
6
+ if (typeof Intl !== "undefined" &&
7
+ typeof Intl.getCanonicalLocales === "function") {
8
+ try {
9
+ Intl.getCanonicalLocales(value);
10
+ return true;
11
+ }
12
+ catch (_a) {
13
+ return false;
14
+ }
15
+ }
16
+ return /^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8})*$/.test(value);
17
+ }
18
+ function toDayjsLocale(locale) {
19
+ if (!locale)
20
+ return undefined;
21
+ const first = Array.isArray(locale)
22
+ ? locale[0]
23
+ : locale;
24
+ return typeof first === "string" ? first : String(first);
25
+ }
26
+ export function formatDate(date, localeOrConfig = getDayjsGlobalIntl().locale, opts) {
27
+ var _a;
28
+ const instance = dayjs(date);
29
+ if (!instance.isValid()) {
30
+ throw new RangeError("Invalid time value");
31
+ }
32
+ if (isFormatDateConfig(localeOrConfig)) {
33
+ const { format, locale, intl, useIntl } = localeOrConfig;
34
+ const baseLocale = locale !== null && locale !== void 0 ? locale : toDayjsLocale(intl === null || intl === void 0 ? void 0 : intl.locale);
35
+ const workingInstance = baseLocale ? instance.locale(baseLocale) : instance;
36
+ if (format && !useIntl) {
37
+ return workingInstance.format(format);
38
+ }
39
+ const intlConfig = intl
40
+ ? {
41
+ locale: (_a = intl.locale) !== null && _a !== void 0 ? _a : baseLocale,
42
+ options: intl.options,
43
+ }
44
+ : baseLocale
45
+ ? {
46
+ locale: baseLocale,
47
+ }
48
+ : undefined;
49
+ return workingInstance.formatIntl(intlConfig);
50
+ }
51
+ if (typeof localeOrConfig === "string" && opts === undefined) {
52
+ if (!isValidLocaleTag(localeOrConfig)) {
53
+ return instance.format(localeOrConfig);
54
+ }
55
+ }
56
+ const localeForDayjs = toDayjsLocale(localeOrConfig);
57
+ const workingInstance = localeForDayjs
58
+ ? instance.locale(localeForDayjs)
59
+ : instance;
60
+ const intlConfig = localeOrConfig !== undefined || opts !== undefined
61
+ ? {
62
+ locale: localeOrConfig,
63
+ options: opts,
64
+ }
65
+ : undefined;
66
+ return workingInstance.formatIntl(intlConfig);
9
67
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@herowcode/utils",
3
- "version": "1.1.10",
3
+ "version": "1.1.11",
4
4
  "description": "A lightweight collection of utility functions for everyday JavaScript/TypeScript development",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",