Skip to content

Commit

Permalink
Added support for objects to SparseVector
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jun 27, 2024
1 parent b7d93e9 commit 7142864
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/utils/sparse-vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class SparseVector {
this.indices = [];
this.values = [];

for (const [i, v] of map.entries()) {
const entries = map instanceof Map ? map.entries() : Object.entries(map);
for (const [i, v] of entries) {
if (v != 0) {
this.indices.push(Number(i));
this.values.push(Number(v));
Expand Down
8 changes: 8 additions & 0 deletions tests/utils/sparse-vector.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ test('fromMap', () => {
expect(vec.values).toStrictEqual([2, 3, 1]);
});

test('fromMapObject', () => {
const map = {2: 2, 4: 3, 0: 1, 3: 0};
const vec = new SparseVector(map, 6);
expect(vec.dimensions).toStrictEqual(6);
expect(vec.indices).toStrictEqual([0, 2, 4]);
expect(vec.values).toStrictEqual([1, 2, 3]);
});

test('toPostgres', () => {
const vec = new SparseVector([1.23456789]);
expect(vec.toPostgres()).toStrictEqual('{1:1.23456789}/1');
Expand Down

0 comments on commit 7142864

Please sign in to comment.