Skip to content

Commit 40344ff

Browse files
committed
lib function to interleave trace updates into a restyle
1 parent 7387b2b commit 40344ff

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
@@ -616,3 +616,42 @@ lib.numSeparate = function(value, separators) {
616616

617617
return x1 + x2;
618618
};
619+
620+
/**
621+
* Interleaves separate trace updates (frames) into a restyle command.
622+
* Attributes not specified in both traces are set to `undefined` so that
623+
* they are not altered by restyle. Object paths are *not* expanded.
624+
*
625+
* @example
626+
* lib.interleaveTraceUpdates([{x: [1]}, {x: [2]}])
627+
* // returns {x: [[1], [2]]}
628+
*
629+
* @param {array} traces the trace updates to be interleaved
630+
*
631+
* @return {object} an object contianing the interleaved properties
632+
*/
633+
lib.interleaveTraceUpdates = function(traces) {
634+
var i, j, k, prop, trace, props;
635+
var n = traces.length;
636+
var output = {};
637+
638+
for(i = 0; i < traces.length; i++) {
639+
trace = traces[i];
640+
props = Object.keys(trace);
641+
for(j = 0; j < props.length; j++) {
642+
prop = props[j];
643+
if(!output[prop]) {
644+
// If not present, allocate a new array:
645+
output[prop] = [];
646+
647+
// Explicitly fill this array with undefined:
648+
for(k = 0; k < n; k++) {
649+
output[prop][k] = undefined;
650+
}
651+
}
652+
output[prop][i] = traces[i][prop];
653+
}
654+
}
655+
656+
return output;
657+
};

test/jasmine/tests/lib_test.js

+28
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,34 @@ describe('Test lib.js:', function() {
13341334
}).toThrowError('Separator string required for formatting!');
13351335
});
13361336
});
1337+
1338+
describe('interleaveTraceUpdates', function() {
1339+
it('wraps property updates in arrays', function() {
1340+
var input = [{x: [1], 'marker.color': 'red'}];
1341+
var output = Lib.interleaveTraceUpdates(input);
1342+
1343+
expect(output).toEqual({
1344+
x: [[1]],
1345+
'marker.color': ['red']
1346+
});
1347+
});
1348+
1349+
it('merges traces into a single restyle', function() {
1350+
var input = [
1351+
{x: [1], 'marker.color': 'red'},
1352+
{y: [[7, 8], [4, 2]], 'marker.goodness': 'very', symbols: {visible: 'please'}}
1353+
];
1354+
var output = Lib.interleaveTraceUpdates(input);
1355+
1356+
expect(output).toEqual({
1357+
x: [[1], undefined],
1358+
y: [undefined, [[7, 8], [4, 2]]],
1359+
'marker.color': ['red', undefined],
1360+
'marker.goodness': [undefined, 'very'],
1361+
'symbols': [undefined, {visible: 'please'}]
1362+
});
1363+
});
1364+
});
13371365
});
13381366

13391367
describe('Queue', function() {

0 commit comments

Comments
 (0)