Skip to content

Commit a94b482

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 1deb4b1 commit a94b482

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/lib/index.js

+39
Original file line numberDiff line numberDiff line change
@@ -736,3 +736,42 @@ lib.computeFrame = function(frameLookup, frameName) {
736736

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

test/jasmine/tests/lib_test.js

+28
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,34 @@ describe('Test lib.js:', function() {
13971397
}).toThrowError('Separator string required for formatting!');
13981398
});
13991399
});
1400+
1401+
describe('interleaveTraceUpdates', function() {
1402+
it('wraps property updates in arrays', function() {
1403+
var input = [{x: [1], 'marker.color': 'red'}];
1404+
var output = Lib.interleaveTraceUpdates(input);
1405+
1406+
expect(output).toEqual({
1407+
x: [[1]],
1408+
'marker.color': ['red']
1409+
});
1410+
});
1411+
1412+
it('merges traces into a single restyle', function() {
1413+
var input = [
1414+
{x: [1], 'marker.color': 'red'},
1415+
{y: [[7, 8], [4, 2]], 'marker.goodness': 'very', symbols: {visible: 'please'}}
1416+
];
1417+
var output = Lib.interleaveTraceUpdates(input);
1418+
1419+
expect(output).toEqual({
1420+
x: [[1], undefined],
1421+
y: [undefined, [[7, 8], [4, 2]]],
1422+
'marker.color': ['red', undefined],
1423+
'marker.goodness': [undefined, 'very'],
1424+
'symbols': [undefined, {visible: 'please'}]
1425+
});
1426+
});
1427+
});
14001428
});
14011429

14021430
describe('Queue', function() {

0 commit comments

Comments
 (0)