list-zipper

Immutable list zipper, inspired by Haskell Data.List.Zipper

Usage no npm install needed!

<script type="module">
  import listZipper from 'https://cdn.skypack.dev/list-zipper';
</script>

README

list-zipper Build Status

Simple list zipper, inspired by Haskell Data.List.Zipper. If you ever found yourself using something like this:

var array = ['foo', 'bar', 'baz'];
var currentIndex = 1;

Then you can replace it with:

var array = ListZipper(['foo', 'bar', 'baz'], 1);

Read the usage section for more details. You can also find more info about zippers here.

Installation

Node.js

npm install list-zipper

Bower

bower install list-zipper

Usage

// Move right
ListZipper([1, 2, 3]).next().next().val(); // 3

// Move left
ListZipper([1, 2, 3], 2); // second argument sets the index of currently focused element
  .prev().prev().val() // 1

// It doesn't change the original array
ListZipper([1, 2, 3]).next().next().toArray(); // [1, 2, 3]

// It is immutable
var lz = ListZipper([1, 2, 3], 1);
lz.val(); // 2
lz.next().val(); // 3
lz.val(); // 2
lz.prev().val(); // 1

More info in API Docs.