Skip to content

Commit

Permalink
refactor: simplify filters by moving back to picomatch
Browse files Browse the repository at this point in the history
  • Loading branch information
byCedric committed Mar 31, 2024
1 parent 9b40741 commit 9bc3c4e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 44 deletions.
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ export { StatsFileSource } from './data/StatsFileSource';

export { AtlasError, AtlasValidationError } from './utils/errors';
export { createAtlasMiddleware } from './utils/middleware';
export { fuzzyFilterModules } from './utils/search';
export { createStatsFile, validateStatsFile, getStatsMetdata, getStatsPath } from './utils/stats';
41 changes: 0 additions & 41 deletions src/utils/search.ts

This file was deleted.

4 changes: 2 additions & 2 deletions webui/src/app/api/stats/[entry]/modules/index+api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { statsModuleFiltersFromUrlParams } from '~/components/forms/StatsModuleFilter';
import { getSource } from '~/utils/atlas';
import { globFilterModules } from '~/utils/search';
import { type StatsEntry, type StatsModule } from '~core/data/types';
import { fuzzyFilterModules } from '~core/utils/search';

/** The partial module data, when listing all available modules from a stats entry */
export type ModuleMetadata = Omit<StatsModule, 'source' | 'output'> & {
Expand Down Expand Up @@ -62,7 +62,7 @@ function filterModules(request: Request, stats: StatsEntry): ModuleMetadata[] {
modules = modules.filter((module) => !module.package);
}

return fuzzyFilterModules(modules, filters).map((module) => ({
return globFilterModules(modules, stats.projectRoot, filters).map((module) => ({
...module,
source: undefined,
output: undefined,
Expand Down
42 changes: 42 additions & 0 deletions webui/src/utils/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import path from 'path';
import picomatch from 'picomatch';

import { type StatsModule } from '~core/data/types';

type ModuleFilters = {
include?: string;
exclude?: string;
};

/**
* Filter the modules based on the include and exclude glob patterns.
* Note, you can provide multiple patterns using the comma separator.
* This also only searches the relative module path from project root, avoiding false positives.
*/
export function globFilterModules(
items: StatsModule[],
projectRoot: string,
options: ModuleFilters
) {
if (!options.include && !options.exclude) {
return items;
}

const matcher = picomatch(options.include ? splitPattern(options.include) : '**', {
cwd: '',
dot: true,
nocase: true,
contains: true,
ignore: !options.exclude ? undefined : splitPattern(options.exclude),
});

return items.filter((item) => matcher(path.relative(projectRoot, item.path)));
}

/**
* Split the comma separated string into an array of separate patterns.
* This splits on any combination of `,` and whitespaces.
*/
function splitPattern(pattern: string) {
return pattern.split(/\s*,\s*/);
}

0 comments on commit 9bc3c4e

Please sign in to comment.