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

array-element-combiner

v1.0.0

Published

Combines or cancels adjacent elements of an array

Downloads

43

Readme

array-element-combiner

Traverses an array, comparing every two adjacent elements. Takes a compare callback, which determines whether to combine elements; and a combine callback, which determines the value of the combination of elements. In the returned array, the combined value will be stored and the two original elements will be deleted.

A possible use case might include an array holding a set of ordered instructions that can be shortened in the case of duplicative or opposite instructions. See the example.

Usage

$ npm install --save array-element-combiner
const combiner = require('array-element-combiner');

let array = [1, 2, 3, 4, 5];
const options = {
  cancel(value) { /* ... */ },
  compare(a, b) { /* ... */ },
  combine(a, b,) { /* ... */ },
  ignore(a, b) { /* ... */ }
};

array = combiner(array, options); // returns new array
console.log(array);

Example

Let's say you have a set of directions that include goForward, turnRight, turnLeft, and turnAround. You also have an array that has a randomized set of directions. The array can possibly be shortened, because some elements can cancel out (turnRight and turnLeft), while others can be combined (two turnRight's).

const directions = [
  'goForward',
  'turnRight',
  'goForward',
  'turnLeft',
  'turnLeft',
  'turnAround',
  'goForward'
];

Now we need to provide logic for how the directions will be combined or cancelled. For example, two turnRights combine to make a turnAround; a turnAround and turnRight make turnLeft; a turnLeft and turnRight cancel out, etc.

const options = {
  compare(a, b) {
    // can be combined if they both include a 'turn'
    return a.includes('turn') && b.includes('turn');
  },
  combine(a, b) {
    // we know that a and b both include 'turn' because they passed the
    // `compare` callback

    const aDir = a.includes('right') ? 1 : a.includes('left') ? -1 : 2;
    const bDir = b.includes('right') ? 1 : b.includes('left') ? -1 : 2;

    let totalDir = aDir + bDir;

    // Two `turnAround`s or a `turnLeft` and `turnRight` will cancel out. Without
    // providing a cancel callback, an empty string will be inserted into the
    // returned array.
    if (totalDir === 0 || totalDir === 4) {
      return '';
    }

    // A `turnAround` and a `turnRight` make a left.
    if (totalDir === 3) {
      totalDir = -1;
    }

    const dirString = totalDir === 1 ? 'Right' : totalDir === 2 ? 'Around' : 'Left'
    return `turn${dirString}`;
  },
  cancel(value) {
    return value === '';
  }
};

Output:

/* ... */
const output = combiner(directions, options);
console.log(output);
// [
//   'goForward',
//   'turnRight',
//   'goForward',
//   'goForward'
// ]