package/package.json000644 0000001232 13434107160011562 0ustar00000000 000000 { "name": "postgres-interval", "main": "index.js", "version": "1.2.0", "description": "Parse Postgres interval columns", "license": "MIT", "repository": "bendrucker/postgres-interval", "author": { "name": "Ben Drucker", "email": "bvdrucker@gmail.com", "url": "bendrucker.me" }, "engines": { "node": ">=0.10.0" }, "scripts": { "test": "standard && tape test.js" }, "keywords": [ "postgres", "interval", "parser" ], "dependencies": { "xtend": "^4.0.0" }, "devDependencies": { "tape": "^4.0.0", "standard": "^12.0.1" }, "files": [ "index.js", "index.d.ts", "readme.md" ] } package/index.d.ts000644 0000000646 13434107131011203 0ustar00000000 000000 declare namespace PostgresInterval { export interface IPostgresInterval { years?: number; months?: number; days?: number; hours?: number; minutes?: number; seconds?: number; milliseconds?: number; toPostgres(): string; toISO(): string; toISOString(): string; } } declare function PostgresInterval(raw: string): PostgresInterval.IPostgresInterval; export = PostgresInterval; package/index.js000644 0000006525 13434107131010751 0ustar00000000 000000 'use strict' var extend = require('xtend/mutable') module.exports = PostgresInterval function PostgresInterval (raw) { if (!(this instanceof PostgresInterval)) { return new PostgresInterval(raw) } extend(this, parse(raw)) } var properties = ['seconds', 'minutes', 'hours', 'days', 'months', 'years'] PostgresInterval.prototype.toPostgres = function () { 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) { var value = this[property] || 0 // Account for fractional part of seconds, // remove trailing zeroes. if (property === 'seconds' && this.milliseconds) { value = (value + this.milliseconds / 1000).toFixed(6).replace(/\.?0+$/, '') } return value + ' ' + property }, this) .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.toISOString = 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 = (value + this.milliseconds / 1000).toFixed(6).replace(/0+$/, '') } return value + propertiesISOEquivalent[property] } } 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) { return '(' + regexString + ')?' }) .join('\\s*')) // Positions of values in regex match var positions = { years: 2, months: 4, days: 6, hours: 9, minutes: 10, seconds: 11, milliseconds: 12 } // We can use negative time var negatives = ['hours', 'minutes', 'seconds', 'milliseconds'] function parseMilliseconds (fraction) { // add omitted zeroes var microseconds = fraction + '000000'.slice(fraction.length) return parseInt(microseconds, 10) / 1000 } function parse (interval) { if (!interval) return {} var matches = INTERVAL.exec(interval) var isNegative = matches[8] === '-' return Object.keys(positions) .reduce(function (parsed, property) { var position = positions[property] var value = matches[position] // no empty string if (!value) return parsed // milliseconds are actually microseconds (up to 6 digits) // with omitted trailing zeroes. value = property === 'milliseconds' ? parseMilliseconds(value) : parseInt(value, 10) // no zeros if (!value) return parsed if (isNegative && ~negatives.indexOf(property)) { value *= -1 } parsed[property] = value return parsed }, {}) } package/license000644 0000002127 13434107131010643 0ustar00000000 000000 The MIT License (MIT) Copyright (c) Ben Drucker (bendrucker.me) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/readme.md000644 0000002133 13434107131011052 0ustar00000000 000000 # 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/) > Parse Postgres interval columns ## Install ``` $ npm install --save postgres-interval ``` ## Usage ```js var parse = require('postgres-interval') var interval = parse('01:02:03') //=> {hours: 1, minutes: 2, seconds: 3} interval.toPostgres() // 3 seconds 2 minutes 1 hours interval.toISO() // P0Y0M0DT1H2M3S ``` ## API #### `parse(pgInterval)` -> `interval` ##### pgInterval *Required* Type: `string` A Postgres interval string. #### `interval.toPostgres()` -> `string` Returns an interval string. This allows the interval object to be passed into prepared statements. #### `interval.toISOString()` -> `string` Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string. Also available as `interval.toISO()` for backwards compatibility. ## License MIT © [Ben Drucker](http://bendrucker.me)