Skip to content

Commit

Permalink
feat: support null and undefined insertion and table iteration no…
Browse files Browse the repository at this point in the history
…w provides row index
  • Loading branch information
CMCDragonkai committed Jul 5, 2023
1 parent cf51689 commit a55b4e5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
25 changes: 14 additions & 11 deletions src/Table.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Counter from 'resource-counter';
import * as utils from './utils';

class Table<R extends Record<string, any>> {
public readonly keys: Set<keyof R>;
Expand Down Expand Up @@ -104,8 +105,8 @@ class Table<R extends Record<string, any>> {
return this.rows.size;
}

public [Symbol.iterator](): IterableIterator<Readonly<R>> {
return this.rows.values();
public [Symbol.iterator](): IterableIterator<[number, Readonly<R>]> {
return this.rows.entries();
}

/**
Expand Down Expand Up @@ -201,10 +202,10 @@ class Table<R extends Record<string, any>> {
}
const index = this.indexes[k];
if (index != null) {
const v_ = index.f != null ? index.f(v) : v.toString();
const v_ = index.f != null ? index.f(v) : utils.toString(v);
return [...(index.index.get(v_) ?? new Set())];
} else if (search) {
const v_ = v.toString();
const v_ = utils.toString(v);
const rIs: Array<number> = [];
for (const [rI, r] of this.rows.entries()) {
if (r[k] === v_) {
Expand All @@ -223,13 +224,15 @@ class Table<R extends Record<string, any>> {
const index = this.indexesDerived[kDerived];
if (index != null) {
const v_ =
index.f != null ? index.f(...v) : v.map((v) => v.toString()).join('');
index.f != null
? index.f(...v)
: v.map((v) => utils.toString(v)).join('');
return [...(index.index.get(v_) ?? new Set())];
} else if (search) {
const v_ = v.map((v) => v.toString()).join('');
const v_ = v.map((v) => utils.toString(v)).join('');
const rIs: Array<number> = [];
for (const [rI, r] of this.rows.entries()) {
if (k.map((k) => r[k].toString()).join('') === v_) {
if (k.map((k) => utils.toString(r[k])).join('') === v_) {
rIs.push(rI);
}
}
Expand All @@ -245,7 +248,7 @@ class Table<R extends Record<string, any>> {
for (const k in r) {
const index = this.indexes[k];
if (index == null) continue;
const v = index.f != null ? index.f(r[k]) : r[k].toString();
const v = index.f != null ? index.f(r[k]) : utils.toString(r[k]);
const rIs = index.index.get(v) ?? new Set();
rIs.add(rI);
index.index.set(v, rIs);
Expand All @@ -264,7 +267,7 @@ class Table<R extends Record<string, any>> {
} else {
v = index.deps
.map((k) => r[k])
.map((v) => v.toString())
.map((v) => utils.toString(v))
.join('');
}
const rIs = index.index.get(v) ?? new Set();
Expand All @@ -280,7 +283,7 @@ class Table<R extends Record<string, any>> {
for (const k in r) {
const index = this.indexes[k];
if (index == null) continue;
const v = index.f != null ? index.f(r[k]) : r[k].toString();
const v = index.f != null ? index.f(r[k]) : utils.toString(r[k]);
const rIs = index.index.get(v)!;
rIs.delete(rI);
if (rIs.size === 0) {
Expand All @@ -301,7 +304,7 @@ class Table<R extends Record<string, any>> {
} else {
v = index.deps
.map((k) => r[k])
.map((v) => v.toString())
.map((v) => utils.toString(v))
.join('');
}
const rIs = index.index.get(v)!;
Expand Down
6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function toString(v: any) {
return v == null ? `${v}` : v.toString();
}

/**
* O(n) intersection
* You can use this if you have multiple keys
Expand All @@ -12,4 +16,4 @@ function intersection<T>(...arrays: Array<Array<T>>): Array<T> {
return [...commonSet];
}

export { intersection };
export { toString, intersection };
10 changes: 9 additions & 1 deletion tests/Table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ describe(Table.name, () => {
const table_ = t.getTable();
expect(table_.get(1)).toBeUndefined();
});
test('insert null and undefined values', () => {
const t = new Table<{ a: null; b: undefined }>(['a', 'b'], ['a', 'b']);
const rI = t.insertRow({ a: null, b: undefined });
const rIs1 = t.whereRows('a', null);
expect(rIs1).toContain(rI);
const rIs2 = t.whereRows('b', undefined);
expect(rIs2).toContain(rI);
});
testProp(
'insert table rows',
[
Expand All @@ -63,7 +71,7 @@ describe(Table.name, () => {
const t = new Table(['a', 'b', 'c', 'd'], keysIndex);
const is = rows.map((r) => t.insertRow(r));
expect(is).toEqual([...Array(rows.length).keys()]);
expect([...t]).toEqual(rows);
expect([...t]).toEqual([...rows.entries()]);
expect(t.count).toBe(rows.length);
},
);
Expand Down

0 comments on commit a55b4e5

Please sign in to comment.