@truck/stack
v1.0.1
Published
A JavaScript implementation of the Stack data structure
Downloads
1
Readme
Stack
A JavaScript Stack data structure.
A Stack follows a LIFO (Last-In-First-Out) methodology, just like pancakes in a plate.
Installation
Install @truck/stack
via npm:
$ npm install --save @truck/stack
Methods
Methods > constructor(maximumLength?: int)
Build a new stack. Pass an optional maximumLength
which will limit the Stack length, the default
is 65535.
Methods > empty(): void
(O(1))
Empties the Stack, resetting the length back to 0.
Methods > isEmpty(): boolean
(O(1))
Checks whether the Stack is empty.
Methods > isFull(): boolean
(O(1))
Checks whether the Stack is full.
Methods > peek(): any
(O(1))
Get the item at the front of the Stack without removing it.
Methods > pop(): any
(O(1))
Removes the top (last inserted) value from the Stack and returns it.
Methods > push(value: any): void
(O(1))
Adds a value to the top of the Stack. Throws a RangeError
when the addition will exceed the
maximum length allowed (defined in the constructor
).
Methods > toArray(): any[]
(O(n))
Returns the Stack as an array.
Properties
Properties > .length: number
Returns the current length of the Stack.
Examples
A Stack is a standard class which can be instantiated with the new
keyword:
// Build a new Stack with a maximum length of 10 values
const stack = new Stack(10);
// Get the length of the Stack
let length = Stack.length; // 0
// Add some values to the Stack
stack.push(1);
stack.push('two');
stack.push({ three: 'three' });
stack.push(false);
// Check if the Stack is full
if (!stack.isFull()) {
stack.push('FIVE');
}
// Get the length of the Stack
length = Stack.length; // 5
// Get the Stack as an array
const stackAsArray = stack.toArray(); // [1, 'two', { three: 'three' }, false, 'FIVE']
// Remove the values from the Stack
stack.pop(); // 'FIVE'
stack.pop(); // false
stack.pop(); // { three: 'three' }
stack.pop(); // 'two'
// Check if the Stack is empty
if (!stack.isEmpty()) {
stack.pop(); // 1
}
// Get the length of the Stack
length = Stack.length; // 0
Testing
Use the following command to run all the tests described below together:
$ docker-compose run --rm app npm test
Testing > Commit messages
Commit messages are linted through the use of husky and @commitlint/cli using the @commitlint/config-conventional commit convention.
Please read through the AngularJS Git Commit Message Conventions to get a better understanding of how commit messages are formatted.
After doing an npm install
the required git hooks wil be added automatically and commit messages
will be linted automatically.
Testing > Linting
Linting is done using eslint using the eslint-config-airbnb-base configuration with very few alterations, all of which can be seen in the .eslintrc file in the root of this repository.
Linting can be run in isolation through the command:
$ docker-compose run --rm app npm run test:lint
Testing > Auditing
Auditing of dependencies is done through the npm audit command-line tool.
Auditing can be run in isolation through the command:
$ docker-compose run --rm app npm run test:vulnerabilities
Testing > Unit testing
Unit testing is done with jest. The test file for each file to be tested is to
be placed alongside the file in testing and marked with the .test.js
extension.
Unit testing can be run in isolation through the command:
$ docker-compose run --rm app npm run test:scripts
Contributing
Contributions are always welcome, just submit a PR to get the conversation going. Please make sure all tests pass before submitting a PR.
Contributing > Releases
The moment a PR is merged into the master
branch
semantic-release will kick-off a new
release, thus the importance of clear commit messages.