-
Notifications
You must be signed in to change notification settings - Fork 79
/
test.js
72 lines (64 loc) · 1.46 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function fillPeriods(arr, { period, from, to }) {
let i = 0
let prevTimestamp = from ? from - period : arr[0].timestamp
let prevPeriodStep = Math.floor(prevTimestamp / period)
let prevItem
const ret = []
while (i < arr.length) {
const item = arr[i]
const periodStep = Math.floor(item.timestamp / period)
if (periodStep - 1 > prevPeriodStep) {
const diff = periodStep - prevPeriodStep
let j = 1
while (j < diff) {
ret.push({
...prevItem,
timestamp: (prevPeriodStep + j) * period
})
j++
}
}
ret.push(item)
if (to && i === arr.length - 1) {
const lastPeriodStep = Math.floor(to / period)
if (lastPeriodStep > periodStep) {
const diff = lastPeriodStep - periodStep
let j = 0
while (j < diff) {
ret.push({
...item,
timestamp: (periodStep + j + 1) * period
})
j++
}
}
}
prevItem = item
prevPeriodStep = periodStep
i++
}
return ret
}
const now = Math.floor(Date.now() / 1000)
const data = [
{
timestamp: now - 15000,
value: 2
},
{
timestamp: now - 5000,
value: 5
},
{
timestamp: now,
value: 10
}
]
const result = fillPeriods(data, { period: 3600, from: now - 86400, to: now + 7200 }).map(item => {
return {
date: new Date(item.timestamp * 1000).toISOString(),
...item
}
})
console.log('RESULTS')
console.log(result)