Skip to content

Commit 79068d0

Browse files
committed
lib function to interleave trace updates into a restyle
Conflicts: src/lib/index.js lib function to interleave trace updates into a restyle Conflicts: src/lib/index.js
1 parent 42dbf69 commit 79068d0

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/lib/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,3 +746,42 @@ lib.computeFrame = function(frameLookup, frameName) {
746746

747747
return result;
748748
};
749+
750+
/**
751+
* Interleaves separate trace updates (frames) into a restyle command.
752+
* Attributes not specified in both traces are set to `undefined` so that
753+
* they are not altered by restyle. Object paths are *not* expanded.
754+
*
755+
* @example
756+
* lib.interleaveTraceUpdates([{x: [1]}, {x: [2]}])
757+
* // returns {x: [[1], [2]]}
758+
*
759+
* @param {array} traces the trace updates to be interleaved
760+
*
761+
* @return {object} an object contianing the interleaved properties
762+
*/
763+
lib.interleaveTraceUpdates = function(traces) {
764+
var i, j, k, prop, trace, props;
765+
var n = traces.length;
766+
var output = {};
767+
768+
for(i = 0; i < traces.length; i++) {
769+
trace = traces[i];
770+
props = Object.keys(trace);
771+
for(j = 0; j < props.length; j++) {
772+
prop = props[j];
773+
if(!output[prop]) {
774+
// If not present, allocate a new array:
775+
output[prop] = [];
776+
777+
// Explicitly fill this array with undefined:
778+
for(k = 0; k < n; k++) {
779+
output[prop][k] = undefined;
780+
}
781+
}
782+
output[prop][i] = traces[i][prop];
783+
}
784+
}
785+
786+
return output;
787+
};

test/jasmine/tests/lib_test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,34 @@ describe('Test lib.js:', function() {
14011401
}).toThrowError('Separator string required for formatting!');
14021402
});
14031403
});
1404+
1405+
describe('interleaveTraceUpdates', function() {
1406+
it('wraps property updates in arrays', function() {
1407+
var input = [{x: [1], 'marker.color': 'red'}];
1408+
var output = Lib.interleaveTraceUpdates(input);
1409+
1410+
expect(output).toEqual({
1411+
x: [[1]],
1412+
'marker.color': ['red']
1413+
});
1414+
});
1415+
1416+
it('merges traces into a single restyle', function() {
1417+
var input = [
1418+
{x: [1], 'marker.color': 'red'},
1419+
{y: [[7, 8], [4, 2]], 'marker.goodness': 'very', symbols: {visible: 'please'}}
1420+
];
1421+
var output = Lib.interleaveTraceUpdates(input);
1422+
1423+
expect(output).toEqual({
1424+
x: [[1], undefined],
1425+
y: [undefined, [[7, 8], [4, 2]]],
1426+
'marker.color': ['red', undefined],
1427+
'marker.goodness': [undefined, 'very'],
1428+
'symbols': [undefined, {visible: 'please'}]
1429+
});
1430+
});
1431+
});
14041432
});
14051433

14061434
describe('Queue', function() {

0 commit comments

Comments
 (0)