From 9162fdd36300d2478f13d6ad346ec2c323f40faa Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 26 Jul 2021 14:12:39 +0200 Subject: Refactor video links building --- shared/core-utils/common/date.ts | 67 ++++++++++++++++++++++++++++++++++++++ shared/core-utils/common/index.ts | 5 +++ shared/core-utils/common/miscs.ts | 51 +++++++++++++++++++++++++++++ shared/core-utils/common/regexp.ts | 5 +++ shared/core-utils/common/types.ts | 45 +++++++++++++++++++++++++ shared/core-utils/common/url.ts | 0 shared/core-utils/index.ts | 2 +- shared/core-utils/miscs/date.ts | 67 -------------------------------------- shared/core-utils/miscs/index.ts | 4 --- shared/core-utils/miscs/miscs.ts | 51 ----------------------------- shared/core-utils/miscs/regexp.ts | 5 --- shared/core-utils/miscs/types.ts | 45 ------------------------- 12 files changed, 174 insertions(+), 173 deletions(-) create mode 100644 shared/core-utils/common/date.ts create mode 100644 shared/core-utils/common/index.ts create mode 100644 shared/core-utils/common/miscs.ts create mode 100644 shared/core-utils/common/regexp.ts create mode 100644 shared/core-utils/common/types.ts create mode 100644 shared/core-utils/common/url.ts delete mode 100644 shared/core-utils/miscs/date.ts delete mode 100644 shared/core-utils/miscs/index.ts delete mode 100644 shared/core-utils/miscs/miscs.ts delete mode 100644 shared/core-utils/miscs/regexp.ts delete mode 100644 shared/core-utils/miscs/types.ts (limited to 'shared') 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 @@ +function isToday (d: Date) { + const today = new Date() + + return areDatesEqual(d, today) +} + +function isYesterday (d: Date) { + const yesterday = new Date() + yesterday.setDate(yesterday.getDate() - 1) + + return areDatesEqual(d, yesterday) +} + +function isThisWeek (d: Date) { + const minDateOfThisWeek = new Date() + minDateOfThisWeek.setHours(0, 0, 0) + + // getDay() -> Sunday - Saturday : 0 - 6 + // We want to start our week on Monday + let dayOfWeek = minDateOfThisWeek.getDay() - 1 + if (dayOfWeek < 0) dayOfWeek = 6 // Sunday + + minDateOfThisWeek.setDate(minDateOfThisWeek.getDate() - dayOfWeek) + + return d >= minDateOfThisWeek +} + +function isThisMonth (d: Date) { + const thisMonth = new Date().getMonth() + + return d.getMonth() === thisMonth +} + +function isLastMonth (d: Date) { + const now = new Date() + + return getDaysDifferences(now, d) <= 30 +} + +function isLastWeek (d: Date) { + const now = new Date() + + return getDaysDifferences(now, d) <= 7 +} + +// --------------------------------------------------------------------------- + +export { + isYesterday, + isThisWeek, + isThisMonth, + isToday, + isLastMonth, + isLastWeek +} + +// --------------------------------------------------------------------------- + +function areDatesEqual (d1: Date, d2: Date) { + return d1.getFullYear() === d2.getFullYear() && + d1.getMonth() === d2.getMonth() && + d1.getDate() === d2.getDate() +} + +function getDaysDifferences (d1: Date, d2: Date) { + return (d1.getTime() - d2.getTime()) / (86400000) +} 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 @@ +export * from './date' +export * from './miscs' +export * from './regexp' +export * from './types' +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 @@ +// high excluded +function randomInt (low: number, high: number) { + return Math.floor(Math.random() * (high - low) + low) +} + +// Thanks https://stackoverflow.com/a/16187766 +function compareSemVer (a: string, b: string) { + const regExStrip0 = /(\.0+)+$/ + const segmentsA = a.replace(regExStrip0, '').split('.') + const segmentsB = b.replace(regExStrip0, '').split('.') + + const l = Math.min(segmentsA.length, segmentsB.length) + + for (let i = 0; i < l; i++) { + const diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10) + + if (diff) return diff + } + + return segmentsA.length - segmentsB.length +} + +function isPromise (value: any) { + return value && typeof value.then === 'function' +} + +function isCatchable (value: any) { + return value && typeof value.catch === 'function' +} + +function sortObjectComparator (key: string, order: 'asc' | 'desc') { + return (a: any, b: any) => { + if (a[key] < b[key]) { + return order === 'asc' ? -1 : 1 + } + + if (a[key] > b[key]) { + return order === 'asc' ? 1 : -1 + } + + return 0 + } +} + +export { + randomInt, + compareSemVer, + isPromise, + isCatchable, + sortObjectComparator +} 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 @@ +export const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' + +export function removeFragmentedMP4Ext (path: string) { + return path.replace(/-fragmented.mp4$/i, '') +} 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 @@ +/* eslint-disable @typescript-eslint/array-type */ + +export type FunctionPropertyNames = { + [K in keyof T]: T[K] extends Function ? K : never +}[keyof T] + +export type FunctionProperties = Pick> + +export type AttributesOnly = { + [K in keyof T]: T[K] extends Function ? never : T[K] +} + +export type PickWith = { + [P in KT]: T[P] extends V ? V : never +} + +export type PickWithOpt = { + [P in KT]?: T[P] extends V ? V : never +} + +// https://github.com/krzkaczor/ts-essentials Rocks! +export type DeepPartial = { + [P in keyof T]?: T[P] extends Array + ? Array> + : T[P] extends ReadonlyArray + ? ReadonlyArray> + : DeepPartial +} + +type Primitive = string | Function | number | boolean | Symbol | undefined | null +export type DeepOmitHelper = { + [P in K]: // extra level of indirection needed to trigger homomorhic behavior + T[P] extends infer TP // distribute over unions + ? TP extends Primitive + ? TP // leave primitives and functions alone + : TP extends any[] + ? DeepOmitArray // Array special handling + : DeepOmit + : never +} +export type DeepOmit = T extends Primitive ? T : DeepOmitHelper> + +export type DeepOmitArray = { + [P in keyof T]: DeepOmit +} diff --git a/shared/core-utils/common/url.ts b/shared/core-utils/common/url.ts new file mode 100644 index 000000000..e69de29bb diff --git a/shared/core-utils/index.ts b/shared/core-utils/index.ts index 42d7cab1d..0b05dc9eb 100644 --- a/shared/core-utils/index.ts +++ b/shared/core-utils/index.ts @@ -1,7 +1,7 @@ export * from './abuse' +export * from './common' export * from './i18n' export * from './logs' -export * from './miscs' export * from './plugins' export * from './renderer' export * from './users' diff --git a/shared/core-utils/miscs/date.ts b/shared/core-utils/miscs/date.ts deleted file mode 100644 index 4f92f758f..000000000 --- a/shared/core-utils/miscs/date.ts +++ /dev/null @@ -1,67 +0,0 @@ -function isToday (d: Date) { - const today = new Date() - - return areDatesEqual(d, today) -} - -function isYesterday (d: Date) { - const yesterday = new Date() - yesterday.setDate(yesterday.getDate() - 1) - - return areDatesEqual(d, yesterday) -} - -function isThisWeek (d: Date) { - const minDateOfThisWeek = new Date() - minDateOfThisWeek.setHours(0, 0, 0) - - // getDay() -> Sunday - Saturday : 0 - 6 - // We want to start our week on Monday - let dayOfWeek = minDateOfThisWeek.getDay() - 1 - if (dayOfWeek < 0) dayOfWeek = 6 // Sunday - - minDateOfThisWeek.setDate(minDateOfThisWeek.getDate() - dayOfWeek) - - return d >= minDateOfThisWeek -} - -function isThisMonth (d: Date) { - const thisMonth = new Date().getMonth() - - return d.getMonth() === thisMonth -} - -function isLastMonth (d: Date) { - const now = new Date() - - return getDaysDifferences(now, d) <= 30 -} - -function isLastWeek (d: Date) { - const now = new Date() - - return getDaysDifferences(now, d) <= 7 -} - -// --------------------------------------------------------------------------- - -export { - isYesterday, - isThisWeek, - isThisMonth, - isToday, - isLastMonth, - isLastWeek -} - -// --------------------------------------------------------------------------- - -function areDatesEqual (d1: Date, d2: Date) { - return d1.getFullYear() === d2.getFullYear() && - d1.getMonth() === d2.getMonth() && - d1.getDate() === d2.getDate() -} - -function getDaysDifferences (d1: Date, d2: Date) { - return (d1.getTime() - d2.getTime()) / (86400000) -} diff --git a/shared/core-utils/miscs/index.ts b/shared/core-utils/miscs/index.ts deleted file mode 100644 index 7764e69ad..000000000 --- a/shared/core-utils/miscs/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from './date' -export * from './miscs' -export * from './types' -export * from './regexp' diff --git a/shared/core-utils/miscs/miscs.ts b/shared/core-utils/miscs/miscs.ts deleted file mode 100644 index 4780ca922..000000000 --- a/shared/core-utils/miscs/miscs.ts +++ /dev/null @@ -1,51 +0,0 @@ -// high excluded -function randomInt (low: number, high: number) { - return Math.floor(Math.random() * (high - low) + low) -} - -// Thanks https://stackoverflow.com/a/16187766 -function compareSemVer (a: string, b: string) { - const regExStrip0 = /(\.0+)+$/ - const segmentsA = a.replace(regExStrip0, '').split('.') - const segmentsB = b.replace(regExStrip0, '').split('.') - - const l = Math.min(segmentsA.length, segmentsB.length) - - for (let i = 0; i < l; i++) { - const diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10) - - if (diff) return diff - } - - return segmentsA.length - segmentsB.length -} - -function isPromise (value: any) { - return value && typeof value.then === 'function' -} - -function isCatchable (value: any) { - return value && typeof value.catch === 'function' -} - -function sortObjectComparator (key: string, order: 'asc' | 'desc') { - return (a: any, b: any) => { - if (a[key] < b[key]) { - return order === 'asc' ? -1 : 1 - } - - if (a[key] > b[key]) { - return order === 'asc' ? 1 : -1 - } - - return 0 - } -} - -export { - randomInt, - compareSemVer, - isPromise, - isCatchable, - sortObjectComparator -} diff --git a/shared/core-utils/miscs/regexp.ts b/shared/core-utils/miscs/regexp.ts deleted file mode 100644 index 59eb87eb6..000000000 --- a/shared/core-utils/miscs/regexp.ts +++ /dev/null @@ -1,5 +0,0 @@ -export const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' - -export function removeFragmentedMP4Ext (path: string) { - return path.replace(/-fragmented.mp4$/i, '') -} diff --git a/shared/core-utils/miscs/types.ts b/shared/core-utils/miscs/types.ts deleted file mode 100644 index bd2a97b98..000000000 --- a/shared/core-utils/miscs/types.ts +++ /dev/null @@ -1,45 +0,0 @@ -/* eslint-disable @typescript-eslint/array-type */ - -export type FunctionPropertyNames = { - [K in keyof T]: T[K] extends Function ? K : never -}[keyof T] - -export type FunctionProperties = Pick> - -export type AttributesOnly = { - [K in keyof T]: T[K] extends Function ? never : T[K] -} - -export type PickWith = { - [P in KT]: T[P] extends V ? V : never -} - -export type PickWithOpt = { - [P in KT]?: T[P] extends V ? V : never -} - -// https://github.com/krzkaczor/ts-essentials Rocks! -export type DeepPartial = { - [P in keyof T]?: T[P] extends Array - ? Array> - : T[P] extends ReadonlyArray - ? ReadonlyArray> - : DeepPartial -} - -type Primitive = string | Function | number | boolean | Symbol | undefined | null -export type DeepOmitHelper = { - [P in K]: // extra level of indirection needed to trigger homomorhic behavior - T[P] extends infer TP // distribute over unions - ? TP extends Primitive - ? TP // leave primitives and functions alone - : TP extends any[] - ? DeepOmitArray // Array special handling - : DeepOmit - : never -} -export type DeepOmit = T extends Primitive ? T : DeepOmitHelper> - -export type DeepOmitArray = { - [P in keyof T]: DeepOmit -} -- cgit v1.2.3