4.2.74 • Published 2 days ago

@thi.ng/intervals v4.2.74

Weekly downloads
3
License
Apache-2.0
Repository
github
Last release
2 days ago

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 parse() for details.

Status

STABLE - used in production

Search or submit any issues for this package

Breaking changes

With version 3.0.0 the API has been updated to be largely functional rather than OOP, with all static (and most instance) Interval methods converted into standalone functions. The only class methods remaining are to implement these standard interfaces: ICompare, IContains, ICopy, IEquiv.

Installation

yarn add @thi.ng/intervals

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/intervals"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const intervals = await import("@thi.ng/intervals");

Package sizes (gzipped, pre-treeshake): ESM: 1.65 KB

Dependencies

Usage examples

Several demos in this repo's /examples directory are using this package.

A selection:

ScreenshotDescriptionLive demoSource
Interactive pixel sorting tool using thi.ng/color & thi.ng/pixelDemoSource

API

Generated API docs

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

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

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

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

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

contains(i, 0.999999);
// true

// classify interval relative to point (true if RHS < x)
isBefore(i, -1)
// false

isBefore(i, 1)
// true

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

isAfter(i, 1);
// false

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

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

// classify WRT given interval arg
// returns Classifier enum
classify(i, infinity());
// 3 (aka Classifier.SUBSET)

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

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

// close RHS
i.ropen = false;

// iterator of 0.25-spaced values in interval
[...values(i, 0.25)];
// [ 0, 0.25, 0.5, 0.75, 1 ] => now includes 1

// iterator of n equidistant samples
[...samples(i, 4)]
// [ 0, 0.3333333333333333, 0.6666666666666666, 1 ]

// constrain values to interval (taking openness into account)
max(interval("(0..1)"), -2)
// 0.000001

// if given value is outside interval, uses opt epsilon value
// to return closest inside value (default: 1e-6)...
max(interval("(0..1)"), -2, 1e-3)
// 0.001

min(interval("(0..1)"), 2, 1e-3)
// 0.999

// clamp on both sides
clamp(interval("[0..1)"), -2, 1e-3)
// 0
clamp(interval("[0..1)"), 2, 1e-3)
// 0.999

Authors

Maintainer

Contributors

If this project contributes to an academic publication, please cite it as:

@misc{thing-intervals,
  title = "@thi.ng/intervals",
  author = "Karsten Schmidt and others",
  note = "https://thi.ng/intervals",
  year = 2018
}

License

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

4.2.74

2 days ago

4.2.73

5 days ago

4.2.72

14 days ago

4.2.71

17 days ago

4.2.70

29 days ago

4.2.69

1 month ago

4.2.68

1 month ago

4.2.67

1 month ago

4.2.66

2 months ago

4.2.65

2 months ago

4.2.64

2 months ago

4.2.63

2 months ago

4.2.61

2 months ago

4.2.62

2 months ago

4.2.60

2 months ago

4.2.59

3 months ago

4.2.56

3 months ago

4.2.57

3 months ago

4.2.58

3 months ago

4.2.54

3 months ago

4.2.53

3 months ago

4.2.51

4 months ago

4.2.52

4 months ago

4.2.49

4 months ago

4.2.50

4 months ago

4.2.48

5 months ago

4.2.47

5 months ago

4.2.42

6 months ago

4.2.44

6 months ago

4.2.45

5 months ago

4.2.40

6 months ago

4.2.41

6 months ago

4.2.46

5 months ago

4.2.34

9 months ago

4.2.39

8 months ago

4.2.36

9 months ago

4.2.37

9 months ago

4.2.38

8 months ago

4.2.33

11 months ago

4.2.32

12 months ago

4.2.31

1 year ago

4.2.28

1 year ago

4.2.29

1 year ago

4.2.27

1 year ago

4.2.30

1 year ago

4.2.26

1 year ago

4.2.23

1 year ago

4.2.25

1 year ago

4.2.20

1 year ago

4.2.21

1 year ago

4.2.22

1 year ago

4.2.19

1 year ago

4.2.10

2 years ago

4.2.11

2 years ago

4.2.12

2 years ago

4.2.17

1 year ago

4.2.18

1 year ago

4.2.13

1 year ago

4.2.14

1 year ago

4.2.15

1 year ago

4.2.16

1 year ago

4.2.9

2 years ago

4.2.8

2 years ago

4.2.3

2 years ago

4.2.5

2 years ago

4.2.4

2 years ago

4.2.7

2 years ago

4.2.6

2 years ago

4.2.2

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago

4.1.6

2 years ago

4.1.5

2 years ago

4.1.4

2 years ago

4.0.8

2 years ago

4.1.3

2 years ago

4.1.0

2 years ago

4.1.2

2 years ago

4.1.1

2 years ago

4.0.7

2 years ago

4.0.4

3 years ago

4.0.6

2 years ago

4.0.1

3 years ago

4.0.0

3 years ago

4.0.3

3 years ago

4.0.2

3 years ago

3.0.13

3 years ago

3.0.12

3 years ago

3.0.11

3 years ago

3.0.10

3 years ago

3.0.9

3 years ago

3.0.8

3 years ago

3.0.7

3 years ago

3.0.6

3 years ago

3.0.5

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.0

3 years ago

2.1.5

3 years ago

2.1.4

3 years ago

2.1.3

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.25

4 years ago

2.0.24

4 years ago

2.0.23

4 years ago

2.0.22

4 years ago

2.0.21

4 years ago

2.0.20

4 years ago

2.0.19

4 years ago

2.0.18

4 years ago

2.0.17

4 years ago

2.0.16

4 years ago

2.0.15

4 years ago

2.0.14

4 years ago

2.0.13

4 years ago

2.0.12

4 years ago

2.0.11

4 years ago

2.0.10

4 years ago

2.0.9

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.3

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.15

4 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago