zippa

The main zipper module.

Every member is importable under the package. I.e. import { makeZipper, value } from 'zippa'

Makes a Zipper factory that uses the implementation provided in the parameters.

makeZipper
Parameters
_isBranch (Function) Function with signature (item: T) => boolean that indicates if the item can have children.
_getChildren (Function) Function with signature (item: T) => Array<T> that returns an array of children for a branch.
_makeItem (Function) Function with signature (item: T, children: Array<T>) => T that returns a new item, given an old item and it's new children.
Returns
Function: zipper factory with signature (item: T) => Zipper . The factory can also be accessed from the factory's from property.

Gets the value of the current location.

value
Parameters
zipper (Zipper)
Returns
(T | null):

Moves location to the root, constructing any changes made.

root
Parameters
zipper (Zipper)
Returns
Zipper:

Moves location to the parent, constructing a new parent if the children have changed.

If already at the top, returns null.

up
Parameters
zipper (Zipper)
Returns
(Zipper | null):

Moves location to the leftmost child. If the current item is a leaf, returns null.

down
Parameters
zipper (Zipper)
Returns
(Zipper | null):

Moves location to the left sibling. If the current location is already the leftmost, returns null.

left
Parameters
zipper (Zipper)
Returns
(Zipper | null):

Moves location to the right sibling. If the current location is already the rightmost, returns null.

right
Parameters
zipper (Zipper)
Returns
(Zipper | null):

Moves location to the leftmost sibling. If the current location is already the leftmost, returns itself.

leftmost
Parameters
zipper (Zipper)
Returns
Zipper:

Moves location to the rightmost sibling. If the current location is already the rightmost, returns itself.

rightmost
Parameters
zipper (Zipper)
Returns
Zipper:

Moves location to the next element in depth-first order.

next
Parameters
zipper (Zipper)
Returns
Zipper:

Moves location to the previous element in depth-first order.

prev
Parameters
zipper (Zipper)
Returns
Zipper:

Returns a boolean indicating if the zipper has been exhausted by calls to next.

isEnd
Parameters
zipper (Zipper)
Returns
boolean:

Returns a boolean indicating if the zipper is at the top.

isTop
Parameters
zipper (Zipper)
Returns
boolean:

Returns a boolean indicating if the zipper is not at the top.

isNotTop
Parameters
zipper (Zipper)
Returns
boolean:

Returns a boolean indicating if the current location is not a leaf.

isBranch
Parameters
zipper (Zipper)
Returns
boolean:

Returns a boolean indicating if the current location is a leaf.

isLeaf
Parameters
zipper (Zipper)
Returns
boolean:

Returns a boolean indicating if the item at the current location is the leftmost sibling.

isLeftmost
Parameters
zipper (Zipper)
Returns
boolean:

isRightmost

./src/zipper.js

Returns a boolean indicating if the item at the current location is the rightmost sibling.

isRightmost
Parameters
zipper (Zipper)
Returns
boolean:

Returns a boolean indicating if the zipper is not at the top.

Alias for isNotTop

canGoUp
Parameters
zipper (Zipper)
Returns
boolean:

Returns a boolean indicating if the item at the current location is the leftmost sibling.

Alias for isLeftmost

canGoLeft
Parameters
zipper (Zipper)
Returns
boolean:

Returns a boolean indicating if the item at the current location is the rightmost sibling.

Alias for isRightmost

canGoRight
Parameters
zipper (Zipper)
Returns
boolean:

Alias for isBranch

canGoDown
Parameters
zipper (Zipper)
ipper (Any)
Returns
boolean:

Replaces the current item with the given value.

replace(replaceWith: T, zipper: Zipper): Zipper
Parameters
replaceWith (T) item to replace the current one with.
zipper (Zipper)
Returns
Zipper:

Replaces the current item with value returned by calling fn with the current item.

edit(fn: Function, zipper: Zipper): Zipper
Parameters
fn (Function) Function that takes the old item and returns a new item.
zipper (Zipper)
Returns
Zipper:

Inserts a new item as the left sibling.

insertLeft(item: T, zipper: Zipper): Zipper
Parameters
item (T)
zipper (Zipper)
Returns
Zipper:

insertRight

./src/zipper.js

Inserts a new item as the right sibling.

insertRight(item: T, zipper: Zipper): Zipper
Parameters
item (T)
zipper (Zipper)
Returns
Zipper:

insertChild

./src/zipper.js

Inserts a new item as the leftmost child.

insertChild(item: T, zipper: Zipper): Zipper
Parameters
item (T)
zipper (Zipper)
Returns
Zipper:

appendChild

./src/zipper.js

Inserts a new item as the rightmost child.

