postgres-interval
Advanced tools
Comparing version 1.0.2 to 1.1.0
66
index.js
@@ -16,6 +16,20 @@ 'use strict' | ||
var filtered = properties.filter(this.hasOwnProperty, this) | ||
// In addition to `properties`, we need to account for fractions of seconds. | ||
if (this.milliseconds && filtered.indexOf('seconds') < 0) { | ||
filtered.push('seconds') | ||
} | ||
if (filtered.length === 0) return '0' | ||
return filtered | ||
.map(function (property) { | ||
return this[property] + ' ' + property | ||
var value = this[property] || 0 | ||
// Account for fractional part of seconds, | ||
// remove trailing zeroes. | ||
if (property === 'seconds' && this.milliseconds) { | ||
value += '.' + String(this.milliseconds * 1000).replace(/[0]+$/g, '') | ||
} | ||
return value + ' ' + property | ||
}, this) | ||
@@ -25,2 +39,38 @@ .join(' ') | ||
var propertiesISOEquivalent = { | ||
years: 'Y', | ||
months: 'M', | ||
days: 'D', | ||
hours: 'H', | ||
minutes: 'M', | ||
seconds: 'S' | ||
} | ||
var dateProperties = ['years', 'months', 'days'] | ||
var timeProperties = ['hours', 'minutes', 'seconds'] | ||
// according to ISO 8601 | ||
PostgresInterval.prototype.toISO = function () { | ||
var datePart = dateProperties | ||
.map(buildProperty, this) | ||
.join('') | ||
var timePart = timeProperties | ||
.map(buildProperty, this) | ||
.join('') | ||
return 'P' + datePart + 'T' + timePart | ||
function buildProperty (property) { | ||
var value = this[property] || 0 | ||
// Account for fractional part of seconds, | ||
// remove trailing zeroes. | ||
if (property === 'seconds' && this.milliseconds) { | ||
value += '.' + String(this.milliseconds * 1000).replace(/[0]+$/g, '') | ||
} | ||
return value + propertiesISOEquivalent[property] | ||
} | ||
} | ||
var NUMBER = '([+-]?\\d+)' | ||
@@ -30,3 +80,3 @@ var YEAR = NUMBER + '\\s+years?' | ||
var DAY = NUMBER + '\\s+days?' | ||
var TIME = '([+-])?([\\d]*):(\\d\\d):(\\d\\d):?(\\d\\d\\d)?' | ||
var TIME = '([+-])?([\\d]*):(\\d\\d):(\\d\\d)\.?(\\d{1,6})?' | ||
var INTERVAL = new RegExp([YEAR, MONTH, DAY, TIME].map(function (regexString) { | ||
@@ -50,2 +100,8 @@ return '(' + regexString + ')?' | ||
function parseMilliseconds (fraction) { | ||
// add omitted zeroes | ||
var microseconds = fraction + '000000'.slice(fraction.length) | ||
return parseInt(microseconds, 10) / 1000 | ||
} | ||
function parse (interval) { | ||
@@ -61,3 +117,7 @@ if (!interval) return {} | ||
if (!value) return parsed | ||
value = parseInt(value, 10) | ||
// milliseconds are actually microseconds (up to 6 digits) | ||
// with omitted trailing zeroes. | ||
value = property === 'milliseconds' | ||
? parseMilliseconds(value) | ||
: parseInt(value, 10) | ||
// no zeros | ||
@@ -64,0 +124,0 @@ if (!value) return parsed |
{ | ||
"name": "postgres-interval", | ||
"main": "index.js", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Parse Postgres interval columns", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -21,2 +21,4 @@ # postgres-interval [![Build Status](https://travis-ci.org/bendrucker/postgres-interval.svg?branch=master)](https://travis-ci.org/bendrucker/postgres-interval) | ||
// 3 seconds 2 minutes 1 hours | ||
interval.toISO() | ||
// P0Y0M0DT1H2M3S | ||
``` | ||
@@ -39,4 +41,8 @@ | ||
#### `interval.toISO()` -> `string` | ||
Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string. | ||
## License | ||
MIT © [Ben Drucker](http://bendrucker.me) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6033
108
47