Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 0824cd3

Browse files
committed
Handle invalid inputs to bulk spell-checking function
1 parent 0d2fe14 commit 0824cd3

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

spec/spellchecker-spec.coffee

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ describe "SpellChecker", ->
3535
{start: 20, end: 25},
3636
]
3737

38+
it "handles invalid inputs", ->
39+
expect(SpellChecker.checkSpelling("")).toEqual []
40+
expect(-> SpellChecker.checkSpelling()).toThrow("Bad argument")
41+
expect(-> SpellChecker.checkSpelling(null)).toThrow("Bad argument")
42+
expect(-> SpellChecker.checkSpelling({})).toThrow("Bad argument")
43+
3844
describe ".getCorrectionsForMisspelling(word)", ->
3945
it "returns an array of possible corrections", ->
4046
corrections = SpellChecker.getCorrectionsForMisspelling('worrd')

src/main.cc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,24 @@ class Spellchecker : public Nan::ObjectWrap {
5656
return Nan::ThrowError("Bad argument");
5757
}
5858

59-
Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder());
6059
Handle<String> string = Handle<String>::Cast(info[0]);
60+
if (!string->IsString()) {
61+
return Nan::ThrowError("Bad argument");
62+
}
63+
64+
Local<Array> result = Nan::New<Array>();
65+
info.GetReturnValue().Set(result);
66+
67+
if (string->Length() == 0) {
68+
return;
69+
}
70+
6171
std::vector<uint16_t> text(string->Length());
6272
string->Write(reinterpret_cast<uint16_t *>(text.data()));
6373

74+
Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder());
6475
std::vector<MisspelledRange> misspelled_ranges = that->impl->CheckSpelling(text.data(), text.size());
6576

66-
Local<Array> result = Nan::New<Array>();
6777
std::vector<MisspelledRange>::const_iterator iter = misspelled_ranges.begin();
6878
for (; iter != misspelled_ranges.end(); ++iter) {
6979
size_t index = iter - misspelled_ranges.begin();
@@ -74,8 +84,6 @@ class Spellchecker : public Nan::ObjectWrap {
7484
misspelled_range->Set(Nan::New("end").ToLocalChecked(), Nan::New<Integer>(end));
7585
result->Set(index, misspelled_range);
7686
}
77-
78-
info.GetReturnValue().Set(result);
7987
}
8088

8189
static NAN_METHOD(Add) {

0 commit comments

Comments
 (0)