Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: JavaScript Change the return value of index.count() to a number #502

Merged
merged 1 commit into from
Oct 10, 2024
Merged
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
2 changes: 1 addition & 1 deletion javascript/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ Napi::Value CompiledIndex::Count(Napi::CallbackInfo const& ctx) {
std::size_t length = keys.ElementLength();
Napi::Array result = Napi::Array::New(env, length);
for (std::size_t i = 0; i < length; ++i)
result[i] = Napi::Boolean::New(env, native_->count(static_cast<default_key_t>(keys[i])));
result[i] = Napi::Number::New(env, native_->count(static_cast<default_key_t>(keys[i])));
return result;
}

Expand Down
20 changes: 20 additions & 0 deletions javascript/usearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,27 @@ test("Expected results", () => {
assertAlmostEqual(results.distances[0], new Float32Array([0]));
});

test('Expected count()', async (t) => {
const index = new usearch.Index({
metric: 'l2sq',
connectivity: 16,
dimensions: 3,
});
index.add(
[42n, 43n],
[new Float32Array([0.2, 0.6, 0.4]), new Float32Array([0.2, 0.6, 0.4])]
);

await t.test('Argument is a number', () => {
assert.equal(1, index.count(43n));
});
await t.test('Argument is a number (does not exist)', () => {
assert.equal(0, index.count(44n));
});
await t.test('Argument is an array', () => {
assert.deepEqual([1, 1, 0], index.count([42n, 43n, 44n]));
});
});

test('Operations with invalid values', () => {
const indexBatch = new usearch.Index(2, 'l2sq');
Expand Down
Loading