mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 10:49:28 +02:00
Refactor video links building
This commit is contained in:
parent
764b1a14fc
commit
9162fdd363
22 changed files with 136 additions and 67 deletions
51
shared/core-utils/common/miscs.ts
Normal file
51
shared/core-utils/common/miscs.ts
Normal file
|
@ -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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue