-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add and use generic playlist attribute list parser #107
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
14082bf
add and use generic playlist attribute list parser
kanongil d7b6da1
revised AttrList implementation and added tests
kanongil b5f25c7
when precision is lost return infinity rather than throwing
kanongil e3c471b
update decimalResolution return value for invalid input and add addit…
kanongil File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
|
||
// adapted from https://github.com/kanongil/node-m3u8parse/blob/master/attrlist.js | ||
class AttrList { | ||
|
||
constructor(attrs) { | ||
if (typeof attrs === 'string') { | ||
attrs = AttrList.parseAttrList(attrs); | ||
} | ||
|
||
Object.assign(this, attrs); | ||
} | ||
|
||
decimalInteger(attrName) { | ||
const intValue = parseInt(this[attrName], 10); | ||
if (intValue > Number.MAX_SAFE_INTEGER) { | ||
return Infinity; | ||
} | ||
return intValue; | ||
} | ||
|
||
hexadecimalInteger(attrName) { | ||
let stringValue = (this[attrName] || '0x').slice(2); | ||
stringValue = ((stringValue.length & 1) ? '0' : '') + stringValue; | ||
|
||
const value = new Uint8Array(stringValue.length / 2); | ||
for (let i = 0; i < stringValue.length / 2; i++) { | ||
value[i] = parseInt(stringValue.slice(i * 2, i * 2 + 2), 16); | ||
} | ||
return value; | ||
} | ||
|
||
hexadecimalIntegerAsNumber(attrName) { | ||
const intValue = parseInt(this[attrName], 16); | ||
if (intValue > Number.MAX_SAFE_INTEGER) { | ||
return Infinity; | ||
} | ||
return intValue; | ||
} | ||
|
||
decimalFloatingPoint(attrName) { | ||
return parseFloat(this[attrName]); | ||
} | ||
|
||
quotedString(attrName) { | ||
const val = this[attrName]; | ||
return val ? val.slice(1, -1) : undefined; | ||
} | ||
|
||
enumeratedString(attrName) { | ||
return this[attrName]; | ||
} | ||
|
||
decimalResolution(attrName) { | ||
const res = /^(\d+)x(\d+)$/.exec(this[attrName]); | ||
if (res === null) { | ||
return undefined; | ||
} | ||
return { | ||
width: parseInt(res[1], 10), | ||
height: parseInt(res[2], 10) | ||
}; | ||
} | ||
|
||
static parseAttrList(input) { | ||
const re = /(.+?)=((?:\".*?\")|.*?)(?:,|$)/g; | ||
var match, attrs = {}; | ||
while ((match = re.exec(input)) !== null) { | ||
attrs[match[1]] = match[2]; | ||
} | ||
return attrs; | ||
} | ||
|
||
} | ||
|
||
export default AttrList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
const assert = require('assert'); | ||
const bufferIsEqual = require('arraybuffer-equal'); | ||
|
||
import AttrList from '../../../src/utils/attr-list'; | ||
|
||
describe('AttrList', () => { | ||
it('constructor() supports empty arguments', () => { | ||
assert.deepEqual(new AttrList(), {}); | ||
assert.deepEqual(new AttrList({}), {}); | ||
assert.deepEqual(new AttrList(undefined), {}); | ||
}); | ||
it('constructor() supports object argument', () => { | ||
const obj = { VALUE: "42" }; | ||
const list = new AttrList(obj); | ||
assert.strictEqual(list.decimalInteger('VALUE'), 42); | ||
assert.strictEqual(Object.keys(list).length, 1); | ||
}); | ||
|
||
it('parses valid decimalInteger attribute', () => { | ||
assert.strictEqual(new AttrList('INT=42').decimalInteger('INT'), 42); | ||
assert.strictEqual(new AttrList('INT=0').decimalInteger('INT'), 0); | ||
}); | ||
it('parses valid hexadecimalInteger attribute', () => { | ||
assert.strictEqual(new AttrList('HEX=0x42').hexadecimalIntegerAsNumber('HEX'), 0x42); | ||
assert.strictEqual(new AttrList('HEX=0X42').hexadecimalIntegerAsNumber('HEX'), 0x42); | ||
assert.strictEqual(new AttrList('HEX=0x0').hexadecimalIntegerAsNumber('HEX'), 0); | ||
}); | ||
it('parses valid decimalFloatingPoint attribute', () => { | ||
assert.strictEqual(new AttrList('FLOAT=0.42').decimalFloatingPoint('FLOAT'), 0.42); | ||
assert.strictEqual(new AttrList('FLOAT=-0.42').decimalFloatingPoint('FLOAT'), -0.42); | ||
assert.strictEqual(new AttrList('FLOAT=0').decimalFloatingPoint('FLOAT'), 0); | ||
}); | ||
it('parses valid quotedString attribute', () => { | ||
assert.strictEqual(new AttrList('STRING="hi"').quotedString('STRING'), 'hi'); | ||
assert.strictEqual(new AttrList('STRING=""').quotedString('STRING'), ''); | ||
}); | ||
it('parses exotic quotedString attribute', () => { | ||
const list = new AttrList('STRING="hi,ENUM=OK,RES=4x2"'); | ||
assert.strictEqual(list.quotedString('STRING'), 'hi,ENUM=OK,RES=4x2'); | ||
assert.strictEqual(Object.keys(list).length, 1); | ||
}); | ||
it('parses valid enumeratedString attribute', () => { | ||
assert.strictEqual(new AttrList('ENUM=OK').enumeratedString('ENUM'), 'OK'); | ||
}); | ||
it('parses exotic enumeratedString attribute', () => { | ||
assert.strictEqual(new AttrList('ENUM=1').enumeratedString('ENUM'), '1'); | ||
assert.strictEqual(new AttrList('ENUM=A=B').enumeratedString('ENUM'), 'A=B'); | ||
assert.strictEqual(new AttrList('ENUM=A=B=C').enumeratedString('ENUM'), 'A=B=C'); | ||
const list = new AttrList('ENUM1=A=B=C,ENUM2=42'); | ||
assert.strictEqual(list.enumeratedString('ENUM1'), 'A=B=C'); | ||
assert.strictEqual(list.enumeratedString('ENUM2'), '42'); | ||
}); | ||
it('parses valid decimalResolution attribute', () => { | ||
assert.deepStrictEqual(new AttrList('RES=400x200').decimalResolution('RES'), { width:400, height:200 }); | ||
assert.deepStrictEqual(new AttrList('RES=0x0').decimalResolution('RES'), { width:0, height:0 }); | ||
}); | ||
it('handles invalid decimalResolution attribute', () => { | ||
assert.deepStrictEqual(new AttrList('RES=400x-200').decimalResolution('RES'), undefined); | ||
assert.deepStrictEqual(new AttrList('RES=400.5x200').decimalResolution('RES'), undefined); | ||
assert.deepStrictEqual(new AttrList('RES=400x200.5').decimalResolution('RES'), undefined); | ||
assert.deepStrictEqual(new AttrList('RES=400').decimalResolution('RES'), undefined); | ||
assert.deepStrictEqual(new AttrList('RES=400x').decimalResolution('RES'), undefined); | ||
assert.deepStrictEqual(new AttrList('RES=x200').decimalResolution('RES'), undefined); | ||
assert.deepStrictEqual(new AttrList('RES=x').decimalResolution('RES'), undefined); | ||
}); | ||
|
||
it('parses multiple attributes', () => { | ||
const list = new AttrList('INT=42,HEX=0x42,FLOAT=0.42,STRING="hi",ENUM=OK,RES=4x2'); | ||
assert.strictEqual(list.decimalInteger('INT'), 42); | ||
assert.strictEqual(list.hexadecimalIntegerAsNumber('HEX'), 0x42); | ||
assert.strictEqual(list.decimalFloatingPoint('FLOAT'), 0.42); | ||
assert.strictEqual(list.quotedString('STRING'), 'hi'); | ||
assert.strictEqual(list.enumeratedString('ENUM'), 'OK'); | ||
assert.deepStrictEqual(list.decimalResolution('RES'), { width:4, height:2 }); | ||
assert.strictEqual(Object.keys(list).length, 6); | ||
}); | ||
|
||
it('handles missing attributes', () => { | ||
const list = new AttrList(); | ||
assert(isNaN(list.decimalInteger('INT'))); | ||
assert(isNaN(list.hexadecimalIntegerAsNumber('HEX'))); | ||
assert(isNaN(list.decimalFloatingPoint('FLOAT'))); | ||
assert.strictEqual(list.quotedString('STRING'), undefined); | ||
assert.strictEqual(list.enumeratedString('ENUM'), undefined); | ||
assert.strictEqual(list.decimalResolution('RES'), undefined); | ||
assert.strictEqual(Object.keys(list).length, 0); | ||
}); | ||
|
||
it('parses dashed attribute names', () => { | ||
const list = new AttrList('INT-VALUE=42,H-E-X=0x42,-FLOAT=0.42,STRING-="hi",ENUM=OK'); | ||
assert.strictEqual(list.decimalInteger('INT-VALUE'), 42); | ||
assert.strictEqual(list.hexadecimalIntegerAsNumber('H-E-X'), 0x42); | ||
assert.strictEqual(list.decimalFloatingPoint('-FLOAT'), 0.42); | ||
assert.strictEqual(list.quotedString('STRING-'), 'hi'); | ||
assert.strictEqual(list.enumeratedString('ENUM'), 'OK'); | ||
assert.strictEqual(Object.keys(list).length, 5); | ||
}); | ||
|
||
it('handles hexadecimalInteger conversions', () => { | ||
const list = new AttrList('HEX1=0x0123456789abcdef0123456789abcdef,HEX2=0x123,HEX3=0x0'); | ||
assert(bufferIsEqual(list.hexadecimalInteger('HEX1').buffer, new Uint8Array([0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef]).buffer)); | ||
assert(bufferIsEqual(list.hexadecimalInteger('HEX2').buffer, new Uint8Array([0x01,0x23]).buffer)); | ||
assert(bufferIsEqual(list.hexadecimalInteger('HEX3').buffer, new Uint8Array([0x0]).buffer)); | ||
}); | ||
|
||
it('returns infinity on large number conversions', () => { | ||
const list = new AttrList('VAL=12345678901234567890,HEX=0x0123456789abcdef0123456789abcdef'); | ||
assert.strictEqual(list.decimalInteger('VAL'), Infinity); | ||
assert.strictEqual(list.hexadecimalIntegerAsNumber('HEX'), Infinity); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @kanongil i am not clear about this new dependency to babel ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mocha
does not understand es6 code and needs a compiler, which I register using--compilers js:babel/register
. As such it needs to be declared a dev dependency as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fine, I will give it a hand soon