npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@peter.naydenov/stack

v3.0.0

Published

Build stacks. FIFO and LIFO.

Downloads

4

Readme

Stack (@peter.naydenov/stack)

version license issues language npm bundle size (scoped version)

Build FIFO or LIFO stack and operate with it. You can set a limit of the stack and define how to react when limit is reached. You can pull single or multiple items from the stack. You can peek the next item without extracting it from the stack. You can reset the stack and start over again. You can check the size of the stack or if it is empty. You can debug the stack and see its content. You can use it in Node.js or in the browser.

Installation

Install the package:

 npm i @peter.naydenov/stack

Import the module from the script

import stack from '@peter.naydenov/stack'

or require it

const stack = require('@peter.naydenov/stack')

Create a stack

Call the stack function will create an empty stack. There is a non-required argument options. Here is an example:

  // Calling function 'stack' without argument will return FIFO stack
  const cache = stack ({
                        type : 'FIFO'  // Possible values: 'FIFO'(default) or 'LIFO'
                        limit : 10  // Possible values: 'false'(default) or number(number of items)
                        onLimit : 'update'  // Possible values: 'full' or 'update'(default)
                    });

Methods

{
  'push'        : 'Insert data in the stack'
, 'pull'        : 'Retreve data from the stack. Single value or pull more then one value at the time'
, 'pullReverse' : 'When we want to pull more then one value, but in reverse order' 
, 'peek'        : 'Peek the next value without extracting it from the stack'
, 'peekReverse' : 'When we want to peek more then one value, but in reverse order'
, 'getSize'     : 'Returns the size of the stack'
, 'isEmpty'     : 'Returns "true" if size of stack is 0'
, 'reset'       : 'Removes content from the stack'
, 'debug'       : 'Will return content of the stack'
}

stack.push ()

Insert item or items to the stack.

 let cache = stack();   // default stack is FIFO with no limit.

  cache.push(12)
  cache.push ( 2 )
  cache.push ([14,15]) // set multiple items together
  // so content of cache is -> [ 15, 14, 2, 12 ]
  cache.pull () // -> 12

Insertion can change according parameters during creation. We can define a stack-limit - how many items can contain the stack. Additional parameter onLimit defines behaviour of the stack when the limit has been reached. Parameter options:

  • 'full' : When limit is reached will stop to receive new values from 'push'.
  • 'update' : When limit is reached will remove of old stack members in a favor of the incoming data

Example:

const cache = stack ({type:'FIFO', limit:3, onLimit: 'full' })
cache.push ([1,2])
cache.push ([3,4]) // on 3 limit is reached, so value 4 will be ignored. We have option 'onLimit' - full
// content of cache is -> [ 3, 2, 1 ]
cache.pull () // -> 1

// same example but with onLimit - update
const st = stack ({type:'FIFO', limit:3, onLimit: 'update' })
st.push ([1,2])
st.push ([3,4])
// content of st is -> [ 4, 3, 2 ]
st.pull () // -> 2

stack.pull ()

Retreve data from the stack. Single value or pull more then one value at the time. You can skip stack elements. Here are some examples:

const 
    lifo = stack ({type:'LIFO'})
  , fifo  = stack () // by default is FIFO
  , items = [1,2,3,4,5,6]
  ;

 lifo.push ( items )
 fifo.push ( items )

 lifo.pull()   // -> 6. Function 'pull' will remove the element from the stack.
 fifo.pull ()   // -> 1. Function 'pull' will remove the element from the stack.

 lifo.pull(2) // -> [5,4]. Stack 'lifo' after operation: [ 1, 2, 3 ]
 fifo.pull(2) // -> [2,3]. Stack 'fifo' after operation: [ 6, 5, 4 ]

 // Let's reset both stacks
 lifo.reset()
 fifo.reset()

 // load items again
 lifo.push ( items )
 fifo.push ( items )

 // Pull can skip some results
 // stack.pull ( numberOfItems, skipItems )
 lifo.pull (3,1) // Skip the first pull, get 3 elements. -> [ 5, 4, 3]
 // stack 'lifo' after the operation -> [ 1, 2 ]
 fifo.pull ( 3,2 ) // Skip 2 elements, get next 3. -> [ 3, 4, 5 ]
 // stack 'fifo' after the operation -> [ 6 ]

Links

Credits

'@peter.naydenov/stack' was created by Peter Naydenov.

License

'@peter.naydenov/stack' is released under the MIT License.