zippa
Version:
A Generic Zipper Library
127 lines (90 loc) • 4.02 kB
JavaScript
'use strict';
var _pipe = require('ramda/src/pipe');
var _pipe2 = _interopRequireDefault(_pipe);
var _chai = require('chai');
var _chai2 = _interopRequireDefault(_chai);
var _sinonChai = require('sinon-chai');
var _sinonChai2 = _interopRequireDefault(_sinonChai);
var _index = require('../index');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
_chai2.default.use(_sinonChai2.default);
var expect = _chai2.default.expect;
describe('ArrayZipper', function () {
var simpleArr = [1, 2, 3, 4];
var nested = [[1, 2], [3, 4]];
it('gets correct values', function () {
var z = _index.ArrayZipper.from(simpleArr);
expect((0, _pipe2.default)(_index.zip.down, _index.zip.right, _index.zip.value)(z)).to.equal(2);
expect(z.down().value()).to.equal(1);
expect(z.down().right().value()).to.equal(2);
expect(z.down().right().right().value()).to.equal(3);
});
it('gets correct values from nested list', function () {
var z = _index.ArrayZipper.from(nested);
expect(z.down().value()).to.equal(nested[0]);
expect(z.down().right().value()).to.equal(nested[1]);
expect(z.down().down().value()).to.equal(1);
expect(z.down().down().up().value()).to.equal(nested[0]);
});
it('successfully replaces elements', function () {
var z = _index.ArrayZipper.from(simpleArr);
expect(z.down().right().replace(10).root().value()).to.deep.equal([1, 10, 3, 4]);
});
it('edits elements', function () {
var z = _index.ArrayZipper.from(simpleArr);
var inc = function inc(x) {
return x + 1;
};
expect(z.down().edit(inc).right().edit(inc).right().edit(inc).right().edit(inc).root().value()).to.deep.equal([2, 3, 4, 5]);
});
it('successfully replaces elements in nested list', function () {
var z = _index.ArrayZipper.from(nested);
expect(z.down().right().replace([3, 4, 5, 6]).root().value()).to.deep.equal([[1, 2], [3, 4, 5, 6]]);
});
it('insert elements to the right', function () {
var z = _index.ArrayZipper.from([[1, 2], [5, 6]]);
expect(z.down().insertRight([3, 4]).root().value()).to.deep.equal([[1, 2], [3, 4], [5, 6]]);
});
it('insert elements to the left', function () {
var z = _index.ArrayZipper.from([[1, 2], [5, 6]]);
expect(z.down().rightmost().insertLeft([3, 4]).root().value()).to.deep.equal([[1, 2], [3, 4], [5, 6]]);
});
it('appends child', function () {
var z = _index.ArrayZipper.from(simpleArr);
expect(z.appendChild(5).appendChild(6).value()).to.deep.equal([1, 2, 3, 4, 5, 6]);
});
it('removes node', function () {
var z = _index.ArrayZipper.from(nested);
expect(z.down().right().down().remove().root().value()).to.deep.equal([[1, 2], [4]]);
});
it('iterates through array with next', function () {
var z = _index.ArrayZipper.from(nested);
var expected = [[1, 2], 1, 2, [3, 4], 3, 4];
expected.forEach(function (result) {
z = z.next();
expect(z.value()).to.deep.equal(result);
});
var end = z.next();
expect(end).to.equal(end.next());
});
it('iterates through array with prev', function () {
var z = _index.ArrayZipper.from(nested);
z = z.down().rightmost().down().rightmost();
expect(z.value()).to.deep.equal(4);
var expected = [3, [3, 4], 2, 1, [1, 2], [[1, 2], [3, 4]]];
expected.forEach(function (result) {
z = z.prev();
expect(z.value()).to.deep.equal(result);
});
expect(z.isTop()).to.be.true;
});
it('isEnd', function () {
var z = _index.ArrayZipper.from(simpleArr);
var iterations = 4;
while (iterations--) {
z = z.next();
expect(z.isEnd()).to.be.false;
}
expect(z.next().isEnd()).to.be.true;
});
});