Skip to content

Commit a4f4a5f

Browse files
committed
info_array dimensions='1-2'
1 parent c046397 commit a4f4a5f

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

src/lib/coerce.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,11 @@ exports.valObjectMeta = {
260260
'An {array} of plot information.'
261261
].join(' '),
262262
requiredOpts: ['items'],
263-
// set dimensions=2 for a 2D array
263+
// set `dimensions=2` for a 2D array or '1-2' for either
264264
// `items` may be a single object instead of an array, in which case
265265
// `freeLength` must be true.
266+
// if `dimensions='1-2'` and items is a 1D array, then the value can
267+
// either be a matching 1D array or an array of such matching 1D arrays
266268
otherOpts: ['dflt', 'freeLength', 'dimensions'],
267269
coerceFunction: function(v, propOut, dflt, opts) {
268270

@@ -278,7 +280,7 @@ exports.valObjectMeta = {
278280
return out;
279281
}
280282

281-
var twoD = opts.dimensions === 2;
283+
var twoD = opts.dimensions === 2 || (opts.dimensions === '1-2' && Array.isArray(v) && Array.isArray(v[0]));
282284

283285
if(!Array.isArray(v)) {
284286
propOut.set(dflt);
@@ -288,19 +290,28 @@ exports.valObjectMeta = {
288290
var items = opts.items;
289291
var vOut = [];
290292
var arrayItems = Array.isArray(items);
291-
var len = arrayItems ? items.length : v.length;
293+
var arrayItems2D = arrayItems && twoD && Array.isArray(items[0]);
294+
var innerItemsOnly = twoD && arrayItems && !arrayItems2D;
295+
var len = (arrayItems && !innerItemsOnly) ? items.length : v.length;
292296

293-
var i, j, len2, vNew;
297+
var i, j, row, item, len2, vNew;
294298

295299
dflt = Array.isArray(dflt) ? dflt : [];
296300

297301
if(twoD) {
298302
for(i = 0; i < len; i++) {
299303
vOut[i] = [];
300-
var row = Array.isArray(v[i]) ? v[i] : [];
301-
len2 = arrayItems ? items[i].length : row.length;
304+
row = Array.isArray(v[i]) ? v[i] : [];
305+
if(innerItemsOnly) len2 = items.length;
306+
else if(arrayItems) len2 = items[i].length;
307+
else len2 = row.length;
308+
302309
for(j = 0; j < len2; j++) {
303-
vNew = coercePart(row[j], arrayItems ? items[i][j] : items, (dflt[i] || [])[j]);
310+
if(innerItemsOnly) item = items[j];
311+
else if(arrayItems) item = items[i][j];
312+
else item = items;
313+
314+
vNew = coercePart(row[j], item, (dflt[i] || [])[j]);
304315
if(vNew !== undefined) vOut[i][j] = vNew;
305316
}
306317
}

test/jasmine/tests/lib_test.js

+37
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,43 @@ describe('Test lib.js:', function() {
885885
expect(coerce({x: [[], [0], [-1, 2], [5, 'a', 4, 6.6]]}, {}, attrs, 'x'))
886886
.toEqual([[], [0], [1, 2], [5, 1, 4, 1]]);
887887
});
888+
889+
it('supports dimensions=\'1-2\' with 1D items array', function() {
890+
var attrs = {
891+
x: {
892+
valType: 'info_array',
893+
freeLength: true, // in this case only the outer length of 2D is free
894+
dimensions: '1-2',
895+
items: [
896+
{valType: 'integer', min: 0, max: 5, dflt: 1},
897+
{valType: 'integer', min: 10, max: 15, dflt: 11}
898+
]
899+
}
900+
};
901+
expect(coerce({}, {}, attrs, 'x')).toBeUndefined();
902+
expect(coerce({x: []}, {}, attrs, 'x')).toEqual([1, 11]);
903+
expect(coerce({x: [4, 4, 4]}, {}, attrs, 'x')).toEqual([4, 11]);
904+
expect(coerce({x: [[]]}, {}, attrs, 'x')).toEqual([[1, 11]]);
905+
expect(coerce({x: [[12, 12, 12]]}, {}, attrs, 'x')).toEqual([[1, 12]]);
906+
expect(coerce({x: [[], 4, true]}, {}, attrs, 'x')).toEqual([[1, 11], [1, 11], [1, 11]]);
907+
});
908+
909+
it('supports dimensions=\'1-2\' with single item', function() {
910+
var attrs = {
911+
x: {
912+
valType: 'info_array',
913+
freeLength: true,
914+
dimensions: '1-2',
915+
items: {valType: 'integer', min: 0, max: 5, dflt: 1}
916+
}
917+
};
918+
expect(coerce({}, {}, attrs, 'x')).toBeUndefined();
919+
expect(coerce({x: []}, {}, attrs, 'x')).toEqual([]);
920+
expect(coerce({x: [-3, 3, 6, 'a']}, {}, attrs, 'x')).toEqual([1, 3, 1, 1]);
921+
expect(coerce({x: [[]]}, {}, attrs, 'x')).toEqual([[]]);
922+
expect(coerce({x: [[-1, 0, 10]]}, {}, attrs, 'x')).toEqual([[1, 0, 1]]);
923+
expect(coerce({x: [[], 4, [3], [-1, 10]]}, {}, attrs, 'x')).toEqual([[], [], [3], [1, 1]]);
924+
});
888925
});
889926

890927
describe('subplotid valtype', function() {

0 commit comments

Comments
 (0)