interval-spans
v0.1.1
Published
[![npm](https://img.shields.io/npm/v/interval-spans?style=plastic)](https://www.npmjs.com/package/interval-spans)
Downloads
91
Readme
Interval Spans
For when you need to create interval spans and no other library will do.
Easily create interval spans, look up to which interval span a number belongs to (if any) and attach metadata to your interval spans.
Installation
yarn add interval-spans
npm install interval-spans
Usage
Interval Span
Creates an interval span from start
(inclusive) to end
(exclusive) and attach
Constructor
API
new IntervalSpan<MetadataShape = Record<string, any>>(start: number, end: number, metadata?: MetadataShape);
Example
import { IntervalSpan } from 'interval-span';
const singleDigitIntegerSpan = new IntervalSpan<{ name: string }>(0, 10, {
name: 'single-digits',
});
Includes
API
instance.includes(numberToTest: number): boolean;
Example
const intervalSpan = new IntervalSpan(0, 10);
intervalSpan.includes(0); // true
intervalSpan.includes(10); // false
Interval Spans
Constructor
API
new IntervalSpans(intervalSpans: IntervalSpan[]);
Example
import { IntervalSpans } from 'interval-spans';
const intervalSpans = new IntervalSpans([new IntervalSpan(0, 10), new IntervalSpan(10, 20)]);
Get Span by Number
Finds the span which includes a number.
API
instance.getSpanByNumber(numberToTest: number): IntervalSpan | null;
Example
const intervalSpans = new IntervalSpans<{ name: string }>([
new IntervalSpan(0, 10, { name: 'single-digits' }),
new IntervalSpan(10, 20, { name: 'ten-to-twenty' }),
]);
intervalSpans.getSpanByNumber(0); // { start: 0, end: 10, metadata: { name: 'single-digits' }}
intervalSpans.getSpanByNumber(10); // { start: 10, end: 20, metadata: { name: 'ten-to-twenty' }}
intervalSpans.getSpanByNumber(20); // undefined;
License
MIT