static void NearestWithin()

in src/maluuba/speech/nodejs/fuzzymatcher.hpp [372:413]


    static void NearestWithin(const v8::FunctionCallbackInfo<v8::Value>& args)
    {
      auto isolate = args.GetIsolate();
      v8::Local<v8::Context> context = isolate->GetCurrentContext();

      if (args.Length() < 2) {
        isolate->ThrowException(v8::Exception::TypeError(
            v8::String::NewFromUtf8(isolate, "Expected 2 arguments.")));
        return;
      }

      if (!args[1]->IsNumber()) {
        isolate->ThrowException(v8::Exception::TypeError(
            v8::String::NewFromUtf8(isolate, "Expected argument to be a number.")));
        return;
      }

      auto obj = ObjectWrap::Unwrap<FuzzyMatcher>(args.Holder());
      double threshold_scale;
      auto target = obj->to_target(isolate, args[0], threshold_scale);
      auto threshold = args[1]->NumberValue(context).ToChecked() * threshold_scale;

      try {
        auto match = obj->matcher().find_nearest_within(target, threshold);

        if (match) {
          speech::FuzzyMatcher<NodeJsTarget>::Match m(match->element().target, match->distance() / threshold_scale);
          auto wrap_match = new Match(std::move(m));
          auto context = isolate->GetCurrentContext();
          const auto argc = 1;
          v8::Local<v8::Value> argv[argc] = { v8::External::New(isolate, wrap_match) };
          auto instance = Match::constructor(isolate)->NewInstance(context, argc, argv).ToLocalChecked();
          args.GetReturnValue().Set(instance);
        } else {
          args.GetReturnValue().Set(v8::Undefined(isolate));
        }
      } catch(const std::exception& e) {
        isolate->ThrowException(v8::Exception::Error(
            v8::String::NewFromUtf8(isolate, e.what())));
        return;
      }
    }