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

korean-index-of

v1.1.1

Published

Improved indexOf function for incremental search in Korean

Downloads

5

Readme

korean-index-of

This package is helpful when you implement incremental search in Korean. Korean combines onset(초성), nucleus(중성), coda(종성) into one letter, it can be a problem in incremental search.

For example, when you type "code", the process "c", "co", "cod" and " code" are all substring of "code" so we can take it with string match function like indexOf or includes.

But when you type "코드", the process is "ㅋ", "코", "콛", and "코드", only "코" and "코드" is substring of "코드". For this reason, incremental search with normal string match function provides a poor UX because it repeats whether the results are included or not.

The improved indexOf function, koreanIndexOf can solve this problem because they can determine that "ㅋ", "코", "콛", and "코드" is substring of "코드".

installation

Install it from npm.

npm install --save korean-index-of

Usage

Use it like this, koreanIndexOf(query, data). It returns matching index or -1 as original indexOf function.

import { koreanIndexOf } from 'korean-index-of';

koreanIndexOf("콛", "내 코드");
/// 2

Of course, it works well even if there are non-Korean characters.

import { koreanIndexOf } from 'korean-index-of';

koreanIndexOf("o 12 !@ 일ㅇ", "OneTwo 12 !@ 일이");
/// 5

If you want to get all the matching results at once, use koreanAllIndexOf. It returns the result indices in an array. If there is no match result, it returns empty array, [].

import { koreanAllIndexOf } from 'korean-index-of';

koreanAllIndexOf("갭", "개와 개불과 개발자 사이의 갭");
/// [3, 7, 15]

For some tasks such as highlighting results, matching length will be required. Generally, the length of the match is the same as the length of the query, but it can be different in the case of Korean because we don't know the last letter is onset(초성) or coda(종성). The matching range can be obtained by koreanIndexRangeOf and koreanAllIndexRangeOf function. The result is given in the form [matchStartIndex, matchEndIndex]. If there is no match result in koreanIndexRangeOf, it returns [-1, -1].

import { koreanIndexRangeOf, koreanAllIndexRangeOf } from 'korean-index-of';

koreanIndexRangeOf("콛", "내 코드");
/// [2, 3]

koreanAllIndexRangeOf("갭", "개와 개불과 개발자 사이의 갭");
/// [[3, 4], [7, 8], [15, 15]]

One of the good search UXs only in Korean is onset search(초성 검색). koreanOnsetIndexOf and koreanAllOnsetIndexOf function can help implement this.

import { koreanOnsetIndexOf, koreanAllOnsetIndexOf } from 'korean-index-of';

koreanOnsetIndexOf("ㄱㅂ", "개와 개불과 개발자 사이의 갭");
/// 3

koreanAllOnsetIndexOf("ㄱㅂ", "개와 개불과 개발자 사이의 갭");
/// [3, 7]

Unfortunately, they works well only when the query consists of onset and non-Korean characters.

import { koreanAllOnsetIndexOf } from 'korean-index-of';

koreanAllOnsetIndexOf("개ㅂ", "개와 개불과 개발자 사이의 갭");
/// []

If you want to use those functions in the prototype function of String as the original indexOf function, try using it like this.

import { koreanIndexOf } from 'korean-index-of';

String.prototype.kIndexOf = function(query) {
    return koreanIndexOf(query, this);
}

"내 코드".kIndexOf("콛");
/// 2

Performance

The Z algorithm is used, all functions in this package take a linear time.

License

MIT