-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cci: show additional editor comments
- Loading branch information
1 parent
25f6a06
commit 0268848
Showing
8 changed files
with
217 additions
and
6 deletions.
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
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,33 @@ | ||
/** | ||
* Iterates over an array and returns an Iterator which checks each element | ||
* of the array sequentially for a given condition (predicated by `condition`) | ||
* and returns another array, containing an element where `true` was returned, | ||
* and every subsequent element where the check returns `false`. | ||
* | ||
* @param arr | ||
* @param condition | ||
* @yield The found sequence | ||
*/ | ||
export default function* pickSequence<T>( | ||
arr: T[], | ||
condition: ( val: T ) => boolean | ||
): Iterable<T[]> { | ||
let currentValues: T[] = null; | ||
let shouldReturnValues = false; | ||
for ( const val of arr ) { | ||
if ( condition( val ) ) { | ||
shouldReturnValues = true; | ||
if ( currentValues != null ) { | ||
yield currentValues; | ||
} | ||
currentValues = [ val ]; | ||
continue; | ||
} | ||
if ( shouldReturnValues ) { | ||
currentValues.push( val ); | ||
} | ||
} | ||
if ( currentValues.length > 0 ) { | ||
yield currentValues; | ||
} | ||
} |
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,56 @@ | ||
import pickSequence from '../../../src/util/pickSequence'; | ||
|
||
describe( 'Utility function tests', () => { | ||
|
||
test( 'pickSequence', async () => { | ||
expect( Array.from( | ||
pickSequence( | ||
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], | ||
( v ) => v % 3 === 0 | ||
) | ||
) ).toStrictEqual( | ||
[ | ||
[ 3, 4, 5 ], | ||
[ 6, 7, 8 ], | ||
[ 9, 10 ] | ||
] | ||
); | ||
} ); | ||
|
||
test( 'pickSequence', async () => { | ||
expect( Array.from( | ||
pickSequence( | ||
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], | ||
( v ) => v % 2 === 0 | ||
) | ||
) ).toStrictEqual( | ||
[ | ||
[ 2, 3 ], [ 4, 5 ], [ 6, 7 ], | ||
[ 8, 9 ], [ 10 ] | ||
] | ||
); | ||
} ); | ||
|
||
test( 'pickSequence', async () => { | ||
expect( Array.from( | ||
pickSequence( | ||
[ | ||
'* line 1', | ||
'* line 2', | ||
'*: comment for line 2', | ||
'* line 3', | ||
'* line 4' | ||
], | ||
( v ) => /\*[^:*#]/.test( v ) | ||
) | ||
) ).toStrictEqual( | ||
[ | ||
[ '* line 1' ], | ||
[ '* line 2', '*: comment for line 2' ], | ||
[ '* line 3' ], | ||
[ '* line 4' ] | ||
] | ||
); | ||
} ); | ||
|
||
} ); |