Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [3.1.0] - 2021-06-13

### Added
- typescript.

## [3.0.0] - 2021-01-02

### Changed
Expand Down
48 changes: 34 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

A wrapper around javascript array push/pop with a standard stack interface.

# Table of Contents
<img src="https://user-images.githubusercontent.com/6517308/121813242-859a9700-cc6b-11eb-99c0-49e5bb63005b.jpg">


# Contents
* [Install](#install)
* [require](#require)
* [import](#import)
* [API](#api)
* [Construction](#construction)
* [constructor](#constructor)
* [.push(element)](#pushelement)
* [.peek()](#peek)
* [.pop()](#pop)
Expand All @@ -29,21 +32,23 @@ A wrapper around javascript array push/pop with a standard stack interface.
npm install --save @datastructures-js/stack
```

### require
### JS
```js
const { Stack } = require('@datastructures-js/stack');
```

### import
### TS
```js
import { Stack } from '@datastructures-js/stack';
```

## API

### Construction
### constructor

#### using "new"

##### JS
```js
// empty stack
const stack = new Stack();
Expand All @@ -52,8 +57,18 @@ const stack = new Stack();
const stack = new Stack([10, 3, 8, 40, 1]);
```

##### TS
```TS
// empty stack
const stack = new Stack<number>();

// from an array
const stack = new Stack<number>([10, 3, 8, 40, 1]);
```

#### using ".fromArray"

##### JS
```js
// empty stack
const stack = Stack.fromArray([]);
Expand All @@ -66,7 +81,12 @@ const stack = Stack.fromArray(list);
const stack = Stack.fromArray(list.slice());
```

### .push(element)
##### TS
```ts
const stack = Stack.fromArray<number>([10, 3, 8, 40, 1]);
```

### .push(element: T)
push an element to the top of the stack.

<table>
Expand All @@ -76,14 +96,14 @@ push an element to the top of the stack.
<th align="center">runtime</th>
</tr>
<tr>
<td align="center">element: any</td>
<td align="center">Stack</td>
<td align="center">element: T</td>
<td align="center">Stack&lt;T&gt;</td>
<td align="center">O(1)</td>
</tr>
</table>

```js
stack.push('test');
stack.push(11);
```

### .peek()
Expand All @@ -95,13 +115,13 @@ returns the top element in the stack.
<th align="center">runtime</th>
</tr>
<tr>
<td align="center">any</td>
<td align="center">T</td>
<td align="center">O(1)</td>
</tr>
</table>

```js
console.log(stack.peek()); // test
console.log(stack.peek()); // 11
```

### .pop()
Expand All @@ -113,13 +133,13 @@ removes and returns the top element of the stack.
<th align="center">runtime</th>
</tr>
<tr>
<td align="center">any</td>
<td align="center">T</td>
<td align="center">O(1)</td>
</tr>
</table>

```js
console.log(stack.pop()); // test
console.log(stack.pop()); // 11
console.log(stack.peek()); // null
```

Expand All @@ -138,7 +158,7 @@ checks if the stack is empty.
</table>

```js
stack.push('test');
stack.push(11);
console.log(stack.isEmpty()); // false
```

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const Stack = require('./src/stack');
const { Stack } = require('./src/stack');

exports.Stack = Stack;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@datastructures-js/stack",
"version": "3.0.0",
"version": "3.1.0",
"description": "stack implementation in javascript",
"main": "index.js",
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions src/stack.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export class Stack<T> {
constructor(elements?: T[]);
isEmpty(): boolean;
size(): number;
peek(): T;
push(element: T): Stack<T>;
pop(): T;
toArray(): T[];
clear(): void;
clone(): Stack<T>;
static fromArray<T>(elements: T[]): Stack<T>;
}
2 changes: 1 addition & 1 deletion src/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@ class Stack {
}
}

module.exports = Stack;
exports.Stack = Stack;
2 changes: 1 addition & 1 deletion test/stack.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expect } = require('chai');
const Stack = require('../src/stack');
const { Stack } = require('../src/stack');

describe('stack tests', () => {
const stack = new Stack();
Expand Down