mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-27 02:43:48 +02:00
[WIP] Add majority of logic for HLS reading support
This commit is contained in:
+81
@@ -850,3 +850,84 @@ export const polyfillSymbolDispose = () => {
|
||||
export const isNumber = (x: unknown) => {
|
||||
return typeof x === 'number' && !Number.isNaN(x);
|
||||
};
|
||||
|
||||
export const joinPaths = (basePath: string, relativePath: string) => {
|
||||
// If relativePath is a full URL with protocol, return it as-is
|
||||
if (relativePath.includes('://')) {
|
||||
return relativePath;
|
||||
}
|
||||
|
||||
let result: string;
|
||||
|
||||
if (relativePath.startsWith('/')) {
|
||||
const protocolIndex = basePath.indexOf('://');
|
||||
if (protocolIndex === -1) {
|
||||
result = relativePath;
|
||||
} else {
|
||||
const pathStart = basePath.indexOf('/', protocolIndex + 3);
|
||||
if (pathStart === -1) {
|
||||
result = basePath + relativePath;
|
||||
} else {
|
||||
result = basePath.slice(0, pathStart) + relativePath;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const lastSlash = basePath.lastIndexOf('/');
|
||||
if (lastSlash === -1) {
|
||||
result = relativePath;
|
||||
} else {
|
||||
result = basePath.slice(0, lastSlash + 1) + relativePath;
|
||||
}
|
||||
}
|
||||
|
||||
// Normalize ./ and ../
|
||||
|
||||
let prefix = '';
|
||||
const protocolIndex = result.indexOf('://');
|
||||
if (protocolIndex !== -1) {
|
||||
const pathStart = result.indexOf('/', protocolIndex + 3);
|
||||
if (pathStart !== -1) {
|
||||
prefix = result.slice(0, pathStart);
|
||||
result = result.slice(pathStart);
|
||||
}
|
||||
}
|
||||
|
||||
const segments = result.split('/');
|
||||
const normalized: string[] = [];
|
||||
for (const segment of segments) {
|
||||
if (segment === '..') {
|
||||
normalized.pop();
|
||||
} else if (segment !== '.') {
|
||||
normalized.push(segment);
|
||||
}
|
||||
}
|
||||
|
||||
return prefix + normalized.join('/');
|
||||
};
|
||||
|
||||
export const arrayCount = <T>(array: T[], predicate: (item: T) => boolean) => {
|
||||
let count = 0;
|
||||
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
if (predicate(array[i]!)) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
};
|
||||
|
||||
export const arrayArgmin = <T>(array: T[], getValue: (item: T) => number): number => {
|
||||
let minIndex = -1;
|
||||
let minValue = Infinity;
|
||||
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
const value = getValue(array[i]!);
|
||||
if (value < minValue) {
|
||||
minValue = value;
|
||||
minIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
return minIndex;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user