Skip to content

Commit a5d2b20

Browse files
authored
Merge pull request #13 from datastructures-js/development
add typescript
2 parents 19cd39a + b0ea1b8 commit a5d2b20

7 files changed

Lines changed: 55 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [3.1.0] - 2021-06-13
10+
11+
### Added
12+
- typescript.
13+
914
## [3.0.0] - 2021-01-02
1015

1116
### Changed

README.md

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66

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

9-
# Table of Contents
9+
<img src="https://user-images.githubusercontent.com/6517308/121813242-859a9700-cc6b-11eb-99c0-49e5bb63005b.jpg">
10+
11+
12+
# Contents
1013
* [Install](#install)
1114
* [require](#require)
1215
* [import](#import)
1316
* [API](#api)
14-
* [Construction](#construction)
17+
* [constructor](#constructor)
1518
* [.push(element)](#pushelement)
1619
* [.peek()](#peek)
1720
* [.pop()](#pop)
@@ -29,21 +32,23 @@ A wrapper around javascript array push/pop with a standard stack interface.
2932
npm install --save @datastructures-js/stack
3033
```
3134

32-
### require
35+
### JS
3336
```js
3437
const { Stack } = require('@datastructures-js/stack');
3538
```
3639

37-
### import
40+
### TS
3841
```js
3942
import { Stack } from '@datastructures-js/stack';
4043
```
44+
4145
## API
4246

43-
### Construction
47+
### constructor
4448

4549
#### using "new"
4650

51+
##### JS
4752
```js
4853
// empty stack
4954
const stack = new Stack();
@@ -52,8 +57,18 @@ const stack = new Stack();
5257
const stack = new Stack([10, 3, 8, 40, 1]);
5358
```
5459

60+
##### TS
61+
```TS
62+
// empty stack
63+
const stack = new Stack<number>();
64+
65+
// from an array
66+
const stack = new Stack<number>([10, 3, 8, 40, 1]);
67+
```
68+
5569
#### using ".fromArray"
5670

71+
##### JS
5772
```js
5873
// empty stack
5974
const stack = Stack.fromArray([]);
@@ -66,7 +81,12 @@ const stack = Stack.fromArray(list);
6681
const stack = Stack.fromArray(list.slice());
6782
```
6883

69-
### .push(element)
84+
##### TS
85+
```ts
86+
const stack = Stack.fromArray<number>([10, 3, 8, 40, 1]);
87+
```
88+
89+
### .push(element: T)
7090
push an element to the top of the stack.
7191

7292
<table>
@@ -76,14 +96,14 @@ push an element to the top of the stack.
7696
<th align="center">runtime</th>
7797
</tr>
7898
<tr>
79-
<td align="center">element: any</td>
80-
<td align="center">Stack</td>
99+
<td align="center">element: T</td>
100+
<td align="center">Stack&lt;T&gt;</td>
81101
<td align="center">O(1)</td>
82102
</tr>
83103
</table>
84104

85105
```js
86-
stack.push('test');
106+
stack.push(11);
87107
```
88108

89109
### .peek()
@@ -95,13 +115,13 @@ returns the top element in the stack.
95115
<th align="center">runtime</th>
96116
</tr>
97117
<tr>
98-
<td align="center">any</td>
118+
<td align="center">T</td>
99119
<td align="center">O(1)</td>
100120
</tr>
101121
</table>
102122

103123
```js
104-
console.log(stack.peek()); // test
124+
console.log(stack.peek()); // 11
105125
```
106126

107127
### .pop()
@@ -113,13 +133,13 @@ removes and returns the top element of the stack.
113133
<th align="center">runtime</th>
114134
</tr>
115135
<tr>
116-
<td align="center">any</td>
136+
<td align="center">T</td>
117137
<td align="center">O(1)</td>
118138
</tr>
119139
</table>
120140

121141
```js
122-
console.log(stack.pop()); // test
142+
console.log(stack.pop()); // 11
123143
console.log(stack.peek()); // null
124144
```
125145

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

140160
```js
141-
stack.push('test');
161+
stack.push(11);
142162
console.log(stack.isEmpty()); // false
143163
```
144164

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
const Stack = require('./src/stack');
1+
const { Stack } = require('./src/stack');
22

33
exports.Stack = Stack;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@datastructures-js/stack",
3-
"version": "3.0.0",
3+
"version": "3.1.0",
44
"description": "stack implementation in javascript",
55
"main": "index.js",
66
"scripts": {

src/stack.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export class Stack<T> {
2+
constructor(elements?: T[]);
3+
isEmpty(): boolean;
4+
size(): number;
5+
peek(): T;
6+
push(element: T): Stack<T>;
7+
pop(): T;
8+
toArray(): T[];
9+
clear(): void;
10+
clone(): Stack<T>;
11+
static fromArray<T>(elements: T[]): Stack<T>;
12+
}

src/stack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,4 @@ class Stack {
101101
}
102102
}
103103

104-
module.exports = Stack;
104+
exports.Stack = Stack;

test/stack.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { expect } = require('chai');
2-
const Stack = require('../src/stack');
2+
const { Stack } = require('../src/stack');
33

44
describe('stack tests', () => {
55
const stack = new Stack();

0 commit comments

Comments
 (0)