deque-typed
v2.6.0
Published
Deque
Maintainers
Keywords
Readme
What
Brief
This is a standalone Deque data structure from the data-structure-typed collection. If you wish to access more data structures or advanced features, you can transition to directly installing the complete data-structure-typed package
How
install
npm
npm i deque-typed --saveyarn
yarn add deque-typedsnippet
Deque as sliding window for stream processing
interface DataPoint {
timestamp: number;
value: number;
sensor: string;
}
// Create a deque-based sliding window for real-time data aggregation
const windowSize = 3;
const dataWindow = new Deque<DataPoint>();
// Simulate incoming sensor data stream
const incomingData: DataPoint[] = [
{ timestamp: 1000, value: 25.5, sensor: 'temp-01' },
{ timestamp: 1100, value: 26.2, sensor: 'temp-01' },
{ timestamp: 1200, value: 25.8, sensor: 'temp-01' },
{ timestamp: 1300, value: 27.1, sensor: 'temp-01' },
{ timestamp: 1400, value: 26.9, sensor: 'temp-01' }
];
const windowResults: Array<{ avgValue: number; windowSize: number }> = [];
for (const dataPoint of incomingData) {
// Add new data to the end
dataWindow.push(dataPoint);
// Remove oldest data when window exceeds size (O(1) from front)
if (dataWindow.length > windowSize) {
dataWindow.shift();
}
// Calculate average of current window
let sum = 0;
for (const point of dataWindow) {
sum += point.value;
}
const avg = sum / dataWindow.length;
windowResults.push({
avgValue: Math.round(avg * 10) / 10,
windowSize: dataWindow.length
});
}
// Verify sliding window behavior
console.log(windowResults.length); // 5;
console.log(windowResults[0].windowSize); // 1; // First window has 1 element
console.log(windowResults[2].windowSize); // 3; // Windows are at max size from 3rd onwards
console.log(windowResults[4].windowSize); // 3; // Last window still has 3 elements
console.log(dataWindow.length); // 3;Convert deque to array
const dq = new Deque<number>([10, 20, 30]);
console.log(dq.toArray()); // [10, 20, 30];API docs & Examples
Examples Repository
