priorities v1.0.0
Priority List
Store values based on their priorities.
Getting Started
Installing and structure
Install the module via NPM: npm i priorities
.
After that you must require this module in any file you'd want to use it in.
const List = require("priorities");
// List is the class. Make an instance of it!
const myList = new List();
The PriorityList
is a multi dimensional array based on the value's priorities! Every value has an index and a subIndex. The index starts from 1 while the subIndex starts from 0. The code below is a simple priority list structure.
{
1: ["First", "Second", "Third"],
2: "Forth"
}
First
's index is 1 and subIndex is 0, making it the value with the highest priority. Forth
's index is 2 and subIndex is 0, making it the value with the lowest priority.
The PriorityList
class has a lot of useful features on working with priorities. See the documentation for more info.
This storage is NOT persistent
It just stores values in a unique way - it does not save them in a file and then load them up. All data in the priority lists will be lost once the node process is killed.
Examples
Adding a value:
myList.add(1, "First") // We added the value 'First' to the first index of the list.
myList.add(1, "Second")
myList.add(2, "Third")
myList.add(2, "Forth")
myList.add(3, "Fifth")
myList.add(4, "Sixth")
Removing a value:
myList.remove("First") // We remove 'First' from the list.
Getting values:
myList.getByVal("Second") // {value: "Second", index: 1, subIndex: 0}
myList.getByPos(1) // {value: "Second", index: 1, subIndex: 0} since there is only 1 value in index 1.
myList.add(1, "RealSecond")
myList.getByPos(1, 1) // {value: "RealSecond", index: 1, subIndex: 1}
myList.find((val, index, subIndex) => val == "Fifth") // {value: "Fifth", index: 1, subIndex: 1}
Working with priorities:
myList.compare("Second", "RealSecond") // Returns: "higher". Because "Second"'s subIndex is smaller than "RealSecond"'s subIndex, so "Second" is a higher priority.
myList.highestOf("Sixth", "Forth", "Second") // Returns: "Second".
myList.lowestOf("Fifth", "Third", "RealSecond") // Returns: "Fifth"
myList.highest // Returns: Second
myList.lowest // Returns: Sixth
Running the tests
Run the tests by doing npm run test
Documentation
Click here to see the documentation.
7 years ago