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

Add case sensitive search #412 #436

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
1 change: 1 addition & 0 deletions src/autoComplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/controllers/dataController.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const findMatches = (query, ctx) => {
mode: searchEngine,
diacritics: ctx.diacritics,
highlight: ctx.resultItem.highlight,
caseSensitive: ctx.caseSensitive
});

if (!match) return;
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/searchController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down