discourse/app/assets/javascripts/select-kit/addon/lib/input-utils.js
Kelv 18bbfa87ed
DEV: replace select-kit utils mixin methods with input-utils lib (#32594)
This PR replaces the set of methods related to validation and
normalization of input values in the select-kit utils mixin with simple
exported functions from a input-utils lib file.

We maintain the `_normalize` method on the select-kit component because
there are external repos dependent on that part of the interface.

We'll replace the remainder of the mixin with another utils lib file in
a separate PR.
2025-05-07 09:25:52 +08:00

24 lines
743 B
JavaScript

export function isValidInput(eventKey) {
// relying on passing the event to the input is risky as it could not work
// dispatching the event won't work as the event won't be trusted
// safest solution is to filter event and prefill filter with it
const nonInputKeysRegex =
/F\d+|Arrow.+|Meta|Alt|Control|Shift|Delete|Enter|Escape|Tab|Space|Insert|Backspace/;
return !nonInputKeysRegex.test(eventKey);
}
export function isNumeric(input) {
return !isNaN(parseFloat(input)) && isFinite(input);
}
export function normalize(input) {
if (input) {
input = input.toLowerCase();
if (typeof input.normalize === "function") {
input = input.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
}
}
return input;
}