Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add indexOf method for useFilter #7174

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions packages/@react-aria/i18n/src/useFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface Filter {
startsWith(string: string, substring: string): boolean,
/** Returns whether a string ends with a given substring. */
endsWith(string: string, substring: string): boolean,
/** Returns the index of the first occurrence of a substring in a string, or -1 if not found. */
indexOf(string: string, substring: string): number,
/** Returns whether a string contains a given substring. */
contains(string: string, substring: string): boolean
}
Expand Down Expand Up @@ -55,29 +57,34 @@ export function useFilter(options?: Intl.CollatorOptions): Filter {
return collator.compare(string.slice(-substring.length), substring) === 0;
}, [collator]);

let contains = useCallback((string, substring) => {
let indexOf = useCallback((string: string, substring: string) => {
if (substring.length === 0) {
return true;
return 0;
}

string = string.normalize('NFC');
substring = substring.normalize('NFC');

let scan = 0;
let sliceLen = substring.length;
for (; scan + sliceLen <= string.length; scan++) {
let strLen = string.length;
for (let scan = 0; scan + sliceLen <= strLen; scan++) {
let slice = string.slice(scan, scan + sliceLen);
if (collator.compare(substring, slice) === 0) {
return true;
return scan;
}
}

return false;
return -1;
}, [collator]);

let contains = useCallback((string, substring) => {
return indexOf(string, substring) !== -1;
}, [indexOf]);

return useMemo(() => ({
startsWith,
endsWith,
indexOf,
contains
}), [startsWith, endsWith, contains]);
}), [startsWith, endsWith, indexOf, contains]);
}