-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpretty.js
More file actions
142 lines (113 loc) · 3.93 KB
/
pretty.js
File metadata and controls
142 lines (113 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import moment from 'moment';
import { createLogger } from '~/common/log';
export function prettyNumber(number) {
return new Intl.NumberFormat('en-US').format(number);
}
export function prettyShortNumber(number) {
return new Intl.NumberFormat('en-US', { notation: 'compact' }).format(number);
}
export function prettyPrice(number) {
return new Intl.NumberFormat('en-US', {
maximumFractionDigits: 2,
minimumFractionDigits: 2,
}).format(number);
}
export function prettyCurrency(number, currency = 'EUR') {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency,
maximumFractionDigits: 2,
minimumFractionDigits: 2,
}).format(number);
}
export function prettyDuration(seconds) {
const duration = moment.duration(seconds, 'seconds');
const durationHours = `${duration.hours()}`.padStart(2, '0');
const durationMinutes = `${duration.minutes()}`.padStart(2, '0');
const durationSeconds = `${duration.seconds()}`.padStart(2, '0');
if (seconds >= 3600) {
return `${durationHours}:${durationMinutes}:${durationSeconds}`;
}
return `${durationMinutes}:${durationSeconds}`;
}
export function ucfirst(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
export function strLimit(text, count = 25) {
const parts = [...text];
if (parts.length > count) {
return `${parts.slice(0, count).join('')}...`;
}
return text;
}
export function strSlug(text) {
return text
.toString()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.toLowerCase()
.trim()
.replace(/\s+/g, '-')
.replace(/[^\w-]+/g, '')
.replace(/--+/g, '-');
}
export function durationToSeconds(duration) {
let modifiedDuration = duration;
// is in format "00:00", moment assumes that last segment is minutes
if (duration.length === 5) {
modifiedDuration = `00:${duration}`;
}
return moment.duration(modifiedDuration).asSeconds();
}
const log = createLogger('why');
export function why(e) {
let message;
log.debug('error', { type: typeof e, name: e?.name, err: e });
if (typeof e === 'string') {
message = e;
} else if (typeof e === 'object') {
if (e.message) {
message = e.message;
// eslint-disable-next-line no-underscore-dangle
} else if (e.response && (e.response.data || e.response._data)) {
// eslint-disable-next-line no-underscore-dangle
const data = e.response.data || e.response._data;
if (typeof data.errors === 'object' && Object.values(data.errors).length > 0) {
[[message]] = Object.values(data.errors);
} else if (typeof data.messages === 'object' && Object.values(data.messages).length > 0) {
[[message]] = Object.values(data.messages);
} else if (typeof data.error === 'string') {
message = data.error;
} else if (typeof data.message === 'string') {
message = data.message;
}
}
} else {
message = `${e}`;
}
return message;
}
function getVideoUrlWithTimestamp(video, ts) {
if (!ts) {
return video.external_tracking_url;
}
return `${video.external_tracking_url}?t=${ts}`;
}
export function buildReactionFromUrl(reaction) {
if (reaction.from_video) {
return getVideoUrlWithTimestamp(reaction.from_video, reaction.video_seconds_from);
}
if (reaction.from_stream) {
const vodVideo = reaction.from_stream?.vods?.find((v) => !!v)?.video;
if (vodVideo) {
return getVideoUrlWithTimestamp(vodVideo, reaction.vod_seconds_from);
}
}
return reaction.from_info?.service_external_url;
}
export function truncateString(str, countDots = null) {
if (!str || str.length <= 1) {
return str;
}
return str[0] + '.'.repeat(countDots || str.length - 1);
}