@@ -8,34 +8,47 @@ Helpers for handling immutable objects with typescript
8
8
Replicator is a tool to replicate and modify immutable objects.
9
9
10
10
### Syntax
11
- ![ image] ( https://user-images.githubusercontent.com/20232625/28767484-22a1d330-75d4-11e7-9667-01271c7e2448.png )
11
+ ##### simply replace property by with new value
12
+ ``` Typescript
13
+ return ReplicationBuilder .forObject (state ).getProperty (' party' ).replaceProperty (' name' ).with (' MyParty' ).build ()
14
+ ```
15
+ ##### replace property depending on old value
16
+ ``` Typescript
17
+ return ReplicationBuilder .forObject (state ).getProperty (' party' ).replaceProperty (' partymemberArray' ).by ((oldPartymemberArray ) => [... oldPartymemberArray , ' new partymember' ]).build ()
18
+ ```
19
+ ##### clone property and apply some function on it
20
+ ``` Typescript
21
+ return ReplicationBuilder .forObject (state ).replaceProperty (' party' ).andDo ((clonedParty ) => clonedParty .addPartyMember (' new partymember' )).build ()
22
+ ```
12
23
13
24
### Characteristics
14
- ###### typesafe properties
25
+ ##### typesafe properties
26
+
15
27
![ image] ( https://user-images.githubusercontent.com/20232625/28767468-14cb5aa6-75d4-11e7-8193-dcf828133035.png )
16
- ###### typesafe property values
28
+ ##### typesafe property values
29
+
17
30
![ image] ( https://user-images.githubusercontent.com/20232625/28767500-3b6f082e-75d4-11e7-8ec3-1e1392209396.png )
18
- ###### chainable
19
- ![ image] ( https://user-images.githubusercontent.com/20232625/28767664-dc00269c-75d4-11e7-9c6d-c179c0b12eaf.png )
31
+ ##### chainable
32
+ ``` Typescript
33
+ return ReplicationBuilder .forObject (state )
34
+ .getProperty (' party' ).replaceProperty (' name' ).with (' MyParty' ).replaceProperty (' members' ).by ((members ) => [... members , newMember ])
35
+ .getProperty (' initiator' ).replaceProperty (' prename' ).with (' Party' ).replaceProperty (' surname' ).with (' guy' )
36
+ .build ();
37
+ ```
20
38
###### refactorable and easy to read
21
39
22
40
### Usage
23
41
24
42
1 . Load an object by calling ` ReplicationBuilder.forObject() `
25
43
2 . Navigate down the object tree through the typesafe function ` getChild() `
26
- 3 . Modify a property with either
27
- - ` modify('prop').to(newValue:T) ` or
28
- - ` modify('prop').by((T) => newValue:T) ` for example ` ((oldValue) => oldValue + newValue) `
29
- - ` delete('prop') ` to remove the property in the resulting object
44
+ 3 . Modify a property with either (see syntax paragraph above)
45
+ - ` replaceProperty('prop').with(newValue:T) `
46
+ - ` replaceProperty('prop').by((T) => newValue:T) `
47
+ - ` replaceProperty('prop').andDo((clonedProp) => clonedProp.doSomething() `
48
+ - ` removeProperty('prop') ` to remove the property in the resulting object
30
49
4 . Repeat step 3 and 4 until all modifications are done
31
50
5 . Produce the replica with ` build() `
32
51
33
- ### Examples
34
-
35
- ![ image] ( https://user-images.githubusercontent.com/20232625/28767484-22a1d330-75d4-11e7-9667-01271c7e2448.png )
36
-
37
- ![ image] ( https://user-images.githubusercontent.com/20232625/28767522-55f40ea6-75d4-11e7-8faf-0c1bf9f91953.png )
38
-
39
52
### Behaviour
40
53
41
54
- deep copies the source object
0 commit comments