Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: '孩',
Expand Down
7 changes: 6 additions & 1 deletion lib/dictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '^(';

Expand All @@ -131,6 +134,8 @@ function dictionarySearch(character, type) {
regexstring = regexstring + character.substring(i, i + 1) + ')+$';
}
}
} else if (type == 'exact') {
regexstring = `^${character}$`;
} else {
regexstring = '[' + character + ']';
}
Expand Down
8 changes: 8 additions & 0 deletions test/dictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
[
Expand Down