Commit 77b559fd authored by Filipa Lacerda's avatar Filipa Lacerda

Creates findAndReplaceOffset funtion

Creates a function to find the last offset and
remove it if repeated
parent c0a42580
......@@ -58,12 +58,12 @@ export const logLinesParser = (lines = [], lineNumberStart) =>
/**
* Finds the repeated offset, removes the old one
*
* Returns a new object with the updated log without
* the repeated offset and the last line number.
* Returns a new array with the updated log without
* the repeated offset
*
* @param Array newLog
* @param Array oldParsed
* @returns Object
* @returns Array
*
*/
export const findOffsetAndRemove = (newLog, oldParsed) => {
......@@ -72,19 +72,17 @@ export const findOffsetAndRemove = (newLog, oldParsed) => {
const last = cloneOldLog[lastIndex];
const firstNew = newLog[0];
const parsed = {};
if (last.offset === firstNew.offset || (last.line && last.line.offset === firstNew.offset)) {
cloneOldLog.splice(lastIndex);
parsed.lastLine = last.lineNumber;
} else if (last.lines && last.lines.length) {
const lastNestedIndex = last.lines.length - 1;
const lastNested = last.lines[lastNestedIndex];
if (lastNested.offset === firstNew.offset) {
last.lines.splice(lastNestedIndex);
parsed.lastLine = lastNested.lineNumber;
}
}
return cloneOldLog;
};
......
......@@ -86,30 +86,56 @@ describe('Jobs Store Utils', () => {
describe('findOffsetAndRemove', () => {
describe('when last item is header', () => {
describe('when last item matches the offset', () => {
it('returns an object with the item removed and the lastLine', () => {
const existingLog = [
{
isHeader: true,
isClosed: true,
line: { content: [{ text: 'bar' }], offset: 10, lineNumber: 1 },
},
];
describe('and matches the offset', () => {
it('returns an array with the item removed', () => {
const newData = [{ offset: 10, content: [{ text: 'foobar' }] }];
const existingLog = [{ line: { content: [{ text: 'bar' }], offset: 10, lineNumber: 1 } }];
const result = findOffsetAndRemove(newData, existingLog);
expect(result).toEqual([]);
});
});
describe('and does not match the offset', () => {
it('returns the provided existing log', () => {
const newData = [{ offset: 110, content: [{ text: 'foobar' }] }];
const result = findOffsetAndRemove(newData, existingLog);
expect(result).toEqual(existingLog);
});
});
});
describe('when last item is a regular line', () => {
const existingLog = [{ content: [{ text: 'bar' }], offset: 10, lineNumber: 1 }];
describe('and matches the offset', () => {
it('returns an object with the item removed and the lastLine', () => {
it('returns an array with the item removed', () => {
const newData = [{ offset: 10, content: [{ text: 'foobar' }] }];
const existingLog = [{ content: [{ text: 'bar' }], offset: 10, lineNumber: 1 }];
const result = findOffsetAndRemove(newData, existingLog);
expect(result).toEqual([]);
});
});
});
describe('when last collaspible line item matches the offset', () => {
it('returns an object with the last nested line item removed and the lastLine', () => {
describe('and does not match the fofset', () => {
it('returns the provided old log', () => {
const newData = [{ offset: 101, content: [{ text: 'foobar' }] }];
const result = findOffsetAndRemove(newData, existingLog);
expect(result).toEqual(existingLog);
});
});
});
describe('when last item is nested', () => {
const existingLog = [
{
isHeader: true,
......@@ -122,20 +148,26 @@ describe('Jobs Store Utils', () => {
},
},
];
describe('and matches the offset', () => {
it('returns an array with the last nested line item removed', () => {
const newData = [{ offset: 101, content: [{ text: 'foobar' }] }];
const result = findOffsetAndRemove(newData, existingLog);
expect(result[0].lines).toEqual([]);
});
});
describe('when it does not match the offset', () => {
it('returns an object with the complete old log and the last line number', () => {
const newData = [{ offset: 101, content: [{ text: 'foobar' }] }];
const existingLog = [{ line: { content: [{ text: 'bar' }], offset: 10, lineNumber: 1 } }];
describe('and does not match the offset', () => {
it('returns the provided old log', () => {
const newData = [{ offset: 120, content: [{ text: 'foobar' }] }];
const result = findOffsetAndRemove(newData, existingLog);
expect(result).toEqual(existingLog);
});
});
});
});
describe('updateIncrementalTrace', () => {
describe('without repeated section', () => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment