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.180 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.179 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.178 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.177 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.176 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.175 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.174 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.173 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.172 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.171 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.170 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.169 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.167 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.166 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.165 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.164 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.163 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.162 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.161 tracks/javascript/exercises/zipper/example.js
trackler-2.2.1.160 tracks/javascript/exercises/zipper/example.js