postgres-interval 1.1.1 → 2.1.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.
Files changed (4) hide show
  1. package/index.d.ts +21 -0
  2. package/index.js +46 -30
  3. package/package.json +6 -7
  4. package/readme.md +17 -5
package/index.d.ts ADDED
@@ -0,0 +1,21 @@
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
+ toISOString(): string;
15
+ toISOStringShort(): string;
16
+ }
17
+ }
18
+
19
+ declare function PostgresInterval(raw: string): PostgresInterval.IPostgresInterval;
20
+
21
+ export = PostgresInterval;
package/index.js CHANGED
@@ -1,18 +1,21 @@
1
1
  'use strict'
2
2
 
3
- var extend = require('xtend/mutable')
4
-
5
3
  module.exports = PostgresInterval
6
4
 
7
5
  function PostgresInterval (raw) {
8
6
  if (!(this instanceof PostgresInterval)) {
9
7
  return new PostgresInterval(raw)
10
8
  }
11
- extend(this, parse(raw))
9
+
10
+ for (const key in positions) {
11
+ this[key] = 0
12
+ }
13
+
14
+ Object.assign(this, parse(raw))
12
15
  }
13
- var properties = ['seconds', 'minutes', 'hours', 'days', 'months', 'years']
16
+ const properties = ['seconds', 'minutes', 'hours', 'days', 'months', 'years']
14
17
  PostgresInterval.prototype.toPostgres = function () {
15
- var filtered = properties.filter(this.hasOwnProperty, this)
18
+ const filtered = properties.filter(key => Object.prototype.hasOwnProperty.call(this, key) && this[key] !== 0)
16
19
 
17
20
  // In addition to `properties`, we need to account for fractions of seconds.
18
21
  if (this.milliseconds && filtered.indexOf('seconds') < 0) {
@@ -22,12 +25,12 @@ PostgresInterval.prototype.toPostgres = function () {
22
25
  if (filtered.length === 0) return '0'
23
26
  return filtered
24
27
  .map(function (property) {
25
- var value = this[property] || 0
28
+ let value = this[property] || 0
26
29
 
27
30
  // Account for fractional part of seconds,
28
31
  // remove trailing zeroes.
29
32
  if (property === 'seconds' && this.milliseconds) {
30
- value = (value + this.milliseconds / 1000).toFixed(6).replace(/0+$/, '')
33
+ value = (value + this.milliseconds / 1000).toFixed(6).replace(/\.?0+$/, '')
31
34
  }
32
35
 
33
36
  return value + ' ' + property
@@ -35,7 +38,7 @@ PostgresInterval.prototype.toPostgres = function () {
35
38
  .join(' ')
36
39
  }
37
40
 
38
- var propertiesISOEquivalent = {
41
+ const propertiesISOEquivalent = {
39
42
  years: 'Y',
40
43
  months: 'M',
41
44
  days: 'D',
@@ -43,22 +46,34 @@ var propertiesISOEquivalent = {
43
46
  minutes: 'M',
44
47
  seconds: 'S'
45
48
  }
46
- var dateProperties = ['years', 'months', 'days']
47
- var timeProperties = ['hours', 'minutes', 'seconds']
49
+ const dateProperties = ['years', 'months', 'days']
50
+ const timeProperties = ['hours', 'minutes', 'seconds']
48
51
  // according to ISO 8601
49
- PostgresInterval.prototype.toISO = function () {
50
- var datePart = dateProperties
52
+ PostgresInterval.prototype.toISOString = PostgresInterval.prototype.toISO = function () {
53
+ return toISOString.call(this, { short: false })
54
+ }
55
+
56
+ PostgresInterval.prototype.toISOStringShort = function () {
57
+ return toISOString.call(this, { short: true })
58
+ }
59
+
60
+ function toISOString ({ short = false }) {
61
+ const datePart = dateProperties
51
62
  .map(buildProperty, this)
52
63
  .join('')
53
64
 
54
- var timePart = timeProperties
65
+ const timePart = timeProperties
55
66
  .map(buildProperty, this)
56
67
  .join('')
57
68
 
58
- return 'P' + datePart + 'T' + timePart
69
+ if (!timePart.length && !datePart.length) return 'PT0S'
70
+
71
+ if (!timePart.length) return `P${datePart}`
72
+
73
+ return `P${datePart}T${timePart}`
59
74
 
60
75
  function buildProperty (property) {
61
- var value = this[property] || 0
76
+ let value = this[property] || 0
62
77
 
63
78
  // Account for fractional part of seconds,
64
79
  // remove trailing zeroes.
@@ -66,23 +81,24 @@ PostgresInterval.prototype.toISO = function () {
66
81
  value = (value + this.milliseconds / 1000).toFixed(6).replace(/0+$/, '')
67
82
  }
68
83
 
84
+ if (short && !value) return ''
85
+
69
86
  return value + propertiesISOEquivalent[property]
70
87
  }
71
-
72
88
  }
73
89
 
74
- var NUMBER = '([+-]?\\d+)'
75
- var YEAR = NUMBER + '\\s+years?'
76
- var MONTH = NUMBER + '\\s+mons?'
77
- var DAY = NUMBER + '\\s+days?'
78
- var TIME = '([+-])?([\\d]*):(\\d\\d):(\\d\\d)\.?(\\d{1,6})?'
79
- var INTERVAL = new RegExp([YEAR, MONTH, DAY, TIME].map(function (regexString) {
90
+ const NUMBER = '([+-]?\\d+)'
91
+ const YEAR = NUMBER + '\\s+years?'
92
+ const MONTH = NUMBER + '\\s+mons?'
93
+ const DAY = NUMBER + '\\s+days?'
94
+ const TIME = '([+-])?([\\d]*):(\\d\\d):(\\d\\d)\\.?(\\d{1,6})?'
95
+ const INTERVAL = new RegExp([YEAR, MONTH, DAY, TIME].map(function (regexString) {
80
96
  return '(' + regexString + ')?'
81
97
  })
82
- .join('\\s*'))
98
+ .join('\\s*'))
83
99
 
84
100
  // Positions of values in regex match
85
- var positions = {
101
+ const positions = {
86
102
  years: 2,
87
103
  months: 4,
88
104
  days: 6,
@@ -92,22 +108,22 @@ var positions = {
92
108
  milliseconds: 12
93
109
  }
94
110
  // We can use negative time
95
- var negatives = ['hours', 'minutes', 'seconds', 'milliseconds']
111
+ const negatives = ['hours', 'minutes', 'seconds', 'milliseconds']
96
112
 
97
113
  function parseMilliseconds (fraction) {
98
114
  // add omitted zeroes
99
- var microseconds = fraction + '000000'.slice(fraction.length)
115
+ const microseconds = fraction + '000000'.slice(fraction.length)
100
116
  return parseInt(microseconds, 10) / 1000
101
117
  }
102
118
 
103
119
  function parse (interval) {
104
120
  if (!interval) return {}
105
- var matches = INTERVAL.exec(interval)
106
- var isNegative = matches[8] === '-'
121
+ const matches = INTERVAL.exec(interval)
122
+ const isNegative = matches[8] === '-'
107
123
  return Object.keys(positions)
108
124
  .reduce(function (parsed, property) {
109
- var position = positions[property]
110
- var value = matches[position]
125
+ const position = positions[property]
126
+ let value = matches[position]
111
127
  // no empty string
112
128
  if (!value) return parsed
113
129
  // milliseconds are actually microseconds (up to 6 digits)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "postgres-interval",
3
3
  "main": "index.js",
4
- "version": "1.1.1",
4
+ "version": "2.1.0",
5
5
  "description": "Parse Postgres interval columns",
6
6
  "license": "MIT",
7
7
  "repository": "bendrucker/postgres-interval",
@@ -11,7 +11,7 @@
11
11
  "url": "bendrucker.me"
12
12
  },
13
13
  "engines": {
14
- "node": ">=0.10.0"
14
+ "node": ">=8"
15
15
  },
16
16
  "scripts": {
17
17
  "test": "standard && tape test.js"
@@ -21,15 +21,14 @@
21
21
  "interval",
22
22
  "parser"
23
23
  ],
24
- "dependencies": {
25
- "xtend": "^4.0.0"
26
- },
24
+ "dependencies": {},
27
25
  "devDependencies": {
28
- "tape": "^4.0.0",
29
- "standard": "^4.0.0"
26
+ "standard": "^14.0.0",
27
+ "tape": "^5.0.0"
30
28
  },
31
29
  "files": [
32
30
  "index.js",
31
+ "index.d.ts",
33
32
  "readme.md"
34
33
  ]
35
34
  }
package/readme.md CHANGED
@@ -5,8 +5,8 @@
5
5
 
6
6
  ## Install
7
7
 
8
- ```
9
- $ npm install --save postgres-interval
8
+ ```sh
9
+ npm install --save postgres-interval
10
10
  ```
11
11
 
12
12
 
@@ -18,10 +18,16 @@ 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()
21
+ interval.toISOString()
22
22
  // P0Y0M0DT1H2M3S
23
23
  ```
24
24
 
25
+ 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:
26
+
27
+ ```sql
28
+ set intervalstyle to default;
29
+ ```
30
+
25
31
  ## API
26
32
 
27
33
  #### `parse(pgInterval)` -> `interval`
@@ -37,9 +43,15 @@ A Postgres interval string.
37
43
 
38
44
  Returns an interval string. This allows the interval object to be passed into prepared statements.
39
45
 
40
- #### `interval.toISO()` -> `string`
46
+ #### `interval.toISOString()` -> `string`
47
+
48
+ Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string, for example `P0Y0M0DT0H9M0S`.
49
+
50
+ Also available as `interval.toISO()` for backwards compatibility.
51
+
52
+ #### `interval.toISOStringShort()` -> `string`
41
53
 
42
- Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string.
54
+ Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string shortened to minimum length, for example `PT9M`.
43
55
 
44
56
  ## License
45
57