包详细信息

@datastructures-js/stack

datastructures-js15.5kMIT3.1.4

stack implementation in javascript

stack, stack es6, stack js

自述文件

@datastructures-js/stack

npm npm npm

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

Contents

Install

npm install --save @datastructures-js/stack

require

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

import

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

API

constructor

JS
// empty stack
const stack = new Stack();

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

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

Stack.fromArray

JS
// empty stack
const stack = Stack.fromArray([]);

// with elements
const list = [10, 3, 8, 40, 1];
const stack = Stack.fromArray(list);

// If the list should not be mutated, use a copy of it.
const stack = Stack.fromArray(list.slice());
TS
// empty stack
const stack = Stack.fromArray<number>([]);

// with elements
const list = [10, 3, 8, 40, 1];
const stack = Stack.fromArray<number>([10, 3, 8, 40, 1]);

push

adds an element to the top of the stack.

stack.push(11);

peek

returns the top element in the stack.

console.log(stack.peek()); // 11

pop

removes and returns the top element of the stack.

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

isEmpty

checks if the stack is empty.

stack.push(11);
console.log(stack.isEmpty()); // false

size

returns the number of elements in the stack.

console.log(stack.size()); // 1

clone

creates a shallow copy of the stack.

const stack = Stack.fromArray([{ id: 2 }, { id: 4 } , { id: 8 }]);
const clone =  stack.clone();

clone.pop();

console.log(stack.peek()); // { id: 8 }
console.log(clone.peek()); // { id: 4 }

toArray

returns a copy of the remaining elements as an array.

console.log(stack.toArray()); // [{ id: 2 }, { id: 4 } , { id: 8 }]

clear

clears all elements from the stack.

stack.clear();
stack.size(); // 0

Build

grunt build

License

The MIT License. Full License is here

更新日志

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[Unreleased]

[3.1.4] - 2022-08-15

Fixed

  • add types to package.json

[3.1.3] - 2022-05-15

Fixed

  • README

[3.1.2] - 2021-06-20

Fixed

  • index.d.ts

[3.1.1] - 2021-06-14

Fixed

  • README

[3.1.0] - 2021-06-13

Added

  • typescript.

[3.0.0] - 2021-01-02

Changed

  • .push can now chain other stack methods.
  • named export for stack class.

Fixed

  • readme

[2.0.4] - 2020-12-26

Fixed

  • jsdoc
  • readme

[2.0.3] - 2020-04-03

Fixed

  • .npmignore

[2.0.2] - 2020-04-02

Fixed

  • README & jsdoc.

[2.0.1] - 2020-03-12

Fixed

  • Readme.

[2.0.0] - 2020-03-12

Changed

  • initial new release.