postgres-interval 1.0.2 → 1.2.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 ADDED
@@ -0,0 +1,20 @@
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
+ }
16
+ }
17
+
18
+ declare function PostgresInterval(raw: string): PostgresInterval.IPostgresInterval;
19
+
20
+ export = PostgresInterval;
package/index.js CHANGED
@@ -13,23 +13,72 @@ 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
- return this[property] + ' ' + property
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.toISOString = 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
+
24
73
  var NUMBER = '([+-]?\\d+)'
25
74
  var YEAR = NUMBER + '\\s+years?'
26
75
  var MONTH = NUMBER + '\\s+mons?'
27
76
  var DAY = NUMBER + '\\s+days?'
28
- var TIME = '([+-])?([\\d]*):(\\d\\d):(\\d\\d):?(\\d\\d\\d)?'
77
+ var TIME = '([+-])?([\\d]*):(\\d\\d):(\\d\\d)\\.?(\\d{1,6})?'
29
78
  var INTERVAL = new RegExp([YEAR, MONTH, DAY, TIME].map(function (regexString) {
30
79
  return '(' + regexString + ')?'
31
80
  })
32
- .join('\\s*'))
81
+ .join('\\s*'))
33
82
 
34
83
  // Positions of values in regex match
35
84
  var positions = {
@@ -42,7 +91,13 @@ var positions = {
42
91
  milliseconds: 12
43
92
  }
44
93
  // We can use negative time
45
- var negatives = ['hours', 'minutes', 'seconds']
94
+ var negatives = ['hours', 'minutes', 'seconds', 'milliseconds']
95
+
96
+ function parseMilliseconds (fraction) {
97
+ // add omitted zeroes
98
+ var microseconds = fraction + '000000'.slice(fraction.length)
99
+ return parseInt(microseconds, 10) / 1000
100
+ }
46
101
 
47
102
  function parse (interval) {
48
103
  if (!interval) return {}
@@ -54,7 +109,11 @@ function parse (interval) {
54
109
  var value = matches[position]
55
110
  // no empty string
56
111
  if (!value) return parsed
57
- value = parseInt(value, 10)
112
+ // milliseconds are actually microseconds (up to 6 digits)
113
+ // with omitted trailing zeroes.
114
+ value = property === 'milliseconds'
115
+ ? parseMilliseconds(value)
116
+ : parseInt(value, 10)
58
117
  // no zeros
59
118
  if (!value) return parsed
60
119
  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.0.2",
4
+ "version": "1.2.0",
5
5
  "description": "Parse Postgres interval columns",
6
6
  "license": "MIT",
7
7
  "repository": "bendrucker/postgres-interval",
@@ -26,10 +26,11 @@
26
26
  },
27
27
  "devDependencies": {
28
28
  "tape": "^4.0.0",
29
- "standard": "^4.0.0"
29
+ "standard": "^12.0.1"
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
@@ -1,4 +1,4 @@
1
- # postgres-interval [![Build Status](https://travis-ci.org/bendrucker/postgres-interval.svg?branch=master)](https://travis-ci.org/bendrucker/postgres-interval)
1
+ # postgres-interval [![Build Status](https://travis-ci.org/bendrucker/postgres-interval.svg?branch=master)](https://travis-ci.org/bendrucker/postgres-interval) [![Greenkeeper badge](https://badges.greenkeeper.io/bendrucker/postgres-interval.svg)](https://greenkeeper.io/)
2
2
 
3
3
  > Parse Postgres interval columns
4
4
 
@@ -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,12 @@ 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.toISOString()` -> `string`
41
+
42
+ Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string.
43
+
44
+ Also available as `interval.toISO()` for backwards compatibility.
45
+
38
46
  ## License
39
47
 
40
48
  MIT © [Ben Drucker](http://bendrucker.me)