Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Apr 19, 2024
1 parent 9666877 commit 4a49277
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
6 changes: 6 additions & 0 deletions packages/knip/src/util/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ export const compact = <T>(collection: Collection<T | undefined>) =>

export const arrayify = (value?: string[] | string) =>
Array.isArray(value) ? value : typeof value === 'string' ? [value] : [];

export const partition = <T>(collection: T[], predicate: (item: T) => unknown) => {
const results: [T[], T[]] = [[], []];
for (const item of collection) results[predicate(item) ? 0 : 1].push(item);
return results;
};
7 changes: 1 addition & 6 deletions packages/knip/src/util/map-workspaces.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import fastGlob from 'fast-glob';
import type { Package } from '../ConfigurationChief.js';
import type { PackageJson } from '../types/package-json.js';
import { partition } from './array.js';
import { debugLog } from './debug.js';
import { ConfigurationError } from './errors.js';
import { getPackageName } from './package-name.js';
import { join } from './path.js';
import { _require } from './require.js';

const partition = <T>(collection: T[], predicate: (item: T) => unknown) => {
const results: [T[], T[]] = [[], []];
for (const item of collection) results[predicate(item) ? 0 : 1].push(item);
return results;
};

export default async function mapWorkspaces(cwd: string, workspaces: string[]) {
const [negatedPatterns, patterns] = partition(workspaces, p => p.match(/^!+/));
const byPkgDir = new Map<string, Package>();
Expand Down
22 changes: 11 additions & 11 deletions packages/knip/src/util/tag.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { Tags } from '../types/cli.js';

export const splitTags = (tags: string[]) =>
tags
.flatMap(tag => tag.split(','))
.reduce<Tags>(
([incl, excl], tag) => {
const match = tag.match(/[a-zA-Z]+/);
if (match) (tag.startsWith('-') ? excl : incl).push(match[0]);
return [incl, excl];
},
[[], []]
);
export const splitTags = (rawTags: string[]) => {
const tags = rawTags.flatMap(tag => tag.split(','));
return tags.reduce<Tags>(
([incl, excl], tag) => {
const match = tag.match(/[a-zA-Z]+/);
if (match) (tag.startsWith('-') ? excl : incl).push(match[0]);
return [incl, excl];
},
[[], []]
);
};

const hasTag = (tags: string[], jsDocTags: Set<string>) => tags.some(tag => jsDocTags.has(`@${tag}`));

Expand Down

0 comments on commit 4a49277

Please sign in to comment.