1
1
import lodash from "lodash" ;
2
2
3
3
// import { ESSE } from "@exabyte-io/esse.js";
4
- import { deepClone } from "../utils/clone" ;
4
+ import { clone , deepClone } from "../utils/clone" ;
5
5
import { getSchemaByClassName } from "../utils/schemas" ;
6
6
7
7
// TODO: https://exabyte.atlassian.net/browse/SOF-5946
@@ -12,8 +12,11 @@ export class InMemoryEntity {
12
12
return new this . prototype . constructor ( config ) ;
13
13
}
14
14
15
- constructor ( config ) {
16
- this . _json = deepClone ( config || { } ) ;
15
+ // Override if config deepClone is needed
16
+ static _isDeepCloneRequired = false ;
17
+
18
+ constructor ( config = { } ) {
19
+ this . _json = this . constructor . _isDeepCloneRequired ? deepClone ( config ) : clone ( config ) ;
17
20
}
18
21
19
22
/**
@@ -46,11 +49,24 @@ export class InMemoryEntity {
46
49
47
50
/**
48
51
* @summary Array of fields to exclude from resulted JSON
49
- * @param exclude {String[]}
52
+ * @param {String[] } exclude
50
53
*/
51
54
toJSON ( exclude = [ ] ) {
52
- const config = deepClone ( lodash . omit ( this . _json , exclude ) ) ;
53
- return this . clean ( config ) ;
55
+ return this . constructor . _isDeepCloneRequired
56
+ ? this . toJSONSafe ( exclude )
57
+ : this . toJSONQuick ( exclude ) ;
58
+ }
59
+
60
+ toJSONSafe ( exclude = [ ] ) {
61
+ const config = lodash . omit ( this . _json , exclude ) ;
62
+
63
+ return this . clean ( deepClone ( config ) ) ;
64
+ }
65
+
66
+ toJSONQuick ( exclude = [ ] ) {
67
+ const config = lodash . omit ( this . _json , exclude ) ;
68
+
69
+ return this . clean ( clone ( config ) ) ;
54
70
}
55
71
56
72
/**
@@ -90,10 +106,12 @@ export class InMemoryEntity {
90
106
91
107
isValid ( ) {
92
108
const ctx = this . schema . newContext ( ) ;
93
- ctx . validate ( this . toJSON ( ) ) ;
109
+ const json = this . toJSON ( ) ;
110
+
111
+ ctx . validate ( json ) ;
94
112
95
113
if ( ! ctx . isValid ( ) ) {
96
- console . log ( JSON . stringify ( this . toJSON ( ) ) ) ;
114
+ console . log ( JSON . stringify ( json ) ) ;
97
115
if ( ctx . getErrorObject ) {
98
116
console . log ( ctx . getErrorObject ( ) ) ;
99
117
}
0 commit comments