-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Management] Ensure non alphanumeric characters do not affect index p…
…attern searching (#14973) (#14999) * - Fix issue with non alphanumeric characters affecting index pattern search - Restructure and add tests * More tests
- Loading branch information
1 parent
8938dd2
commit 3d68d3f
Showing
3 changed files
with
75 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...s/indices/create_index_pattern_wizard/step_index_pattern/lib/__tests__/append_wildcard.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import expect from 'expect.js'; | ||
|
||
import { appendWildcard } from '../append_wildcard'; | ||
|
||
describe('append_wildcard', function () { | ||
it('should add a wildcard for an alphabet input', () => { | ||
[ | ||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', | ||
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', | ||
's', 't', 'u', 'v', 'w', 'x', 'y', 'z' | ||
].forEach(char => { | ||
expect(appendWildcard({ key: char }, '')).to.be(`${char}*`); | ||
expect(appendWildcard({ key: char.toUpperCase() }, '')).to.be(`${char.toUpperCase()}*`); | ||
}); | ||
}); | ||
|
||
it('should add a wildcard for a number input', () => { | ||
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].forEach(num => { | ||
expect(appendWildcard({ key: num }, '')).to.be(`${num}*`); | ||
}); | ||
}); | ||
|
||
it('should NOT add a wildcard for a non alphanumeric input', () => { | ||
['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+'].forEach(char => { | ||
expect(appendWildcard({ key: char }, '')).to.be(undefined); | ||
}); | ||
}); | ||
|
||
it('should NOT add a wildcard for multi-length input', () => { | ||
expect(appendWildcard({ key: 'Tab' }, '')).to.be(undefined); | ||
}); | ||
|
||
it('should NOT add a wildcard if the value is longer than 1 character', () => { | ||
expect(appendWildcard({ key: 'a' }, 'b')).to.be(undefined); | ||
}); | ||
|
||
it('should NOT add a wildcard if the input is a wildcard', () => { | ||
expect(appendWildcard({ key: '*' }, '')).to.be(undefined); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
...nt/sections/indices/create_index_pattern_wizard/step_index_pattern/lib/append_wildcard.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export const appendWildcard = (keyboardEvent, value) => { | ||
const { key: keyPressed, metaKey } = keyboardEvent; | ||
|
||
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode | ||
// is not recommended so we need to rely on `key` but browser support | ||
// is still spotty (https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) | ||
// so just bail if it's not supported | ||
if (!keyPressed) { | ||
return; | ||
} | ||
|
||
// If the user is holding down ctrl/cmd, they are performing some shortcut | ||
// and do not interpret literally | ||
if (metaKey) { | ||
return; | ||
} | ||
|
||
// If it's not a letter, number or is something longer, reject it | ||
if (!/[a-z0-9]/i.test(keyPressed) || keyPressed.length !== 1) { | ||
return; | ||
} | ||
|
||
let newValue = value + keyPressed; | ||
if (newValue.length !== 1 || newValue === '*') { | ||
return; | ||
} | ||
|
||
newValue += '*'; | ||
return newValue; | ||
}; |