@devexperts/dxcharts-lite
Version:
48 lines (47 loc) • 1.94 kB
JavaScript
/*
* Copyright (C) 2019 - 2025 Devexperts Solutions IE Limited
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { binarySearch, firstOf, lastOf } from './array.utils';
import { floor } from './math.utils';
export const searchCandleIndex = (timestamp, shouldExtrapolate, candles, periodMs = 1000) => {
var _a, _b, _c, _d;
const firstTimestamp = (_b = (_a = firstOf(candles)) === null || _a === void 0 ? void 0 : _a.timestamp) !== null && _b !== void 0 ? _b : 0;
const lastTimestamp = (_d = (_c = lastOf(candles)) === null || _c === void 0 ? void 0 : _c.timestamp) !== null && _d !== void 0 ? _d : 0;
if (timestamp > lastTimestamp) {
// TODO rework the code below, it looks very very sus ( ≖.≖)
if (shouldExtrapolate) {
// div by 1000 because periodDuration is in seconds
return {
// -1 to skip fake candle and get the last existing/visible one if reached last index
index: candles.length - 1 + Math.ceil((timestamp - lastTimestamp) / periodMs),
exact: true,
};
}
else {
return {
// -1 to skip fake candle and get the last existing/visible one if reached last index
index: candles.length - 1,
exact: true,
};
}
}
else if (timestamp < firstTimestamp) {
if (shouldExtrapolate) {
return {
index: floor((timestamp - firstTimestamp) / periodMs),
exact: true,
};
}
else {
return {
index: -1,
exact: true,
};
}
}
else {
return binarySearch(candles, timestamp, candle => candle.timestamp);
}
};