Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial writer

  • Loading branch information...
commit beecc99cbc2806cb24d68e9bdb5266dcb06c0b6e 0 parents
Felix Geisendörfer authored
1  index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/m3u');
5 lib/m3u.js
@@ -0,0 +1,5 @@
+var Writer = require('./writer');
+
+exports.createWriter = function() {
+ return new Writer();
+};
33 lib/writer.js
@@ -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
+ );
+};
7 test/fixture/basic.m3u8
@@ -0,0 +1,7 @@
+#EXTM3U
+#I am a comment
+#EXTINF:10,Artist - Title
+#EXTINF:42,
+foo.mp3
+#EXTINF:23,
+bar.mp3
27 test/integration/test-writer.js
@@ -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
+ );
+ });
Please sign in to comment.
Something went wrong with that request. Please try again.