Skip to content

Commit

Permalink
feat: remove scoped package / when pattern without / (#1168)
Browse files Browse the repository at this point in the history
Co-authored-by: Raine Revere <raine@cybersemics.org>
  • Loading branch information
magicdawn and raineorshine committed Jul 22, 2022
1 parent ef008ce commit 7bab8bb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/lib/filterAndReject.ts
@@ -1,4 +1,4 @@
import { and } from 'fp-and-or'
import { and, or } from 'fp-and-or'
import identity from 'lodash/identity'
import negate from 'lodash/negate'
import minimatch from 'minimatch'
Expand Down Expand Up @@ -31,7 +31,19 @@ function composeFilter(filterPattern: FilterRejectPattern): (name: string, versi
// glob string
else {
const patterns = filterPattern.split(/[\s,]+/)
predicate = (dependencyName: string) => patterns.some(pattern => minimatch(dependencyName, pattern))
predicate = (dependencyName: string) => {
/** Returns true if the pattern matches an unscoped dependency name. */
const matchUnscoped = (pattern: string) => minimatch(dependencyName, pattern)

/** Returns true if the pattern matches a scoped dependency name. */
const matchScoped = (pattern: string) =>
!pattern.includes('/') &&
dependencyName.includes('/') &&
minimatch(dependencyName.replace(/\//g, '_'), pattern)

// return true if any of the provided patterns match the dependency name
return patterns.some(or(matchUnscoped, matchScoped))
}
}
}
// array
Expand Down
45 changes: 45 additions & 0 deletions test/filter.test.ts
Expand Up @@ -41,6 +41,51 @@ describe('filter', () => {
upgraded.should.have.property('lodash.filter')
})

it('filter with wildcard for scoped package', async () => {
const pkg = {
dependencies: {
vite: '1.0.0',
'@vitejs/plugin-react': '1.0.0',
'@vitejs/plugin-vue': '1.0.0',
},
}

{
const upgraded = await ncu({ packageData: pkg, filter: ['vite'] })
upgraded!.should.have.property('vite')
upgraded!.should.not.have.property('@vitejs/plugin-react')
upgraded!.should.not.have.property('@vitejs/plugin-vue')
}

{
const upgraded = await ncu({ packageData: pkg, filter: ['@vite*'] })
upgraded!.should.not.have.property('vite')
upgraded!.should.have.property('@vitejs/plugin-react')
upgraded!.should.have.property('@vitejs/plugin-vue')
}

{
const upgraded = await ncu({ packageData: pkg, filter: ['*vite*'] })
upgraded!.should.have.property('vite')
upgraded!.should.have.property('@vitejs/plugin-react')
upgraded!.should.have.property('@vitejs/plugin-vue')
}

{
const upgraded = await ncu({ packageData: pkg, filter: ['*vite*/*react*'] })
upgraded!.should.not.have.property('vite')
upgraded!.should.have.property('@vitejs/plugin-react')
upgraded!.should.not.have.property('@vitejs/plugin-vue')
}

{
const upgraded = await ncu({ packageData: pkg, filter: ['*vite*vue*'] })
upgraded!.should.not.have.property('vite')
upgraded!.should.not.have.property('@vitejs/plugin-react')
upgraded!.should.have.property('@vitejs/plugin-vue')
}
})

it('filter with negated wildcard', async () => {
const upgraded = (await ncu({
packageData: {
Expand Down

0 comments on commit 7bab8bb

Please sign in to comment.