package/license000644 0000002127 3560116604 010627 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/index.js000644 0000013503 3560116604 010727 0ustar00000000 000000 'use strict' module.exports = PostgresInterval function PostgresInterval (raw) { if (!(this instanceof PostgresInterval)) { return new PostgresInterval(raw) } this.years = 0 this.months = 0 this.days = 0 this.hours = 0 this.minutes = 0 this.seconds = 0 this.milliseconds = 0 parse(this, raw) } PostgresInterval.prototype.toPostgres = function () { let postgresString = '' if (this.years) { postgresString += this.years === 1 ? this.years + ' year' : this.years + ' years' } if (this.months) { if (postgresString.length) { postgresString += ' ' } postgresString += this.months === 1 ? this.months + ' month' : this.months + ' months' } if (this.days) { if (postgresString.length) { postgresString += ' ' } postgresString += this.days === 1 ? this.days + ' day' : this.days + ' days' } if (this.hours) { if (postgresString.length) { postgresString += ' ' } postgresString += this.hours === 1 ? this.hours + ' hour' : this.hours + ' hours' } if (this.minutes) { if (postgresString.length) { postgresString += ' ' } postgresString += this.minutes === 1 ? this.minutes + ' minute' : this.minutes + ' minutes' } if (this.seconds || this.milliseconds) { if (postgresString.length) { postgresString += ' ' } if (this.milliseconds) { const value = Math.trunc((this.seconds + this.milliseconds / 1000) * 1000000) / 1000000 postgresString += value === 1 ? value + ' second' : value + ' seconds' } else { postgresString += this.seconds === 1 ? this.seconds + ' second' : this.seconds + ' seconds' } } return postgresString === '' ? '0' : postgresString } const propertiesISOEquivalent = { years: 'Y', months: 'M', days: 'D', hours: 'H', minutes: 'M', seconds: 'S' } // according to ISO 8601 PostgresInterval.prototype.toISOString = PostgresInterval.prototype.toISO = function () { return toISOString.call(this, { short: false }) } PostgresInterval.prototype.toISOStringShort = function () { return toISOString.call(this, { short: true }) } function toISOString ({ short }) { let datePart = '' if (!short || this.years) { datePart += this.years + propertiesISOEquivalent.years } if (!short || this.months) { datePart += this.months + propertiesISOEquivalent.months } if (!short || this.days) { datePart += this.days + propertiesISOEquivalent.days } let timePart = '' if (!short || this.hours) { timePart += this.hours + propertiesISOEquivalent.hours } if (!short || this.minutes) { timePart += this.minutes + propertiesISOEquivalent.minutes } if (!short || (this.seconds || this.milliseconds)) { if (this.milliseconds) { timePart += (Math.trunc((this.seconds + this.milliseconds / 1000) * 1000000) / 1000000) + propertiesISOEquivalent.seconds } else { timePart += this.seconds + propertiesISOEquivalent.seconds } } if (!timePart && !datePart) { return 'PT0S' } if (!timePart) { return `P${datePart}` } return `P${datePart}T${timePart}` } const position = { value: 0 } function readNextNum (interval) { let val = 0 while (position.value < interval.length) { const char = interval[position.value] if (char >= '0' && char <= '9') { val = val * 10 + +char position.value++ } else { break } } return val } function parseMillisecond (interval) { const previousPosition = position.value const currentValue = readNextNum(interval) const valueStringLength = position.value - previousPosition switch (valueStringLength) { case 1: return currentValue * 100 case 2: return currentValue * 10 case 3: return currentValue case 4: return currentValue / 10 case 5: return currentValue / 100 case 6: return currentValue / 1000 } // slow path const remainder = valueStringLength - 3 return currentValue / Math.pow(10, remainder) } function parse (instance, interval) { if (!interval) { return } position.value = 0 let currentValue let nextNegative = 1 while (position.value < interval.length) { const char = interval[position.value] if (char === '-') { nextNegative = -1 position.value++ continue } else if (char === '+') { position.value++ continue } else if (char === ' ') { position.value++ continue } else if (char < '0' || char > '9') { position.value++ continue } else { currentValue = readNextNum(interval) if (interval[position.value] === ':') { instance.hours = currentValue ? nextNegative * currentValue : 0 position.value++ currentValue = readNextNum(interval) instance.minutes = currentValue ? nextNegative * currentValue : 0 position.value++ currentValue = readNextNum(interval) instance.seconds = currentValue ? nextNegative * currentValue : 0 if (interval[position.value] === '.') { position.value++ currentValue = parseMillisecond(interval) instance.milliseconds = currentValue ? nextNegative * currentValue : 0 } return } // skip space position.value++ const unit = interval[position.value] if (unit === 'y') { instance.years = currentValue ? nextNegative * currentValue : 0 } else if (unit === 'm') { instance.months = currentValue ? nextNegative * currentValue : 0 } else if (unit === 'd') { instance.days = currentValue ? nextNegative * currentValue : 0 } nextNegative = 1 } } } PostgresInterval.parse = function (interval) { const instance = { years: 0, months: 0, days: 0, hours: 0, minutes: 0, seconds: 0, milliseconds: 0 } parse(instance, interval) return instance } package/package.json000644 0000001211 3560116604 011541 0ustar00000000 000000 { "name": "postgres-interval", "main": "index.js", "version": "4.0.2", "description": "Parse Postgres interval columns", "license": "MIT", "repository": "bendrucker/postgres-interval", "author": { "name": "Ben Drucker", "email": "bvdrucker@gmail.com", "url": "https://www.bendrucker.me" }, "engines": { "node": ">=12" }, "scripts": { "test": "standard && tape test.js" }, "keywords": [ "postgres", "interval", "parser" ], "dependencies": {}, "devDependencies": { "standard": "^17.0.0", "tape": "^5.0.0" }, "files": [ "index.js", "index.d.ts", "readme.md" ] } package/readme.md000644 0000003471 3560116604 011044 0ustar00000000 000000 # postgres-interval [![tests](https://github.com/bendrucker/postgres-interval/workflows/tests/badge.svg)](https://github.com/bendrucker/postgres-interval/actions?query=workflow%3Atests) > Parse Postgres interval columns ## Install ```sh 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() // 1 hour 2 minutes 3 seconds interval.toISOString() // P0Y0M0DT1H2M3S interval.toISOStringShort() // PT1H2M3S ``` 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 #### `parse(pgInterval)` -> `interval` ##### pgInterval *Required* Type: `string` A Postgres interval string. This package is focused on parsing Postgres outputs. It optimizes for performance by assuming that inputs follow the default interval format. It does not perform any validation on the input. If any interval field is not found, its value will be set to `0` in the returned `interval`. #### `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, for example `P0Y0M0DT0H9M0S`. Also available as `interval.toISO()` for backwards compatibility. #### `interval.toISOStringShort()` -> `string` Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string shortened to minimum length, for example `PT9M`. ## License MIT © [Ben Drucker](http://bendrucker.me) package/index.d.ts000644 0000004613 3560116604 011165 0ustar00000000 000000 declare namespace PostgresInterval { export interface IPostgresInterval { years: number; months: number; days: number; hours: number; minutes: number; seconds: number; milliseconds: number; /** * Returns an interval string. This allows the interval object to be passed into prepared statements. * * ```js * var parse = require('postgres-interval') * var interval = parse('01:02:03') * // => { hours: 1, minutes: 2, seconds: 3 } * interval.toPostgres() * // 1 hour 2 minutes 3 seconds * ``` */ toPostgres(): string; /** * Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string, for example P0Y0M0DT0H9M0S. * * Also available as {@link toISOString toISOString}. * * ```js * var parse = require('postgres-interval') * var interval = parse('01:02:03') * // => { hours: 1, minutes: 2, seconds: 3 } * interval.toISO() * // P0Y0M0DT1H2M3S * ``` */ toISO(): string; /** * Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string, for example P0Y0M0DT0H9M0S. * * Also available as {@link toISO toISO} for backwards compatibility. * * ```js * var parse = require('postgres-interval') * var interval = parse('01:02:03') * // => { hours: 1, minutes: 2, seconds: 3 } * interval.toISOString() * // P0Y0M0DT1H2M3S * ``` */ toISOString(): string; /** * Returns an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) compliant string shortened to minimum length, for example `PT9M`. * * ```js * var parse = require('postgres-interval') * var interval = parse('01:02:03') * // => { hours: 1, minutes: 2, seconds: 3 } * interval.toISOStringShort() * // PT1H2M3S * ``` */ toISOStringShort(): string; } } /** * Parse Postgres interval columns. * * ```js * var parse = require('postgres-interval') * var interval = parse('01:02:03') * // => { hours: 1, minutes: 2, seconds: 3 } * interval.toPostgres() * // 1 hour 2 minutes 3 seconds * interval.toISOString() * // P0Y0M0DT1H2M3S * interval.toISOStringShort() * // PT1H2M3S * ``` * * @param raw A Postgres interval string. */ declare function PostgresInterval(raw: string): PostgresInterval.IPostgresInterval; export = PostgresInterval;