Skip to content

Commit 295830d

Browse files
committed
make sure delete alter the data array and also provides deleteAll() method
1 parent c3ce42a commit 295830d

5 files changed

Lines changed: 28 additions & 8 deletions

File tree

src/decorator/predicate.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,9 @@ export interface IPredicate<T, U = void> {
118118
* Remove a list of nodes.
119119
*/
120120
delete(nodes: T[]): IPredicate<T, U>;
121+
122+
/**
123+
* Removes all nodes from the predicate.
124+
*/
125+
deleteAll(): IPredicate<T, U>;
121126
}

src/transaction/mutation-builder.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ export class MutationBuilder {
9595

9696
public getDeleteNQuadsString(): string {
9797
const quads = this.getDeleteNQuads();
98-
return new Writer({ format: 'N-Quads' }).quadsToString(quads);
98+
const quadsString = new Writer({ format: 'N-Quads' }).quadsToString(quads);
99+
100+
const lines = quadsString.split('\n');
101+
102+
return Array.from(new Set(lines)).join('\n');
99103
}
100104

101105
public getDeleteNQuads(): Quad[] {

src/transaction/transaction.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ export class Transaction<T extends Object, V> implements ITransaction<T> {
7676
return new MutationBuilder(this.diff, this.tempIDS).getSetNQuadsString();
7777
}
7878

79-
public delete(node: T): void;
80-
public delete(nodes: T[]): void;
81-
public delete(nodeOrNodes: T | T[]) {
79+
public delete(node: T): ITransaction<T>;
80+
public delete(nodes: T[]): ITransaction<T>;
81+
public delete(nodeOrNodes: T | T[]): ITransaction<T> {
8282
if (!Array.isArray(nodeOrNodes)) {
8383
nodeOrNodes = [nodeOrNodes];
8484
}

tests/delete.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,24 @@ describe('Delete handling', function() {
4545
transaction.delete(kamil);
4646
transaction.tree[0].friends.delete(jane);
4747

48+
// XXX: here kamil is not deleted in the friends predicate
49+
expect(transaction.tree[0].friends.get().length).toBe(1);
50+
51+
expect(transaction.getDeleteNQuadsString()).toEqual(
52+
`<0x3> * * .
53+
<0x2> * * .
54+
<0x1> <Person.friends> <0x2> .
55+
`
56+
);
57+
58+
transaction.tree[0].friends.deleteAll();
59+
expect(transaction.tree[0].friends.get().length).toBe(0);
60+
4861
expect(transaction.getDeleteNQuadsString()).toEqual(
4962
`<0x3> * * .
5063
<0x2> * * .
5164
<0x1> <Person.friends> <0x2> .
65+
<0x1> <Person.friends> <0x3> .
5266
`
5367
);
5468
});

tests/perf.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @ts-ignore
2-
import * as util from 'util';
31

42
import { IPredicate, Node, Predicate, Property, TransactionBuilder, Uid } from '../src';
53

@@ -77,15 +75,14 @@ describe('Performance testing', () => {
7775
console.timeEnd('Second');
7876
});
7977

80-
it.only('Fuzz with random data', () => {
78+
it('Fuzz with random data', () => {
8179
interface IStation {
8280
uid: string;
8381
name: string;
8482
connects: IStation[];
8583
}
8684

8785
@Node()
88-
// @ts-ignore
8986
class Station {
9087
@Uid()
9188
uid: string;

0 commit comments

Comments
 (0)