From 20f7713dd2d1d6f22996fc6a3fd8513469644579 Mon Sep 17 00:00:00 2001 From: "alan.rudzinski" Date: Wed, 16 Oct 2024 16:03:09 +0200 Subject: [PATCH] Add case sensitive search #412 --- docs/configuration.md | 2 ++ src/autoComplete.js | 1 + src/controllers/dataController.js | 1 + src/controllers/searchController.js | 6 +++--- src/helpers/io.js | 5 +++-- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 34b8d00..2abeb7c 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -486,12 +486,14 @@ Arguments: - `"loose"` search mode - diacritics: `Boolean` - highlight: `Boolean` | `String` of class values + - caseSensitive: `Boolean` Defaults: - options: `Object` - mode: `"strict"` - diacritics: `false` - highlight: `false` + - caseSensitive: `false` ##### Example: diff --git a/src/autoComplete.js b/src/autoComplete.js index e9d7c0a..4ce639c 100644 --- a/src/autoComplete.js +++ b/src/autoComplete.js @@ -26,6 +26,7 @@ import init from "./services/init"; * @param {Boolean} [config.wrapper=true] - Wraps the input element in a div for a11y purposes, adding some ARIA attributes. * @param {(String|Function)} [config.searchEngine=strict] - "strict" checks if the given query is contained within the data, "loose" returns every result where every character in the query is present in the data in any order and location. Signature: (query: string, record: any), given the manipulated query input and each data.src array entry or for each entry[config.data.keys]. * @param {Boolean} [config.diacritics=false] - Enable to normalize query and data values using String.normalize and by removing u0300 through u036f. See {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize}. + * @param {Boolean} [config.caseSensitive=false] - Enable to make search result case sensitive. * @param {(Object|Boolean)} [config.resultsList] - false to disable result list rendering. * @param {String} [config.resultsList.tag=ul] - HTML tag to use for rendering the result container. * @param {String} [config.resultsList.id=autoComplete_list_index] - ID given to the result container. diff --git a/src/controllers/dataController.js b/src/controllers/dataController.js index 7b5357d..6596e75 100644 --- a/src/controllers/dataController.js +++ b/src/controllers/dataController.js @@ -47,6 +47,7 @@ const findMatches = (query, ctx) => { mode: searchEngine, diacritics: ctx.diacritics, highlight: ctx.resultItem.highlight, + caseSensitive: ctx.caseSensitive }); if (!match) return; diff --git a/src/controllers/searchController.js b/src/controllers/searchController.js index e62daa1..e00f4ed 100644 --- a/src/controllers/searchController.js +++ b/src/controllers/searchController.js @@ -10,11 +10,11 @@ import { format, mark } from "../helpers/io"; * @returns {String} - Matching data record */ export default (query, record, options) => { - const { mode, diacritics, highlight } = options || {}; + const { mode, diacritics, highlight, caseSensitive } = options || {}; - const nRecord = format(record, diacritics); + const nRecord = format(record, diacritics, caseSensitive); record = String(record); - query = format(query, diacritics); + query = format(query, diacritics, caseSensitive); if (mode === "loose") { // Query string with no spaces diff --git a/src/helpers/io.js b/src/helpers/io.js index 98c99e9..46dc035 100644 --- a/src/helpers/io.js +++ b/src/helpers/io.js @@ -57,11 +57,12 @@ const getQuery = (field) => * * @param {String} value - user's raw search query value * @param {Object} diacritics - formatting on/off + * @param {Object} caseSensitive - formatting to lowerCase * * @returns {String} - Raw "input" value as a string */ -const format = (value, diacritics) => { - value = String(value).toLowerCase(); +const format = (value, diacritics, caseSensitive = false) => { + value = caseSensitive ? String(value) : String(value).toLowerCase(); return diacritics ? value