Skip to main content
deleted 1 character in body
Source Link
Flambino
  • 33k
  • 2
  • 45
  • 90

You can use the String.prototype.replace function to "loop" through all the relevant lines of the data. It accepts a regex pattern, and a function to handle the the matched text and return the replacement text. Here, however, we don't care about actually replacing anything; we just just use it to find each key/value pair

function parsePlaylist(data) {
  var tracks = [], currentTrack;
  
  // do a multi-line replace-all for "(key): (value)", but don't
  // bother actually replacing anything, or storing the resulting
  // string. We're just using it to walk through the data
  data.replace(/^([^:]+):\s*(.+)$/gmi, function (match, key, value) {
    // if this is the start of a new track (indicated
    // by a "file" field), or the start of the loop,
    // create a new object to hold the data
    if( !currentTrack || key === "file" ) {
      currentTrack = {};
      tracks.push(currentTrack);
    }
    
    // add the key/value pair
    currentTrack[key] = value;
  });
  
  return tracks;
}

That'll give you this:

[ { file: 'http://192.168.100.214/Music/justchill.mp3',
    Artist: 'Brian G',
    Title: 'Just Chill',
    Track: '1',
    Date: '2014',
    Genre: 'Instrumental',
    Pos: '0',
    Id: '0' },
  { file: 'http://192.168.100.214/Music/justchill.mp3',
    Pos: '1',
    Id: '1' },
  { file: 'http://streaming.radionomy.com/ABC-Lounge',
    Pos: '2',
    Id: '2' } ]

You can use the String.prototype.replace function to "loop" through all the relevant lines of the data. It accepts a regex pattern, and a function to handle the the matched text and return the replacement text. Here, however, we don't care about actually replacing anything; we just just it to find each key/value pair

function parsePlaylist(data) {
  var tracks = [], currentTrack;
  
  // do a multi-line replace-all for "(key): (value)", but don't
  // bother actually replacing anything, or storing the resulting
  // string. We're just using it to walk through the data
  data.replace(/^([^:]+):\s*(.+)$/gmi, function (match, key, value) {
    // if this is the start of a new track (indicated
    // by a "file" field), or the start of the loop,
    // create a new object to hold the data
    if( !currentTrack || key === "file" ) {
      currentTrack = {};
      tracks.push(currentTrack);
    }
    
    // add the key/value pair
    currentTrack[key] = value;
  });
  
  return tracks;
}

That'll give you this:

[ { file: 'http://192.168.100.214/Music/justchill.mp3',
    Artist: 'Brian G',
    Title: 'Just Chill',
    Track: '1',
    Date: '2014',
    Genre: 'Instrumental',
    Pos: '0',
    Id: '0' },
  { file: 'http://192.168.100.214/Music/justchill.mp3',
    Pos: '1',
    Id: '1' },
  { file: 'http://streaming.radionomy.com/ABC-Lounge',
    Pos: '2',
    Id: '2' } ]

You can use the String.prototype.replace function to "loop" through all the relevant lines of the data. It accepts a regex pattern, and a function to handle the the matched text and return the replacement text. Here, however, we don't care about actually replacing anything; we just use it to find each key/value pair

function parsePlaylist(data) {
  var tracks = [], currentTrack;
  
  // do a multi-line replace-all for "(key): (value)", but don't
  // bother actually replacing anything, or storing the resulting
  // string. We're just using it to walk through the data
  data.replace(/^([^:]+):\s*(.+)$/gmi, function (match, key, value) {
    // if this is the start of a new track (indicated
    // by a "file" field), or the start of the loop,
    // create a new object to hold the data
    if( !currentTrack || key === "file" ) {
      currentTrack = {};
      tracks.push(currentTrack);
    }
    
    // add the key/value pair
    currentTrack[key] = value;
  });
  
  return tracks;
}

That'll give you this:

[ { file: 'http://192.168.100.214/Music/justchill.mp3',
    Artist: 'Brian G',
    Title: 'Just Chill',
    Track: '1',
    Date: '2014',
    Genre: 'Instrumental',
    Pos: '0',
    Id: '0' },
  { file: 'http://192.168.100.214/Music/justchill.mp3',
    Pos: '1',
    Id: '1' },
  { file: 'http://streaming.radionomy.com/ABC-Lounge',
    Pos: '2',
    Id: '2' } ]
Source Link
Flambino
  • 33k
  • 2
  • 45
  • 90

You can use the String.prototype.replace function to "loop" through all the relevant lines of the data. It accepts a regex pattern, and a function to handle the the matched text and return the replacement text. Here, however, we don't care about actually replacing anything; we just just it to find each key/value pair

function parsePlaylist(data) {
  var tracks = [], currentTrack;
  
  // do a multi-line replace-all for "(key): (value)", but don't
  // bother actually replacing anything, or storing the resulting
  // string. We're just using it to walk through the data
  data.replace(/^([^:]+):\s*(.+)$/gmi, function (match, key, value) {
    // if this is the start of a new track (indicated
    // by a "file" field), or the start of the loop,
    // create a new object to hold the data
    if( !currentTrack || key === "file" ) {
      currentTrack = {};
      tracks.push(currentTrack);
    }
    
    // add the key/value pair
    currentTrack[key] = value;
  });
  
  return tracks;
}

That'll give you this:

[ { file: 'http://192.168.100.214/Music/justchill.mp3',
    Artist: 'Brian G',
    Title: 'Just Chill',
    Track: '1',
    Date: '2014',
    Genre: 'Instrumental',
    Pos: '0',
    Id: '0' },
  { file: 'http://192.168.100.214/Music/justchill.mp3',
    Pos: '1',
    Id: '1' },
  { file: 'http://streaming.radionomy.com/ABC-Lounge',
    Pos: '2',
    Id: '2' } ]