Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
stack-typescript
Advanced tools
Simple Typescript Stack with generics type templating and support for iterator and iterable protocols.
This stack uses the linked-list-typescript as the underlying datastructure.
See Also:
npm:
npm install --save stack-typescript
yarn:
yarn add stack-typescript
install dev dependencies. There are no production dependencies.
yarn
npm install
build using the options in tsconfig.json
yarn|npm run build
run all package tests
yarn|npm run test
see the test coverage report
yarn|npm run coverage
yarn|npm run coverage:report
Importing:
import { Stack } from 'stack-typescript';
const { Stack } = require('stack-typescript')
Create an empty stack by omitting any arguments during instantiation.
let stack = new Stack<number>()
Create a new stack and initialize it with values. Values will be added from top to bottom. i.e. the first argument will be at the top and the last argument will be at the bottom.
Specify the type using the typescript templating to enable type-checking of all values going into and out of the stack.
let items: number[] = [4, 5, 6, 7];
let stack = new Stack<number>(...items);
let items: string[] = ['one', 'two', 'three', 'four'];
let stack = new Stack<string>(...items);
Typescript will check if the values match the type given to the template when initializing the new stack.
let items: = ['one', 'two', 'three', 4];
let stack = new Stack<string>(...items); // arguments are not all strings
Create a new stack using custom types or classes. All values are retained as references and not copies so removed values can be compared using strict comparison.
class Foo {
private val:number;
constructor(val: number) {
this.val = val;
}
get bar(): number { return this.val }
}
let foo1 = new Foo(1);
let foo2 = new Foo(2);
let foo3 = new Foo(3);
let fooStack = new Stack<Foo>(foo1, foo2, foo3)
fooStack.top.bar // => 1
let val = stack.pop()
val // => foo1
Specify any
to allow the stack to take values of any type.
let stack = new Stack<any>(4, 'hello' { hello: 'world' })
stack.size // => 3
stack.top // => 4
The stack supports both iterator and iterable protocols allowing it to be used
with the for...of
and ...spread
operators and with deconstruction.
for...of
:
let items: number[] = [4, 5, 6];
let stack = new Stack<number>(...items);
for (let item of stack) {
console.log(item)
}
//4
//5
//6
...spread
:
let items: number[] = [4, 5, 6];
let stack = new Stack<number>(...items);
function manyArgs(...args) {
for (let i in args) {
console.log(args[i])
}
}
manyArgs(...stack);
//4
//5
//6
deconstruction
:
let items: number[] = [4, 5, 6, 7];
let stack = new Stack<number>(...items);
let [a, b, c] = stack;
//a => 4
//b => 5
//c => 6
Peek at the top of the stack. This will not remove the value from the stack.
let items: number[] = [4, 5, 6, 7];
let stack = new Stack<number>(...items);
stack.top // => 4
Query the size of the stack. An empty stack will return 0.
let items: number[] = [4, 5, 6, 7];
let stack = new Stack<number>(...items);
stack.size // => 4
Push an item to the top of the stack. The new item will replace the previous top item and subsequent calls to Stack#top will now recall the new item.
let items: number[] = [4, 5, 6, 7];
let stack = new Stack<number>(...items);
stack.size // => 4
stack.push(8)
stack.size // => 5
Removes the item at the top of the stack and returns the item.
let items: number[] = [4, 5, 6, 7];
let stack = new Stack<number>(...items);
stack.size // => 4
let val = stack.pop()
stack.size // => 3
stack.top // => 5
val // => 4
This method simply returns [...this]
.
Converts the stack into an array and returns the array representation. This method does not mutate the stack in any way.
Objects are not copied, so all non-primitive items in the array are still referencing the stack items.
let items: number[] = [4, 5, 6, 7];
let stack = new Stack<number>(...items);
let result = stack.toArray()
result // => [4, 5, 6, 7]
FAQs
Simple Typescript Stack with generics type support
The npm package stack-typescript receives a total of 6,274 weekly downloads. As such, stack-typescript popularity was classified as popular.
We found that stack-typescript demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.