|
| 1 | +#include <vector> |
1 | 2 | #include "nan.h" |
2 | 3 | #include "spellchecker.h" |
3 | 4 |
|
@@ -49,6 +50,42 @@ 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 | + 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 | + |
| 71 | + std::vector<uint16_t> text(string->Length() + 1); |
| 72 | + string->Write(reinterpret_cast<uint16_t *>(text.data())); |
| 73 | + |
| 74 | + Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder()); |
| 75 | + std::vector<MisspelledRange> misspelled_ranges = that->impl->CheckSpelling(text.data(), text.size()); |
| 76 | + |
| 77 | + std::vector<MisspelledRange>::const_iterator iter = misspelled_ranges.begin(); |
| 78 | + for (; iter != misspelled_ranges.end(); ++iter) { |
| 79 | + size_t index = iter - misspelled_ranges.begin(); |
| 80 | + uint32_t start = iter->start, end = iter->end; |
| 81 | + |
| 82 | + Local<Object> misspelled_range = Nan::New<Object>(); |
| 83 | + misspelled_range->Set(Nan::New("start").ToLocalChecked(), Nan::New<Integer>(start)); |
| 84 | + misspelled_range->Set(Nan::New("end").ToLocalChecked(), Nan::New<Integer>(end)); |
| 85 | + result->Set(index, misspelled_range); |
| 86 | + } |
| 87 | + } |
| 88 | + |
52 | 89 | static NAN_METHOD(Add) { |
53 | 90 | Nan::HandleScope scope; |
54 | 91 | if (info.Length() < 1) { |
@@ -127,6 +164,7 @@ class Spellchecker : public Nan::ObjectWrap { |
127 | 164 | Nan::SetMethod(tpl->InstanceTemplate(), "getAvailableDictionaries", Spellchecker::GetAvailableDictionaries); |
128 | 165 | Nan::SetMethod(tpl->InstanceTemplate(), "getCorrectionsForMisspelling", Spellchecker::GetCorrectionsForMisspelling); |
129 | 166 | Nan::SetMethod(tpl->InstanceTemplate(), "isMisspelled", Spellchecker::IsMisspelled); |
| 167 | + Nan::SetMethod(tpl->InstanceTemplate(), "checkSpelling", Spellchecker::CheckSpelling); |
130 | 168 | Nan::SetMethod(tpl->InstanceTemplate(), "add", Spellchecker::Add); |
131 | 169 |
|
132 | 170 | exports->Set(Nan::New("Spellchecker").ToLocalChecked(), tpl->GetFunction()); |
|
0 commit comments