Skip to content

Commit f4590f1

Browse files
committed
update: place default entity in first position
1 parent e8704bf commit f4590f1

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/utils/filter.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import uniqBy from "lodash/uniqBy";
2+
import { filter } from "underscore";
3+
import { pathToFileURL } from "url";
24

35
// Entity or object with path property
46
interface PathObject {
@@ -9,6 +11,7 @@ interface PathObject {
911
export interface FilterObject {
1012
path?: string;
1113
regex?: RegExp;
14+
isDefault?: boolean;
1215
}
1316

1417
/**
@@ -42,6 +45,24 @@ function isMultiPathSupported(
4245
return expandedPaths.every((expandedPath) => isPathSupported(expandedPath, filterObjects));
4346
}
4447

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+
4566
interface FilterEntityListProps {
4667
entitiesOrPaths: PathObject[]; // Array of objects defining entity path
4768
filterObjects?: FilterObject[]; // Array of path or regular expression objects
@@ -50,13 +71,13 @@ interface FilterEntityListProps {
5071

5172
/**
5273
* 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
5475
*/
5576
export function filterEntityList({
5677
entitiesOrPaths,
5778
filterObjects = [],
5879
multiPathSeparator = "",
59-
}: FilterEntityListProps) {
80+
}: FilterEntityListProps): PathObject[] {
6081
if (!filterObjects || !filterObjects.length) return [];
6182
const filterObjects_ = filterObjects.map((o) => (o.regex ? { regex: new RegExp(o.regex) } : o));
6283

@@ -69,5 +90,7 @@ export function filterEntityList({
6990
filtered = entitiesOrPaths.filter((e) => isPathSupported(e, filterObjects_));
7091
}
7192

93+
filtered = setDefaultAsFirst(filtered, filterObjects_);
94+
7295
return uniqBy(filtered, "path");
7396
}

0 commit comments

Comments
 (0)