Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 081bec4

Browse files
committed
coerce: add 'freeLength' val object option for 'info_array'
- by default, input info array must have same length as the declared version, now with 'freeLength' this doesn't have to be. - set freeLength to true for the updatem menus 'args' attribute
1 parent 66e8a22 commit 081bec4

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/components/updatemenus/attributes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var buttonsAttrs = {
2727
args: {
2828
valType: 'info_array',
2929
role: 'info',
30+
freeLength: true,
3031
items: [
3132
{ valType: 'any' },
3233
{ valType: 'any' },

src/lib/coerce.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ exports.valObjects = {
233233
'An {array} of plot information.'
234234
].join(' '),
235235
requiredOpts: ['items'],
236-
otherOpts: ['dflt'],
236+
otherOpts: ['dflt', 'noFixLength'],
237237
coerceFunction: function(v, propOut, dflt, opts) {
238238
if(!Array.isArray(v)) {
239239
propOut.set(dflt);
@@ -255,10 +255,11 @@ exports.valObjects = {
255255

256256
var items = opts.items;
257257

258-
if(v.length !== items.length) return false;
258+
// when free length is off, input and declared lengths must match
259+
if(!opts.freeLength && v.length !== items.length) return false;
259260

260-
// valid when all items are valid
261-
for(var i = 0; i < items.length; i++) {
261+
// valid when all input items are valid
262+
for(var i = 0; i < v.length; i++) {
262263
var isItemValid = exports.validate(v[i], opts.items[i]);
263264

264265
if(!isItemValid) return false;

test/jasmine/tests/lib_test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,29 @@ describe('Test lib.js:', function() {
985985
}]
986986
});
987987
});
988+
989+
it('should work for valType \'info_array\' (freeLength case)', function() {
990+
var shouldPass = [
991+
['marker.color', 'red'],
992+
[{ 'marker.color': 'red' }, [1, 2]]
993+
];
994+
var shouldFail = [
995+
['marker.color', 'red', 'red'],
996+
[{ 'marker.color': 'red' }, [1, 2], 'blue']
997+
];
998+
999+
assert(shouldPass, shouldFail, {
1000+
valType: 'info_array',
1001+
freeLength: true,
1002+
items: [{
1003+
valType: 'any'
1004+
}, {
1005+
valType: 'any'
1006+
}, {
1007+
valType: 'number'
1008+
}]
1009+
});
1010+
});
9881011
});
9891012

9901013
describe('setCursor', function() {

0 commit comments

Comments
 (0)