npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

locale-index-of

v3.1.0

Published

A prollyfill for String.prototype.localeIndexOf - a locale-aware Intl-powered version of indexOf.

Downloads

1,445

Readme

localeIndexOf

Find „cafe“ in „Fondation Café“

Build Status

A prollyfill for String.prototype.localeIndexOf - a locale-aware Intl-powered version of indexOf with zero dependencies.

Many texts out there contain accents and other diacritical characters, and when they are not strictly necessary, like in „café“, it is hard to match them against user input.

Here’s a relatable example. A website has a search field that provides suggestions, and the matched substring in each suggestion is highlighted. User has typed in „cafe“. You smartly find „café“ and display it as a suggestion. However you cannot really highlight it as a match, because the string is slightly different.

Intl.Collator with sensitivity: base to the rescue! Except that it only has a compare() method, not indexOf(). So you can’t use it to find substrings. Well, now you can. localeIndexOf can do some grinding for you. It is modeled after String.prototype.localeCompare and can be used in a similar fashion. It extends the functionality of Intl.Collator.compare() to search, so you can even set ignorePunctuation: true.

ES modules only

Version 2.0.0 introduces a breaking change: the support for CommonJS is removed, minimal Node.js version becomes 14.

Installation

npm install locale-index-of

Usage

import localeIndexOfMaker from 'locale-index-of';
const localeIndexOf = localeIndexOfMaker(Intl);
localeIndexOf('a café', 'cafe', 'de', { sensitivity: 'base' }); // = 2

or alternatively

import { prollyfill } from 'locale-index-of';
prollyfill();
'a café'.localeIndexOf('cafe', 'de', { sensitivity: 'base' }); // = 2

API

default export localeIndexOfMaker

The default export of the module is a function. Give it the Intl object and get the localeIndexOf function in return:

import localeIndexOfMaker from 'locale-index-of';
const localeIndexOf = localeIndexOfMaker(Intl);

localeIndexOf(string, substring, locales, options)

It can take four parameters: where to search, what to look for, and the same locales and options as Intl.Collator or String.prototype.localeCompare.

Return value: the offset of substring in the string or -1.

localeIndexOf(string, substring, collator)

It can take three parameters: where to search, what to look for, and an instance of Intl.Collator.

Return value: the offset of substring in the string or -1.

prototypeLocaleIndexOf(Intl)

The module also exports method prototypeLocaleIndexOf. Give it the Intl object and get back the localeIndexOf function suitable for putting on String.prototype:

import { prototypeLocaleIndexOf } from 'locale-index-of';
String.prototype.localeIndexOf = prototypeLocaleIndexOf(Intl);

prollyfill()

You can achieve the same effect by calling exported method prollyfill(). As a convenience, it will make String.prototype.localeIndexOf fall back to String.prototype.indexOf when Intl is not available.

String.prototype.localeIndexOf(substring, locales, options)

It can take three parameters: what to look for and the same locales and options as Intl.Collator or String.prototype.localeCompare.

Return value: the offset of substring in this or -1 when not found.

localeIndexOf(string, substring, collator)

It can take two parameters: what to look for and an instance of Intl.Collator.

Return value: the offset of substring in this or -1 when not found.

Note on ignorePunctuation: true

The default behavior of Intl.Collator is to consider the whitespace punctuation.

Since the length of the matched fragment can be different from the length of what you have looked for, this length is exposed on indexOf.lastLength.

See also