forked from bendrucker/postgres-interval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
180 lines (170 loc) · 4.39 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"use strict";
export default class PostgresInterval {
constructor() {
this.years = 0;
this.months = 0;
this.days = 0;
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
this.milliseconds = 0;
}
toISOString() {
return toISOString.call(this, { short: false });
}
toISOStringShort() {
return toISOString.call(this, { short: true });
}
}
const propertiesISOEquivalent = {
years: "Y",
months: "M",
days: "D",
hours: "H",
minutes: "M",
seconds: "S",
};
function toISOString({ short }) {
let datePart = "";
if (!short || this.years) {
datePart += this.years + propertiesISOEquivalent.years;
}
if (!short || this.months) {
datePart += this.months + propertiesISOEquivalent.months;
}
if (!short || this.days) {
datePart += this.days + propertiesISOEquivalent.days;
}
let timePart = "";
if (!short || this.hours) {
timePart += this.hours + propertiesISOEquivalent.hours;
}
if (!short || this.minutes) {
timePart += this.minutes + propertiesISOEquivalent.minutes;
}
if (!short || this.seconds || this.milliseconds) {
if (this.milliseconds) {
timePart +=
Math.trunc((this.seconds + this.milliseconds / 1000) * 1000000) /
1000000 +
propertiesISOEquivalent.seconds;
} else {
timePart += this.seconds + propertiesISOEquivalent.seconds;
}
}
if (!timePart && !datePart) {
return "PT0S";
}
if (!timePart) {
return `P${datePart}`;
}
return `P${datePart}T${timePart}`;
}
const position = { value: 0 };
function readNextNum(interval) {
let val = 0;
while (position.value < interval.length) {
const char = interval[position.value];
if (char >= "0" && char <= "9") {
val = val * 10 + +char;
position.value++;
} else {
break;
}
}
return val;
}
function parseMillisecond(interval) {
const previousPosition = position.value;
const currentValue = readNextNum(interval);
const valueStringLength = position.value - previousPosition;
switch (valueStringLength) {
case 1:
return currentValue * 100;
case 2:
return currentValue * 10;
case 3:
return currentValue;
case 4:
return currentValue / 10;
case 5:
return currentValue / 100;
case 6:
return currentValue / 1000;
}
// slow path
const remainder = valueStringLength - 3;
return currentValue / Math.pow(10, remainder);
}
function parseInterval(instance, interval) {
if (!interval) {
return;
}
position.value = 0;
let currentValue;
let nextNegative = 1;
while (position.value < interval.length) {
const char = interval[position.value];
if (char === "-") {
nextNegative = -1;
position.value++;
continue;
} else if (char === "+") {
position.value++;
continue;
} else if (char === " ") {
position.value++;
continue;
} else if (char < "0" || char > "9") {
position.value++;
continue;
} else {
currentValue = readNextNum(interval);
if (interval[position.value] === ":") {
instance.hours = currentValue ? nextNegative * currentValue : 0;
position.value++;
currentValue = readNextNum(interval);
instance.minutes = currentValue ? nextNegative * currentValue : 0;
position.value++;
currentValue = readNextNum(interval);
instance.seconds = currentValue ? nextNegative * currentValue : 0;
if (interval[position.value] === ".") {
position.value++;
currentValue = parseMillisecond(interval);
instance.milliseconds = currentValue
? nextNegative * currentValue
: 0;
}
return;
}
// skip space
position.value++;
const unit = interval[position.value];
if (unit === "y") {
instance.years = currentValue ? nextNegative * currentValue : 0;
} else if (unit === "m") {
instance.months = currentValue ? nextNegative * currentValue : 0;
} else if (unit === "d") {
instance.days = currentValue ? nextNegative * currentValue : 0;
}
nextNegative = 1;
}
}
}
export function parse(interval) {
const instance = {
years: 0,
months: 0,
days: 0,
hours: 0,
minutes: 0,
seconds: 0,
milliseconds: 0,
};
if (typeof interval === 'string') {
parseInterval(instance, interval);
} else {
Object.assign(instance, interval);
}
return instance;
}