I have incoming strings that are formatted using some bracket notation method I haven't seen before (I assume to compress size). Understanding the method itself is straightforward but I'm having trouble turning that into code it out.
Here is a simple example of an incoming string: 'ADFAS[AA,BT]'
This string needs to be parsed so that it results in the array: ['ADFASAA', 'ADFASBT'].
It is important that order is maintained in the resulting array. I'm open to any nice clean answer although I like the idea of an elegant recursive method here. I'm not having luck.
Here is a more complicated example:
'INGFKQRRG[I5EEOR[T,U,V,W],Y5ECOR[T,U]]'
Which should yield the result:
[
'INGFKQRRGI5EEORT', //--
'INGFKQRRGI5EEORU', // \
'INGFKQRRGI5EEORV', // / - these are from the first nested level block and its nested letter endings
'INGFKQRRGI5EEORW', //--
'INGFKQRRGY5ECORT', //--\
'INGFKQRRGY5ECORU', //--/ - these are from the second nested level block and its nested letter endings
]
The incoming strings may have multiple nested levels. Ex: 'GMZTAOSIHI[2Q,3[A,Q],4A]'
Which should yield:
[
'GMZTAOSIHI2Q',
'GMZTAOSIHI3A',
'GMZTAOSIHI3Q',
'GMZTAOSIHI4A'
]
Here's the approach I've been taking so far:
A function that finds the outermost bracketed substr
function getInnerBracket(s) {
const sBracket = s.indexOf('['),
eBracket = s.lastIndexOf(']');
return t.slice(sBracket + 1, eBracket);
}
Then I can test the string to see if it has a ], which indicates that within the returned string has multiple parts, some of which may have more nested items.
function testForSubItems(s) {
return /\],/.test(s)
? s.split('],')
: s;
}
At this point I feel very stuck because I can't figure out how to write one wrapper function to recursively keep track of when to pass it back down to these two helper functions. Every attempt just ends up with logic holes and I'm stuck.