UNPKG

@react-aria/i18n

Version:
73 lines (66 loc) 2.8 kB
var $27a5ce66022270ad$exports = require("./useCollator.main.js"); var $c0oFg$react = require("react"); function $parcel$export(e, n, v, s) { Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true}); } $parcel$export(module.exports, "useFilter", () => $832d079b867c7223$export$3274cf84b703fff); /* * Copyright 2020 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ function $832d079b867c7223$export$3274cf84b703fff(options) { let collator = (0, $27a5ce66022270ad$exports.useCollator)({ usage: 'search', ...options }); // TODO(later): these methods don't currently support the ignorePunctuation option. let startsWith = (0, $c0oFg$react.useCallback)((string, substring)=>{ if (substring.length === 0) return true; // Normalize both strings so we can slice safely // TODO: take into account the ignorePunctuation option as well... string = string.normalize('NFC'); substring = substring.normalize('NFC'); return collator.compare(string.slice(0, substring.length), substring) === 0; }, [ collator ]); let endsWith = (0, $c0oFg$react.useCallback)((string, substring)=>{ if (substring.length === 0) return true; string = string.normalize('NFC'); substring = substring.normalize('NFC'); return collator.compare(string.slice(-substring.length), substring) === 0; }, [ collator ]); let contains = (0, $c0oFg$react.useCallback)((string, substring)=>{ if (substring.length === 0) return true; string = string.normalize('NFC'); substring = substring.normalize('NFC'); let scan = 0; let sliceLen = substring.length; for(; scan + sliceLen <= string.length; scan++){ let slice = string.slice(scan, scan + sliceLen); if (collator.compare(substring, slice) === 0) return true; } return false; }, [ collator ]); return (0, $c0oFg$react.useMemo)(()=>({ startsWith: startsWith, endsWith: endsWith, contains: contains }), [ startsWith, endsWith, contains ]); } //# sourceMappingURL=useFilter.main.js.map