From 7425cd718081eee25ca74329864adf2ec3476e7c Mon Sep 17 00:00:00 2001 From: Abe Tomoaki Date: Thu, 10 Oct 2024 18:33:22 +0900 Subject: [PATCH] Fix: JavaScript Change the return value of index.count() to a number The return value of index.count() was a boolean, so it was changed to a number. --- javascript/lib.cpp | 2 +- javascript/usearch.test.js | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/javascript/lib.cpp b/javascript/lib.cpp index 38856b3f..07346828 100644 --- a/javascript/lib.cpp +++ b/javascript/lib.cpp @@ -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(keys[i]))); + result[i] = Napi::Number::New(env, native_->count(static_cast(keys[i]))); return result; } diff --git a/javascript/usearch.test.js b/javascript/usearch.test.js index 34c41523..607ddcc6 100644 --- a/javascript/usearch.test.js +++ b/javascript/usearch.test.js @@ -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');