Class RangeMap<V>

Map with entries that contains a range key and a value

David Hsing

Type Parameters

  • V

Implements

  • Omit<Map<RangeMapKey, V>,
        | "delete"
        | "forEach"
        | "get"
        | "has"
        | "set"
        | "entries"
        | "keys"
        | "values"
        | "push">

Constructors

  • Construct a range map instance

    Type Parameters

    • V

    Parameters

    • Optionalentries: [RangeMapKey | [number, number] | [number, number, boolean, boolean], V][]

      the map entries

    • validation: boolean = true

      whether to compare the ranges to all the ranges previously, to determine if there are any conflicts

    Returns RangeMap<V>

    const map = new RangeMap([
    [[1, 30], 'green'],
    [[30, 60], 'blue'],
    [[60, 90], 'orange'],
    [[90, 100, true, true], 'red']
    ]);

Accessors

  • get [toStringTag](): string
  • Returns the string representation of the map identifier ('RangeMap')

    Returns string

    the string representation of the map identifier

  • get size(): number
  • Returns the size of map

    Returns number

    the size of map

Methods

  • Response for returning the list of key/value to iterate

    Returns IterableIterator<[RangeMapKey, V], any, any>

    for (const [key, value] of map) {
    console.log(value);
    }
  • Clears the map

    Returns void

  • Deletes the entry with the given key

    Parameters

    • key: RangeMapKey | [number, number] | [number, number, boolean, boolean]

      the key to delete

    Returns boolean

    whether the entry has been deleted

    map.deleteByKey([1, 50]);
    
  • Deletes all the entries with the given keys

    Parameters

    • keys: (RangeMapKey | [number, number] | [number, number, boolean, boolean])[]

      the keys to delete

    Returns boolean

    whether any of the entries has been deleted

    map.deleteByKeys([[1, 30], [30, 50]]);
    
  • Deletes the entry/entries with the given value

    Parameters

    • value: V

      the value to delete

    Returns boolean

    whether the entry/entries has been deleted

    map.deleteByValue('red');
    
  • Deletes all the entries with the given values

    Parameters

    • values: V[]

      the values to delete

    Returns boolean

    whether any of the entries has been deleted

    map.deleteByValues(['green', 'blue']);
    
  • Returns the key/value entries of the map

    Returns [RangeMapKey, V][]

    the key/value entries of the map

  • Processes each entry in the map

    Parameters

    • callback: ((value: V, key: RangeMapKey) => void)

      a callback function that processes each entry

        • (value, key): void
        • Parameters

          Returns void

    • OptionalthisArg: any

      any instance to retrieve 'this' reference in the callback function

    Returns void

    map.forEach((value, key) => {
    console.log(value);
    });
  • Processes each entry in the map with breakable capability

    Parameters

    • callback: ((value: V, key: RangeMapKey) => boolean)

      a callback function that processes each entry. Returning false indicates to break the map iteration

        • (value, key): boolean
        • Parameters

          Returns boolean

    • OptionalthisArg: any

      any instance to retrieve 'this' reference in the callback function

    Returns void

    map.forEachBreakable((value, key) => {
    return true;
    });
  • Processes each entry in the map with index capability

    Parameters

    • callback: ((value: V, key: RangeMapKey, index: number) => void)

      a callback function that processes each entry

        • (value, key, index): void
        • Parameters

          Returns void

    • OptionalthisArg: any

      any instance to retrieve 'this' reference in the callback function

    Returns void

    map.forEachIndexing((value, key, index) => {
    console.log(index);
    });
  • Returns the value of the given key

    Parameters

    • key: RangeMapKey | [number, number] | [number, number, boolean, boolean]

      the key to retrieve

    • Optionaldefaults: V

      the default value if not found

    Returns undefined | V

    the value of the given key

    const map = RangeMap.of([
    [[1, 50, true, true], 'white'],
    [[51, 100, false, true], 'black']
    ]);
    map.get([1, 50, true, true]); // 'white'
    map.get([51, 100, false, true]); // 'black'
  • Returns the value that associated to the number, by determining which bound contains the given number

    Parameters

    • digit: number

      the number to retrieve

    • Optionaldefaults: V

      the default value if not found

    Returns undefined | V

    the value that associated to the number, by determining which bound contains the given number

    const map = RangeMap.of([
    [[1, 50, true, true], 'white'],
    [[51, 100, false, true], 'black']
    ]);
    map.getByDigit(25); // 'white'
    map.getByDigit(50); // 'white'
    map.getByDigit(51); // undefined
    map.getByDigit(52); // 'black'
    map.getByDigit(200); // undefined
  • Returns whether the map contains all the given keys

    Parameters

    • keys: (RangeMapKey | [number, number] | [number, number, boolean, boolean])[]

      the keys to check

    Returns boolean

    whether the map contains all the given keys

    map.hasAllKeys([[1, 50], [51, 100]]);
    
  • Returns whether the map contains all the given values, matching exactly

    Parameters

    • values: V[]

      the values to check

    Returns boolean

    whether the map contains all the given values, matching exactly

    map.hasAllValues(['red', 'green', 'blue']);
    
  • Returns whether the map contains any of the given keys

    Parameters

    • keys: (RangeMapKey | [number, number] | [number, number, boolean, boolean])[]

      the keys to check

    Returns boolean

    whether the map contains any of the given keys

    const map = RangeMap.of([
    [[1, 50, true, true], 'white'],
    [[51, 100, false, true], 'black']
    ]);
    map.hasAnyKeys([[1, 50, true, true], [20, 60]]); // true
  • Returns whether the map contains any of the given values

    Parameters

    • values: V[]

      the values to check

    Returns boolean

    whether the map contains any of the given values

    const map = RangeMap.of([
    [[1, 50, true, true], 'white'],
    [[51, 100, false, true], 'black']
    ]);
    map.hasAnyValues(['red', 'black']); // true
    map.hasAnyValues(['top', 'right']); // false
  • Returns whether the map contains the given key

    Parameters

    • key: RangeMapKey | [number, number] | [number, number, boolean, boolean]

      the key to check

    Returns boolean

    whether the map contains the given key

    map.hasKey([1, 30]);
    
  • Returns whether the map contains the given key/value pair

    Parameters

    • key: RangeMapKey | [number, number] | [number, number, boolean, boolean]

      the key to check

    • value: V

      the value to check

    Returns boolean

    whether the map contains the given key/value pair

    const map = RangeMap.of([
    [[1, 50], 'white']
    ]);
    map.hasKeyValue([1, 50], 'white'); // true
    map.hasKeyValue([1, 50], 'black'); // false
  • Returns whether any entries of the map that contains the given values

    Parameters

    • value: V

      the value to check

    Returns boolean

    whether any entries of the map that contains the given values

    const map = RangeMap.of([
    [[1, 50, true, true], 'white'],
    [[51, 100, false, true], 'black']
    ]);
    map.hasValue('white'); // true
    map.hasValue('blue'); // false
  • Returns whether the map is empty

    Returns boolean

    whether the map is empty

  • Returns whether the map is not empty

    Returns boolean

    whether the map is not empty

  • Sets the value of the given key

    Parameters

    • key: RangeMapKey | [number, number] | [number, number, boolean, boolean]

      the key to set

    • value: V

      the value to set

    • validation: boolean = true

      whether to compare the range to all the ranges previously, to determine if there are any conflicts

    Returns void

    map.set([60, 80, true, true], 'volcano');
    
  • Returns the string representation of the map elements

    Returns string

    the string representation of the map elements

    const map = RangeMap.of([
    [[1, 50, true, true], 'white'],
    [[51, 100, false, true], 'black']
    ]);
    console.log(map.toString()); // '[start:1,end:50,startInclusive:true,endInclusive:true]:white;[start:51,end:100,startInclusive:false,endInclusive:true]:black'
  • Returns the value array of the map

    Returns V[]

    the value array of the map

  • Construct a range map instance

    Type Parameters

    • V

    Parameters

    • Optionalentries: [RangeMapKey | [number, number] | [number, number, boolean, boolean], V][]

      the map entries

    • validation: boolean = true

      whether to compare the ranges to all the ranges previously, to determine if there are any conflicts

    Returns RangeMap<V>

    a range map instance

    const map = RangeMap.of([
    [[1, 30], 'green'],
    [[30, 60], 'blue'],
    [[60, 90], 'orange'],
    [[90, 100, true, true], 'red']
    ]);