postgres-interval 2.0.0 → 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.
package/index.d.ts CHANGED
@@ -12,6 +12,7 @@ declare namespace PostgresInterval {
12
12
 
13
13
  toISO(): string;
14
14
  toISOString(): string;
15
+ toISOStringShort(): string;
15
16
  }
16
17
  }
17
18
 
package/index.js CHANGED
@@ -50,6 +50,14 @@ const dateProperties = ['years', 'months', 'days']
50
50
  const timeProperties = ['hours', 'minutes', 'seconds']
51
51
  // according to ISO 8601
52
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 }) {
53
61
  const datePart = dateProperties
54
62
  .map(buildProperty, this)
55
63
  .join('')
@@ -58,7 +66,11 @@ PostgresInterval.prototype.toISOString = PostgresInterval.prototype.toISO = func
58
66
  .map(buildProperty, this)
59
67
  .join('')
60
68
 
61
- 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}`
62
74
 
63
75
  function buildProperty (property) {
64
76
  let value = this[property] || 0
@@ -69,6 +81,8 @@ PostgresInterval.prototype.toISOString = PostgresInterval.prototype.toISO = func
69
81
  value = (value + this.milliseconds / 1000).toFixed(6).replace(/0+$/, '')
70
82
  }
71
83
 
84
+ if (short && !value) return ''
85
+
72
86
  return value + propertiesISOEquivalent[property]
73
87
  }
74
88
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "postgres-interval",
3
3
  "main": "index.js",
4
- "version": "2.0.0",
4
+ "version": "2.1.0",
5
5
  "description": "Parse Postgres interval columns",
6
6
  "license": "MIT",
7
7
  "repository": "bendrucker/postgres-interval",
package/readme.md CHANGED
@@ -45,10 +45,14 @@ Returns an interval string. This allows the interval object to be passed into pr
45
45
 
46
46
  #### `interval.toISOString()` -> `string`
47
47
 
48
- Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string.
48
+ Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string, for example `P0Y0M0DT0H9M0S`.
49
49
 
50
50
  Also available as `interval.toISO()` for backwards compatibility.
51
51
 
52
+ #### `interval.toISOStringShort()` -> `string`
53
+
54
+ Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string shortened to minimum length, for example `PT9M`.
55
+
52
56
  ## License
53
57
 
54
58
  MIT © [Ben Drucker](http://bendrucker.me)