Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.
Draft
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
67 changes: 67 additions & 0 deletions src/fuzzy-native.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "fuzzy_native.h"
#include "MatcherBase.h"

using namespace Napi;

#define CHECK(env, cond, msg) \
if (!(cond)) \
{ \
Napi::Error::New(env, msg).ThrowAsJavaScriptException(); \
return; \
}

Matcher::Matcher(const Napi::CallbackInfo &info) : ObjectWrap(info)
{
}

void Matcher::Match(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
CHECK(env, info.Length() > 1, "Wrong Number of arguments");
CHECK(env, info[0].IsString(), "First argument should be a query string");
CHECK(env, info[1].IsObject(), "Second argument should be an options object");

MatcherOptions options;
Napi::Object options_obj = info[1].As<Napi::Object>();

options.case_sensitive = options_obj.Get("caseSensitive");
options.smart_case = options_obj.Get("smartCase");
options.num_threads = options_obj.Get("numThreads").ToNumber().Uint32Value();
options.max_results = options_obj.Get("maxResults").ToNumber().Uint32Value();
options.max_gap = options_obj.Get("maxGap").ToNumber().Uint32Value();
options.record_match_indexes = options_obj.Get("recordMatchIndexes");
options.root_path = options_obj.Get("rootPath").ToString();
}

void Matcher::AddCandidates(const Napi::CallbackInfo &info)
{
}

void Matcher::RemoveCandidates(const Napi::CallbackInfo &info)
{
}

void Matcher::SetCandidates(const Napi::CallbackInfo &info)
{
}

Napi::FunctionReference Matcher::constructor;

Napi::Function Matcher::GetClass(Napi::Env env)
{
Napi::Function func = DefineClass(env, "Matcher", {InstanceMethod("match", &Matcher::Match), InstanceMethod("addCandidates", &Matcher::AddCandidates), InstanceMethod("removeCandidates", &Matcher::RemoveCandidates), InstanceMethod("setCandidates", &Matcher::SetCandidates)});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();

return func;
}

Napi::Object Init(Napi::Env env, Napi::Object exports)
{
Napi::String matcher = Napi::String::New(env, "Matcher");
exports.Set(matcher, Matcher::GetClass(env));

return exports;
}

NODE_API_MODULE(addon, Init)
19 changes: 19 additions & 0 deletions src/fuzzy-native.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <napi.h>

class Matcher : public Napi::ObjectWrap<Matcher>
{
private:
static Napi::FunctionReference constructor;

public:
Matcher(const Napi::CallbackInfo &info);
void Create(const Napi::CallbackInfo &info);
void Match(const Napi::CallbackInfo &info);
void AddCandidates(const Napi::CallbackInfo &info);
void RemoveCandidates(const Napi::CallbackInfo &info);
void SetCandidates(const Napi::CallbackInfo &info);

static Napi::Function GetClass(Napi::Env);
};