Permalink
Browse files

Initial writer

  • Loading branch information...
0 parents commit beecc99cbc2806cb24d68e9bdb5266dcb06c0b6e @felixge committed Apr 11, 2011
Showing with 73 additions and 0 deletions.
  1. +1 −0 index.js
  2. +5 −0 lib/m3u.js
  3. +33 −0 lib/writer.js
  4. +7 −0 test/fixture/basic.m3u8
  5. +27 −0 test/integration/test-writer.js
@@ -0,0 +1 @@
+module.exports = require('./lib/m3u');
@@ -0,0 +1,5 @@
+var Writer = require('./writer');
+
+exports.createWriter = function() {
+ return new Writer();
+};
@@ -0,0 +1,33 @@
+function Writer(properties) {
+ this._data = '';
+}
+module.exports = Writer;
+
+Writer.prototype.comment = function(comment) {
+ this._write('#' + comment);
+};
+
+Writer.prototype.info = function(duration, title) {
+ title = title || '';
+
+ this.comment('EXTINF:' + duration + ',' + title);
+};
+
+Writer.prototype.uri = function(uri, duration, title) {
+ if (duration || title) {
+ this.info(duration, title);
+ }
+
+ this._write(uri);
+};
+
+Writer.prototype._write = function(line) {
+ this._data += line + '\n';
+};
+
+Writer.prototype.toString = function() {
+ return (
+ '#EXTM3U\n' +
+ this._data
+ );
+};
@@ -0,0 +1,7 @@
+#EXTM3U
+#I am a comment
+#EXTINF:10,Artist - Title
+#EXTINF:42,
+foo.mp3
+#EXTINF:23,
+bar.mp3
@@ -0,0 +1,27 @@
+var fs = require('fs');
+var assert = require('assert');
+var writer = require('../..').createWriter();
+
+writer.comment('I am a comment');
+writer.info('10', 'Artist - Title');
+writer.info('42');
+writer.uri('foo.mp3');
+writer.uri('bar.mp3', 23);
+
+var m3uLines = writer
+ .toString()
+ .split('\n');
+
+fs
+ .readFileSync(__dirname + '/../fixture/basic.m3u8', 'utf8')
+ .split('\n')
+ .forEach(function(line, number) {
+ var expected = m3uLines[number];
+ assert.strictEqual(
+ expected,
+ line,
+ 'Error on line ' + (number + 1) + '\n\n' +
+ 'Expected: ' + expected + '\n' +
+ 'Got: ' + line
+ );
+ });

0 comments on commit beecc99

Please sign in to comment.