Skip to content

Latest commit

 

History

History
131 lines (111 loc) · 4.21 KB

README.md

File metadata and controls

131 lines (111 loc) · 4.21 KB

Range Admin - Effectively Managing Range

license npm-version GitHub last commit

import { Range } from "range-admin"

(new Range(1, 10)).isEqual(new Range(1, 10)) // true
(new Range(1, 10)).compare(new Range(5, 13)) // "OVERLAP"
(new Range(1, 10)).findCommon(new Range(5, 10)) // new Range(5, 10)
# npm
npm install range-admin

# yarn
yarn add range-admin

Feature

Range

new Range(1, 10)
  • toString() - Returns string representation of the range: start~end
  • toArray(step: number) - Returns array based on the range
  • isEqual(otherRange: Range) - Check if two ranges are equal
  • isDisjoint(otherRange: Range) - Check if two ranges are disjoint (no relationship)
  • isSubsetOf(otherRange: Range) - Check if range is subset of otherRange (range is contained in otherRange)
  • isOverlap(otherRange: Range) - Check if two ranges are overlapped with each other
  • isAdjacent(otherRange: Range) - Check if two ranges are adjacent
  • compare(otherRange: Range) - Compare relationship with other range.
  • findCommon(otherRange: Range) - Find common part of the range with other range.

toArray(step: number)

(new Range(1, 11)).toArray() // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
(new Range(0, 1001)).toArray(2) // [0, 2, 4, 6, ..., 998, 1000]

compare(otherRange: Range)

(new Range(3, 5)).compare(new Range(1, 4)) // "OVERLAP"
(new Range(3, 5)).compare(new Range(1, 5)) // "SUBSET"

The types of relationship are as follows: "equal", "subset", "superset", "disjoint", "overlap"

findCommon(otherRange: Range)

(new Range(3, 5)).compare(new Range(1, 4)) // new Range(3, 4)
(new Range(3, 5)).compare(new Range(9, 10)) // null

isEqual(...ranges: Range[])

const range1 = new Range(1, 3)
const range2 = new Range(2, 5)

isEqual(range1, range1, range1) // true
isEqual(range2, range1, range1) // false

Check if all the ranges are equal.

isDisjoint(...ranges: Range[])

const range1 = new Range(1, 3)
const range2 = new Range(3, 5)

isDisjoint(range1, range2, new Range(9, 10)) // true
isDisjoint(range1, range1, new Range(9, 10)) // false

Check if all the ranges are disjoint.

isSubset(...ranges: Range[])

isSubset(new Range(1, 3), new Range(1, 8)) // false (2nd range is superset of 1st range)
isSubset(new Range(1, 100), new Range(1, 10), new Range(1, 2)) // true

Check if the range is subset of its preceding range. (e.g. 2nd range should be subset of the 1st range)
Opposite with isSuperset.

isSuperset(...ranges: Range[])

isSuperset(new Range(1, 3), new Range(1, 8)) // false (2nd range is superset of 1st range)
isSuperset(new Range(1, 100), new Range(1, 10), new Range(1, 2)) // true

Check if the range is superset of its following range. (e.g. 2nd range should be superset of the 1st range)
Opposite with isSubset.

isAdjacent(...ranges: Range[])

isAdjacent(new Range(1, 2), new Range(2, 3), new Range(3, 10)) // true
isAdjacent(new Range(1, 2), new Range(4, 5), new Range(5, 6)) // false

Check if all the ranges are adjacent with each other.

makeRangeArray(range: Range | [number, number], step?: number)

makeRangeArray([0, 11], 2) // [0, 2, 4, 6, 8, 10]
makeRangeArray(new Range(0, 11), 2) // [0, 2, 4, 6, 8, 10]
// equivalent of new Range(0, 11).toArray(2)

Make array of range.

Contribution

# build
npm run build

# build (using nodemon)
npm run dev

# test (using jest)
npm test