Skip to content

Commit

Permalink
Merge pull request #297 from nairihar/dev-js-nan
Browse files Browse the repository at this point in the history
Improve: Add NaN related checks for the JavaScript layer
  • Loading branch information
ashvardanian committed Nov 5, 2023
2 parents 28b3a6c + 985e595 commit a5bb475
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors:
given-names: "Ash"
orcid: "https://orcid.org/0000-0002-4882-1815"
title: "USearch by Unum Cloud"
version: 2.8.1
version: 2.8.2
doi: 10.5281/zenodo.7949416
date-released: 2023-10-22
url: "https://github.com/unum-cloud/usearch"
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "usearch"
version = "2.8.1"
version = "2.8.2"
authors = ["Ash Vardanian <1983160+ashvardanian@users.noreply.github.com>"]
description = "Smaller & Faster Single-File Vector Search Engine from Unum"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ doi = {10.5281/zenodo.7949416},
author = {Vardanian, Ash},
title = {{USearch by Unum Cloud}},
url = {https://github.com/unum-cloud/usearch},
version = {2.8.1},
version = {2.8.2},
year = {2023},
month = oct,
}
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.8.1
2.8.2
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class USearchConan(ConanFile):

name = "usearch"
version = '2.8.1'
version = '2.8.2'
license = "Apache-2.0"
description = "Smaller & Faster Single-File Vector Search Engine from Unum"
homepage = "https://github.com/unum-cloud/usearch"
Expand Down
2 changes: 1 addition & 1 deletion csharp/nuget/nuget-package.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version Condition="'$(Version)' == ''">2.8.1</Version>
<Version Condition="'$(Version)' == ''">2.8.2</Version>

<Authors>Unum</Authors>
<Company>Unum</Company>
Expand Down
2 changes: 1 addition & 1 deletion include/usearch/index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#define USEARCH_VERSION_MAJOR 2
#define USEARCH_VERSION_MINOR 8
#define USEARCH_VERSION_PATCH 1
#define USEARCH_VERSION_PATCH 2

// Inferring C++ version
// https://stackoverflow.com/a/61552074
Expand Down
8 changes: 4 additions & 4 deletions javascript/usearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ class BatchMatches {
}

function isOneKey(keys) {
return typeof keys === 'number' || typeof keys === 'bigint';
return (!Number.isNaN(keys) && typeof keys === 'number') || typeof keys === 'bigint';
}

function normalizeKeys(keys) {
if (isOneKey(keys)) {
keys = BigUint64Array.of(BigInt(keys));
} else if (Array.isArray(keys)) {
keys = keys.map(key => {
if (typeof key !== 'bigint' && typeof key !== 'number')
if ((typeof key !== 'bigint' && typeof key !== 'number') || Number.isNaN(key))
throw new Error("All keys must be integers or bigints.");
return BigInt(key);
});
Expand Down Expand Up @@ -151,7 +151,7 @@ class Index {
if (typeof dimensionsOrConfigs === 'object' && dimensionsOrConfigs !== null) {
// Parameters are provided as an object
({ dimensions, metric = MetricKind.Cos, quantization = ScalarKind.F32, connectivity = 0, expansion_add = 0, expansion_search = 0, multi = false } = dimensionsOrConfigs);
} else if (typeof dimensionsOrConfigs === 'number' || typeof dimensionsOrConfigs === 'bigint') {
} else if ((typeof dimensionsOrConfigs === 'number' && !Number.isNaN(dimensionsOrConfigs)) || typeof dimensionsOrConfigs === 'bigint') {
// Parameters are provided as individual arguments
dimensions = dimensionsOrConfigs;
} else {
Expand Down Expand Up @@ -238,7 +238,7 @@ class Index {
* @throws Will throw an error if `vectors` is not a valid input type (TypedArray or an array of arrays) or if its flattened size is not a multiple of dimensions.
*/
search(vectors, k) {
if (typeof k !== 'number' || k <= 0) {
if ((!Number.isNaN(k) && typeof k !== 'number') || k <= 0) {
throw new Error("`k` must be a positive integer representing the number of nearest neighbors to search for.");
}

Expand Down
22 changes: 22 additions & 0 deletions javascript/usearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,25 @@ test('Batch operations', () => {
assert.deepEqual(results.keys, new BigUint64Array([15n, 16n]), 'keys should be 15 and 16');
assert.deepEqual(results.distances, new Float32Array([45, 130]), 'distances should be 45 and 130');
});


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

const keys = [NaN, 16n];
const vectors = [new Float32Array([10, 30]), new Float32Array([1, 5])];

try {
indexBatch.add(keys, vectors);
throw new Error('indexBatch.add should have thrown an error.');
} catch (err) {
assert.equal(err.message, 'All keys must be integers or bigints.');
}

try {
indexBatch.search(NaN, 2);
throw new Error('indexBatch.search should have thrown an error.');
} catch (err) {
assert.equal(err.message, 'Vectors must be a TypedArray or an array of arrays.');
}
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "usearch",
"version": "2.8.1",
"version": "2.8.2",
"description": "Smaller & Faster Single-File Vector Search Engine from Unum",
"author": "Ash Vardanian (https://ashvardanian.com/)",
"license": "Apache 2.0",
Expand Down
2 changes: 1 addition & 1 deletion wasmer.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name="unum/usearch"
version="2.8.1"
version="2.8.2"
description="Smaller & Faster Single-File Vector Search Engine from Unum"
license="Apache-2.0"
readme="README.md"
Expand Down

0 comments on commit a5bb475

Please sign in to comment.