postgres-interval 3.0.0 → 4.0.0
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 +65 -0
- package/index.js +6 -3
- package/package.json +1 -1
- package/readme.md +4 -2
package/index.d.ts
CHANGED
|
@@ -8,14 +8,79 @@ declare namespace PostgresInterval {
|
|
|
8
8
|
seconds: number;
|
|
9
9
|
milliseconds: number;
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Returns an interval string. This allows the interval object to be passed into prepared statements.
|
|
13
|
+
*
|
|
14
|
+
* ```js
|
|
15
|
+
* var parse = require('postgres-interval')
|
|
16
|
+
* var interval = parse('01:02:03')
|
|
17
|
+
* // => { hours: 1, minutes: 2, seconds: 3 }
|
|
18
|
+
* interval.toPostgres()
|
|
19
|
+
* // 1 hour 2 minutes 3 seconds
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
11
22
|
toPostgres(): string;
|
|
12
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string, for example P0Y0M0DT0H9M0S.
|
|
26
|
+
*
|
|
27
|
+
* Also available as {@link toISOString toISOString}.
|
|
28
|
+
*
|
|
29
|
+
* ```js
|
|
30
|
+
* var parse = require('postgres-interval')
|
|
31
|
+
* var interval = parse('01:02:03')
|
|
32
|
+
* // => { hours: 1, minutes: 2, seconds: 3 }
|
|
33
|
+
* interval.toISO()
|
|
34
|
+
* // P0Y0M0DT1H2M3S
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
13
37
|
toISO(): string;
|
|
38
|
+
/**
|
|
39
|
+
* Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string, for example P0Y0M0DT0H9M0S.
|
|
40
|
+
*
|
|
41
|
+
* Also available as {@link toISO toISO} for backwards compatibility.
|
|
42
|
+
*
|
|
43
|
+
* ```js
|
|
44
|
+
* var parse = require('postgres-interval')
|
|
45
|
+
* var interval = parse('01:02:03')
|
|
46
|
+
* // => { hours: 1, minutes: 2, seconds: 3 }
|
|
47
|
+
* interval.toISOString()
|
|
48
|
+
* // P0Y0M0DT1H2M3S
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
14
51
|
toISOString(): string;
|
|
52
|
+
/**
|
|
53
|
+
* Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string shortened to minimum length, for example `PT9M`.
|
|
54
|
+
*
|
|
55
|
+
* ```js
|
|
56
|
+
* var parse = require('postgres-interval')
|
|
57
|
+
* var interval = parse('01:02:03')
|
|
58
|
+
* // => { hours: 1, minutes: 2, seconds: 3 }
|
|
59
|
+
* interval.toISOStringShort()
|
|
60
|
+
* // PT1H2M3S
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
15
63
|
toISOStringShort(): string;
|
|
16
64
|
}
|
|
17
65
|
}
|
|
18
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Parse Postgres interval columns.
|
|
69
|
+
*
|
|
70
|
+
* ```js
|
|
71
|
+
* var parse = require('postgres-interval')
|
|
72
|
+
* var interval = parse('01:02:03')
|
|
73
|
+
* // => { hours: 1, minutes: 2, seconds: 3 }
|
|
74
|
+
* interval.toPostgres()
|
|
75
|
+
* // 1 hour 2 minutes 3 seconds
|
|
76
|
+
* interval.toISOString()
|
|
77
|
+
* // P0Y0M0DT1H2M3S
|
|
78
|
+
* interval.toISOStringShort()
|
|
79
|
+
* // PT1H2M3S
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @param raw A Postgres interval string.
|
|
83
|
+
*/
|
|
19
84
|
declare function PostgresInterval(raw: string): PostgresInterval.IPostgresInterval;
|
|
20
85
|
|
|
21
86
|
export = PostgresInterval;
|
package/index.js
CHANGED
|
@@ -9,12 +9,12 @@ function PostgresInterval (raw) {
|
|
|
9
9
|
|
|
10
10
|
Object.assign(this, parse(raw))
|
|
11
11
|
}
|
|
12
|
-
const properties = ['
|
|
12
|
+
const properties = ['years', 'months', 'days', 'hours', 'minutes', 'seconds']
|
|
13
13
|
PostgresInterval.prototype.toPostgres = function () {
|
|
14
14
|
const filtered = properties.filter(key => Object.prototype.hasOwnProperty.call(this, key) && this[key] !== 0)
|
|
15
15
|
|
|
16
16
|
// In addition to `properties`, we need to account for fractions of seconds.
|
|
17
|
-
if (this.milliseconds && filtered.
|
|
17
|
+
if (this.milliseconds && !filtered.includes('seconds')) {
|
|
18
18
|
filtered.push('seconds')
|
|
19
19
|
}
|
|
20
20
|
|
|
@@ -29,7 +29,10 @@ PostgresInterval.prototype.toPostgres = function () {
|
|
|
29
29
|
value = (value + this.milliseconds / 1000).toFixed(6).replace(/\.?0+$/, '')
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
// fractional seconds will be a String, all others are Number
|
|
33
|
+
const isSingular = String(value) === '1'
|
|
34
|
+
// Remove plural 's' when the value is singular
|
|
35
|
+
return value + ' ' + (isSingular ? property.replace(/s$/, '') : property)
|
|
33
36
|
}, this)
|
|
34
37
|
.join(' ')
|
|
35
38
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -15,11 +15,13 @@ npm install --save postgres-interval
|
|
|
15
15
|
```js
|
|
16
16
|
var parse = require('postgres-interval')
|
|
17
17
|
var interval = parse('01:02:03')
|
|
18
|
-
|
|
18
|
+
// => { hours: 1, minutes: 2, seconds: 3 }
|
|
19
19
|
interval.toPostgres()
|
|
20
|
-
//
|
|
20
|
+
// 1 hour 2 minutes 3 seconds
|
|
21
21
|
interval.toISOString()
|
|
22
22
|
// P0Y0M0DT1H2M3S
|
|
23
|
+
interval.toISOStringShort()
|
|
24
|
+
// PT1H2M3S
|
|
23
25
|
```
|
|
24
26
|
|
|
25
27
|
This package parses the default Postgres interval style. If you have changed [`intervalstyle`](https://www.postgresql.org/docs/current/runtime-config-client.html#GUC-INTERVALSTYLE), you will need to set it back to the default:
|