Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions packages/graphile-build-pg/src/plugins/PgTypesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import type { Plugin } from "graphile-build";

import makeGraphQLJSONType from "../GraphQLJSON";

import { parseInterval as rawParseInterval } from "../postgresInterval";
import LRU from "@graphile/lru";
import { parseInterval } from "../postgresInterval";

function indent(str) {
return " " + str.replace(/\n/g, "\n ");
Expand All @@ -14,17 +13,6 @@ function identity(value) {
return value;
}

const parseCache = new LRU({ maxLength: 500 });
function parseInterval(str) {
let result = parseCache.get(str);
if (!result) {
result = rawParseInterval(str);
Object.freeze(result);
parseCache.set(str, result);
}
return result;
}

export default (function PgTypesPlugin(
builder,
{
Expand Down
35 changes: 12 additions & 23 deletions packages/graphile-build-pg/src/postgresInterval.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
// @flow

export type Interval = {
years: number,
months: number,
days: number,
hours: number,
minutes: number,
seconds: number,
};

// Regexp construction enhanced from `postgres-interval`, which is licensed
// under the MIT license and is copyright (c) Ben Drucker <[email protected]>
// (bendrucker.me).
Expand All @@ -15,35 +24,15 @@ const DAY = `${NUMBER}\\s+days?`;
const TIME = "([+-])?(\\d+):(\\d\\d):(\\d\\d(?:\\.\\d{1,6})?)";

const INTERVAL = new RegExp(
"^\\s*" +
"^" +
// All parts of an interval are optional
[YEAR, MONTH, DAY, TIME].map(str => "(?:" + str + ")?").join("\\s*") +
"\\s*$"
"$"
);

export type Interval = {
years: number,
months: number,
days: number,
hours: number,
minutes: number,
seconds: number,
};

export function parseInterval(interval: string): Interval {
if (!interval) {
return {
years: 0,
months: 0,
days: 0,
hours: 0,
minutes: 0,
seconds: 0.0,
};
}

const [, years, months, days, plusMinusTime, hours, minutes, seconds] =
INTERVAL.exec(interval) || [];
INTERVAL.exec(interval || "") || [];

const timeMultiplier = plusMinusTime === "-" ? -1 : 1;

Expand Down