Options
All
  • Public
  • Public/Protected
  • All
Menu

@thi.ng/intervals

@thi.ng/intervals

npm (scoped) npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Data type for closed, open and semi-open intervals, point / range queries and set operations with other intervals (union, intersection, difference).

Furthermore, a parser for ISO 80000-2 / ISO 31-11 interval notation is provided. See Interval.parse() for details.

Installation

yarn add @thi.ng/intervals

Dependencies

Usage examples

import { Interval } from "@thi.ng/intervals";

// [0 .. +∞] (fully closed)
a = Interval.withMin(0);

// [-∞ .. 1) (open on RHS)
b = Interval.withMax(1, true);

i = a.intersection(b);
i.toString();
// [0 .. 1)

// parse from string
Interval.parse("[0 .. 1)")
// Interval { l: 0, r: 1, lopen: false, ropen: true }

i.contains(1);
// false (because interval is open on RHS)

i.contains(0.999999);
// true

// classify point (true if x < LHS)
i.isBefore(-1)
// true

// classify point (true if x > RHS)
i.isAfter(1);
// true

// grow interval to include 2 => [0 ... 2]
i2 = i.include(2);

// sort order: LHS -> RHS
i.compare(i2);
// -1

// classify WRT given interval arg
i.classify(Interval.infinity());
// Classifier.SUBSET

// create transformed interval
// (here scaled around centroid)
i.map((x) => x + (x - i.centroid()) * 2);
// [-1 .. 2)

// iterator of decimated interval values
[...i.values(0.25)];
// [ 0, 0.25, 0.5, 0.75 ]

// close RHS
i.ropen = false;

[...i.values(0.25)];
// [ 0, 0.25, 0.5, 0.75, 1 ] => now includes 1

Authors

  • Karsten Schmidt

License

© 2018 Karsten Schmidt // Apache Software License 2.0

Generated using TypeDoc