Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced operation infos helper #5

Merged
merged 1 commit into from Apr 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
@@ -1,10 +1,10 @@
const array = require('./src/array');
const object = require('./src/object');
const operations = require('./src/operations');
const infos = require('./src/infos');

module.exports = {
array,
object,
operations,
infos,
};

13 changes: 7 additions & 6 deletions src/errors.js
@@ -1,8 +1,9 @@

const errors = {
notAllowed(methodName) {
return new Error(`${methodName} not allowed`);
}
};

module.exports = errors;
function notAllowed(methodName) {
return new Error(`${methodName} not allowed`);
}

module.exports = {
notAllowed,
};
32 changes: 32 additions & 0 deletions src/infos.js
@@ -0,0 +1,32 @@

/**
* QUESTION(lbays): ot.go indicates that SpliceInfo will take empty collections
* to indicate deletions, but the splice implementation is richer than this. A
* caller may use splice to simultaneously insert and delete. Should we
* separate these mutations into different methods or add information to this
* info object?
*
* Also, if this info object must store two collection references before and
* after a mutation, this seems like a memory and performance problem for
* even moderately sized collections.
*/
function spliceInfo(offset, beforeSet, afterSet) {
return {
offset: offset,
before: beforeSet,
after: afterSet,
};
}

function setInfo(key, beforeSet, afterSet) {
return {
key: key,
before: beforeSet,
after: afterSet,
};
}

module.exports = {
setInfo,
spliceInfo,
};
5 changes: 0 additions & 5 deletions src/operations.js

This file was deleted.

31 changes: 31 additions & 0 deletions test/infos_test.js
@@ -0,0 +1,31 @@
const assert = require('chai').assert;
const object = require('../').object;
const infos = require('../').infos;

describe('infos', () => {
let arr;
let obj;

beforeEach(() => {
arr = ['a', 'b', 'c'];
obj = {a: 10, b: 20, c: 30};
});

it('creates a spliceInfo object', () => {
const after = [];
const info = infos.spliceInfo(1, arr, after);
assert(info);
assert.equal(info.offset, 1);
assert(info.before === arr);
assert(info.after === after);
});

it('creates setInfo object', () => {
const after = {};
const info = infos.setInfo('b', obj, object.set(obj, 'b', 40));
assert(info);
assert.equal(info.key, 'b');
assert.equal(info.before.b, 20);
assert.equal(info.after.b, 40);
});
});
9 changes: 0 additions & 9 deletions test/operations_test.js

This file was deleted.