24 lines
798 B
TypeScript
24 lines
798 B
TypeScript
/**
|
|
* Convert a TradingView interval string to seconds.
|
|
* Plain numbers are minutes (TradingView convention), suffixed forms use the suffix.
|
|
* Examples: "15" → 900, "1D" → 86400, "1W" → 604800
|
|
*/
|
|
export function intervalToSeconds(interval: string): number {
|
|
const numericInterval = parseInt(interval)
|
|
if (!isNaN(numericInterval) && interval === numericInterval.toString()) {
|
|
return numericInterval * 60 // plain number = minutes
|
|
}
|
|
const match = interval.match(/^(\d+)([SMHDW])$/)
|
|
if (match) {
|
|
const value = parseInt(match[1])
|
|
switch (match[2]) {
|
|
case 'S': return value
|
|
case 'M': return value * 60
|
|
case 'H': return value * 3600
|
|
case 'D': return value * 86400
|
|
case 'W': return value * 604800
|
|
}
|
|
}
|
|
return 60 // fallback: 1 minute
|
|
}
|