Skip to content

Latest commit

 

History

History

intervals

intervals

npm version npm downloads Twitter Follow

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

About

Closed/open/semi-open interval data type, queries & operations.

Supports 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.

Status

STABLE - used in production

Installation

yarn add @thi.ng/intervals
// ES module
<script type="module" src="https://unpkg.com/@thi.ng/intervals?module" crossorigin></script>

// UMD
<script src="https://unpkg.com/@thi.ng/intervals/lib/index.umd.js" crossorigin></script>

Package sizes (gzipped, pre-treeshake): ESM: 1.43 KB / CJS: 1.48 KB / UMD: 1.57 KB

Dependencies

API

Generated API docs

import { interval, 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("[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 interval relative to point (true if RHS < x)
i.isBefore(-1)
// false

i.isBefore(1)
// true

// classify interval relative to point (true if LHS > x)
i.isAfter(-1);
// true

i.isAfter(1);
// false

// 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());
// 3 (aka Classifier.SUBSET)

// create transformed interval
// (here scaled around centroid)
i.map((x) => x + (x - i.centroid()) * 2).toString();
// [-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

Maintainer

Contributors

License

© 2018 - 2020 Karsten Schmidt // Apache Software License 2.0