JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4
  • Score
    100M100P100Q32475F
  • License MIT

The sequenced array class which maintains sorted order.

Package Exports

  • sequenced-array

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sequenced-array) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Build Status Coverage

sequenced-array

The sequenced array class which maintains sorted order.

Install

$ npm i sequenced-array

Usage

import SequencedArray from 'sequenced-array'

new SequencedArray(arrayLength, {desc, compare})

new SequencedArray(array, {desc, compare})

  • desc ?boolean=false Whether the array should be sorted in decending order. By default SequencedArrays are in ascending order.
  • compare ?Function=(a, b) => a - b The compare function as the same as the compareFunction of Array.prototype.filter(compareFunction)

Creates a SequencedArray.

// creates an empty array
new SequencedArray([])

// creates an array of length 10 with 10 empty items.
new SequencedArray(10)

// creates an array of [1, 2, 3]
new SequencedArray([1, 2, 3])

// creates an order book which puts the highest bid at the top
const orderBook = new SequencedArray([], {
  desc: true,
  compare: (a, b) => a.price - b.price
})
orderBook.insert({price: 3, amount: 2})
orderBook.insert({price: 1, amount: 1})
orderBook.insert({price: 2, amount: 1})

console.log(orderBook[0])   // {price: 1, amount: 1}
console.log(orderBook[1])   // {price: 2, amount: 1}
console.log(orderBook[2])   // {price: 3, amount: 2}

.match(item, index): number | undefined

  • item any
  • index number

Matches item with the item at index index

Returns

  • < 0 indicates that item should be before the index index
  • = 0 indicates that item equals the item at index index
  • > 0 indicates that item should be after the index index
  • undefined indicates that the item at index index is an empty item, so it can not match item
const arr = new SequencedArray(4)
arr[2] = 2
arr.match(5, 0)     // undefined
arr.match(1, 2)     // -1
arr.match(3, 2)     // 1
arr.match(2, 2)     // 0

.find(item): Array

Finds which location should item be located at.

Returns [min, max]

  • If min equals to max, it indicates that item is equals to array[index]
  • Otherwise, it indicates that item could be inserted between index min and index max
new SequencedArray([1, 2, 3, 4]).find(2.5)  // [1, 2]
new SequencedArray([1, 2, 3, 4]).find(2)    // [1, 1]

.insert(item): {index: number, inserted: boolean}

Insert item into the array and maintains the sorting order.

Returns Object

  • index number The item has been located at index index
  • inserted boolean
    • true the new item has been inserted into the array
    • false the item equals to array[index] and it is unnecessary to insert the item as a new one.
const a = new SequencedArray([1, 2, 3, 4])
a.insert(2.5)
// {
//   index: 2,
//   inserted: true
// }

// Then, the array is:
// [1, 2, 2.5, 3, 4]
const b = new SequencedArray([1, 2, 3, 4])
b.insert(2)
// {
//   index: 1,
//   inserted: false
// }

// `2` is already located at index 1
const c = new SequencedArray([])
c.insert(3)
c.insert(1)
c.insert(2)
// Then the array is: [1, 2, 3]

const d = new SequencedArray([], {desc: true})
c.insert(3)
c.insert(1)
c.insert(2)
// Then the array is: [3, 2, 1]

License

MIT