Skip to content

Commit f2f67cb

Browse files
committed
Fix case-insensitivity of internal cache
The internal cache really should be caching the generated output of the reduce pattern, not the generated RegExp object, which may not have the right behavior for "ignorecase"
1 parent b44d72a commit f2f67cb

2 files changed

Lines changed: 32 additions & 15 deletions

File tree

index.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -288,22 +288,17 @@ const REPLACERS = [
288288
const regexCache = Object.create(null)
289289

290290
// @param {pattern}
291-
const makeRegex = (pattern, negative, ignorecase) => {
292-
const r = regexCache[pattern]
293-
if (r) {
294-
return r
295-
}
296-
297-
// const replacers = negative
298-
// ? NEGATIVE_REPLACERS
299-
// : POSITIVE_REPLACERS
291+
const makeRegex = (pattern, ignorecase) => {
292+
let source = regexCache[pattern]
300293

301-
const source = REPLACERS.reduce(
302-
(prev, current) => prev.replace(current[0], current[1].bind(pattern)),
303-
pattern
304-
)
294+
if (!source) {
295+
regexCache[pattern] = source = REPLACERS.reduce(
296+
(prev, current) => prev.replace(current[0], current[1].bind(pattern)),
297+
pattern
298+
)
299+
}
305300

306-
return regexCache[pattern] = ignorecase
301+
return ignorecase
307302
? new RegExp(source, 'i')
308303
: new RegExp(source)
309304
}
@@ -352,7 +347,7 @@ const createRule = (pattern, ignorecase) => {
352347
// > begin with a hash.
353348
.replace(REGEX_REPLACE_LEADING_EXCAPED_HASH, '#')
354349

355-
const regex = makeRegex(pattern, negative, ignorecase)
350+
const regex = makeRegex(pattern, ignorecase)
356351

357352
return new IgnoreRule(
358353
origin,

test/others.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ _test('options.ignorecase', t => {
8585
t.end()
8686
})
8787

88+
_test('special case: internal cache respects ignorecase', t => {
89+
const rule = '*.[jJ][pP]g'
90+
91+
const ig = ignore({
92+
ignorecase: false
93+
})
94+
95+
ig.add(rule)
96+
97+
t.is(ig.ignores('a.JPG'), false)
98+
99+
const ig2 = ignore({
100+
ignorecase: true
101+
})
102+
103+
ig2.add(rule)
104+
105+
t.is(ig2.ignores('a.JPG'), true)
106+
107+
t.end()
108+
})
109+
88110
_test('special case: invalid paths, throw', t => {
89111
const ig = ignore()
90112

0 commit comments

Comments
 (0)