I want to reduce this nested array:
const list = ['Map<%s,%s>', ['string', 'Map<%s,%s>', ['string', 'boolean']]];
so that list becomes:
'Map<string,Map<string,boolean>>'
Here is a start, but the recusion is really confusing to me:
const util = require('util');
const reduceToString = function(l){
return l.reduceRight((a,b) => {
if(Array.isArray(a)){
return reduceToString(a);
}
return util.format(b, a);
});
};
console.log(reduce(list));
For better understanding to see how this needs to work generically, this input:
const list = ['Map<%s,%s,%s>', ['string', 'Map<%s,%s>', ['string', 'boolean'], 'number']];
should yield:
'Map<string,Map<string,boolean>,number>'
The general rule is: any array to the right of a string, should be interpolated into the string, and the reduceToString function should always return a string.
%s, so it should either have three elements total, where the 2nd to last are inserted into the first, or it should have two elements, where the 2nd is an array that has in turn two elements, reflecting the two%s. The nested array you have doesn't seem to have a consistent grammar.Maptypes, or the type is dynamic (could be others)?list.join('').replace(/%s,%s>/g, '').replace(/<,/g, '<') + '>>'if your Array format is guaranteed to be always the same.