@creejs/commons-lang 2.1.18 → 2.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,7 +14,9 @@ var LangUtils = {
14
14
  extends: extend,
15
15
  equals: equals$2,
16
16
  isBrowser,
17
- isNode
17
+ isNode,
18
+ cloneToPlainObject,
19
+ deepCloneToPlainObject
18
20
  };
19
21
 
20
22
  /**
@@ -72,6 +74,41 @@ function extend (target, ...sources) {
72
74
  return target
73
75
  }
74
76
 
77
+ /**
78
+ * Creates a shallow clone of an object by copying its enumerable properties to a new plain object.
79
+ * 1. This returns a PlainObject
80
+ * 2. It lose the type information of the original object
81
+ * 3. Shallow clone, copy only the first level properties
82
+ * @param {{[key:string]: any}} obj - The object to clone.
83
+ * @returns {{[key:string]: any}} A new plain object with the same enumerable properties as the input object.
84
+ */
85
+ function cloneToPlainObject (obj) {
86
+ if (obj == null) {
87
+ return obj
88
+ }
89
+ if (typeof obj !== 'object') {
90
+ throw new Error('Only Object allowed to clone')
91
+ }
92
+ return { ...obj }
93
+ }
94
+
95
+ /**
96
+ * Deep clones an object by converting it to JSON and back to a plain object.
97
+ * 1. This will lose any non-JSON-serializable properties (e.g. functions, Symbols).
98
+ * 2. Only Use this to clone PlainObject
99
+ * @param {{[key:string]: any}} obj - The object to clone
100
+ * @returns {{[key:string]: any}} A new plain object with the cloned properties
101
+ */
102
+ function deepCloneToPlainObject (obj) {
103
+ if (obj == null) {
104
+ return obj
105
+ }
106
+ if (typeof obj !== 'object') {
107
+ throw new Error('Only Object allowed to clone')
108
+ }
109
+ return JSON.parse(JSON.stringify(obj))
110
+ }
111
+
75
112
  /**
76
113
  * Compares two values for equality
77
114
  * 1. First checks strict equality (===),