appendChild(item: T): Zipper
Parameters
item (T)
Returns
Zipper:

Removes item at the current location. Returns location that would be previous in depth first search.

remove
Parameters
zipper (Zipper)
Returns
Zipper:

The Zipper class.

Keeps track of the current item, path, and metadata (implementation functions).

Don't use this constructor directly. Create your own Zipper factory with makeZipper, and use it to create instances of Zipper.

Zipper
Parameters
item (Any)
path (Any)
meta (Any)
Instance Members
Zipper.prototype.value
Zipper.prototype.root
Zipper.prototype.up
Zipper.prototype.down
Zipper.prototype.left
Zipper.prototype.right
Zipper.prototype.leftmost
Zipper.prototype.rightmost
Zipper.prototype.next
Zipper.prototype.prev
Zipper.prototype.isEnd
Zipper.prototype.isTop
Zipper.prototype.isBranch
Zipper.prototype.isLeaf
Zipper.prototype.isLeftmost
Zipper.prototype.isRightmost
Zipper.prototype.canGoUp
Zipper.prototype.canGoLeft
Zipper.prototype.canGoRight
Zipper.prototype.canGoDown
Zipper.prototype.edit
Zipper.prototype.replace
Zipper.prototype.insertLeft
Zipper.prototype.insertRight
Zipper.prototype.insertChild
Zipper.prototype.appendChild
Zipper.prototype.remove

Zipper for nested Arrays.

Don't use with new keyword - use the function plainly or with ArrayZipper.from([1, 2, 3]).

ArrayZipper
Parameters
arr (Array) the data structure to make a zipper for
Returns
Zipper:

Visitors

Module for implementing visitors to a data structure with zippers.

Every member is importable under zippa. import { visit, onPre, onPost } from 'zippa'

Visits the data structure in depth-first order, calling one or more visitors on each node, on pre and post events.

The visitor functions are called with three arguments:

  • event - PRE or POST
  • item - the item currently being visited
  • state - the state of the visit

If you're visiting for side-effects, you don't have to return anything from your visitor function. To edit items, visit state, or stop the visit, you must return an object which can have zero or more of the following keys:

  • item: if supplied, will replace the item at the current location with the supplied value. The data structure won't change if the value has the same reference as the current value.
  • state: if supplied, will update the state of the visit with the value. State is shared with all visitors.
  • stop: if truthy, will stop the visit
  • cut: if truthy, will skip the subtree of the current node.

Returns a new data structure of modified items, or the original zipper if the structure wasn't modified.

visit
Parameters
visitors (Array<Function>) Array of visitor functions
initialState ([Any]) Initial state for the visit
initialZipper (Zipper) A Zipper value to visit
Returns
Zipper:

Takes a visitor function that takes an item and state argument, and returns a visitor function that is only invoked on the pre-event.

Equal to:

const myVisitor = (item, state) => console.log('visited item', item);
function visitor(event, item, state) {
    if (event === PRE) return myVisitor(item, state);
}
onPre
Parameters
fn (Function) visitor function that takes item and state arguments
Returns
Function: visitor function

Takes a visitor function that takes an item and state argument, and returns a visitor function that is only invoked on the post-event.

Equal to:

const myVisitor = (item, state) => console.log('visited item', item);
function visitor(event, item, state) {
    if (event === POST) return myVisitor(item, state);
}
onPost
Parameters
fn (Function) visitor function that takes item and state arguments
Returns
Function: visitor function

Pre-event identifier.

PRE

Post-event identifier.

POST

Walking

Module for walking data structures with zippers.

Every member is importable under zippa. import { walk, preWalk, postWalk } from 'zippa'

Walks the data structure in depth-first order, applying inner and outer functions before and after (respectively) each item's subtree is walked.

Returns a new data structure from modified items, or the original zipper if the structure wasn't modified.

walk
Parameters
inner (Function) function applied to each item before it's subtree is walked
outer (Function) function applied to each item after it's subtree was walked
zipper (Zipper) A Zipper value to walk
Returns
Zipper:

Walks the data structure in depth-first order, applying the function before the item's subtree has been walked.

Returns a new data structure of modified items, or the original zipper if the structure wasn't modified.

preWalk
Parameters
fn (Function) function applied to each item before it's subtree is walked
zipper (Zipper) A Zipper value to walk
Returns
Zipper:

Walks the data structure in depth-first order, applying the function after the item's subtree has been walked.

Returns a new data structure of modified items, or the original zipper if the structure wasn't modified.

postWalk
Parameters
fn (Function) function applied to each item after it's subtree was walked
zipper (Zipper) A Zipper value to walk
Returns
Zipper: