@herowcode/utils 1.1.10 → 1.2.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.
Files changed (44) hide show
  1. package/README.md +128 -1
  2. package/dist/api/client.d.ts +25 -0
  3. package/dist/api/client.d.ts.map +1 -0
  4. package/dist/api/index.d.ts +3 -0
  5. package/dist/api/index.d.ts.map +1 -0
  6. package/dist/api/wrapper.d.ts +27 -0
  7. package/dist/api/wrapper.d.ts.map +1 -0
  8. package/dist/cjs/api/client.d.ts +25 -0
  9. package/dist/cjs/api/client.d.ts.map +1 -0
  10. package/dist/cjs/api/client.js +126 -0
  11. package/dist/cjs/api/client.js.map +1 -0
  12. package/dist/cjs/api/index.d.ts +3 -0
  13. package/dist/cjs/api/index.d.ts.map +1 -0
  14. package/dist/cjs/api/index.js +6 -0
  15. package/dist/cjs/api/index.js.map +1 -0
  16. package/dist/cjs/api/wrapper.d.ts +27 -0
  17. package/dist/cjs/api/wrapper.d.ts.map +1 -0
  18. package/dist/cjs/api/wrapper.js +152 -0
  19. package/dist/cjs/api/wrapper.js.map +1 -0
  20. package/dist/cjs/date/dayjs.d.ts +17 -0
  21. package/dist/cjs/date/dayjs.d.ts.map +1 -1
  22. package/dist/cjs/date/dayjs.js +115 -12
  23. package/dist/cjs/date/dayjs.js.map +1 -1
  24. package/dist/cjs/date/format-date.d.ts +22 -1
  25. package/dist/cjs/date/format-date.d.ts.map +1 -1
  26. package/dist/cjs/date/format-date.js +66 -8
  27. package/dist/cjs/date/format-date.js.map +1 -1
  28. package/dist/cjs/index.d.ts +1 -0
  29. package/dist/cjs/index.d.ts.map +1 -1
  30. package/dist/cjs/index.js +1 -0
  31. package/dist/cjs/index.js.map +1 -1
  32. package/dist/date/dayjs.d.ts +17 -0
  33. package/dist/date/dayjs.d.ts.map +1 -1
  34. package/dist/date/format-date.d.ts +22 -1
  35. package/dist/date/format-date.d.ts.map +1 -1
  36. package/dist/esm/api/client.js +121 -0
  37. package/dist/esm/api/index.js +1 -0
  38. package/dist/esm/api/wrapper.js +148 -0
  39. package/dist/esm/date/dayjs.js +104 -6
  40. package/dist/esm/date/format-date.js +66 -8
  41. package/dist/esm/index.js +1 -0
  42. package/dist/index.d.ts +1 -0
  43. package/dist/index.d.ts.map +1 -1
  44. package/package.json +8 -2
@@ -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/dist/esm/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ export * from "./api/index.js"; // API utilities
1
2
  export * from "./array/index.js"; // Array utilities
2
3
  export * from "./date/index.js"; // Date utilities
3
4
  export * from "./files/index.js"; // Files utilities
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from "./api/index.js";
1
2
  export * from "./array/index.js";
2
3
  export * from "./date/index.js";
3
4
  export * from "./files/index.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAChC,cAAc,qBAAqB,CAAA;AACnC,cAAc,mBAAmB,CAAA;AACjC,cAAc,oBAAoB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAChC,cAAc,qBAAqB,CAAA;AACnC,cAAc,mBAAmB,CAAA;AACjC,cAAc,oBAAoB,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@herowcode/utils",
3
- "version": "1.1.10",
3
+ "version": "1.2.1",
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",
@@ -40,6 +40,11 @@
40
40
  "types": "./dist/youtube/index.d.ts",
41
41
  "import": "./dist/esm/youtube/index.js",
42
42
  "require": "./dist/cjs/youtube/index.js"
43
+ },
44
+ "./api": {
45
+ "types": "./dist/api/index.d.ts",
46
+ "import": "./dist/esm/api/index.js",
47
+ "require": "./dist/cjs/api/index.js"
43
48
  }
44
49
  },
45
50
  "files": [
@@ -78,11 +83,12 @@
78
83
  },
79
84
  "license": "MIT",
80
85
  "dependencies": {
86
+ "axios": "1.12.2",
81
87
  "dayjs": "^1.11.10",
82
88
  "react": "19.1.1"
83
89
  },
84
90
  "devDependencies": {
85
- "@biomejs/biome": "2.2.3",
91
+ "@biomejs/biome": "2.1.4",
86
92
  "@testing-library/dom": "10.4.1",
87
93
  "@testing-library/jest-dom": "6.8.0",
88
94
  "@testing-library/react": "16.3.0",