Tell me, how can I optimally convert a string like
const data = "1350, 1351, 1390-1391, 1401, 1402 - 1407";
to an array like
const range = [
{
min: 1350,
max: 1350,
},
{
min: 1351,
max: 1351,
},
{
min: 1390,
max: 1391,
},
{
min: 1401,
max: 1401,
},
{
min: 1402,
max: 1407,
},
];
?
In other words, you need to create an array of number ranges using a string in which these numbers ranges are explicitly specified.
The most obvious of the possible algorithms is:
1) split the string using a delimiter,
2) the resulting parts are cleaned of spaces using the command trim
3) check whether the part is a number
4) if not, then split the part using the delimiter -
5) the parts obtained are cleaned of spaces using the command trim,
6) check that the amount of component eq 2 and it's a number
But is it possible to make it more optimal, more beautiful, more effective?