Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update filter type to allow mixed string and RegExp arrays #1327

Merged
merged 3 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ const cliOptions: CLIOption[] = [
arg: 'p',
description:
'Include only package names matching the given string, wildcard, glob, comma-or-space-delimited list, /regex/, or predicate function.',
type: 'string | string[] | RegExp | RegExp[] | FilterFunction',
type: 'string | RegExp | (string | RegExp)[] | FilterFunction',
parse: (value, accum) => [...(accum || []), value],
},
{
Expand All @@ -518,7 +518,7 @@ const cliOptions: CLIOption[] = [
long: 'filterVersion',
arg: 'p',
description: 'Filter on package version using comma-or-space-delimited list, /regex/, or predicate function.',
type: 'string | string[] | RegExp | RegExp[] | FilterFunction',
type: 'string | RegExp | (string | RegExp)[] | FilterFunction',
parse: (value, accum) => [...(accum || []), value],
},
{
Expand Down Expand Up @@ -659,14 +659,14 @@ const cliOptions: CLIOption[] = [
arg: 'p',
description:
'Exclude packages matching the given string, wildcard, glob, comma-or-space-delimited list, /regex/, or predicate function.',
type: 'string | string[] | RegExp | RegExp[] | FilterFunction',
type: 'string | RegExp | (string | RegExp)[] | FilterFunction',
parse: (value, accum) => [...(accum || []), value],
},
{
long: 'rejectVersion',
arg: 'p',
description: 'Exclude package.json versions using comma-or-space-delimited list, /regex/, or predicate function.',
type: 'string | string[] | RegExp | RegExp[] | FilterFunction',
type: 'string | RegExp | (string | RegExp)[] | FilterFunction',
parse: (value, accum) => [...(accum || []), value],
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib/filterAndReject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function composeFilter(filterPattern: FilterPattern): (name: string, versionSpec
// array
else if (Array.isArray(filterPattern)) {
predicate = (dependencyName: string, versionSpec: string) =>
filterPattern.some((subpattern: string | RegExp) => composeFilter(subpattern)(dependencyName, versionSpec))
filterPattern.some(subpattern => composeFilter(subpattern)(dependencyName, versionSpec))
}
// raw RegExp
else if (filterPattern instanceof RegExp) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/initOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function parseFilterExpression(filterExpression: FilterPattern | undefined): Fil
Array.isArray(filterExpression) &&
(filterExpression.length === 0 || typeof filterExpression[0] === 'string')
) {
const filtered = (filterExpression as string[]).map(s => s.trim()).filter(x => x)
const filtered = filterExpression.map(s => (typeof s === 'string' ? s.trim() : s)).filter(x => x)
return filtered.length > 0 ? filtered : undefined
} else {
return filterExpression
Expand Down
2 changes: 1 addition & 1 deletion src/types/FilterPattern.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FilterFunction } from './FilterFunction'

/** Supported patterns for the --filter and --reject options. */
export type FilterPattern = string | string[] | RegExp | RegExp[] | FilterFunction
export type FilterPattern = string | RegExp | (string | RegExp)[] | FilterFunction
60 changes: 32 additions & 28 deletions src/types/RunOptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,20 @@
{
"$ref": "#/definitions/RegExp"
},
{
"items": {
"type": "string"
},
"type": "array"
},
{
"description": "Supported function for the --filter and --reject options.",
"type": "object"
},
{
"items": {
"$ref": "#/definitions/RegExp"
"anyOf": [
{
"$ref": "#/definitions/RegExp"
},
{
"type": "string"
}
]
},
"type": "array"
},
Expand All @@ -262,19 +263,20 @@
{
"$ref": "#/definitions/RegExp"
},
{
"items": {
"type": "string"
},
"type": "array"
},
{
"description": "Supported function for the --filter and --reject options.",
"type": "object"
},
{
"items": {
"$ref": "#/definitions/RegExp"
"anyOf": [
{
"$ref": "#/definitions/RegExp"
},
{
"type": "string"
}
]
},
"type": "array"
},
Expand Down Expand Up @@ -380,19 +382,20 @@
{
"$ref": "#/definitions/RegExp"
},
{
"items": {
"type": "string"
},
"type": "array"
},
{
"description": "Supported function for the --filter and --reject options.",
"type": "object"
},
{
"items": {
"$ref": "#/definitions/RegExp"
"anyOf": [
{
"$ref": "#/definitions/RegExp"
},
{
"type": "string"
}
]
},
"type": "array"
},
Expand All @@ -407,19 +410,20 @@
{
"$ref": "#/definitions/RegExp"
},
{
"items": {
"type": "string"
},
"type": "array"
},
{
"description": "Supported function for the --filter and --reject options.",
"type": "object"
},
{
"items": {
"$ref": "#/definitions/RegExp"
"anyOf": [
{
"$ref": "#/definitions/RegExp"
},
{
"type": "string"
}
]
},
"type": "array"
},
Expand Down
8 changes: 4 additions & 4 deletions src/types/RunOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ export interface RunOptions {
errorLevel?: number

/** Include only package names matching the given string, wildcard, glob, comma-or-space-delimited list, /regex/, or predicate function. */
filter?: string | string[] | RegExp | RegExp[] | FilterFunction
filter?: string | RegExp | (string | RegExp)[] | FilterFunction

/** Filters out upgrades based on a user provided function. Run "ncu --help --filterResults" for details. */
filterResults?: FilterResultsFunction

/** Filter on package version using comma-or-space-delimited list, /regex/, or predicate function. */
filterVersion?: string | string[] | RegExp | RegExp[] | FilterFunction
filterVersion?: string | RegExp | (string | RegExp)[] | FilterFunction

/** Modify the output formatting or show additional information. Specify one or more comma-delimited values: group, ownerChanged, repo, time, lines. Run "ncu --help --format" for details. */
format?: string[]
Expand Down Expand Up @@ -146,10 +146,10 @@ export interface RunOptions {
registryType?: 'npm' | 'json'

/** Exclude packages matching the given string, wildcard, glob, comma-or-space-delimited list, /regex/, or predicate function. */
reject?: string | string[] | RegExp | RegExp[] | FilterFunction
reject?: string | RegExp | (string | RegExp)[] | FilterFunction

/** Exclude package.json versions using comma-or-space-delimited list, /regex/, or predicate function. */
rejectVersion?: string | string[] | RegExp | RegExp[] | FilterFunction
rejectVersion?: string | RegExp | (string | RegExp)[] | FilterFunction

/** Remove version ranges from the final package version. */
removeRange?: boolean
Expand Down
19 changes: 19 additions & 0 deletions test/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,25 @@ describe('filter', () => {
upgraded.should.have.property('fp-and-or')
})

it('filter with array of mixed strings and regex', async () => {
const upgraded = (await ncu({
packageData: {
dependencies: {
'fp-and-or': '0.1.0',
lodash: '2.0.0',
'lodash.map': '2.0.0',
'lodash.filter': '2.0.0',
},
},
filter: ['fp-and-or', /lodash\..*/],
})) as Index<string>
upgraded.should.deep.equal({
'fp-and-or': '99.9.9',
'lodash.map': '99.9.9',
'lodash.filter': '99.9.9',
})
})

it('filter with array of regex strings', async () => {
const upgraded = (await ncu({
packageData: {
Expand Down
Loading