11import lodash from "lodash" ;
22
33// import { ESSE } from "@exabyte-io/esse.js";
4- import { deepClone } from "../utils/clone" ;
4+ import { clone , deepClone } from "../utils/clone" ;
55import { getSchemaByClassName } from "../utils/schemas" ;
66
77// TODO: https://exabyte.atlassian.net/browse/SOF-5946
@@ -12,8 +12,11 @@ export class InMemoryEntity {
1212 return new this . prototype . constructor ( config ) ;
1313 }
1414
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 ) ;
1720 }
1821
1922 /**
@@ -46,11 +49,24 @@ export class InMemoryEntity {
4649
4750 /**
4851 * @summary Array of fields to exclude from resulted JSON
49- * @param exclude {String[]}
52+ * @param {String[] } exclude
5053 */
5154 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 ) ) ;
5470 }
5571
5672 /**
@@ -90,10 +106,12 @@ export class InMemoryEntity {
90106
91107 isValid ( ) {
92108 const ctx = this . schema . newContext ( ) ;
93- ctx . validate ( this . toJSON ( ) ) ;
109+ const json = this . toJSON ( ) ;
110+
111+ ctx . validate ( json ) ;
94112
95113 if ( ! ctx . isValid ( ) ) {
96- console . log ( JSON . stringify ( this . toJSON ( ) ) ) ;
114+ console . log ( JSON . stringify ( json ) ) ;
97115 if ( ctx . getErrorObject ) {
98116 console . log ( ctx . getErrorObject ( ) ) ;
99117 }
0 commit comments