From 9cc0671728003c080d5359f67dea69e914ef8380 Mon Sep 17 00:00:00 2001 From: Stephen Wade Date: Tue, 12 Sep 2023 18:50:33 -0400 Subject: [PATCH 1/2] Update filter type to allow mixed string and RegExp arrays (#1327) Co-authored-by: Raine Revere --- src/cli-options.ts | 8 ++--- src/lib/filterAndReject.ts | 2 +- src/lib/initOptions.ts | 2 +- src/types/FilterPattern.ts | 2 +- src/types/RunOptions.json | 60 ++++++++++++++++++++------------------ src/types/RunOptions.ts | 8 ++--- test/filter.test.ts | 19 ++++++++++++ 7 files changed, 62 insertions(+), 39 deletions(-) diff --git a/src/cli-options.ts b/src/cli-options.ts index b65455f27..26bcd6415 100755 --- a/src/cli-options.ts +++ b/src/cli-options.ts @@ -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], }, { @@ -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], }, { @@ -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], }, { diff --git a/src/lib/filterAndReject.ts b/src/lib/filterAndReject.ts index 69873cfee..52280d2fc 100644 --- a/src/lib/filterAndReject.ts +++ b/src/lib/filterAndReject.ts @@ -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) { diff --git a/src/lib/initOptions.ts b/src/lib/initOptions.ts index de580c8a0..04840f84a 100644 --- a/src/lib/initOptions.ts +++ b/src/lib/initOptions.ts @@ -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 diff --git a/src/types/FilterPattern.ts b/src/types/FilterPattern.ts index e90247db7..6891bc486 100644 --- a/src/types/FilterPattern.ts +++ b/src/types/FilterPattern.ts @@ -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 diff --git a/src/types/RunOptions.json b/src/types/RunOptions.json index 1e9b7b1d7..20866125f 100644 --- a/src/types/RunOptions.json +++ b/src/types/RunOptions.json @@ -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" }, @@ -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" }, @@ -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" }, @@ -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" }, diff --git a/src/types/RunOptions.ts b/src/types/RunOptions.ts index 45da00fd2..a389636e3 100644 --- a/src/types/RunOptions.ts +++ b/src/types/RunOptions.ts @@ -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[] @@ -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 diff --git a/test/filter.test.ts b/test/filter.test.ts index c0344ef62..896ffb0ad 100644 --- a/test/filter.test.ts +++ b/test/filter.test.ts @@ -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 + 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: { From 79fc218da0ec4bf8933293b1cb512bf4f51e5da3 Mon Sep 17 00:00:00 2001 From: Raine Revere Date: Tue, 12 Sep 2023 22:51:21 +0000 Subject: [PATCH 2/2] 16.13.4 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 81fe0499a..76618e7e0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "npm-check-updates", - "version": "16.13.3", + "version": "16.13.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "npm-check-updates", - "version": "16.13.3", + "version": "16.13.4", "license": "Apache-2.0", "dependencies": { "chalk": "^5.3.0", diff --git a/package.json b/package.json index ef3cc9243..b91d1c0f2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "npm-check-updates", - "version": "16.13.3", + "version": "16.13.4", "author": "Tomas Junnonen ", "license": "Apache-2.0", "contributors": [