Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle infinite ranges #1

Open
benjie opened this issue Jun 28, 2017 · 0 comments
Open

Handle infinite ranges #1

benjie opened this issue Jun 28, 2017 · 0 comments

Comments

@benjie
Copy link

benjie commented Jun 28, 2017

From my reading of the PG manual I think it's valid to have [,] as a range (also (,), [,), (,], (1,], [,20], etc).

https://www.postgresql.org/docs/9.6/static/rangetypes.html#RANGETYPES-INFINITE

Here's some (untested) code that might handle these if you're happy with nulls (since it's not clear what ±infinity would be represented as for each type that can be in a range):

"use strict";

module.exports = {
  parse: function parse(str) {
    var parts = str.split(",");
    if (parts.length !== 2) {
      throw new Error("Invalid range");
    }

    return {
      start: parts[0] ? {
        inclusive: parts[0][0] === "[",
        value: parts[0].slice(1)
      } : null,
      end: parts[1] ? {
        inclusive: parts[1][parts[1].length - 1] === "]",
        value: parts[1].slice(0, -1)
      } : null
    };
  },
  serialize: function serialize(obj) {
    var start = obj.start,
        end = obj.end;

    var inclusivity = {
      true: "[]",
      false: "()"
    };

    return [start ? inclusivity[start.inclusive][0] + start.value : "[", end ? end.value + inclusivity[end.inclusive][1] : "]"].join(",");
  }
};

This does not handle the difference between inclusive and exclusive infinity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant