1
1
import uniqBy from "lodash/uniqBy" ;
2
+ import { filter } from "underscore" ;
3
+ import { pathToFileURL } from "url" ;
2
4
3
5
// Entity or object with path property
4
6
interface PathObject {
@@ -9,6 +11,7 @@ interface PathObject {
9
11
export interface FilterObject {
10
12
path ?: string ;
11
13
regex ?: RegExp ;
14
+ isDefault ?: boolean ;
12
15
}
13
16
14
17
/**
@@ -42,6 +45,24 @@ function isMultiPathSupported(
42
45
return expandedPaths . every ( ( expandedPath ) => isPathSupported ( expandedPath , filterObjects ) ) ;
43
46
}
44
47
48
+ function setDefaultAsFirst (
49
+ pathObjects : PathObject [ ] ,
50
+ filterObjects : FilterObject [ ] ,
51
+ ) : PathObject [ ] {
52
+ const defaultFilter = filterObjects . find ( ( f ) => f . isDefault ) ;
53
+ if ( ! defaultFilter ) return pathObjects ;
54
+
55
+ const defaultIndex = pathObjects . findIndex ( ( pathObj ) => {
56
+ return isPathSupported ( pathObj , [ defaultFilter ] ) ;
57
+ } ) ;
58
+ if ( defaultIndex < 0 || pathObjects . length < 2 ) return pathObjects ;
59
+
60
+ const tmp = pathObjects [ 1 ] ;
61
+ pathObjects [ 0 ] = pathObjects [ defaultIndex ] ;
62
+ pathObjects [ 1 ] = tmp ;
63
+ return pathObjects ;
64
+ }
65
+
45
66
interface FilterEntityListProps {
46
67
entitiesOrPaths : PathObject [ ] ; // Array of objects defining entity path
47
68
filterObjects ?: FilterObject [ ] ; // Array of path or regular expression objects
@@ -50,13 +71,13 @@ interface FilterEntityListProps {
50
71
51
72
/**
52
73
* Filter list of entity paths or entities by paths and regular expressions.
53
- * @return { Object[] } - filtered entity path objects or entities
74
+ * @return - filtered entity path objects or entities
54
75
*/
55
76
export function filterEntityList ( {
56
77
entitiesOrPaths,
57
78
filterObjects = [ ] ,
58
79
multiPathSeparator = "" ,
59
- } : FilterEntityListProps ) {
80
+ } : FilterEntityListProps ) : PathObject [ ] {
60
81
if ( ! filterObjects || ! filterObjects . length ) return [ ] ;
61
82
const filterObjects_ = filterObjects . map ( ( o ) => ( o . regex ? { regex : new RegExp ( o . regex ) } : o ) ) ;
62
83
@@ -69,5 +90,7 @@ export function filterEntityList({
69
90
filtered = entitiesOrPaths . filter ( ( e ) => isPathSupported ( e , filterObjects_ ) ) ;
70
91
}
71
92
93
+ filtered = setDefaultAsFirst ( filtered , filterObjects_ ) ;
94
+
72
95
return uniqBy ( filtered , "path" ) ;
73
96
}
0 commit comments