@@ -34,40 +34,45 @@ function fromTo (arr, options = {}) {
3434 } )
3535 } ) ;
3636
37- const toFn = arrayBack ( options . to ) . map ( convertToFunction ) ;
38- let toIndex = - 1 ;
39- if ( toFn . length ) {
40- for ( const fn of toFn ) {
41- toIndex = arr . findIndex ( ( item , index , arr ) => {
42- if ( index > fromIndex ) {
43- const valueIndex = index - fromIndex ;
44- return fn ( item , index , arr , valueIndex )
45- } else {
46- return false
37+ /* From item not found */
38+ if ( fromIndex === - 1 ) {
39+ return [ ]
40+ } else {
41+ const toFn = arrayBack ( options . to ) . map ( convertToFunction ) ;
42+ let toIndex = - 1 ;
43+ if ( toFn . length ) {
44+ for ( const fn of toFn ) {
45+ toIndex = arr . findIndex ( ( item , index , arr ) => {
46+ if ( index > fromIndex ) {
47+ const valueIndex = index - fromIndex ;
48+ return fn ( item , index , arr , valueIndex )
49+ } else {
50+ return false
51+ }
52+ } ) ;
53+ /* Keep looping until a match is found. */
54+ if ( toIndex > - 1 ) {
55+ break
4756 }
48- } ) ;
49- /* Keep looping until a match is found. */
50- if ( toIndex > - 1 ) {
51- break
5257 }
5358 }
54- }
5559
56- /* step 2: extract items between the from and to indices */
57- const output = toIndex === - 1
58- ? arr . slice ( fromIndex ) /* Return all to the end */
59- : arr . slice ( fromIndex , toIndex ) ;
60+ /* step 2: extract items between the from and to indices */
61+ const output = toIndex === - 1
62+ ? arr . slice ( fromIndex ) /* Return all to the end */
63+ : arr . slice ( fromIndex , toIndex ) ;
6064
61- /* step 3: optionally remove from input array */
62- if ( options . remove ) {
63- if ( toIndex === - 1 ) {
64- arr . splice ( fromIndex ) ;
65- } else {
66- arr . splice ( fromIndex , toIndex - fromIndex ) ;
65+ /* step 3: optionally remove from input array */
66+ if ( options . remove ) {
67+ if ( toIndex === - 1 ) {
68+ arr . splice ( fromIndex ) ;
69+ } else {
70+ arr . splice ( fromIndex , toIndex - fromIndex ) ;
71+ }
6772 }
68- }
6973
70- return output
74+ return output
75+ }
7176}
7277
7378function convertToFunction ( fn ) {
0 commit comments