Skip to content

Commit da2c24c

Browse files
committed
validate transform module on registration:
- module.transform must be a function - module.attributes and module.supplyDefaults are recommended but optional - log warning if transform module associated with transform type isn't found - extend traceIn if no supplyDefaults method is found.
1 parent a6fda02 commit da2c24c

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

src/plotly.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
require('es6-promise').polyfill();
2323

2424
// lib functions
25-
exports.Lib = require('./lib');
25+
var Lib = exports.Lib = require('./lib');
2626
exports.util = require('./lib/svg_text_utils');
2727
exports.Queue = require('./lib/queue');
2828

@@ -55,7 +55,8 @@ exports.ModeBar = require('./components/modebar');
5555
exports.register = function register(_modules) {
5656
if(!_modules) {
5757
throw new Error('No argument passed to Plotly.register.');
58-
} else if(_modules && !Array.isArray(_modules)) {
58+
}
59+
else if(_modules && !Array.isArray(_modules)) {
5960
_modules = [_modules];
6061
}
6162

@@ -77,6 +78,22 @@ exports.register = function register(_modules) {
7778
break;
7879

7980
case 'transform':
81+
if(typeof newModule.name !== 'string') {
82+
throw new Error('Transform module *name* must be a string.');
83+
}
84+
85+
var prefix = 'Transform module ' + newModule.name;
86+
87+
if(typeof newModule.transform !== 'function') {
88+
throw new Error(prefix + ' is missing a *transform* function.');
89+
}
90+
if(!Lib.isPlainObject(newModule.attributes)) {
91+
Lib.log(prefix + ' registered without an *attributes* object.');
92+
}
93+
if(typeof newModule.supplyDefaults !== 'function') {
94+
Lib.log(prefix + ' registered without a *supplyDefaults* function.');
95+
}
96+
8097
Plots.transformsRegistry[newModule.name] = newModule;
8198

8299
break;

src/plots/plots.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -758,10 +758,18 @@ function supplyTransformDefaults(traceIn, traceOut, layout) {
758758
for(var i = 0; i < containerIn.length; i++) {
759759
var transformIn = containerIn[i],
760760
type = transformIn.type,
761-
_module = transformsRegistry[type];
761+
_module = transformsRegistry[type],
762+
transformOut;
763+
764+
if(!_module) Lib.warn('Unrecognized transform type ' + type + '.');
762765

763-
var transformOut = _module.supplyDefaults(transformIn, traceOut, layout);
764-
transformOut.type = type;
766+
if(_module && _module.supplyDefaults) {
767+
transformOut = _module.supplyDefaults(transformIn, traceOut, layout);
768+
transformOut.type = type;
769+
}
770+
else {
771+
transformOut = Lib.extendFlat({}, transformIn);
772+
}
765773

766774
containerOut.push(transformOut);
767775
}
@@ -776,12 +784,14 @@ function applyTransforms(fullTrace, fullData, layout) {
776784
type = transform.type,
777785
_module = transformsRegistry[type];
778786

779-
dataOut = _module.transform(dataOut, {
780-
transform: transform,
781-
fullTrace: fullTrace,
782-
fullData: fullData,
783-
layout: layout
784-
});
787+
if(_module) {
788+
dataOut = _module.transform(dataOut, {
789+
transform: transform,
790+
fullTrace: fullTrace,
791+
fullData: fullData,
792+
layout: layout
793+
});
794+
}
785795
}
786796

787797
return dataOut;

test/jasmine/tests/register_test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('the register function', function() {
99
this.modulesKeys = Object.keys(Plots.modules);
1010
this.allTypesKeys = Object.keys(Plots.allTypes);
1111
this.allCategoriesKeys = Object.keys(Plots.allCategories);
12+
this.allTransformsKeys = Object.keys(Plots.transformsRegistry);
1213
});
1314

1415
afterEach(function() {
@@ -21,6 +22,7 @@ describe('the register function', function() {
2122
revertObj(Plots.modules, this.modulesKeys);
2223
revertObj(Plots.allTypes, this.allTypesKeys);
2324
revertObj(Plots.allCategories, this.allCategoriesKeys);
25+
revertObj(Plots.transformsRegistry, this.allTransformsKeys);
2426
});
2527

2628
it('should throw an error when no argument is given', function() {
@@ -68,4 +70,35 @@ describe('the register function', function() {
6870
Plotly.register([invalidTrace]);
6971
}).toThrowError(Error, 'Invalid module was attempted to be registered!');
7072
});
73+
74+
it('should throw when if transform module is invalid', function() {
75+
var missingTransformName = {
76+
moduleType: 'transform'
77+
};
78+
79+
expect(function() {
80+
Plotly.register(missingTransformName);
81+
}).toThrowError(Error, 'Transform module *name* must be a string.');
82+
83+
var missingTransformFunc = {
84+
moduleType: 'transform',
85+
name: 'mah-transform'
86+
};
87+
88+
expect(function() {
89+
Plotly.register(missingTransformFunc);
90+
}).toThrowError(Error, 'Transform module mah-transform is missing a *transform* function.');
91+
92+
var transformModule = {
93+
moduleType: 'transform',
94+
name: 'mah-transform',
95+
transform: function() {}
96+
};
97+
98+
expect(function() {
99+
Plotly.register(transformModule);
100+
}).not.toThrow();
101+
102+
expect(Plots.transformsRegistry['mah-transform']).toBeDefined();
103+
});
71104
});

0 commit comments

Comments
 (0)