diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-26 14:12:39 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-07-26 14:12:50 +0200 |
commit | 9162fdd36300d2478f13d6ad346ec2c323f40faa (patch) | |
tree | dea0ca43f3ea2fb72a73ca77338f5b7c990acdd7 /shared/core-utils/common | |
parent | 764b1a14fc494f2cfd7ea590d2f07b01df65c7ad (diff) | |
download | PeerTube-9162fdd36300d2478f13d6ad346ec2c323f40faa.tar.gz PeerTube-9162fdd36300d2478f13d6ad346ec2c323f40faa.tar.zst PeerTube-9162fdd36300d2478f13d6ad346ec2c323f40faa.zip |
Refactor video links building
Diffstat (limited to 'shared/core-utils/common')
-rw-r--r-- | shared/core-utils/common/date.ts | 67 | ||||
-rw-r--r-- | shared/core-utils/common/index.ts | 5 | ||||
-rw-r--r-- | shared/core-utils/common/miscs.ts | 51 | ||||
-rw-r--r-- | shared/core-utils/common/regexp.ts | 5 | ||||
-rw-r--r-- | shared/core-utils/common/types.ts | 45 | ||||
-rw-r--r-- | shared/core-utils/common/url.ts | 0 |
6 files changed, 173 insertions, 0 deletions
diff --git a/shared/core-utils/common/date.ts b/shared/core-utils/common/date.ts new file mode 100644 index 000000000..4f92f758f --- /dev/null +++ b/shared/core-utils/common/date.ts | |||
@@ -0,0 +1,67 @@ | |||
1 | function isToday (d: Date) { | ||
2 | const today = new Date() | ||
3 | |||
4 | return areDatesEqual(d, today) | ||
5 | } | ||
6 | |||
7 | function isYesterday (d: Date) { | ||
8 | const yesterday = new Date() | ||
9 | yesterday.setDate(yesterday.getDate() - 1) | ||
10 | |||
11 | return areDatesEqual(d, yesterday) | ||
12 | } | ||
13 | |||
14 | function isThisWeek (d: Date) { | ||
15 | const minDateOfThisWeek = new Date() | ||
16 | minDateOfThisWeek.setHours(0, 0, 0) | ||
17 | |||
18 | // getDay() -> Sunday - Saturday : 0 - 6 | ||
19 | // We want to start our week on Monday | ||
20 | let dayOfWeek = minDateOfThisWeek.getDay() - 1 | ||
21 | if (dayOfWeek < 0) dayOfWeek = 6 // Sunday | ||
22 | |||
23 | minDateOfThisWeek.setDate(minDateOfThisWeek.getDate() - dayOfWeek) | ||
24 | |||
25 | return d >= minDateOfThisWeek | ||
26 | } | ||
27 | |||
28 | function isThisMonth (d: Date) { | ||
29 | const thisMonth = new Date().getMonth() | ||
30 | |||
31 | return d.getMonth() === thisMonth | ||
32 | } | ||
33 | |||
34 | function isLastMonth (d: Date) { | ||
35 | const now = new Date() | ||
36 | |||
37 | return getDaysDifferences(now, d) <= 30 | ||
38 | } | ||
39 | |||
40 | function isLastWeek (d: Date) { | ||
41 | const now = new Date() | ||
42 | |||
43 | return getDaysDifferences(now, d) <= 7 | ||
44 | } | ||
45 | |||
46 | // --------------------------------------------------------------------------- | ||
47 | |||
48 | export { | ||
49 | isYesterday, | ||
50 | isThisWeek, | ||
51 | isThisMonth, | ||
52 | isToday, | ||
53 | isLastMonth, | ||
54 | isLastWeek | ||
55 | } | ||
56 | |||
57 | // --------------------------------------------------------------------------- | ||
58 | |||
59 | function areDatesEqual (d1: Date, d2: Date) { | ||
60 | return d1.getFullYear() === d2.getFullYear() && | ||
61 | d1.getMonth() === d2.getMonth() && | ||
62 | d1.getDate() === d2.getDate() | ||
63 | } | ||
64 | |||
65 | function getDaysDifferences (d1: Date, d2: Date) { | ||
66 | return (d1.getTime() - d2.getTime()) / (86400000) | ||
67 | } | ||
diff --git a/shared/core-utils/common/index.ts b/shared/core-utils/common/index.ts new file mode 100644 index 000000000..83f2ccbb6 --- /dev/null +++ b/shared/core-utils/common/index.ts | |||
@@ -0,0 +1,5 @@ | |||
1 | export * from './date' | ||
2 | export * from './miscs' | ||
3 | export * from './regexp' | ||
4 | export * from './types' | ||
5 | export * from './url | ||
diff --git a/shared/core-utils/common/miscs.ts b/shared/core-utils/common/miscs.ts new file mode 100644 index 000000000..4780ca922 --- /dev/null +++ b/shared/core-utils/common/miscs.ts | |||
@@ -0,0 +1,51 @@ | |||
1 | // high excluded | ||
2 | function randomInt (low: number, high: number) { | ||
3 | return Math.floor(Math.random() * (high - low) + low) | ||
4 | } | ||
5 | |||
6 | // Thanks https://stackoverflow.com/a/16187766 | ||
7 | function compareSemVer (a: string, b: string) { | ||
8 | const regExStrip0 = /(\.0+)+$/ | ||
9 | const segmentsA = a.replace(regExStrip0, '').split('.') | ||
10 | const segmentsB = b.replace(regExStrip0, '').split('.') | ||
11 | |||
12 | const l = Math.min(segmentsA.length, segmentsB.length) | ||
13 | |||
14 | for (let i = 0; i < l; i++) { | ||
15 | const diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10) | ||
16 | |||
17 | if (diff) return diff | ||
18 | } | ||
19 | |||
20 | return segmentsA.length - segmentsB.length | ||
21 | } | ||
22 | |||
23 | function isPromise (value: any) { | ||
24 | return value && typeof value.then === 'function' | ||
25 | } | ||
26 | |||
27 | function isCatchable (value: any) { | ||
28 | return value && typeof value.catch === 'function' | ||
29 | } | ||
30 | |||
31 | function sortObjectComparator (key: string, order: 'asc' | 'desc') { | ||
32 | return (a: any, b: any) => { | ||
33 | if (a[key] < b[key]) { | ||
34 | return order === 'asc' ? -1 : 1 | ||
35 | } | ||
36 | |||
37 | if (a[key] > b[key]) { | ||
38 | return order === 'asc' ? 1 : -1 | ||
39 | } | ||
40 | |||
41 | return 0 | ||
42 | } | ||
43 | } | ||
44 | |||
45 | export { | ||
46 | randomInt, | ||
47 | compareSemVer, | ||
48 | isPromise, | ||
49 | isCatchable, | ||
50 | sortObjectComparator | ||
51 | } | ||
diff --git a/shared/core-utils/common/regexp.ts b/shared/core-utils/common/regexp.ts new file mode 100644 index 000000000..59eb87eb6 --- /dev/null +++ b/shared/core-utils/common/regexp.ts | |||
@@ -0,0 +1,5 @@ | |||
1 | export const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' | ||
2 | |||
3 | export function removeFragmentedMP4Ext (path: string) { | ||
4 | return path.replace(/-fragmented.mp4$/i, '') | ||
5 | } | ||
diff --git a/shared/core-utils/common/types.ts b/shared/core-utils/common/types.ts new file mode 100644 index 000000000..bd2a97b98 --- /dev/null +++ b/shared/core-utils/common/types.ts | |||
@@ -0,0 +1,45 @@ | |||
1 | /* eslint-disable @typescript-eslint/array-type */ | ||
2 | |||
3 | export type FunctionPropertyNames<T> = { | ||
4 | [K in keyof T]: T[K] extends Function ? K : never | ||
5 | }[keyof T] | ||
6 | |||
7 | export type FunctionProperties<T> = Pick<T, FunctionPropertyNames<T>> | ||
8 | |||
9 | export type AttributesOnly<T> = { | ||
10 | [K in keyof T]: T[K] extends Function ? never : T[K] | ||
11 | } | ||
12 | |||
13 | export type PickWith<T, KT extends keyof T, V> = { | ||
14 | [P in KT]: T[P] extends V ? V : never | ||
15 | } | ||
16 | |||
17 | export type PickWithOpt<T, KT extends keyof T, V> = { | ||
18 | [P in KT]?: T[P] extends V ? V : never | ||
19 | } | ||
20 | |||
21 | // https://github.com/krzkaczor/ts-essentials Rocks! | ||
22 | export type DeepPartial<T> = { | ||
23 | [P in keyof T]?: T[P] extends Array<infer U> | ||
24 | ? Array<DeepPartial<U>> | ||
25 | : T[P] extends ReadonlyArray<infer U> | ||
26 | ? ReadonlyArray<DeepPartial<U>> | ||
27 | : DeepPartial<T[P]> | ||
28 | } | ||
29 | |||
30 | type Primitive = string | Function | number | boolean | Symbol | undefined | null | ||
31 | export type DeepOmitHelper<T, K extends keyof T> = { | ||
32 | [P in K]: // extra level of indirection needed to trigger homomorhic behavior | ||
33 | T[P] extends infer TP // distribute over unions | ||
34 | ? TP extends Primitive | ||
35 | ? TP // leave primitives and functions alone | ||
36 | : TP extends any[] | ||
37 | ? DeepOmitArray<TP, K> // Array special handling | ||
38 | : DeepOmit<TP, K> | ||
39 | : never | ||
40 | } | ||
41 | export type DeepOmit<T, K> = T extends Primitive ? T : DeepOmitHelper<T, Exclude<keyof T, K>> | ||
42 | |||
43 | export type DeepOmitArray<T extends any[], K> = { | ||
44 | [P in keyof T]: DeepOmit<T[P], K> | ||
45 | } | ||
diff --git a/shared/core-utils/common/url.ts b/shared/core-utils/common/url.ts new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/shared/core-utils/common/url.ts | |||