@@ -55,7 +55,7 @@ export default function waveformDataTests(WaveformData) {
5555 context ( "with " + bits + "-bit " + format + " data" , function ( ) {
5656 beforeEach ( function ( ) {
5757 const data = format === "binary" ? getBinaryData ( { channels : 1 , bits : bits } )
58- : getJSONData ( { channels : 1 , bits : bits } ) ;
58+ : getJSONData ( { channels : 1 , bits : bits } ) ;
5959
6060 instance = WaveformData . create ( data ) ;
6161 } ) ;
@@ -284,6 +284,96 @@ export default function waveformDataTests(WaveformData) {
284284 expect ( result . duration ) . to . equal ( expectations . duration * 3 ) ;
285285 } ) ;
286286 } ) ;
287+
288+ describe ( ".slice()" , function ( ) {
289+ context ( "with valid startIndex and endIndex" , function ( ) {
290+ it ( "should return a subset of the waveform" , function ( ) {
291+ var extract = instance . slice ( { startIndex : 1 , endIndex : 4 } ) ;
292+
293+ expect ( extract . length ) . to . equal ( 3 ) ;
294+ expect ( extract . channels ) . to . equal ( instance . channels ) ;
295+ expect ( extract . bits ) . to . equal ( instance . bits ) ;
296+ expect ( extract . channel ( 0 ) . min_array ( ) ) . to . deep . equal ( [ - 10 , 0 , - 5 ] ) ;
297+ expect ( extract . channel ( 0 ) . max_array ( ) ) . to . deep . equal ( [ 10 , 0 , 7 ] ) ;
298+ } ) ;
299+ } ) ;
300+
301+ context ( "with endIndex beyond the length of the waveform" , function ( ) {
302+ it ( "should limit to the end of the waveform" , function ( ) {
303+ var extract = instance . slice ( { startIndex : 1 , endIndex : 12 } ) ;
304+
305+ expect ( extract . length ) . to . equal ( 9 ) ;
306+ expect ( extract . channels ) . to . equal ( instance . channels ) ;
307+ expect ( extract . bits ) . to . equal ( instance . bits ) ;
308+ expect ( extract . channel ( 0 ) . min_array ( ) ) . to . deep . equal ( [ - 10 , 0 , - 5 , - 5 , 0 , 0 , 0 , 0 , - 2 ] ) ;
309+ expect ( extract . channel ( 0 ) . max_array ( ) ) . to . deep . equal ( [ 10 , 0 , 7 , 7 , 0 , 0 , 0 , 0 , 2 ] ) ;
310+ } ) ;
311+ } ) ;
312+
313+ context ( "with startIndex equal to endIndex" , function ( ) {
314+ it ( "should return a zero length waveform" , function ( ) {
315+ var extract = instance . slice ( { startIndex : 1 , endIndex : 1 } ) ;
316+
317+ expect ( extract . length ) . to . equal ( 0 ) ;
318+ expect ( extract . channels ) . to . equal ( instance . channels ) ;
319+ expect ( extract . bits ) . to . equal ( instance . bits ) ;
320+ expect ( extract . channel ( 0 ) . min_array ( ) ) . to . deep . equal ( [ ] ) ;
321+ expect ( extract . channel ( 0 ) . max_array ( ) ) . to . deep . equal ( [ ] ) ;
322+ } ) ;
323+ } ) ;
324+
325+ context ( "with startIndex greater than endIndex" , function ( ) {
326+ it ( "should return a zero length waveform" , function ( ) {
327+ var extract = instance . slice ( { startIndex : 1 , endIndex : 1 } ) ;
328+
329+ expect ( extract . length ) . to . equal ( 0 ) ;
330+ expect ( extract . channels ) . to . equal ( instance . channels ) ;
331+ expect ( extract . bits ) . to . equal ( instance . bits ) ;
332+ expect ( extract . channel ( 0 ) . min_array ( ) ) . to . deep . equal ( [ ] ) ;
333+ expect ( extract . channel ( 0 ) . max_array ( ) ) . to . deep . equal ( [ ] ) ;
334+ } ) ;
335+ } ) ;
336+
337+ context ( "with startIndex and endIndex beyond the length of the waveform" , function ( ) {
338+ it ( "should return a zero length waveform" , function ( ) {
339+ var extract = instance . slice ( { startIndex : 10 , endIndex : 12 } ) ;
340+
341+ expect ( extract . length ) . to . equal ( 0 ) ;
342+ expect ( extract . channels ) . to . equal ( instance . channels ) ;
343+ expect ( extract . bits ) . to . equal ( instance . bits ) ;
344+ expect ( extract . channel ( 0 ) . min_array ( ) ) . to . deep . equal ( [ ] ) ;
345+ expect ( extract . channel ( 0 ) . max_array ( ) ) . to . deep . equal ( [ ] ) ;
346+ } ) ;
347+ } ) ;
348+
349+ context ( "with negative startIndex" , function ( ) {
350+ it ( "should throw RangeError" , function ( ) {
351+ expect ( function ( ) {
352+ instance . slice ( { startIndex : - 1 , endIndex : 4 } ) ;
353+ } ) . to . throw ( RangeError ) ;
354+ } ) ;
355+ } ) ;
356+
357+ context ( "with negative endIndex" , function ( ) {
358+ it ( "should throw RangeError" , function ( ) {
359+ expect ( function ( ) {
360+ instance . slice ( { startIndex : 1 , endIndex : - 1 } ) ;
361+ } ) . to . throw ( RangeError ) ;
362+ } ) ;
363+ } ) ;
364+
365+ context ( "with valid startTime and endTime" , function ( ) {
366+ it ( "should return a subset of the waveform" , function ( ) {
367+ var extract = instance . slice ( { startTime : instance . time ( 1 ) , endTime : instance . time ( 4 ) } ) ;
368+
369+ expect ( extract . length ) . to . equal ( 3 ) ;
370+ expect ( extract . channels ) . to . equal ( instance . channels ) ;
371+ expect ( extract . bits ) . to . equal ( instance . bits ) ;
372+ expect ( extract . channel ( 0 ) . min_array ( ) ) . to . deep . equal ( [ - 10 , 0 , - 5 ] ) ;
373+ expect ( extract . channel ( 0 ) . max_array ( ) ) . to . deep . equal ( [ 10 , 0 , 7 ] ) ;
374+ } ) ;
375+ } ) ;
376+ } ) ;
287377 } ) ;
288378 } ) ;
289379 } ) ;
@@ -295,7 +385,7 @@ export default function waveformDataTests(WaveformData) {
295385 context ( "with " + bits + "-bit " + format + " data" , function ( ) {
296386 beforeEach ( function ( ) {
297387 const data = format === "binary" ? getBinaryData ( { channels : 2 , bits : bits } )
298- : getJSONData ( { channels : 2 , bits : bits } ) ;
388+ : getJSONData ( { channels : 2 , bits : bits } ) ;
299389
300390 instance = WaveformData . create ( data ) ;
301391 } ) ;
0 commit comments