Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/onsetsu/stack-es2015-module

The stack data type as es2015 module
https://github.com/onsetsu/stack-es2015-module

Last synced: 27 days ago
JSON representation

The stack data type as es2015 module

Awesome Lists containing this project

README

        

# stack-es2015-module [![Build Status](https://travis-ci.org/onsetsu/stack-es2015-module.svg?branch=master)](https://travis-ci.org/onsetsu/stack-es2015-module)
A simple stack data structure provided as es2015 module.

## Installation

As npm for Node.js:

```
$ npm install stack-es2015-module --save
```

Or download the [bundle](https://raw.githubusercontent.com/onsetsu/stack-es2015-module/master/dist/stack-es2015-modules.js) file.

## Building

```
$ npm run-script build
```

creates the *bundle* file in the `dist` folder.

## Testing

As npm package:

```
$ npm test
```

## Example

```js
import Stack from 'stack-es2015-module';

let stack = new Stack();

stack.push(42);
stack.push(17);
stack.top(); // 17
stack.pop();
stack.top(); // 42
stack.withElement(33, () => {
stack.top(); // 33
});
```

## API

### Stack()

Initializes a new empty `Stack`.

### Stack#top()

Returns the top element of the stack.

### Stack#pop()

Pops the top element of the stack.

### Stack#push(element)

Pushes the `element` at the top of the stack.

### Stack#withElement(element, callback, context)

Pushes the `element` at the top of the stack and executes the `callback` with the optional `context`.
After successfully returning from the `callback` or upon an uncatched error, the top element is poped from the stack.

## Licence

MIT