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

listwrap

v1.0.0

Published

Wraps any list-like object with an interface containing a full set of superpowered, chainable list methods, including toggle, replace, etc.

Downloads

4

Readme

listwrap

Wraps any list-like object with an interface containing a full set of superpowered, chainable list methods, including toggle, replace, etc.

Installation

Requires Node.js 6.0.0 or above.

npm i listwrap

Usage

The module exports a single class. The constructor expects an object argument with 6 function properties: add, remove, has, values, item, and length. These are the 6 foundational functions which Listwrap combines to produce new functions like toggle and replace. The object argument may optionally also include the optional methods removeAll and/or sanitize.

The following example makes use of Listwrap to extend the basic JavaScript array with new methods:

const Listwrap = require('listwrap')
const vdel = require('vdel')

class SuperArray extends Listwrap {
  constructor () {
    const array = []
    super({
      add: v => { array.push(v) },
      remove: v => { vdel(array, v) },
      has: v => array.includes(v),
      values: () => array,
      item: i => array[i],
      length: () => array.length,
    })
  }
}

const super = new SuperArray()
super.add(1, 2, 3)
  .toggle(1) // Removes 1 if present
  .replace(3, 4) // Replaces 3 with 4
  .hasAny(4, 5) // Returns true, since one of the arguments (4) is present
super.values() // [2, 4]

Methods

In the function definitions below, the ...items parameter is a rest parameter that will accept any number of arguments.

  • constructor (methods)
    • methods: An object containing the underlying methods for the list:
      • Required functions: add, remove, has, values, item, and length.
      • Optional functions: removeAll and sanitize.
  • has (...items)
    • Will return true only if all items are present.
  • hasAny (...items)
    • Will return true if at least one item is present.
  • add (...items)
  • remove (...items)
  • removeIf (callback)
  • removeAll()
  • removeAllExcept (...permittedValues)
  • replace (oldItems, newItems)
    • oldItems: A value or an array of values.
    • newItems: A value or an array of values.
    • If all the oldItems are present, they are removed and replaced with newItems.
  • toggle (...items)
    • For each given item, removes it if it’s present, and adds it if not. Each item is toggled independently of the others.
  • toggleTogether (...items)
    • If all given items are present, removes all of them. Otherwise adds any that are not present. Ensures that no item is present or absent without the others.
  • if (condition, thenItems, [elseItems])
    • condition: A boolean that determines whether or not the items should be present.
    • thenItems: A value or an array of values.
    • elseItems: A value or an array of values.
    • If condition is true, removes elseItems and adds thenItems. Otherwise removes themItems and adds elseItems.
  • item (index)
  • length