1.0.2 • Published 1 year ago

string-like-query v1.0.2

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

String Like Query

A looser but configurable approach to searching a string for a matching substring 🔍

Install

With a CDN

<script
  defer
  src="https://unpkg.com/string-like-query@latest/dist/like.min.js"
></script>

### With a Package Manager

yarn add -D string-like-query

npm install -D string-like-query
import like from 'string-like-query'

Example

const bookDetail = {
  title: 'Business Secrets of the Pharoahs',
  author: 'Mark Crorigan',
}

const searchInput = document.getElementById('searchInput')

searchInput.addEventListener('input', function () {
  const searchValue = searchInput.value

  const hasSearch = searchValue.like({
    splitAt: 3,
    searchAgainst: [bookDetail.title, bookDetail.author],
  })
})

Options

OptionTypeDefaultDescription
splitAtNumber3Point in the string to split it into a chunk.
searchAgainstArray[]What values to search against.
forceLowercaseBooleanfalseIf the search should be done all in lowercase.
looseSearchBooleanfalseLimit the search to string chunks with a length of the splitAt.
eagerSearchBooleanfalseIf the search should filter before a string chunk is created.

How it Works

Here are some searches and the results based on the example.

SearchFoundReason
MarkTrueBecause "Mar" is in the bookDetail.author
kFalseBecause looseSearch isn't enabled, therefore "k" is never searched as it doesn't meet the min length of splitAt.
BusZZZTrueBecause "Bus" is in the bookDetail.title