Giter Club home page Giter Club logo

korean-index-of's Introduction

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

korean-index-of's People

Contributors

ialy1595 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

piohan83

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.