Json객체에서 한글 검색을 수행. 한글 초성 검색을 지원. Korean search within JSON objects. Supports Korean initial consonant search.
npm install hangul-searchUsage 2 섹션을 참고하세요.
shell
npm i hangul-search
`
코드를 작성합니다.
`js
// 라이브러리 import
import hansearch from "hangul-search";
// Json 객체 정의
var json = [
{
title: "Javascript find, findIndex, filter",
users: ["김정환", "홍길동", "강감찬", "아이유"],
description: "구글 네이버 다음",
},
{
title: "Javascript 직렬화(serialization)",
users: ["골리앗", "이순신", "김길동"],
description: "직렬화를 알아본다.",
},
{
title: "Javascript 템플릿 리터럴(Template literal)",
users: ["James", "Tom", "David"],
description: "고글 가글 고고씽",
},
];
// 검색 수행
const result = hansearch(json, "ㅈㅀ");
console.log(result);
/* 출력 결과
{
"items" :
[
{
"title": "Javascript 직렬화(serialization)",
"users": ["골리앗", "이순신", "김길동"],
"description": "직렬화를 알아본다."
}
]
}
*/
`
Usage 2
CDN을 이용하여 실행가능합니다.
`html
`
Options
3번째 인자에 검색 대상 키 컬럼을 지정할 수 있습니다. 값을 지정하지 않으면 모든 키를 대상으로 검색을 수행합니다.
`js
var result = hansearch(json, "ㅈ렬화"); // 모든 키를 대상으로 검색을 수행합니다.
var result = hansearch(json, "ㅈ렬화", ["title"]); // title 키를 대상으로 검색을 수행합니다.
var result = hansearch(json, "ㅈ렬화", ["title", "users"]); // title과 users 키를 대상으로 검색을 수행합니다.
`
.mark() 메소드 체이닝을 지원합니다. 일치한 검색어를 태그로 감싸주며, 원하는 태그를 인자로 넘겨줄 수 있습니다.
`js
var result = hansearch(json, "ㅈ렬화").mark(); // 검색어와 일치한 단어를 태그로 감싼 결과를 리턴합니다.
var result = hansearch(json, "ㅈ렬화").mark("tags"); // 검색어와 일치한 단어를 태그로 감싼 결과를 리턴합니다.
`
hansearch 의 3번째 인자에는 json 형태로 옵션값을 전달할 수도 있습니다.
`js
var options = {
mode: "exact",
keys: ["key1", "key2"],
};
var result = hansearch(json, "ㅈ렬화", options);
`
mode string "exact" : 정확히 일치하는 검색을 수행합니다. default 는 자음 분해 검색을 수행합니다.
| exact 모드인 경우 "파라미터"를 검색하기 위해 "ㅍㄻㅌ" 와 같이 입력한 경우 검색이 되지 않습니다. default 모드에서는 ㄻ 을 ㄹㅁ 으로 분해하여 검색을 수행합니다. 특별히 값을 지정하지 않으면 분해 검색을 수행합니다.
keys string[] : 검색 대상 키값을 지정합니다. 값을 지정하지 않으면 모든 키를 대상으로 검색을 수행합니다.
| 다음 두 코드는 동일한 동작을 수행합니다.
`js
var keys = ["title", "users"];
var options = { keys: ["title", "users"] };
var result = hansearch(json, "ㅈ렬화", keys);
var result = hansearch(json, "ㅈ렬화", options);
``