From 29c53ec8b9d256c509a6469dd0ad6860bd9f8257 Mon Sep 17 00:00:00 2001 From: Benjamin Moses Lieb Date: Sun, 1 Mar 2020 01:24:44 -0800 Subject: [PATCH 1/2] add exact lookup type for dictionarySearch --- README.md | 8 ++++++++ lib/dictionary.js | 7 ++++++- test/dictionary.js | 8 ++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b9a1853..07957ea 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,7 @@ Searches the dictionary based on input. Search type changes what data it returns Search type paramaters: * 'only' - this parameter returns only entries with the characters specfied. This is a means to find all compounds words with the characters specified. +* 'exact' - this parameter returns entries that exactly match the characters specfied. * null - returns all occurences of the character. ```javascript @@ -183,6 +184,13 @@ console.log(hanzi.dictionarySearch('雪')); [....] //Truncated for display purposes +console.log(hanzi.dictionarySearch('小孩', 'exact')); + +[ { traditional: '小孩', + simplified: '小孩', + pinyin: "xiao3 hai2", + definition: 'child/CL:個|个[ge4]' } ] + console.log(hanzi.dictionarySearch('心的小孩真', 'only')); [ [ { traditional: '孩', diff --git a/lib/dictionary.js b/lib/dictionary.js index 747ce83..9c5204a 100644 --- a/lib/dictionary.js +++ b/lib/dictionary.js @@ -118,8 +118,11 @@ function definitionLookup(word, scripttype) { } } +// types: +// only - Just the characters and no alternatives. +// exact - Just the characters and no alternatives. +// else - finds all cases of that character function dictionarySearch(character, type) { - /*--- Types: Only = Just the characters and no alternatives. If not then finds all cases of that character ---*/ var search = []; var regexstring = '^('; @@ -131,6 +134,8 @@ function dictionarySearch(character, type) { regexstring = regexstring + character.substring(i, i + 1) + ')+$'; } } + } else if (type == 'exact') { + regexstring = `^${character}$`; } else { regexstring = '[' + character + ']'; } diff --git a/test/dictionary.js b/test/dictionary.js index df4ef49..1088f45 100644 --- a/test/dictionary.js +++ b/test/dictionary.js @@ -699,6 +699,14 @@ describe('hanzidictionary', function() { assert.deepEqual(hanzi.dictionarySearch('爸', 'only'), expected); }); + it('should do a dictionary search and return exact matches by passing the exact type', function() { + const results = hanzi.dictionarySearch('小孩', 'exact')[0] + + assert.deepEqual(results.length, 1); + assert.deepEqual(results[0].traditional, '小孩'); + }); + + it('should now do a dictionary search with the same character and ignore the only condition', function() { var expected = [ [ From 234d8b10191b6fb4f396b093eca131cbb6dd1465 Mon Sep 17 00:00:00 2001 From: Benjamin Moses Lieb Date: Sun, 1 Mar 2020 01:31:11 -0800 Subject: [PATCH 2/2] consistent wording with readme --- lib/dictionary.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dictionary.js b/lib/dictionary.js index 9c5204a..a7de5f3 100644 --- a/lib/dictionary.js +++ b/lib/dictionary.js @@ -121,7 +121,7 @@ function definitionLookup(word, scripttype) { // types: // only - Just the characters and no alternatives. // exact - Just the characters and no alternatives. -// else - finds all cases of that character +// null - finds all cases of that character function dictionarySearch(character, type) { var search = []; var regexstring = '^(';