Sha256: d539e17ca1377e2170a2ff46874b8ccbae2aef38f14dbc44ed8e60725e8453b8

Contents?: true

Size: 1.81 KB

Versions: 125

Compression:

Stored size: 1.81 KB

Contents

'use strict';

function fromTrail(tree, last) {
  if (last[0] === 'left') {
    return {
      value: last[1],
      left: tree,
      right: last[2]
    };
  }
  return {
    value: last[1],
    left: last[2],
    right: tree
  };
}

function rebuildTree(tree, trail) {
  if (trail.length === 0) return tree;

  var last = trail[0];
  return rebuildTree(fromTrail(tree, last), trail.slice(1));
}

var Zipper = function (tree, trail) {
  this.tree = tree;
  this.trail = trail;
};

Zipper.fromTree = function (tree) {
  return new Zipper(tree, []);
};

Zipper.prototype.toTree = function () {
  return rebuildTree(this.tree, this.trail);
};

Zipper.prototype.value = function () {
  return this.tree.value;
};

Zipper.prototype.left = function () {
  if (!this.tree.left) return null;

  return new Zipper(
    this.tree.left,
    [['left', this.tree.value, this.tree.right]].concat(this.trail)
  );
};

Zipper.prototype.right = function () {
  if (!this.tree.right) return null;

  return new Zipper(
    this.tree.right,
    [['right', this.tree.value, this.tree.left]].concat(this.trail)
  );
};

Zipper.prototype.up = function () {
  if (this.trail.length === 0) return null;

  var last = this.trail[0];
  return new Zipper(fromTrail(this.tree, last), this.trail.slice(1));
};

Zipper.prototype.setValue = function (value) {
  return new Zipper(
    {
      value: value,
      left: this.tree.left,
      right: this.tree.right
    },
    this.trail
  );
};

Zipper.prototype.setLeft = function (left) {
  return new Zipper(
    {
      value: this.tree.value,
      left: left,
      right: this.tree.right
    },
    this.trail
  );
};

Zipper.prototype.setRight = function (right) {
  return new Zipper(
    {
      value: this.tree.value,
      left: this.tree.left,
      right: right
    },
    this.trail
  );
};

module.exports = Zipper;

Version data entries

125 entries across 125 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.138 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.137 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.136 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.135 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.134 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.133 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.132 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.131 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.130 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.129 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.128 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.127 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.126 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.125 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.124 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.123 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.122 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.121 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.120 tracks/javascript/exercises/zipper/example.js