Skip to content

Commit c7bf891

Browse files
author
Maier, Martin
committed
* added feature #andDo to apply a function on a property
1 parent 7bb20f5 commit c7bf891

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

src/replicator.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,15 @@ export class PropertyModifier<PT, VT> {
195195
let value = setFunction(currentvalue);
196196
return this.with(value)
197197
}
198+
199+
/**
200+
* applies a function on this property
201+
* @param {(VT) => void} executeOnCloneFunction function that is executed
202+
* @returns {PT}
203+
*/
204+
andDo(executeOnCloneFunction: (VT) => void): PT {
205+
let currentvalue = _.get(this.replica, this.relativePathToRoot);
206+
executeOnCloneFunction(currentvalue);
207+
return this.parent;
208+
}
198209
}

src/tests/replicator.spec.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import {deepFreeze, isDeepFrozen} from '../deepFreeze'
55

66
describe('ReplicationBuilder', () => {
77

8+
it('should clone a property and execute function on the clone ', function () {
9+
let rootState = new ClassorientedTeststate();
10+
let someNewValue = 'someNewValue';
11+
let manipulatedRoot = ReplicationBuilder.forObject(rootState).replaceProperty('subTypeA').andDo((clonedSubTypeA) => {
12+
clonedSubTypeA.setSubTypeAAttribute(someNewValue)
13+
}).build();
14+
expect(manipulatedRoot.subTypeA.subTypeAAttribute).to.equal(someNewValue);
15+
});
16+
817
it('Inputstate must not be modified, output must be modified', () => {
918
let rootState = new ClassorientedTeststate();
1019
let manipulatedRoot = ReplicationBuilder.forObject(rootState).getProperty('subTypeA').getProperty('subTypeB').replaceProperty('subTypeBAttribute').with('Test').build();
@@ -76,7 +85,7 @@ describe('ReplicationBuilder', () => {
7685
});
7786

7887
it('test performance of frozen big array', () => {
79-
let objectCount = 5000;
88+
let objectCount = 5000;
8089
let rootState = new ObjectArray(objectCount);
8190
deepFreeze(rootState);
8291

@@ -93,7 +102,7 @@ describe('ReplicationBuilder', () => {
93102
});
94103

95104
it('test performance of not frozen big array', () => {
96-
let objectCount = 5000;
105+
let objectCount = 5000;
97106
let rootState = new ObjectArray(objectCount);
98107

99108
let repeatCount = 1;

src/tests/testobjects.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,29 @@ interface ISubTypeA{
99
}
1010

1111
export class SubTypeA extends ParentTypeA implements ISubTypeA{
12-
subTypeAAttribute: string = null
13-
subTypeAArray: string[] = []
14-
subTypeB: SubTypeB = new SubTypeB()
12+
subTypeAAttribute: string = null;
13+
subTypeAArray: string[] = [];
14+
subTypeB: SubTypeB = new SubTypeB();
15+
16+
setSubTypeAAttribute(newValue: string) {
17+
this.subTypeAAttribute = newValue;
18+
}
1519

1620
doNothingFunction() {
1721
// do nothing
18-
let test:number = 0
22+
let test: number = 0;
1923
test = 1
2024
}
2125
}
2226

2327
class SubTypeB {
24-
subTypeBAttribute: string = null
25-
subTypeBArray: string[] = []
28+
subTypeBAttribute: string = null;
29+
subTypeBArray: string[] = [];
2630
subTypeB: SubTypeC = new SubTypeC()
2731
}
2832

2933
class SubTypeC {
30-
subTypeCAttribute: string = null
34+
subTypeCAttribute: string = null;
3135
subTypeCArray: string[] = []
3236
}
3337

@@ -41,7 +45,7 @@ export const SimpleTeststate = {
4145
subTypeB: {
4246
subtypeBAttribute: 'initial'
4347
}
44-
}
48+
};
4549

4650
export class SomeObject {
4751
attA: string;
@@ -51,9 +55,9 @@ export class SomeObject {
5155

5256

5357
constructor(attA: string, attB: string, attC: string, attD: string) {
54-
this.attA = attA
55-
this.attB = attB
56-
this.attC = attC
58+
this.attA = attA;
59+
this.attB = attB;
60+
this.attC = attC;
5761
this.attD = attD
5862
}
5963
}

0 commit comments

Comments
 (0)