Skip to content

Commit

Permalink
Fix: JavaScript Change the return value of index.count() to a number
Browse files Browse the repository at this point in the history
The return value of index.count() was a boolean, so it was changed to a number.
  • Loading branch information
abetomo committed Oct 10, 2024
1 parent 9bc9936 commit 7425cd7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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

0 comments on commit 7425cd7

Please sign in to comment.