File tree 2 files changed +28
-3
lines changed
2 files changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ describe('find-scripts', function () {
3
3
const scripts = {
4
4
foo : 'foo' ,
5
5
bar : 'bar' ,
6
+ barMore : 'bar-more' ,
6
7
baz : 'baz'
7
8
} ;
8
9
@@ -30,9 +31,16 @@ describe('find-scripts', function () {
30
31
console . assert ( found [ 0 ] === 'foo' ) ;
31
32
} ) ;
32
33
33
- it ( 'finds 2 ' , function ( ) {
34
+ it ( 'finds several ' , function ( ) {
34
35
const found = find ( 'b' , scripts ) ;
35
36
console . assert ( Array . isArray ( found ) ) ;
36
- console . assert ( found . length === 2 ) ;
37
+ console . assert ( found . length === 3 ) ;
38
+ } ) ;
39
+
40
+ it ( 'finds an exact match if possible' , function ( ) {
41
+ const found = find ( 'bar' , scripts ) ;
42
+ console . assert ( Array . isArray ( found ) ) ;
43
+ console . assert ( found . length === 1 , found ) ;
44
+ console . assert ( found [ 0 ] === 'bar' ) ;
37
45
} ) ;
38
46
} ) ;
Original file line number Diff line number Diff line change 1
1
function startsWith ( prefix , str ) {
2
+ console . assert ( typeof str === 'string' , 'expected string' , str ) ;
2
3
return str . indexOf ( prefix ) === 0 ;
3
4
}
4
5
6
+ function sameLength ( a , b ) {
7
+ return a . length === b . length ;
8
+ }
9
+
10
+ function matchesExactly ( prefix , str ) {
11
+ return startsWith ( prefix , str ) &&
12
+ sameLength ( prefix , str ) ;
13
+ }
14
+
5
15
function findScripts ( prefix , scripts ) {
16
+ const labels = Object . keys ( scripts ) ;
17
+ const matchesExactlyPrefix = matchesExactly . bind ( null , prefix ) ;
18
+ const exactMatches = labels . filter ( matchesExactlyPrefix ) ;
19
+ if ( exactMatches . length === 1 ) {
20
+ return exactMatches ;
21
+ }
22
+
6
23
const startsWithPrefix = startsWith . bind ( null , prefix ) ;
7
- const matchingScripts = Object . keys ( scripts ) . filter ( startsWithPrefix ) ;
24
+ const matchingScripts = labels . filter ( startsWithPrefix ) ;
8
25
return matchingScripts ;
9
26
}
10
27
You can’t perform that action at this time.
0 commit comments