Skip to content

altipla-consulting/normalize-search

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

normalize-search

Normalize strings to search them client side.

Install

npm i @altipla/normalize-search

Usage

Basic usage

Import the package to have the pair of functions in scope:

import { prepareSearch, filterSearch } from '@altipla/normalize-search'

To filter a list of items you have first to prepare them:

// From the server or any other data source
let items = [
  {name: 'foo', lastName: 'bar'},
  {name: 'baz', lastName: 'qux'},
]

items.forEach(item => {
  prepareSearch(item, [
    item.name,
    item.lastName,
    // ... any other thing you want to link to the item
  ])
})

Then you can generate a new function to filter the items with the user input:

let userInput = 'qu' // obtained from your user interface
let filteredItems = items.filter(filterSearch(userInput))

Custom normalization

If you want to prepare a custom search of any kind where you need the normalized text call normalizeSearch:

import { normalizeSearch } from '@altipla/normalize-search'


let result1 = normalizeSearch('your text here')
let result2 = normalizeSearch([
  item.name,
  item.lastName,
])