|
| 1 | +#include <vector> |
1 | 2 | #include "nan.h" |
2 | 3 | #include "spellchecker.h" |
3 | 4 |
|
@@ -49,6 +50,29 @@ class Spellchecker : public Nan::ObjectWrap { |
49 | 50 | info.GetReturnValue().Set(Nan::New(that->impl->IsMisspelled(word))); |
50 | 51 | } |
51 | 52 |
|
| 53 | + static NAN_METHOD(CheckSpelling) { |
| 54 | + Nan::HandleScope scope; |
| 55 | + if (info.Length() < 1) { |
| 56 | + return Nan::ThrowError("Bad argument"); |
| 57 | + } |
| 58 | + |
| 59 | + Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder()); |
| 60 | + String::Utf8Value text(info[0]); |
| 61 | + |
| 62 | + std::vector<MisspelledRange> misspelled_ranges = that->impl->CheckSpelling(*text, text.length()); |
| 63 | + |
| 64 | + Local<Array> result = Nan::New<Array>(); |
| 65 | + std::vector<MisspelledRange>::const_iterator iter = misspelled_ranges.begin(); |
| 66 | + for (; iter != misspelled_ranges.end(); ++iter) { |
| 67 | + Local<Object> misspelled_range = Nan::New<Object>(); |
| 68 | + misspelled_range->Set(Nan::New("start").ToLocalChecked(), Nan::New<Number>(iter->start)); |
| 69 | + misspelled_range->Set(Nan::New("end").ToLocalChecked(), Nan::New<Number>(iter->end)); |
| 70 | + result->Set(iter - misspelled_ranges.begin(), misspelled_range); |
| 71 | + } |
| 72 | + |
| 73 | + info.GetReturnValue().Set(result); |
| 74 | + } |
| 75 | + |
52 | 76 | static NAN_METHOD(Add) { |
53 | 77 | Nan::HandleScope scope; |
54 | 78 | if (info.Length() < 1) { |
@@ -127,6 +151,7 @@ class Spellchecker : public Nan::ObjectWrap { |
127 | 151 | Nan::SetMethod(tpl->InstanceTemplate(), "getAvailableDictionaries", Spellchecker::GetAvailableDictionaries); |
128 | 152 | Nan::SetMethod(tpl->InstanceTemplate(), "getCorrectionsForMisspelling", Spellchecker::GetCorrectionsForMisspelling); |
129 | 153 | Nan::SetMethod(tpl->InstanceTemplate(), "isMisspelled", Spellchecker::IsMisspelled); |
| 154 | + Nan::SetMethod(tpl->InstanceTemplate(), "checkSpelling", Spellchecker::CheckSpelling); |
130 | 155 | Nan::SetMethod(tpl->InstanceTemplate(), "add", Spellchecker::Add); |
131 | 156 |
|
132 | 157 | exports->Set(Nan::New("Spellchecker").ToLocalChecked(), tpl->GetFunction()); |
|
0 commit comments