forked from boyska/radiomanifest.js
parseM3U tested
This commit is contained in:
parent
1fba07bd67
commit
f55ccec939
2 changed files with 77 additions and 20 deletions
|
@ -135,11 +135,15 @@ async function get (siteurl, options) {
|
|||
}
|
||||
|
||||
function parseM3U (body) {
|
||||
body.split('\n').filter((e) => {
|
||||
if (e.startsWith('#')) {
|
||||
return body.split('\n').filter((line) => {
|
||||
if (line.startsWith('#')) {
|
||||
return false
|
||||
} else {
|
||||
try { new URL(e); return true } catch { return false }
|
||||
try {
|
||||
new URL(line); return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
53
test/parser-M3U.test.js
Normal file
53
test/parser-M3U.test.js
Normal file
|
@ -0,0 +1,53 @@
|
|||
const parseM3U = require('../radiomanifest.js').parsers.M3U
|
||||
const chai = require('chai')
|
||||
chai.use(require('chai-as-promised'))
|
||||
const assert = chai.assert
|
||||
|
||||
const expect = chai.expect
|
||||
|
||||
describe('parseM3U parses basic M3U', () => {
|
||||
describe('empty M3U', () => {
|
||||
it('should return empty list', () => {
|
||||
var r = parseM3U("")
|
||||
assert.equal(r.length, 0)
|
||||
})
|
||||
it('should discard empty lines', () => {
|
||||
var r = parseM3U("\n\n\n")
|
||||
assert.equal(r.length, 0)
|
||||
})
|
||||
})
|
||||
describe('just some lines', () => {
|
||||
it('should return appropriate list', () => {
|
||||
var r = parseM3U("http://foo")
|
||||
assert.equal(r.length, 1)
|
||||
assert.equal(r[0], "http://foo")
|
||||
})
|
||||
it('should work with longer list', () => {
|
||||
var r = parseM3U("http://foo\nhttp://baz\nhttp://bar\n")
|
||||
assert.equal(r.length, 3)
|
||||
assert.equal(r[0], "http://foo")
|
||||
assert.equal(r[1], "http://baz")
|
||||
assert.equal(r[2], "http://bar")
|
||||
})
|
||||
it('should discard empty lines', () => {
|
||||
var r = parseM3U("http://foo\n\nhttp://baz\nhttp://bar\n\n\n")
|
||||
assert.equal(r.length, 3)
|
||||
assert.equal(r[0], "http://foo")
|
||||
assert.equal(r[1], "http://baz")
|
||||
assert.equal(r[2], "http://bar")
|
||||
})
|
||||
})
|
||||
describe('comments', () => {
|
||||
it('comments should be ignored', () => {
|
||||
var r = parseM3U("http://foo\n#asd")
|
||||
assert.equal(r.length, 1)
|
||||
assert.equal(r[0], "http://foo")
|
||||
})
|
||||
it('mid-line hash is not a comment', () => {
|
||||
var r = parseM3U("http://foo#asd")
|
||||
assert.equal(r.length, 1)
|
||||
assert.equal(r[0], "http://foo#asd")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in a new issue