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..a7de5f3 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. +// null - 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 = [ [