Browse Source

add isValidSeqNo to test if a sequence number is contained in an index

Gil Pedersen 12 năm trước cách đây
mục cha
commit
0a0d3cf909
2 tập tin đã thay đổi với 23 bổ sung0 xóa
  1. 5 0
      lib/m3u8.js
  2. 18 0
      test/m3u8.test.js

+ 5 - 0
lib/m3u8.js

@@ -60,6 +60,11 @@ M3U8Playlist.prototype.lastSeqNo = function() {
   return this.first_seq_no + this.segments.length - 1;
 };
 
+// return whether the seqNo is in the index
+M3U8Playlist.prototype.isValidSeqNo = function(seqNo) {
+  return (seqNo >= this.first_seq_no) && (seqNo <= this.lastSeqNo());
+};
+
 M3U8Playlist.prototype.getSegment = function(seqNo) {
   // TODO: should we check for number type and throw if not?
   var index = seqNo-this.first_seq_no;

+ 18 - 0
test/m3u8.test.js

@@ -90,6 +90,24 @@ describe('M3U8Playlist', function() {
     })
   })
 
+  describe('#isValidSeqNo()', function() {
+    it('should return false for early numbers', function() {
+      testIndex.isValidSeqNo(-1000).should.be.false;
+      testIndex.isValidSeqNo(0).should.be.false;
+      testIndex.isValidSeqNo("100").should.be.false;
+    })
+    it('should return false for future numbers', function() {
+      testIndex.isValidSeqNo(10000).should.be.false;
+      testIndex.isValidSeqNo("10000").should.be.false;
+    })
+    it('should return true for numbers in range', function() {
+      testIndex.isValidSeqNo(7794).should.be.true;
+      testIndex.isValidSeqNo("7795").should.be.true;
+      testIndex.isValidSeqNo(7796).should.be.true;
+      testIndex.isValidSeqNo(7797).should.be.true;
+    })
+  })
+
   describe('#getSegment()', function() {
     it('should return segment data for valid sequence numbers', function() {
       testIndex.getSegment(7794).should.be.an.instanceof(m3u8.M3U8Segment);