@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.
- package/README.md +128 -1
- package/dist/api/client.d.ts +25 -0
- package/dist/api/client.d.ts.map +1 -0
- package/dist/api/index.d.ts +3 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/wrapper.d.ts +27 -0
- package/dist/api/wrapper.d.ts.map +1 -0
- package/dist/cjs/api/client.d.ts +25 -0
- package/dist/cjs/api/client.d.ts.map +1 -0
- package/dist/cjs/api/client.js +126 -0
- package/dist/cjs/api/client.js.map +1 -0
- package/dist/cjs/api/index.d.ts +3 -0
- package/dist/cjs/api/index.d.ts.map +1 -0
- package/dist/cjs/api/index.js +6 -0
- package/dist/cjs/api/index.js.map +1 -0
- package/dist/cjs/api/wrapper.d.ts +27 -0
- package/dist/cjs/api/wrapper.d.ts.map +1 -0
- package/dist/cjs/api/wrapper.js +152 -0
- package/dist/cjs/api/wrapper.js.map +1 -0
- package/dist/cjs/date/dayjs.d.ts +17 -0
- package/dist/cjs/date/dayjs.d.ts.map +1 -1
- package/dist/cjs/date/dayjs.js +115 -12
- package/dist/cjs/date/dayjs.js.map +1 -1
- package/dist/cjs/date/format-date.d.ts +22 -1
- package/dist/cjs/date/format-date.d.ts.map +1 -1
- package/dist/cjs/date/format-date.js +66 -8
- package/dist/cjs/date/format-date.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/date/dayjs.d.ts +17 -0
- package/dist/date/dayjs.d.ts.map +1 -1
- package/dist/date/format-date.d.ts +22 -1
- package/dist/date/format-date.d.ts.map +1 -1
- package/dist/esm/api/client.js +121 -0
- package/dist/esm/api/index.js +1 -0
- package/dist/esm/api/wrapper.js +148 -0
- package/dist/esm/date/dayjs.js +104 -6
- package/dist/esm/date/format-date.js +66 -8
- package/dist/esm/index.js +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/package.json +8 -2
package/dist/esm/date/dayjs.js
CHANGED
|
@@ -1,14 +1,112 @@
|
|
|
1
1
|
import dayJs from "dayjs";
|
|
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
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
return
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -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
|
|
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.
|
|
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",
|