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

@ragaeeb/locale-index-of

v1.0.0

Published

A locale-aware Intl-powered version of indexOf.

Downloads

17

Readme

Enhanced Locale IndexOf

This project is an enhanced version of the original localeIndexOf library, which provides a locale-aware, Intl-powered version of indexOf with additional functionalities and improvements. It extends the capabilities of Intl.Collator.compare() to search, allowing for more sophisticated string comparison and searching based on locale settings.

Acknowledgments

This project is built upon the work of Tom Adler, the creator of the original localeIndexOf library. His innovative approach to locale-aware string searching laid the groundwork for further enhancements. You can find the original project here.

Features

  • Locale-Aware Searching: Utilizes Intl.Collator for locale-specific string comparison.
  • Generator Functionality: Includes a generator function for creating slices of a string, aiding in complex search scenarios.
  • Enhanced Options: Additional options for handling numbers, punctuation, and other locale-specific nuances in string comparison.
  • Flexible API: Provides various methods for different use cases, including direct string comparison and functional programming approaches.

Installation

npm install @ragaeeb/locale-index-of

Usage

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

// Example usage
localeIndexOf('a café', 'cafe', 'de', { sensitivity: 'base' }); // = 2

Advanced Usage

Ignoring Numbers with ignoreNumbers Option

The ignoreNumbers option allows you to perform searches while ignoring numeric characters in the string. This is particularly useful in scenarios where numbers in the text should not influence the search results for textual content.

Example

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

// Example with ignoreNumbers
const result = localeIndexOf('123 Hello World 456', 'Hello', 'en', { ignoreNumbers: true });
console.log(result); // { index: 4, match: 'Hello' }

In this example, the search for 'Hello' is performed in a string that contains numbers. With the ignoreNumbers option set to true, the function ignores the numbers and correctly finds 'Hello' at index 4.

Returning {match, index} Objects

For more detailed results, the function can return objects containing both the match substring and its index in the original string.

Example

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

// Example usage returning {match, index}
const detailedResult = localeIndexOf('A quick brown fox', 'quick', 'en');
console.log(detailedResult); // { index: 2, match: 'quick' }

This example demonstrates how you can retrieve both the matched substring and its position in the source string, providing more context for the search result.

API

getCollator(Intl, localesOrCollator, options)

  • Parameters:
    • Intl: The global Intl object.
    • localesOrCollator: A locale string, an array of locale strings, or an existing Intl.Collator instance.
    • options: Options for Intl.Collator.
  • Returns: An Intl.Collator instance.

makeSlicesGenerator(Intl, collator, string, substring)

  • Parameters:
    • Intl: The global Intl object.
    • collator: An Intl.Collator instance for locale-specific comparison.
    • string: The string to be searched.
    • substring: The substring to search for.
  • Yields: Objects containing the index of the slice and the slice itself.

indexOf(Intl, collator, string, substring)

  • Parameters:
    • Intl: The global Intl object.
    • collator: An Intl.Collator instance for locale-specific comparison.
    • string: The string to be searched.
    • substring: The substring to search for.
  • Returns: An object containing the index of the first occurrence of the substring and the matched substring.

Additional Functions

  • functional(Intl, string, substring, localesOrCollator, options): A high-level functional interface for substring searching.
  • Default export function: Creates a locale-specific substring search function.

Contributing

Contributions, issues, and feature requests are welcome. Feel free to check issues page if you want to contribute.

License

This project is MIT licensed.

See Also