postgres-interval
Advanced tools
Comparing version 1.2.0 to 2.0.0
declare namespace PostgresInterval { | ||
export interface IPostgresInterval { | ||
years?: number; | ||
months?: number; | ||
days?: number; | ||
hours?: number; | ||
minutes?: number; | ||
seconds?: number; | ||
milliseconds?: number; | ||
years: number; | ||
months: number; | ||
days: number; | ||
hours: number; | ||
minutes: number; | ||
seconds: number; | ||
milliseconds: number; | ||
@@ -11,0 +11,0 @@ toPostgres(): string; |
53
index.js
'use strict' | ||
var extend = require('xtend/mutable') | ||
module.exports = PostgresInterval | ||
@@ -11,7 +9,12 @@ | ||
} | ||
extend(this, parse(raw)) | ||
for (const key in positions) { | ||
this[key] = 0 | ||
} | ||
Object.assign(this, parse(raw)) | ||
} | ||
var properties = ['seconds', 'minutes', 'hours', 'days', 'months', 'years'] | ||
const properties = ['seconds', 'minutes', 'hours', 'days', 'months', 'years'] | ||
PostgresInterval.prototype.toPostgres = function () { | ||
var filtered = properties.filter(this.hasOwnProperty, this) | ||
const filtered = properties.filter(key => Object.prototype.hasOwnProperty.call(this, key) && this[key] !== 0) | ||
@@ -26,3 +29,3 @@ // In addition to `properties`, we need to account for fractions of seconds. | ||
.map(function (property) { | ||
var value = this[property] || 0 | ||
let value = this[property] || 0 | ||
@@ -40,3 +43,3 @@ // Account for fractional part of seconds, | ||
var propertiesISOEquivalent = { | ||
const propertiesISOEquivalent = { | ||
years: 'Y', | ||
@@ -49,11 +52,11 @@ months: 'M', | ||
} | ||
var dateProperties = ['years', 'months', 'days'] | ||
var timeProperties = ['hours', 'minutes', 'seconds'] | ||
const dateProperties = ['years', 'months', 'days'] | ||
const timeProperties = ['hours', 'minutes', 'seconds'] | ||
// according to ISO 8601 | ||
PostgresInterval.prototype.toISOString = PostgresInterval.prototype.toISO = function () { | ||
var datePart = dateProperties | ||
const datePart = dateProperties | ||
.map(buildProperty, this) | ||
.join('') | ||
var timePart = timeProperties | ||
const timePart = timeProperties | ||
.map(buildProperty, this) | ||
@@ -65,3 +68,3 @@ .join('') | ||
function buildProperty (property) { | ||
var value = this[property] || 0 | ||
let value = this[property] || 0 | ||
@@ -78,8 +81,8 @@ // Account for fractional part of seconds, | ||
var NUMBER = '([+-]?\\d+)' | ||
var YEAR = NUMBER + '\\s+years?' | ||
var MONTH = NUMBER + '\\s+mons?' | ||
var DAY = NUMBER + '\\s+days?' | ||
var TIME = '([+-])?([\\d]*):(\\d\\d):(\\d\\d)\\.?(\\d{1,6})?' | ||
var INTERVAL = new RegExp([YEAR, MONTH, DAY, TIME].map(function (regexString) { | ||
const NUMBER = '([+-]?\\d+)' | ||
const YEAR = NUMBER + '\\s+years?' | ||
const MONTH = NUMBER + '\\s+mons?' | ||
const DAY = NUMBER + '\\s+days?' | ||
const TIME = '([+-])?([\\d]*):(\\d\\d):(\\d\\d)\\.?(\\d{1,6})?' | ||
const INTERVAL = new RegExp([YEAR, MONTH, DAY, TIME].map(function (regexString) { | ||
return '(' + regexString + ')?' | ||
@@ -90,3 +93,3 @@ }) | ||
// Positions of values in regex match | ||
var positions = { | ||
const positions = { | ||
years: 2, | ||
@@ -101,7 +104,7 @@ months: 4, | ||
// We can use negative time | ||
var negatives = ['hours', 'minutes', 'seconds', 'milliseconds'] | ||
const negatives = ['hours', 'minutes', 'seconds', 'milliseconds'] | ||
function parseMilliseconds (fraction) { | ||
// add omitted zeroes | ||
var microseconds = fraction + '000000'.slice(fraction.length) | ||
const microseconds = fraction + '000000'.slice(fraction.length) | ||
return parseInt(microseconds, 10) / 1000 | ||
@@ -112,8 +115,8 @@ } | ||
if (!interval) return {} | ||
var matches = INTERVAL.exec(interval) | ||
var isNegative = matches[8] === '-' | ||
const matches = INTERVAL.exec(interval) | ||
const isNegative = matches[8] === '-' | ||
return Object.keys(positions) | ||
.reduce(function (parsed, property) { | ||
var position = positions[property] | ||
var value = matches[position] | ||
const position = positions[property] | ||
let value = matches[position] | ||
// no empty string | ||
@@ -120,0 +123,0 @@ if (!value) return parsed |
{ | ||
"name": "postgres-interval", | ||
"main": "index.js", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "Parse Postgres interval columns", | ||
@@ -14,3 +14,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=8" | ||
}, | ||
@@ -25,8 +25,6 @@ "scripts": { | ||
], | ||
"dependencies": { | ||
"xtend": "^4.0.0" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"tape": "^4.0.0", | ||
"standard": "^12.0.1" | ||
"standard": "^14.0.0", | ||
"tape": "^5.0.0" | ||
}, | ||
@@ -33,0 +31,0 @@ "files": [ |
@@ -1,2 +0,2 @@ | ||
# 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/) | ||
# postgres-interval [![Build Status](https://travis-ci.org/bendrucker/postgres-interval.svg?branch=master)](https://travis-ci.org/bendrucker/postgres-interval) | ||
@@ -8,5 +8,5 @@ > Parse Postgres interval columns | ||
```sh | ||
npm install --save postgres-interval | ||
``` | ||
$ npm install --save postgres-interval | ||
``` | ||
@@ -22,6 +22,12 @@ | ||
// 3 seconds 2 minutes 1 hours | ||
interval.toISO() | ||
interval.toISOString() | ||
// P0Y0M0DT1H2M3S | ||
``` | ||
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: | ||
```sql | ||
set intervalstyle to default; | ||
``` | ||
## API | ||
@@ -28,0 +34,0 @@ |
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
6965
0
126
55
- Removedxtend@^4.0.0
- Removedxtend@4.0.2(transitive)