From 68e611a0508977eb10baddb620657653b13dc165 Mon Sep 17 00:00:00 2001 From: Ladd Date: Mon, 23 Dec 2024 20:44:54 -0600 Subject: [PATCH] added delta filtering for lossless view --- __tests__/lossless.ts | 69 +++++++++++++++++++++++++++++++++++++++++-- src/lossless.ts | 24 +++++++++------ src/types.ts | 2 ++ 3 files changed, 84 insertions(+), 11 deletions(-) diff --git a/__tests__/lossless.ts b/__tests__/lossless.ts index 6f68b70..fc7356c 100644 --- a/__tests__/lossless.ts +++ b/__tests__/lossless.ts @@ -1,8 +1,8 @@ import {Lossless} from '../src/lossless'; -import {Delta} from '../src/types'; +import {Delta, DeltaFilter} from '../src/types'; describe('Lossless', () => { - it('creates a lossless view of neo in the matrix', () => { + it('creates a lossless view of keanu as neo in the matrix', () => { const delta: Delta = { creator: 'a', host: 'h', @@ -72,5 +72,70 @@ describe('Lossless', () => { }], } }); + + + }); + + describe('can filter deltas', () => { + const lossless = new Lossless(); + + beforeAll(() => { + lossless.ingestDelta({ + creator: 'A', + host: 'H', + pointers: [{ + localContext: "1", + target: "ace", + targetContext: "value" + }] + }); + + lossless.ingestDelta({ + creator: 'B', + host: 'H', + pointers: [{ + // 10 11j 12q 13k 14a + localContext: "14", + target: "ace", + targetContext: "value" + }] + }); + + expect(lossless.view()).toEqual({ + ace: { + value: [{ + creator: 'A', + host: 'H', + pointers: [ + {"1": "ace"}, + ] + }, { + creator: 'B', + host: 'H', + pointers: [ + {"14": "ace"}, + ] + }], + } + }); + }); + + it('filter by creator and host', () => { + const filter: DeltaFilter = ({creator, host}) => { + return creator === 'A' && host === 'H'; + }; + + expect(lossless.view(filter)).toEqual({ + ace: { + value: [{ + creator: 'A', + host: 'H', + pointers: [ + {"1": "ace"}, + ] + }] + } + }); + }); }); }); diff --git a/src/lossless.ts b/src/lossless.ts index f612780..865b874 100644 --- a/src/lossless.ts +++ b/src/lossless.ts @@ -1,7 +1,7 @@ // Deltas target entities. // We can maintain a record of all the targeted entities, and the deltas that targeted them -import {Delta, PropertyTypes} from "./types"; +import {Delta, DeltaFilter, PropertyTypes} from "./types"; type DomainEntityID = string; type PropertyID = string; @@ -65,21 +65,27 @@ export class Lossless { } } - view() { + //TODO: json logic -- view(deltaFilter?: FilterExpr) { + view(deltaFilter?: DeltaFilter) { const view: {[key: DomainEntityID]: {[key: PropertyID]: CollapsedDelta[]}} = {}; for (const ent of this.domainEntities.values()) { - const obj: {[key: PropertyID]: CollapsedDelta[]} = {}; - view[ent.id] = obj; + // const obj: {[key: PropertyID]: CollapsedDelta[]} = {}; + view[ent.id] = {}; for (const prop of ent.properties.values()) { - obj[prop.id] = obj[prop.id] || []; + view[ent.id][prop.id] = view[ent.id][prop.id] || []; for (const delta of prop.deltas) { + if (deltaFilter) { + const include = deltaFilter(delta); + if (!include) continue; + } const collapsedDelta: CollapsedDelta = { ...delta, - pointers: delta.pointers.map(({localContext, target}) => ({ - [localContext]: target - })) + pointers: delta.pointers + .map(({localContext, target}) => ({ + [localContext]: target + })) }; - obj[prop.id].push(collapsedDelta); + view[ent.id][prop.id].push(collapsedDelta); } } } diff --git a/src/types.ts b/src/types.ts index c83e6ce..e72b124 100644 --- a/src/types.ts +++ b/src/types.ts @@ -37,6 +37,8 @@ export type FilterExpr = JSONLogic; export type FilterGenerator = () => FilterExpr; +export type DeltaFilter = (delta: Delta) => boolean; + export type PropertyTypes = string | number | undefined; export type Properties = {[key: string]: PropertyTypes};