Giter VIP home page Giter VIP logo

Comments (3)

mmkal avatar mmkal commented on September 10, 2024

What's the use case? You should be able to use JavaScript's -Infinty and Infinty keywords. They have number type in typescript.

Using strings for number-typed arguments is exactly the sort of thing this library is supposed to protect against, unless you've found something you're unable to do. This works, and IMO is better than using "-inf"/"+inf":

it("can use zrangebyscore with infinite limits", async () => {
    const client = createHandyClient();
    await client.zadd("myzset", [1, "one"], [2, "two"], [3, "three"], [-Number.MAX_VALUE, "lowest"], [Number.MAX_VALUE, "highest"]);

    const all = await client.zrangebyscore("myzset", -Infinity, Infinity);
    expect(all).toEqual(["lowest", "one", "two", "three", "highest"]);

    const positives = await client.zrangebyscore("myzset", 0, Infinity);
    expect(positives).toEqual(["one", "two", "three", "highest"]);

    const negatives = await client.zrangebyscore("myzset", -Infinity, 0);
    expect(negatives).toEqual(["lowest"]);
});

from handy-redis.

mateuszlewko avatar mateuszlewko commented on September 10, 2024

Function parameter that has type: number | "+inf" | "-inf", doesn't allow any string. It either has to be string literal "+inf" / "-inf" or variable of type "+inf" | "-inf". Passing variable of type string results in:

Argument of type 'string' is not assignable to parameter of type 'number | "+inf" | "-inf"'.ts(2345)

Although passing Infinity has the same result (thanks to the fact that node uses the same 64 bit floating point numbers as redis), it doesn't mean it has the same effect under the hood. I can imagine that passing "+inf" could be slightly faster, as it always means 'last element in set' and this search could be done in O(1), where as search for any number (including infinity) takes O(log n). I could be wrong about the fact that there is any noticeable difference, but having type number | "+inf" | "-inf" doesn't have any downsides, and it feels more api compliant.

Anyway, it's still probably low priority.

from handy-redis.

mmkal avatar mmkal commented on September 10, 2024

That's a good point, if redis and javascript used different float implementations using Infinty might not work anymore. Right now the types are automatically generated from redis-doc's command.json (otherwise this project would be very hard to maintain). But the redis-doc just lists max and min as double.

So possibly this issue should be moved to that repo - since it's not actually a double. It's a double, or -inf, or +inf, or in fact open ranges like (123. That last one will be very hard to model in a non-hacky way in typescript, without resorting to string. Most double values in commands.json don't allow those special values because they're not limits. I can't off the top of my head think of a clean way to separate min/max from, say, the increment amount in incrbyfloat. Open to suggestions though.

from handy-redis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.