Skip to content

Commit

Permalink
Merge pull request #74 from forking-repos/tap-cache-fixes
Browse files Browse the repository at this point in the history
Tap + Internal Caching Fixes
  • Loading branch information
kaelzhang authored Nov 3, 2021
2 parents 7cc95d2 + f2f67cb commit f19a790
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x]
node-version: [12.x, 14.x, 16.x]

steps:
- uses: actions/checkout@v1
Expand Down
25 changes: 10 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,22 +288,17 @@ const REPLACERS = [
const regexCache = Object.create(null)

// @param {pattern}
const makeRegex = (pattern, negative, ignorecase) => {
const r = regexCache[pattern]
if (r) {
return r
}

// const replacers = negative
// ? NEGATIVE_REPLACERS
// : POSITIVE_REPLACERS
const makeRegex = (pattern, ignorecase) => {
let source = regexCache[pattern]

const source = REPLACERS.reduce(
(prev, current) => prev.replace(current[0], current[1].bind(pattern)),
pattern
)
if (!source) {
regexCache[pattern] = source = REPLACERS.reduce(
(prev, current) => prev.replace(current[0], current[1].bind(pattern)),
pattern
)
}

return regexCache[pattern] = ignorecase
return ignorecase
? new RegExp(source, 'i')
: new RegExp(source)
}
Expand Down Expand Up @@ -352,7 +347,7 @@ const createRule = (pattern, ignorecase) => {
// > begin with a hash.
.replace(REGEX_REPLACE_LEADING_EXCAPED_HASH, '#')

const regex = makeRegex(pattern, negative, ignorecase)
const regex = makeRegex(pattern, ignorecase)

return new IgnoreRule(
origin,
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
"test:lint": "eslint .",
"test:tsc": "tsc ./test/ts/simple.ts --lib ES6",
"test:ts": "node ./test/ts/simple.js",
"test:git": "tap test/git-check-ignore.js",
"test:ignore": "tap test/ignore.js",
"test:others": "tap test/others.js",
"test:cases": "tap test/*.js --coverage",
"tap": "tap --reporter classic",
"test:git": "npm run tap test/git-check-ignore.js",
"test:ignore": "npm run tap test/ignore.js",
"test:others": "npm run tap test/others.js",
"test:cases": "npm run tap test/*.js -- --coverage",
"test:only": "npm run test:lint && npm run test:tsc && npm run test:ts && npm run test:cases",
"test": "npm run test:only",
"test:win32": "IGNORE_TEST_WIN32=1 npm run test",
Expand Down
22 changes: 22 additions & 0 deletions test/others.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ _test('options.ignorecase', t => {
t.end()
})

_test('special case: internal cache respects ignorecase', t => {
const rule = '*.[jJ][pP]g'

const ig = ignore({
ignorecase: false
})

ig.add(rule)

t.is(ig.ignores('a.JPG'), false)

const ig2 = ignore({
ignorecase: true
})

ig2.add(rule)

t.is(ig2.ignores('a.JPG'), true)

t.end()
})

_test('special case: invalid paths, throw', t => {
const ig = ignore()

Expand Down

0 comments on commit f19a790

Please sign in to comment.