Skip to content

Commit 7142864

Browse files
committed
Added support for objects to SparseVector
1 parent b7d93e9 commit 7142864

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

src/utils/sparse-vector.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ class SparseVector {
6262
this.indices = [];
6363
this.values = [];
6464

65-
for (const [i, v] of map.entries()) {
65+
const entries = map instanceof Map ? map.entries() : Object.entries(map);
66+
for (const [i, v] of entries) {
6667
if (v != 0) {
6768
this.indices.push(Number(i));
6869
this.values.push(Number(v));

tests/utils/sparse-vector.test.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ test('fromMap', () => {
2828
expect(vec.values).toStrictEqual([2, 3, 1]);
2929
});
3030

31+
test('fromMapObject', () => {
32+
const map = {2: 2, 4: 3, 0: 1, 3: 0};
33+
const vec = new SparseVector(map, 6);
34+
expect(vec.dimensions).toStrictEqual(6);
35+
expect(vec.indices).toStrictEqual([0, 2, 4]);
36+
expect(vec.values).toStrictEqual([1, 2, 3]);
37+
});
38+
3139
test('toPostgres', () => {
3240
const vec = new SparseVector([1.23456789]);
3341
expect(vec.toPostgres()).toStrictEqual('{1:1.23456789}/1');

0 commit comments

Comments
 (0)