Skip to content

Commit cf249dc

Browse files
Merge pull request #49 from Exabyte-io/update/SOF-6584
SOF-6584: js spread operator instead deepClone fro in_memory entity
2 parents 2c7ee7b + 2b7b3b7 commit cf249dc

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/entity/in_memory.js

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 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";
55
import { 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
}

src/utils/clone.js

+4
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
export function deepClone(obj) {
33
return JSON.parse(JSON.stringify(obj));
44
}
5+
6+
export function clone(obj) {
7+
return { ...obj };
8+
}

0 commit comments

Comments
 (0)