postgres-interval 1.0.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +19 -0
- package/index.js +64 -4
- package/package.json +2 -1
- package/readme.md +6 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
declare namespace PostgresInterval {
|
|
2
|
+
export interface IPostgresInterval {
|
|
3
|
+
years?: number;
|
|
4
|
+
months?: number;
|
|
5
|
+
days?: number;
|
|
6
|
+
hours?: number;
|
|
7
|
+
minutes?: number;
|
|
8
|
+
seconds?: number;
|
|
9
|
+
milliseconds?: number;
|
|
10
|
+
|
|
11
|
+
toPostgres(): string;
|
|
12
|
+
|
|
13
|
+
toISO(): string;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
declare function PostgresInterval(raw: string): PostgresInterval.IPostgresInterval;
|
|
18
|
+
|
|
19
|
+
export = PostgresInterval;
|
package/index.js
CHANGED
|
@@ -13,19 +13,69 @@ function PostgresInterval (raw) {
|
|
|
13
13
|
var properties = ['seconds', 'minutes', 'hours', 'days', 'months', 'years']
|
|
14
14
|
PostgresInterval.prototype.toPostgres = function () {
|
|
15
15
|
var filtered = properties.filter(this.hasOwnProperty, this)
|
|
16
|
+
|
|
17
|
+
// In addition to `properties`, we need to account for fractions of seconds.
|
|
18
|
+
if (this.milliseconds && filtered.indexOf('seconds') < 0) {
|
|
19
|
+
filtered.push('seconds')
|
|
20
|
+
}
|
|
21
|
+
|
|
16
22
|
if (filtered.length === 0) return '0'
|
|
17
23
|
return filtered
|
|
18
24
|
.map(function (property) {
|
|
19
|
-
|
|
25
|
+
var value = this[property] || 0
|
|
26
|
+
|
|
27
|
+
// Account for fractional part of seconds,
|
|
28
|
+
// remove trailing zeroes.
|
|
29
|
+
if (property === 'seconds' && this.milliseconds) {
|
|
30
|
+
value = (value + this.milliseconds / 1000).toFixed(6).replace(/\.?0+$/, '')
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return value + ' ' + property
|
|
20
34
|
}, this)
|
|
21
35
|
.join(' ')
|
|
22
36
|
}
|
|
23
37
|
|
|
38
|
+
var propertiesISOEquivalent = {
|
|
39
|
+
years: 'Y',
|
|
40
|
+
months: 'M',
|
|
41
|
+
days: 'D',
|
|
42
|
+
hours: 'H',
|
|
43
|
+
minutes: 'M',
|
|
44
|
+
seconds: 'S'
|
|
45
|
+
}
|
|
46
|
+
var dateProperties = ['years', 'months', 'days']
|
|
47
|
+
var timeProperties = ['hours', 'minutes', 'seconds']
|
|
48
|
+
// according to ISO 8601
|
|
49
|
+
PostgresInterval.prototype.toISO = function () {
|
|
50
|
+
var datePart = dateProperties
|
|
51
|
+
.map(buildProperty, this)
|
|
52
|
+
.join('')
|
|
53
|
+
|
|
54
|
+
var timePart = timeProperties
|
|
55
|
+
.map(buildProperty, this)
|
|
56
|
+
.join('')
|
|
57
|
+
|
|
58
|
+
return 'P' + datePart + 'T' + timePart
|
|
59
|
+
|
|
60
|
+
function buildProperty (property) {
|
|
61
|
+
var value = this[property] || 0
|
|
62
|
+
|
|
63
|
+
// Account for fractional part of seconds,
|
|
64
|
+
// remove trailing zeroes.
|
|
65
|
+
if (property === 'seconds' && this.milliseconds) {
|
|
66
|
+
value = (value + this.milliseconds / 1000).toFixed(6).replace(/0+$/, '')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return value + propertiesISOEquivalent[property]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
}
|
|
73
|
+
|
|
24
74
|
var NUMBER = '([+-]?\\d+)'
|
|
25
75
|
var YEAR = NUMBER + '\\s+years?'
|
|
26
76
|
var MONTH = NUMBER + '\\s+mons?'
|
|
27
77
|
var DAY = NUMBER + '\\s+days?'
|
|
28
|
-
var TIME = '([+-])?(\\d
|
|
78
|
+
var TIME = '([+-])?([\\d]*):(\\d\\d):(\\d\\d)\.?(\\d{1,6})?'
|
|
29
79
|
var INTERVAL = new RegExp([YEAR, MONTH, DAY, TIME].map(function (regexString) {
|
|
30
80
|
return '(' + regexString + ')?'
|
|
31
81
|
})
|
|
@@ -42,7 +92,13 @@ var positions = {
|
|
|
42
92
|
milliseconds: 12
|
|
43
93
|
}
|
|
44
94
|
// We can use negative time
|
|
45
|
-
var negatives = ['hours', 'minutes', 'seconds']
|
|
95
|
+
var negatives = ['hours', 'minutes', 'seconds', 'milliseconds']
|
|
96
|
+
|
|
97
|
+
function parseMilliseconds (fraction) {
|
|
98
|
+
// add omitted zeroes
|
|
99
|
+
var microseconds = fraction + '000000'.slice(fraction.length)
|
|
100
|
+
return parseInt(microseconds, 10) / 1000
|
|
101
|
+
}
|
|
46
102
|
|
|
47
103
|
function parse (interval) {
|
|
48
104
|
if (!interval) return {}
|
|
@@ -54,7 +110,11 @@ function parse (interval) {
|
|
|
54
110
|
var value = matches[position]
|
|
55
111
|
// no empty string
|
|
56
112
|
if (!value) return parsed
|
|
57
|
-
|
|
113
|
+
// milliseconds are actually microseconds (up to 6 digits)
|
|
114
|
+
// with omitted trailing zeroes.
|
|
115
|
+
value = property === 'milliseconds'
|
|
116
|
+
? parseMilliseconds(value)
|
|
117
|
+
: parseInt(value, 10)
|
|
58
118
|
// no zeros
|
|
59
119
|
if (!value) return parsed
|
|
60
120
|
if (isNegative && ~negatives.indexOf(property)) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postgres-interval",
|
|
3
3
|
"main": "index.js",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.1.2",
|
|
5
5
|
"description": "Parse Postgres interval columns",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "bendrucker/postgres-interval",
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
},
|
|
31
31
|
"files": [
|
|
32
32
|
"index.js",
|
|
33
|
+
"index.d.ts",
|
|
33
34
|
"readme.md"
|
|
34
35
|
]
|
|
35
36
|
}
|
package/readme.md
CHANGED
|
@@ -18,6 +18,8 @@ var interval = parse('01:02:03')
|
|
|
18
18
|
//=> {hours: 1, minutes: 2, seconds: 3}
|
|
19
19
|
interval.toPostgres()
|
|
20
20
|
// 3 seconds 2 minutes 1 hours
|
|
21
|
+
interval.toISO()
|
|
22
|
+
// P0Y0M0DT1H2M3S
|
|
21
23
|
```
|
|
22
24
|
|
|
23
25
|
## API
|
|
@@ -35,6 +37,10 @@ A Postgres interval string.
|
|
|
35
37
|
|
|
36
38
|
Returns an interval string. This allows the interval object to be passed into prepared statements.
|
|
37
39
|
|
|
40
|
+
#### `interval.toISO()` -> `string`
|
|
41
|
+
|
|
42
|
+
Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string.
|
|
43
|
+
|
|
38
44
|
## License
|
|
39
45
|
|
|
40
46
|
MIT © [Ben Drucker](http://bendrucker.me)
|