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 +1 -0
- package/index.js +15 -1
- package/package.json +1 -1
- package/readme.md +5 -1
package/index.d.ts
CHANGED
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
|
-
|
|
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
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)
|