|
1 | 1 | import { describe, it, assert } from "vitest"; |
2 | | -import parse from "./index"; |
3 | | - |
4 | | -describe("parseRange(len, str)", function () { |
5 | | - it("should reject non-string str", function () { |
6 | | - assert.throws( |
7 | | - parse.bind(null, 200, {} as any), |
8 | | - /argument str must be a string/, |
9 | | - ); |
10 | | - }); |
| 2 | +import { parse, combineRanges } from "./index"; |
11 | 3 |
|
| 4 | +describe("parse(len, str)", function () { |
12 | 5 | it("should return -2 for invalid str", function () { |
13 | 6 | assert.strictEqual(parse(200, "malformed"), -2); |
14 | 7 | }); |
@@ -57,125 +50,135 @@ describe("parseRange(len, str)", function () { |
57 | 50 |
|
58 | 51 | it("should parse str", function () { |
59 | 52 | var range = parse(1000, "bytes=0-499"); |
60 | | - assert.deepEqual( |
61 | | - range, |
62 | | - Object.assign([{ start: 0, end: 499 }], { type: "bytes" }), |
63 | | - ); |
| 53 | + assert.deepEqual(range, { |
| 54 | + type: "bytes", |
| 55 | + ranges: [{ start: 0, end: 499 }], |
| 56 | + }); |
64 | 57 | }); |
65 | 58 |
|
66 | 59 | it("should cap end at size", function () { |
67 | 60 | var range = parse(200, "bytes=0-499"); |
68 | | - assert.deepEqual( |
69 | | - range, |
70 | | - Object.assign([{ start: 0, end: 199 }], { type: "bytes" }), |
71 | | - ); |
| 61 | + assert.deepEqual(range, { |
| 62 | + type: "bytes", |
| 63 | + ranges: [{ start: 0, end: 199 }], |
| 64 | + }); |
72 | 65 | }); |
73 | 66 |
|
74 | 67 | it("should parse str", function () { |
75 | 68 | var range = parse(1000, "bytes=40-80"); |
76 | | - assert.deepEqual( |
77 | | - range, |
78 | | - Object.assign([{ start: 40, end: 80 }], { type: "bytes" }), |
79 | | - ); |
| 69 | + assert.deepEqual(range, { |
| 70 | + type: "bytes", |
| 71 | + ranges: [{ start: 40, end: 80 }], |
| 72 | + }); |
80 | 73 | }); |
81 | 74 |
|
82 | 75 | it("should parse str asking for last n bytes", function () { |
83 | 76 | var range = parse(1000, "bytes=-400"); |
84 | | - assert.deepEqual( |
85 | | - range, |
86 | | - Object.assign([{ start: 600, end: 999 }], { type: "bytes" }), |
87 | | - ); |
| 77 | + assert.deepEqual(range, { |
| 78 | + type: "bytes", |
| 79 | + ranges: [{ start: 600, end: 999 }], |
| 80 | + }); |
88 | 81 | }); |
89 | 82 |
|
90 | 83 | it("should parse str with only start", function () { |
91 | 84 | var range = parse(1000, "bytes=400-"); |
92 | | - assert.deepEqual( |
93 | | - range, |
94 | | - Object.assign([{ start: 400, end: 999 }], { type: "bytes" }), |
95 | | - ); |
| 85 | + assert.deepEqual(range, { |
| 86 | + type: "bytes", |
| 87 | + ranges: [{ start: 400, end: 999 }], |
| 88 | + }); |
96 | 89 | }); |
97 | 90 |
|
98 | 91 | it('should parse "bytes=0-"', function () { |
99 | 92 | var range = parse(1000, "bytes=0-"); |
100 | | - assert.deepEqual( |
101 | | - range, |
102 | | - Object.assign([{ start: 0, end: 999 }], { type: "bytes" }), |
103 | | - ); |
| 93 | + assert.deepEqual(range, { |
| 94 | + type: "bytes", |
| 95 | + ranges: [{ start: 0, end: 999 }], |
| 96 | + }); |
104 | 97 | }); |
105 | 98 |
|
106 | 99 | it("should parse str with no bytes", function () { |
107 | 100 | var range = parse(1000, "bytes=0-0"); |
108 | | - assert.deepEqual( |
109 | | - range, |
110 | | - Object.assign([{ start: 0, end: 0 }], { type: "bytes" }), |
111 | | - ); |
| 101 | + assert.deepEqual(range, { |
| 102 | + type: "bytes", |
| 103 | + ranges: [{ start: 0, end: 0 }], |
| 104 | + }); |
112 | 105 | }); |
113 | 106 |
|
114 | 107 | it("should parse str asking for last byte", function () { |
115 | 108 | var range = parse(1000, "bytes=-1"); |
116 | | - assert.deepEqual( |
117 | | - range, |
118 | | - Object.assign([{ start: 999, end: 999 }], { type: "bytes" }), |
119 | | - ); |
| 109 | + assert.deepEqual(range, { |
| 110 | + type: "bytes", |
| 111 | + ranges: [{ start: 999, end: 999 }], |
| 112 | + }); |
120 | 113 | }); |
121 | 114 |
|
122 | 115 | it("should parse str with multiple ranges", function () { |
123 | 116 | var range = parse(1000, "bytes=40-80,81-90,-1"); |
124 | | - assert.deepEqual( |
125 | | - range, |
126 | | - Object.assign( |
127 | | - [ |
128 | | - { start: 40, end: 80 }, |
129 | | - { start: 81, end: 90 }, |
130 | | - { start: 999, end: 999 }, |
131 | | - ], |
132 | | - { type: "bytes" }, |
133 | | - ), |
134 | | - ); |
| 117 | + assert.deepEqual(range, { |
| 118 | + type: "bytes", |
| 119 | + ranges: [ |
| 120 | + { start: 40, end: 80 }, |
| 121 | + { start: 81, end: 90 }, |
| 122 | + { start: 999, end: 999 }, |
| 123 | + ], |
| 124 | + }); |
135 | 125 | }); |
136 | 126 |
|
137 | 127 | it("should parse str with some invalid ranges", function () { |
138 | 128 | var range = parse(200, "bytes=0-499,1000-,500-999"); |
139 | | - assert.deepEqual( |
140 | | - range, |
141 | | - Object.assign([{ start: 0, end: 199 }], { type: "bytes" }), |
142 | | - ); |
| 129 | + assert.deepEqual(range, { |
| 130 | + type: "bytes", |
| 131 | + ranges: [{ start: 0, end: 199 }], |
| 132 | + }); |
143 | 133 | }); |
144 | 134 |
|
145 | 135 | it("should parse str with whitespace", function () { |
146 | 136 | var range = parse(1000, "bytes= 40-80 , 81-90 , -1 "); |
147 | | - assert.deepEqual( |
148 | | - range, |
149 | | - Object.assign( |
150 | | - [ |
151 | | - { start: 40, end: 80 }, |
152 | | - { start: 81, end: 90 }, |
153 | | - { start: 999, end: 999 }, |
154 | | - ], |
155 | | - { type: "bytes" }, |
156 | | - ), |
157 | | - ); |
| 137 | + assert.deepEqual(range, { |
| 138 | + type: "bytes", |
| 139 | + ranges: [ |
| 140 | + { start: 40, end: 80 }, |
| 141 | + { start: 81, end: 90 }, |
| 142 | + { start: 999, end: 999 }, |
| 143 | + ], |
| 144 | + }); |
158 | 145 | }); |
159 | 146 |
|
160 | 147 | it("should parse non-byte range", function () { |
161 | 148 | var range = parse(1000, "items=0-5"); |
162 | | - assert.deepEqual( |
163 | | - range, |
164 | | - Object.assign([{ start: 0, end: 5 }], { type: "items" }), |
165 | | - ); |
166 | | - }); |
167 | | - |
168 | | - describe("when combine: true", function () { |
169 | | - it("should combine overlapping ranges", function () { |
170 | | - var range = parse(150, "bytes=0-4,90-99,5-75,100-199,101-102", { |
171 | | - combine: true, |
172 | | - }); |
173 | | - assert.deepEqual(range, parse(150, "bytes=0-75,90-149")); |
| 149 | + assert.deepEqual(range, { |
| 150 | + type: "items", |
| 151 | + ranges: [{ start: 0, end: 5 }], |
174 | 152 | }); |
| 153 | + }); |
| 154 | +}); |
175 | 155 |
|
176 | | - it("should retain original order", function () { |
177 | | - var range = parse(150, "bytes=-1,20-100,0-1,101-120", { combine: true }); |
178 | | - assert.deepEqual(range, parse(150, "bytes=149-149,20-120,0-1")); |
179 | | - }); |
| 156 | +describe("combineRanges(ranges)", function () { |
| 157 | + it("should combine overlapping ranges", function () { |
| 158 | + var combined = combineRanges([ |
| 159 | + { start: 0, end: 4 }, |
| 160 | + { start: 90, end: 99 }, |
| 161 | + { start: 5, end: 75 }, |
| 162 | + { start: 100, end: 199 }, |
| 163 | + { start: 101, end: 102 }, |
| 164 | + ]); |
| 165 | + assert.deepEqual(combined, [ |
| 166 | + { start: 0, end: 75 }, |
| 167 | + { start: 90, end: 199 }, |
| 168 | + ]); |
| 169 | + }); |
| 170 | + |
| 171 | + it("should retain original order", function () { |
| 172 | + var combined = combineRanges([ |
| 173 | + { start: 149, end: 149 }, |
| 174 | + { start: 20, end: 100 }, |
| 175 | + { start: 0, end: 1 }, |
| 176 | + { start: 101, end: 120 }, |
| 177 | + ]); |
| 178 | + assert.deepEqual(combined, [ |
| 179 | + { start: 149, end: 149 }, |
| 180 | + { start: 20, end: 120 }, |
| 181 | + { start: 0, end: 1 }, |
| 182 | + ]); |
180 | 183 | }); |
181 | 184 | }); |
0 commit comments