@herowcode/utils 1.1.2 → 1.1.4
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 +47 -4
- package/dist/date/parse-time-spent.d.ts +14 -1
- package/dist/date/parse-time-spent.d.ts.map +1 -1
- package/dist/date/parse-time-spent.esm.js +126 -8
- package/dist/date/parse-time-spent.js +126 -8
- package/dist/date/parse-time-spent.js.map +1 -1
- package/dist/youtube/get-youtube-thumbnail.d.ts +2 -0
- package/dist/youtube/get-youtube-thumbnail.d.ts.map +1 -0
- package/dist/youtube/get-youtube-thumbnail.esm.js +31 -0
- package/dist/youtube/get-youtube-thumbnail.js +31 -0
- package/dist/youtube/get-youtube-thumbnail.js.map +1 -0
- package/dist/youtube/index.d.ts +1 -0
- package/dist/youtube/index.d.ts.map +1 -1
- package/dist/youtube/index.esm.js +1 -0
- package/dist/youtube/index.js +1 -0
- package/dist/youtube/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -107,11 +107,41 @@ Converts a Date to a Dayjs object in UTC.
|
|
|
107
107
|
getDateInUTC(new Date());
|
|
108
108
|
```
|
|
109
109
|
|
|
110
|
-
#### `parseTimeSpent(initialDate: string | Date, finalDate: string | Date, locale?: string):
|
|
111
|
-
|
|
110
|
+
#### `parseTimeSpent(initialDate: string | Date, finalDate: string | Date, locale?: string, options?: { format?: 'verbose' | 'compact'; minimal?: boolean }): ITimeSpent`
|
|
111
|
+
|
|
112
|
+
Retorna um objeto com a decomposição do tempo entre duas datas e uma string formatada. Estrutura retornada (ITimeSpent):
|
|
113
|
+
|
|
114
|
+
- years: number
|
|
115
|
+
- months: number
|
|
116
|
+
- days: number
|
|
117
|
+
- hours: number
|
|
118
|
+
- minutes: number
|
|
119
|
+
- seconds: number
|
|
120
|
+
- formatted: string
|
|
121
|
+
|
|
122
|
+
Parâmetros:
|
|
123
|
+
- initialDate: Data inicial (string ou Date)
|
|
124
|
+
- finalDate: Data final (string ou Date)
|
|
125
|
+
- locale (opcional): Código de localidade (ex.: "pt-BR", "en-US"). Padrão: "pt-BR"
|
|
126
|
+
- options (opcional):
|
|
127
|
+
- format: "verbose" | "compact" — "verbose" gera texto humanizado (ex.: "1 dia, 3 horas"), "compact" gera tokens curtos (ex.: "1d3h")
|
|
128
|
+
- minimal: boolean — quando true, mantém apenas a maior unidade não-zero (ex.: "1d" ou "1 dia")
|
|
129
|
+
|
|
130
|
+
Exemplos:
|
|
112
131
|
|
|
113
132
|
```typescript
|
|
114
|
-
|
|
133
|
+
// verbose (padrão)
|
|
134
|
+
const result = parseTimeSpent('2020-01-01', '2022-04-16', 'en-US')
|
|
135
|
+
// result.formatted -> "2 years, 3 months, and 15 days"
|
|
136
|
+
// result.years -> 2, result.months -> 3, result.days -> 15
|
|
137
|
+
|
|
138
|
+
// compact
|
|
139
|
+
const compact = parseTimeSpent('2022-04-01T00:00:00', '2022-04-02T03:04:05', 'pt-BR', { format: 'compact' })
|
|
140
|
+
// compact.formatted -> "1d3h4min5s"
|
|
141
|
+
|
|
142
|
+
// minimal (maior unidade apenas)
|
|
143
|
+
const minimal = parseTimeSpent('2022-04-01T00:00:00', '2022-04-02T03:04:05', 'pt-BR', { minimal: true })
|
|
144
|
+
// minimal.formatted -> "1 dia"
|
|
115
145
|
```
|
|
116
146
|
|
|
117
147
|
### Files Utilities
|
|
@@ -337,11 +367,24 @@ function VideoComponent() {
|
|
|
337
367
|
#### `validateYoutubeLink(videoUrl: string): Promise<boolean>`
|
|
338
368
|
Checks whether a YouTube video exists by probing thumbnails and falling back to the oEmbed endpoint. Returns `true` for found/public videos and `false` otherwise.
|
|
339
369
|
|
|
340
|
-
```
|
|
370
|
+
```ts
|
|
341
371
|
const ok = await validateYoutubeLink('https://youtu.be/dQw4w9WgXcQ');
|
|
342
372
|
// true | false
|
|
343
373
|
```
|
|
344
374
|
|
|
375
|
+
#### `getYoutubeThumbnail(videoUrl: string): Promise<string | null>`
|
|
376
|
+
Attempts to load YouTube thumbnail images in priority order (maxresdefault, hqdefault, mqdefault, default). It creates an Image in the browser and returns the first URL that successfully loads, or `null` if none are available or the video ID cannot be extracted.
|
|
377
|
+
|
|
378
|
+
Example:
|
|
379
|
+
```ts
|
|
380
|
+
const thumb = await getYoutubeThumbnail('https://youtu.be/abc123');
|
|
381
|
+
// e.g. "https://img.youtube.com/vi/abc123/hqdefault.jpg" or null
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
Notes:
|
|
385
|
+
- Uses the browser Image load/error events to avoid CORS issues.
|
|
386
|
+
- Returns `null` when the video ID cannot be extracted or no thumbnails load.
|
|
387
|
+
|
|
345
388
|
## Browser Support
|
|
346
389
|
|
|
347
390
|
This library supports all modern browsers and Node.js environments. It uses ES2018 features and requires:
|
|
@@ -1,2 +1,15 @@
|
|
|
1
|
-
export
|
|
1
|
+
export interface ITimeSpent {
|
|
2
|
+
years: number;
|
|
3
|
+
months: number;
|
|
4
|
+
days: number;
|
|
5
|
+
hours: number;
|
|
6
|
+
minutes: number;
|
|
7
|
+
seconds: number;
|
|
8
|
+
formatted: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function parseTimeSpent(initialDate: string | Date, finalDate: string | Date, locale?: string, // Default to Portuguese for backward compatibility
|
|
11
|
+
options?: {
|
|
12
|
+
format?: "verbose" | "compact";
|
|
13
|
+
minimal?: boolean;
|
|
14
|
+
}): ITimeSpent;
|
|
2
15
|
//# sourceMappingURL=parse-time-spent.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse-time-spent.d.ts","sourceRoot":"","sources":["../../src/date/parse-time-spent.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"parse-time-spent.d.ts","sourceRoot":"","sources":["../../src/date/parse-time-spent.ts"],"names":[],"mappings":"AA2HA,MAAM,WAAW,UAAU;IACzB,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;IACb,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;CAClB;AAGD,wBAAgB,cAAc,CAC5B,WAAW,EAAE,MAAM,GAAG,IAAI,EAC1B,SAAS,EAAE,MAAM,GAAG,IAAI,EACxB,MAAM,SAAU,EAAE,mDAAmD;AACrE,OAAO,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,SAAS,GAAG,SAAS,CAAC;IAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAAE,GAC9D,UAAU,CAkHZ"}
|
|
@@ -5,6 +5,9 @@ const locales = {
|
|
|
5
5
|
year: { singular: "ano", plural: "anos" },
|
|
6
6
|
month: { singular: "mês", plural: "meses" },
|
|
7
7
|
day: { singular: "dia", plural: "dias" },
|
|
8
|
+
hour: { singular: "hora", plural: "horas" },
|
|
9
|
+
minute: { singular: "minuto", plural: "minutos" },
|
|
10
|
+
second: { singular: "segundo", plural: "segundos" },
|
|
8
11
|
formatList: (parts) => {
|
|
9
12
|
if (parts.length === 0)
|
|
10
13
|
return "";
|
|
@@ -14,11 +17,22 @@ const locales = {
|
|
|
14
17
|
const otherParts = parts.slice(0, -1);
|
|
15
18
|
return `${otherParts.join(", ")} e ${lastPart}`;
|
|
16
19
|
},
|
|
20
|
+
compactTokens: {
|
|
21
|
+
year: "a",
|
|
22
|
+
month: "m",
|
|
23
|
+
day: "d",
|
|
24
|
+
hour: "h",
|
|
25
|
+
minute: "min",
|
|
26
|
+
second: "s",
|
|
27
|
+
},
|
|
17
28
|
},
|
|
18
29
|
"en-US": {
|
|
19
30
|
year: { singular: "year", plural: "years" },
|
|
20
31
|
month: { singular: "month", plural: "months" },
|
|
21
32
|
day: { singular: "day", plural: "days" },
|
|
33
|
+
hour: { singular: "hour", plural: "hours" },
|
|
34
|
+
minute: { singular: "minute", plural: "minutes" },
|
|
35
|
+
second: { singular: "second", plural: "seconds" },
|
|
22
36
|
formatList: (parts) => {
|
|
23
37
|
if (parts.length === 0)
|
|
24
38
|
return "";
|
|
@@ -28,11 +42,22 @@ const locales = {
|
|
|
28
42
|
return `${parts[0]} and ${parts[1]}`;
|
|
29
43
|
return `${parts.slice(0, -1).join(", ")}, and ${parts[parts.length - 1]}`;
|
|
30
44
|
},
|
|
45
|
+
compactTokens: {
|
|
46
|
+
year: "y",
|
|
47
|
+
month: "mo",
|
|
48
|
+
day: "d",
|
|
49
|
+
hour: "h",
|
|
50
|
+
minute: "m",
|
|
51
|
+
second: "s",
|
|
52
|
+
},
|
|
31
53
|
},
|
|
32
54
|
"es-ES": {
|
|
33
55
|
year: { singular: "año", plural: "años" },
|
|
34
56
|
month: { singular: "mes", plural: "meses" },
|
|
35
57
|
day: { singular: "día", plural: "días" },
|
|
58
|
+
hour: { singular: "hora", plural: "horas" },
|
|
59
|
+
minute: { singular: "minuto", plural: "minutos" },
|
|
60
|
+
second: { singular: "segundo", plural: "segundos" },
|
|
36
61
|
formatList: (parts) => {
|
|
37
62
|
if (parts.length === 0)
|
|
38
63
|
return "";
|
|
@@ -42,11 +67,22 @@ const locales = {
|
|
|
42
67
|
const otherParts = parts.slice(0, -1);
|
|
43
68
|
return `${otherParts.join(", ")} y ${lastPart}`;
|
|
44
69
|
},
|
|
70
|
+
compactTokens: {
|
|
71
|
+
year: "a",
|
|
72
|
+
month: "mes",
|
|
73
|
+
day: "d",
|
|
74
|
+
hour: "h",
|
|
75
|
+
minute: "min",
|
|
76
|
+
second: "s",
|
|
77
|
+
},
|
|
45
78
|
},
|
|
46
79
|
"fr-FR": {
|
|
47
80
|
year: { singular: "an", plural: "ans" },
|
|
48
81
|
month: { singular: "mois", plural: "mois" },
|
|
49
82
|
day: { singular: "jour", plural: "jours" },
|
|
83
|
+
hour: { singular: "heure", plural: "heures" },
|
|
84
|
+
minute: { singular: "minute", plural: "minutes" },
|
|
85
|
+
second: { singular: "seconde", plural: "secondes" },
|
|
50
86
|
formatList: (parts) => {
|
|
51
87
|
if (parts.length === 0)
|
|
52
88
|
return "";
|
|
@@ -56,30 +92,112 @@ const locales = {
|
|
|
56
92
|
const otherParts = parts.slice(0, -1);
|
|
57
93
|
return `${otherParts.join(", ")} et ${lastPart}`;
|
|
58
94
|
},
|
|
95
|
+
compactTokens: {
|
|
96
|
+
year: "a",
|
|
97
|
+
month: "m",
|
|
98
|
+
day: "j",
|
|
99
|
+
hour: "h",
|
|
100
|
+
minute: "min",
|
|
101
|
+
second: "s",
|
|
102
|
+
},
|
|
59
103
|
},
|
|
60
104
|
};
|
|
61
|
-
|
|
105
|
+
// options: format -> 'verbose' | 'compact', minimal -> show only largest non-zero unit
|
|
106
|
+
export function parseTimeSpent(initialDate, finalDate, locale = "pt-BR", // Default to Portuguese for backward compatibility
|
|
107
|
+
options) {
|
|
108
|
+
var _a, _b;
|
|
62
109
|
const final = fixTimezoneOffset(finalDate);
|
|
63
110
|
const initial = fixTimezoneOffset(initialDate);
|
|
64
111
|
const years = final.diff(initial, "year");
|
|
65
112
|
const months = final.diff(initial.add(years, "year"), "month");
|
|
66
113
|
const days = final.diff(initial.add(years, "year").add(months, "month"), "day");
|
|
114
|
+
const hours = final.diff(initial.add(years, "year").add(months, "month").add(days, "day"), "hour");
|
|
115
|
+
const minutes = final.diff(initial
|
|
116
|
+
.add(years, "year")
|
|
117
|
+
.add(months, "month")
|
|
118
|
+
.add(days, "day")
|
|
119
|
+
.add(hours, "hour"), "minute");
|
|
120
|
+
const seconds = final.diff(initial
|
|
121
|
+
.add(years, "year")
|
|
122
|
+
.add(months, "month")
|
|
123
|
+
.add(days, "day")
|
|
124
|
+
.add(hours, "hour")
|
|
125
|
+
.add(minutes, "minute"), "second");
|
|
67
126
|
// Use default locale if the requested one isn't supported
|
|
68
127
|
const translations = locales[locale] || locales["pt-BR"];
|
|
69
|
-
//
|
|
70
|
-
const
|
|
128
|
+
// Build verbose parts (localized words)
|
|
129
|
+
const verboseParts = [];
|
|
71
130
|
if (years > 0) {
|
|
72
131
|
const unit = years === 1 ? translations.year.singular : translations.year.plural;
|
|
73
|
-
|
|
132
|
+
verboseParts.push(`${years} ${unit}`);
|
|
74
133
|
}
|
|
75
134
|
if (months > 0) {
|
|
76
135
|
const unit = months === 1 ? translations.month.singular : translations.month.plural;
|
|
77
|
-
|
|
136
|
+
verboseParts.push(`${months} ${unit}`);
|
|
78
137
|
}
|
|
79
138
|
if (days > 0) {
|
|
80
139
|
const unit = days === 1 ? translations.day.singular : translations.day.plural;
|
|
81
|
-
|
|
140
|
+
verboseParts.push(`${days} ${unit}`);
|
|
141
|
+
}
|
|
142
|
+
if (hours > 0) {
|
|
143
|
+
const unit = hours === 1 ? translations.hour.singular : translations.hour.plural;
|
|
144
|
+
verboseParts.push(`${hours} ${unit}`);
|
|
145
|
+
}
|
|
146
|
+
if (minutes > 0) {
|
|
147
|
+
const unit = minutes === 1 ? translations.minute.singular : translations.minute.plural;
|
|
148
|
+
verboseParts.push(`${minutes} ${unit}`);
|
|
149
|
+
}
|
|
150
|
+
if (seconds > 0) {
|
|
151
|
+
const unit = seconds === 1 ? translations.second.singular : translations.second.plural;
|
|
152
|
+
verboseParts.push(`${seconds} ${unit}`);
|
|
153
|
+
}
|
|
154
|
+
// Build compact parts (e.g. "1a2m3d")
|
|
155
|
+
const compactPieces = [];
|
|
156
|
+
if (years > 0)
|
|
157
|
+
compactPieces.push(`${years}${translations.compactTokens.year}`);
|
|
158
|
+
if (months > 0)
|
|
159
|
+
compactPieces.push(`${months}${translations.compactTokens.month}`);
|
|
160
|
+
if (days > 0)
|
|
161
|
+
compactPieces.push(`${days}${translations.compactTokens.day}`);
|
|
162
|
+
if (hours > 0)
|
|
163
|
+
compactPieces.push(`${hours}${translations.compactTokens.hour}`);
|
|
164
|
+
if (minutes > 0)
|
|
165
|
+
compactPieces.push(`${minutes}${translations.compactTokens.minute}`);
|
|
166
|
+
if (seconds > 0)
|
|
167
|
+
compactPieces.push(`${seconds}${translations.compactTokens.second}`);
|
|
168
|
+
const format = (_a = options === null || options === void 0 ? void 0 : options.format) !== null && _a !== void 0 ? _a : "verbose";
|
|
169
|
+
const minimal = (_b = options === null || options === void 0 ? void 0 : options.minimal) !== null && _b !== void 0 ? _b : false;
|
|
170
|
+
let formatted = "";
|
|
171
|
+
if (format === "compact") {
|
|
172
|
+
if (compactPieces.length === 0) {
|
|
173
|
+
formatted = "";
|
|
174
|
+
}
|
|
175
|
+
else if (minimal) {
|
|
176
|
+
formatted = compactPieces[0];
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
formatted = compactPieces.join("");
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
// verbose
|
|
184
|
+
if (verboseParts.length === 0) {
|
|
185
|
+
formatted = "";
|
|
186
|
+
}
|
|
187
|
+
else if (minimal) {
|
|
188
|
+
formatted = verboseParts[0];
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
formatted = translations.formatList(verboseParts);
|
|
192
|
+
}
|
|
82
193
|
}
|
|
83
|
-
|
|
84
|
-
|
|
194
|
+
return {
|
|
195
|
+
years,
|
|
196
|
+
months,
|
|
197
|
+
days,
|
|
198
|
+
hours,
|
|
199
|
+
minutes,
|
|
200
|
+
seconds,
|
|
201
|
+
formatted,
|
|
202
|
+
};
|
|
85
203
|
}
|
|
@@ -5,6 +5,9 @@ const locales = {
|
|
|
5
5
|
year: { singular: "ano", plural: "anos" },
|
|
6
6
|
month: { singular: "mês", plural: "meses" },
|
|
7
7
|
day: { singular: "dia", plural: "dias" },
|
|
8
|
+
hour: { singular: "hora", plural: "horas" },
|
|
9
|
+
minute: { singular: "minuto", plural: "minutos" },
|
|
10
|
+
second: { singular: "segundo", plural: "segundos" },
|
|
8
11
|
formatList: (parts) => {
|
|
9
12
|
if (parts.length === 0)
|
|
10
13
|
return "";
|
|
@@ -14,11 +17,22 @@ const locales = {
|
|
|
14
17
|
const otherParts = parts.slice(0, -1);
|
|
15
18
|
return `${otherParts.join(", ")} e ${lastPart}`;
|
|
16
19
|
},
|
|
20
|
+
compactTokens: {
|
|
21
|
+
year: "a",
|
|
22
|
+
month: "m",
|
|
23
|
+
day: "d",
|
|
24
|
+
hour: "h",
|
|
25
|
+
minute: "min",
|
|
26
|
+
second: "s",
|
|
27
|
+
},
|
|
17
28
|
},
|
|
18
29
|
"en-US": {
|
|
19
30
|
year: { singular: "year", plural: "years" },
|
|
20
31
|
month: { singular: "month", plural: "months" },
|
|
21
32
|
day: { singular: "day", plural: "days" },
|
|
33
|
+
hour: { singular: "hour", plural: "hours" },
|
|
34
|
+
minute: { singular: "minute", plural: "minutes" },
|
|
35
|
+
second: { singular: "second", plural: "seconds" },
|
|
22
36
|
formatList: (parts) => {
|
|
23
37
|
if (parts.length === 0)
|
|
24
38
|
return "";
|
|
@@ -28,11 +42,22 @@ const locales = {
|
|
|
28
42
|
return `${parts[0]} and ${parts[1]}`;
|
|
29
43
|
return `${parts.slice(0, -1).join(", ")}, and ${parts[parts.length - 1]}`;
|
|
30
44
|
},
|
|
45
|
+
compactTokens: {
|
|
46
|
+
year: "y",
|
|
47
|
+
month: "mo",
|
|
48
|
+
day: "d",
|
|
49
|
+
hour: "h",
|
|
50
|
+
minute: "m",
|
|
51
|
+
second: "s",
|
|
52
|
+
},
|
|
31
53
|
},
|
|
32
54
|
"es-ES": {
|
|
33
55
|
year: { singular: "año", plural: "años" },
|
|
34
56
|
month: { singular: "mes", plural: "meses" },
|
|
35
57
|
day: { singular: "día", plural: "días" },
|
|
58
|
+
hour: { singular: "hora", plural: "horas" },
|
|
59
|
+
minute: { singular: "minuto", plural: "minutos" },
|
|
60
|
+
second: { singular: "segundo", plural: "segundos" },
|
|
36
61
|
formatList: (parts) => {
|
|
37
62
|
if (parts.length === 0)
|
|
38
63
|
return "";
|
|
@@ -42,11 +67,22 @@ const locales = {
|
|
|
42
67
|
const otherParts = parts.slice(0, -1);
|
|
43
68
|
return `${otherParts.join(", ")} y ${lastPart}`;
|
|
44
69
|
},
|
|
70
|
+
compactTokens: {
|
|
71
|
+
year: "a",
|
|
72
|
+
month: "mes",
|
|
73
|
+
day: "d",
|
|
74
|
+
hour: "h",
|
|
75
|
+
minute: "min",
|
|
76
|
+
second: "s",
|
|
77
|
+
},
|
|
45
78
|
},
|
|
46
79
|
"fr-FR": {
|
|
47
80
|
year: { singular: "an", plural: "ans" },
|
|
48
81
|
month: { singular: "mois", plural: "mois" },
|
|
49
82
|
day: { singular: "jour", plural: "jours" },
|
|
83
|
+
hour: { singular: "heure", plural: "heures" },
|
|
84
|
+
minute: { singular: "minute", plural: "minutes" },
|
|
85
|
+
second: { singular: "seconde", plural: "secondes" },
|
|
50
86
|
formatList: (parts) => {
|
|
51
87
|
if (parts.length === 0)
|
|
52
88
|
return "";
|
|
@@ -56,30 +92,112 @@ const locales = {
|
|
|
56
92
|
const otherParts = parts.slice(0, -1);
|
|
57
93
|
return `${otherParts.join(", ")} et ${lastPart}`;
|
|
58
94
|
},
|
|
95
|
+
compactTokens: {
|
|
96
|
+
year: "a",
|
|
97
|
+
month: "m",
|
|
98
|
+
day: "j",
|
|
99
|
+
hour: "h",
|
|
100
|
+
minute: "min",
|
|
101
|
+
second: "s",
|
|
102
|
+
},
|
|
59
103
|
},
|
|
60
104
|
};
|
|
61
|
-
|
|
105
|
+
// options: format -> 'verbose' | 'compact', minimal -> show only largest non-zero unit
|
|
106
|
+
export function parseTimeSpent(initialDate, finalDate, locale = "pt-BR", // Default to Portuguese for backward compatibility
|
|
107
|
+
options) {
|
|
108
|
+
var _a, _b;
|
|
62
109
|
const final = fixTimezoneOffset(finalDate);
|
|
63
110
|
const initial = fixTimezoneOffset(initialDate);
|
|
64
111
|
const years = final.diff(initial, "year");
|
|
65
112
|
const months = final.diff(initial.add(years, "year"), "month");
|
|
66
113
|
const days = final.diff(initial.add(years, "year").add(months, "month"), "day");
|
|
114
|
+
const hours = final.diff(initial.add(years, "year").add(months, "month").add(days, "day"), "hour");
|
|
115
|
+
const minutes = final.diff(initial
|
|
116
|
+
.add(years, "year")
|
|
117
|
+
.add(months, "month")
|
|
118
|
+
.add(days, "day")
|
|
119
|
+
.add(hours, "hour"), "minute");
|
|
120
|
+
const seconds = final.diff(initial
|
|
121
|
+
.add(years, "year")
|
|
122
|
+
.add(months, "month")
|
|
123
|
+
.add(days, "day")
|
|
124
|
+
.add(hours, "hour")
|
|
125
|
+
.add(minutes, "minute"), "second");
|
|
67
126
|
// Use default locale if the requested one isn't supported
|
|
68
127
|
const translations = locales[locale] || locales["pt-BR"];
|
|
69
|
-
//
|
|
70
|
-
const
|
|
128
|
+
// Build verbose parts (localized words)
|
|
129
|
+
const verboseParts = [];
|
|
71
130
|
if (years > 0) {
|
|
72
131
|
const unit = years === 1 ? translations.year.singular : translations.year.plural;
|
|
73
|
-
|
|
132
|
+
verboseParts.push(`${years} ${unit}`);
|
|
74
133
|
}
|
|
75
134
|
if (months > 0) {
|
|
76
135
|
const unit = months === 1 ? translations.month.singular : translations.month.plural;
|
|
77
|
-
|
|
136
|
+
verboseParts.push(`${months} ${unit}`);
|
|
78
137
|
}
|
|
79
138
|
if (days > 0) {
|
|
80
139
|
const unit = days === 1 ? translations.day.singular : translations.day.plural;
|
|
81
|
-
|
|
140
|
+
verboseParts.push(`${days} ${unit}`);
|
|
141
|
+
}
|
|
142
|
+
if (hours > 0) {
|
|
143
|
+
const unit = hours === 1 ? translations.hour.singular : translations.hour.plural;
|
|
144
|
+
verboseParts.push(`${hours} ${unit}`);
|
|
145
|
+
}
|
|
146
|
+
if (minutes > 0) {
|
|
147
|
+
const unit = minutes === 1 ? translations.minute.singular : translations.minute.plural;
|
|
148
|
+
verboseParts.push(`${minutes} ${unit}`);
|
|
149
|
+
}
|
|
150
|
+
if (seconds > 0) {
|
|
151
|
+
const unit = seconds === 1 ? translations.second.singular : translations.second.plural;
|
|
152
|
+
verboseParts.push(`${seconds} ${unit}`);
|
|
153
|
+
}
|
|
154
|
+
// Build compact parts (e.g. "1a2m3d")
|
|
155
|
+
const compactPieces = [];
|
|
156
|
+
if (years > 0)
|
|
157
|
+
compactPieces.push(`${years}${translations.compactTokens.year}`);
|
|
158
|
+
if (months > 0)
|
|
159
|
+
compactPieces.push(`${months}${translations.compactTokens.month}`);
|
|
160
|
+
if (days > 0)
|
|
161
|
+
compactPieces.push(`${days}${translations.compactTokens.day}`);
|
|
162
|
+
if (hours > 0)
|
|
163
|
+
compactPieces.push(`${hours}${translations.compactTokens.hour}`);
|
|
164
|
+
if (minutes > 0)
|
|
165
|
+
compactPieces.push(`${minutes}${translations.compactTokens.minute}`);
|
|
166
|
+
if (seconds > 0)
|
|
167
|
+
compactPieces.push(`${seconds}${translations.compactTokens.second}`);
|
|
168
|
+
const format = (_a = options === null || options === void 0 ? void 0 : options.format) !== null && _a !== void 0 ? _a : "verbose";
|
|
169
|
+
const minimal = (_b = options === null || options === void 0 ? void 0 : options.minimal) !== null && _b !== void 0 ? _b : false;
|
|
170
|
+
let formatted = "";
|
|
171
|
+
if (format === "compact") {
|
|
172
|
+
if (compactPieces.length === 0) {
|
|
173
|
+
formatted = "";
|
|
174
|
+
}
|
|
175
|
+
else if (minimal) {
|
|
176
|
+
formatted = compactPieces[0];
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
formatted = compactPieces.join("");
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
// verbose
|
|
184
|
+
if (verboseParts.length === 0) {
|
|
185
|
+
formatted = "";
|
|
186
|
+
}
|
|
187
|
+
else if (minimal) {
|
|
188
|
+
formatted = verboseParts[0];
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
formatted = translations.formatList(verboseParts);
|
|
192
|
+
}
|
|
82
193
|
}
|
|
83
|
-
|
|
84
|
-
|
|
194
|
+
return {
|
|
195
|
+
years,
|
|
196
|
+
months,
|
|
197
|
+
days,
|
|
198
|
+
hours,
|
|
199
|
+
minutes,
|
|
200
|
+
seconds,
|
|
201
|
+
formatted,
|
|
202
|
+
};
|
|
85
203
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse-time-spent.js","sourceRoot":"","sources":["../../src/date/parse-time-spent.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"parse-time-spent.js","sourceRoot":"","sources":["../../src/date/parse-time-spent.ts"],"names":[],"mappings":";;AAsIA,wCAuHC;AA7PD,+DAAyD;AA2BzD,2CAA2C;AAC3C,MAAM,OAAO,GAAwC;IACnD,OAAO,EAAE;QACP,IAAI,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE;QACzC,KAAK,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE;QAC3C,GAAG,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE;QACxC,IAAI,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE;QAC3C,MAAM,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE;QACjD,MAAM,EAAE,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE;QACnD,UAAU,EAAE,CAAC,KAAK,EAAE,EAAE;YACpB,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO,EAAE,CAAA;YACjC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO,KAAK,CAAC,CAAC,CAAC,CAAA;YACvC,MAAM,QAAQ,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;YACxC,MAAM,UAAU,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;YACrC,OAAO,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,QAAQ,EAAE,CAAA;QACjD,CAAC;QACD,aAAa,EAAE;YACb,IAAI,EAAE,GAAG;YACT,KAAK,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MAAM,mCAAI,SAAS,CAAA;IAC3C,MAAM,OAAO,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,mCAAI,KAAK,CAAA;IAEzC,IAAI,SAAS,GAAG,EAAE,CAAA;IAClB,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,IAAI,aAAa,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC/B,SAAS,GAAG,EAAE,CAAA;QAChB,CAAC;aAAM,IAAI,OAAO,EAAE,CAAC;YACnB,SAAS,GAAG,aAAa,CAAC,CAAC,CAAC,CAAA;QAC9B,CAAC;aAAM,CAAC;YACN,SAAS,GAAG,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;QACpC,CAAC;IACH,CAAC;SAAM,CAAC;QACN,UAAU;QACV,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC9B,SAAS,GAAG,EAAE,CAAA;QAChB,CAAC;aAAM,IAAI,OAAO,EAAE,CAAC;YACnB,SAAS,GAAG,YAAY,CAAC,CAAC,CAAC,CAAA;QAC7B,CAAC;aAAM,CAAC;YACN,SAAS,GAAG,YAAY,CAAC,UAAU,CAAC,YAAY,CAAC,CAAA;QACnD,CAAC;IACH,CAAC;IAED,OAAO;QACL,KAAK;QACL,MAAM;QACN,IAAI;QACJ,KAAK;QACL,OAAO;QACP,OAAO;QACP,SAAS;KACV,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-youtube-thumbnail.d.ts","sourceRoot":"","sources":["../../src/youtube/get-youtube-thumbnail.ts"],"names":[],"mappings":"AAAA,wBAAsB,mBAAmB,CACvC,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAgCxB"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export async function getYoutubeThumbnail(videoUrl) {
|
|
2
|
+
const { extractYouTubeId } = await import("./extract-youtube-video-id");
|
|
3
|
+
const videoId = extractYouTubeId(videoUrl);
|
|
4
|
+
if (!videoId)
|
|
5
|
+
return null;
|
|
6
|
+
// Try loading YouTube thumbnail images — avoids CORS problems because
|
|
7
|
+
// creating an Image and listening for load/error is not blocked by CORS.
|
|
8
|
+
const thumbs = [
|
|
9
|
+
`https://img.youtube.com/vi/${videoId}/maxresdefault.jpg`,
|
|
10
|
+
`https://img.youtube.com/vi/${videoId}/hqdefault.jpg`,
|
|
11
|
+
`https://img.youtube.com/vi/${videoId}/mqdefault.jpg`,
|
|
12
|
+
`https://img.youtube.com/vi/${videoId}/default.jpg`,
|
|
13
|
+
];
|
|
14
|
+
const loadImage = (src) => new Promise((resolve) => {
|
|
15
|
+
const img = new Image();
|
|
16
|
+
img.onload = () => resolve(true);
|
|
17
|
+
img.onerror = () => resolve(false);
|
|
18
|
+
img.src = src;
|
|
19
|
+
});
|
|
20
|
+
for (const url of thumbs) {
|
|
21
|
+
try {
|
|
22
|
+
const ok = await loadImage(url);
|
|
23
|
+
if (ok)
|
|
24
|
+
return url;
|
|
25
|
+
}
|
|
26
|
+
catch (_a) {
|
|
27
|
+
// ignore and try next thumbnail
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export async function getYoutubeThumbnail(videoUrl) {
|
|
2
|
+
const { extractYouTubeId } = await import("./extract-youtube-video-id");
|
|
3
|
+
const videoId = extractYouTubeId(videoUrl);
|
|
4
|
+
if (!videoId)
|
|
5
|
+
return null;
|
|
6
|
+
// Try loading YouTube thumbnail images — avoids CORS problems because
|
|
7
|
+
// creating an Image and listening for load/error is not blocked by CORS.
|
|
8
|
+
const thumbs = [
|
|
9
|
+
`https://img.youtube.com/vi/${videoId}/maxresdefault.jpg`,
|
|
10
|
+
`https://img.youtube.com/vi/${videoId}/hqdefault.jpg`,
|
|
11
|
+
`https://img.youtube.com/vi/${videoId}/mqdefault.jpg`,
|
|
12
|
+
`https://img.youtube.com/vi/${videoId}/default.jpg`,
|
|
13
|
+
];
|
|
14
|
+
const loadImage = (src) => new Promise((resolve) => {
|
|
15
|
+
const img = new Image();
|
|
16
|
+
img.onload = () => resolve(true);
|
|
17
|
+
img.onerror = () => resolve(false);
|
|
18
|
+
img.src = src;
|
|
19
|
+
});
|
|
20
|
+
for (const url of thumbs) {
|
|
21
|
+
try {
|
|
22
|
+
const ok = await loadImage(url);
|
|
23
|
+
if (ok)
|
|
24
|
+
return url;
|
|
25
|
+
}
|
|
26
|
+
catch (_a) {
|
|
27
|
+
// ignore and try next thumbnail
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-youtube-thumbnail.js","sourceRoot":"","sources":["../../src/youtube/get-youtube-thumbnail.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAkCC;AAlCM,KAAK,UAAU,mBAAmB,CACvC,QAAgB;IAEhB,MAAM,EAAE,gBAAgB,EAAE,GAAG,wDAAa,4BAA4B,GAAC,CAAA;IACvE,MAAM,OAAO,GAAG,gBAAgB,CAAC,QAAQ,CAAC,CAAA;IAC1C,IAAI,CAAC,OAAO;QAAE,OAAO,IAAI,CAAA;IAEzB,sEAAsE;IACtE,yEAAyE;IACzE,MAAM,MAAM,GAAG;QACb,8BAA8B,OAAO,oBAAoB;QACzD,8BAA8B,OAAO,gBAAgB;QACrD,8BAA8B,OAAO,gBAAgB;QACrD,8BAA8B,OAAO,cAAc;KACpD,CAAA;IAED,MAAM,SAAS,GAAG,CAAC,GAAW,EAAE,EAAE,CAChC,IAAI,OAAO,CAAU,CAAC,OAAO,EAAE,EAAE;QAC/B,MAAM,GAAG,GAAG,IAAI,KAAK,EAAE,CAAA;QACvB,GAAG,CAAC,MAAM,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QAChC,GAAG,CAAC,OAAO,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QAClC,GAAG,CAAC,GAAG,GAAG,GAAG,CAAA;IACf,CAAC,CAAC,CAAA;IAEJ,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;QACzB,IAAI,CAAC;YACH,MAAM,EAAE,GAAG,MAAM,SAAS,CAAC,GAAG,CAAC,CAAA;YAC/B,IAAI,EAAE;gBAAE,OAAO,GAAG,CAAA;QACpB,CAAC;QAAC,WAAM,CAAC;YACP,gCAAgC;QAClC,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAA;AACb,CAAC"}
|
package/dist/youtube/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/youtube/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAA;AAC1C,cAAc,wBAAwB,CAAA;AACtC,cAAc,0BAA0B,CAAA;AACxC,cAAc,yBAAyB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/youtube/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAA;AAC1C,cAAc,wBAAwB,CAAA;AACtC,cAAc,yBAAyB,CAAA;AACvC,cAAc,0BAA0B,CAAA;AACxC,cAAc,yBAAyB,CAAA"}
|
package/dist/youtube/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/youtube/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6DAA0C;AAC1C,yDAAsC;AACtC,2DAAwC;AACxC,0DAAuC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/youtube/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6DAA0C;AAC1C,yDAAsC;AACtC,0DAAuC;AACvC,2DAAwC;AACxC,0DAAuC"}
|
package/package.json
CHANGED